3

I am using MarkdownSharp in one of my projects and have noticed that if any of my Url's contain pairs of underscore characters somewhere within it, it's treated as italic and therefore replaces the _ with <em>.

I've had a look on google but can't find any reference to this problem behaviour, and from reading some of the comments in the MarkdownSharp code, it suggests that the code is written the way it is to prevent this from happening. See below snippet from the markdown code:

The order in which other subs are called here is essential. Link and image substitutions need to happen before EscapeSpecialChars(), so that any *'s or _'s in the a and img tags get encoded.

    public string Transform(string text)
    {
        if (String.IsNullOrEmpty(text)) return "";

        Setup();

        text = Normalize(text);

        text = HashHTMLBlocks(text);
        text = StripLinkDefinitions(text);
        text = RunBlockGamut(text);
        text = Unescape(text);

        Cleanup();

        return text + "\n";
    }

Is there a known workaround for this behaviour?

** UPDATE: I have just tested entering a url on StackOverflow which I believe uses a version of MarkdownSharp (and AutoHyperlink is enabled as per my project) and whilst it handles a single underscore instance within the url, as soon as a pair of underscores appear in the url, it breaks.

marcusstarnes
  • 6,393
  • 14
  • 65
  • 112
  • What is WMD? The tag has been used very few times and contains no description. I also don't see anything that could be abbreviated by WMD in your question. – Merlyn Morgan-Graham Nov 28 '11 at 11:15
  • @MerlynMorgan-Graham: it must refer to this editor: http://code.google.com/p/wmd/ – Ben Parsons Nov 28 '11 at 11:24
  • Yes, as per Ben P's comment, it refers to the markdown editor which I use in conjunction with MarkdownSharp. Perhaps I shouldn't have assigned that tag as it's only loosely related in this instance. – marcusstarnes Nov 28 '11 at 11:59
  • Can anybody tell me where is the documentation of the `MarkdownSharp`. – carl johnson Jan 27 '21 at 07:56

2 Answers2

4

MarkdownSharp has a configuration option that was created for this very reason:

/// <summary>
/// when true, bold and italic require non-word characters on either side  
/// WARNING: this is a significant deviation from the markdown spec
/// </summary>
public bool StrictBoldItalic { get; set; }

For some background info, see point 1. in https://blog.stackoverflow.com/2008/06/three-markdown-gotcha/.

Community
  • 1
  • 1
balpha
  • 50,022
  • 18
  • 110
  • 131
  • Thanks, this works great for the server-side MarkdownSharp side of things. Only problem I am left with is that it doesn't look like 'StrictBoldItalic' is a configuration option within 'Pagedown' (the javascript markdown tools), so what is shown on the clientside isnt what's then rendered by Markdown Sharp :( – marcusstarnes Nov 30 '11 at 16:13
  • 1
    @marcusstarnes: Actually that's the *only* option in PageDown -- you don't even have the option of *not* going with StrictBoldItalic there (here on Stack Overflow, `StrictBoldItalic` is true, and our PageDown is precisely the same code as in the open source repo, so the two have to match). – balpha Nov 30 '11 at 17:41
  • Thanks, it looks like I didn't have the original Pagedown code as it wasn't behaving like that. I've now downloaded the Pagedown code again and it's working as you said. There are still one or two scenarios both in my project and here on stack overflow where the underscore within a url will break, but its an 'unlikely' real-world scenario so I'll not worry about it (e.g. http://mysite.com/_test_/ will render 'test' in italic within the url, dropping the underscores whereas http://mysite.com/_test_2/ will work). Thanks for the help balpha. – marcusstarnes Dec 01 '11 at 09:45
1

Have you tried escaping the _ characters that you want to preserve by using a backslash?

See: http://daringfireball.net/projects/markdown/syntax#backslash

From the article:

For example, if you wanted to surround a word with literal asterisks (instead of an HTML <em> tag), you can use backslashes before the asterisks, like this:

\*literal asterisks\*

Asterisks in Markdown are more or less the same as underscores.


Edit: The answer was accepted due to this comment I made. The OP decided to disable auto hyperlinks and manually insert the links with either HTML markup or Markdown link syntax.

Sounds like that feature wasn't thought through well enough. It comes with a warning saying it will make the system do things that aren't compliant with standard markdown, which seems to me like a warning that they didn't solve every problem with those features. You might need to change the library to fix bugs like that...

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • I have tried that but if I enable AutoHyperlinks (which I am using) it closes the anchor tag immediately before the escaped underscore, so the link is essentially broken. – marcusstarnes Nov 28 '11 at 11:39
  • @marcusstarnes: Sounds like that feature wasn't thought through well enough. It comes with a warning saying it will make the system do things that aren't compliant with standard markdown, which seems to me like a warning that they didn't solve every problem with those features. You might need to change the library to fix bugs like that... – Merlyn Morgan-Graham Nov 28 '11 at 19:21
  • @marcusstarnes: Also, if your problem is *in* hyperlinks, MD is supposed to leave manual markup untouched. Have you tried manually inserting anchor tags? – Merlyn Morgan-Graham Nov 28 '11 at 19:23
  • @marcusstarnes: Also see - http://meta.stackexchange.com/questions/71639/how-to-use-star-symbol-in-hyperlink - you could simply URL encode the underscores: `%5F` if my calculations are correct. – Merlyn Morgan-Graham Nov 28 '11 at 19:28
  • Thanks for suggestions. Manual markup works ok whether AutoHyperlinks are enabled or not, which is great, as does the Markdown anchor markup. I think I'll take your comment on board re the AutoHyperlink feature not really being 100%, so will leave it disabled. At least now I know the workarounds, thanks. – marcusstarnes Nov 29 '11 at 09:30