Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

markdown - can I have underscores w/o escaping them and not have markdown italics?

I want to have text that has underscores in it.

It's not code and so I don't want to use code format.

I want to stop markdown treating it as an instruction to italicize it.

I can escape _each_underscore (see!) with \ but I have a total of 20 and that looks ugly in the source, hard to maintain and not very DRY.

Any other options?

like image 201
Michael Durrant Avatar asked Apr 24 '12 16:04

Michael Durrant


People also ask

How do you use special characters in Markdown?

5.2 Special characters If you want any special characters in R Markdown, LaTeX, or pandoc to appear as text, rather than having them perform some function, you need to “escape” them with a backslash.

How do you italicize in Markdown?

To italicise text, use single asterisks ( *text* ) or underscores ( _text_ ) before and after the text. I **love** _writing_ in __Markdown__. The word 'love' and 'Markdown' will show up as bold. The word 'writing' will be italicised.

How do you bold and italicize in Markdown?

To emphasize text with bold and italics at the same time, add three asterisks or underscores before and after a word or phrase. To bold and italicize the middle of a word for emphasis, add three asterisks without spaces around the letters. This text is ***really important***.

Can you underline in Markdown?

Text can't be underlined in Markdown. Although this is possible using the '<u>' tags in HTML, it's usually inadvisable to do so. That's because underlined text is used for hyperlinks online and it's best to avoid confusing the two uses.


1 Answers

Some Markdown implementations – in particular Stack Overflow's server-side C# version MarkdownSharp (where it's optional behavior) and client-side JavaScript version PageDown, but also e.g. GitHub's flavor – have deviated from the Markdown spec for the very reason you describe.

For some history on this as far as Stack Overflow goes, see the two blog posts Three Markdown Gotchas and Markdown, One Year Later.

Since this is a commonly uttered criticism of Markdown, there are probably more implementations that either make this behavior user-settable, or just go with the stricter version altogether. So it depends on what implementation you're using.

If you're using a port that is based on John Gruber's original Perl implementation (i.e. the "tons of regex replacements" version), it should be fairly easy to make this change yourself. The relevant function is likely called _DoItalicsAndBold (original Perl version, Showdown/PageDown), DoItalicsAndBold (MarkdownSharp), _do_italics_and_bold (python-markdown2) or similar.

Look at our PageDown version of that function for the stricter regular expressions that are used here on Stack Overflow:

function _DoItalicsAndBold(text) {

    // <strong> must go first:
    text = text.replace(/([\W_]|^)(\*\*|__)(?=\S)([^\r]*?\S[\*_]*)\2([\W_]|$)/g,
    "$1<strong>$3</strong>$4");

    text = text.replace(/([\W_]|^)(\*|_)(?=\S)([^\r\*_]*?\S)\2([\W_]|$)/g,
    "$1<em>$3</em>$4");

    return text;
}
like image 109
balpha Avatar answered Oct 18 '22 08:10

balpha