tpope / vim-markdown

Vim Markdown runtime files
1.22k stars 191 forks source link

Inappropriate Italic #60

Open zeis opened 10 years ago

zeis commented 10 years ago

First of all thanks for this awesome vim script.

I'm using the GitHub version. I noticed a little problem with italic text after a single underscore or asterisk.

This work as expected: foo_bar

This doesn't, and "bar" is italicized: foo_<any non-alphanumeric character>bar

This is particularly annoying with LaTex formulas : $ x_{3} $

One last thing, it would be nice to have no highlighting at all for single underscores: foo_bar, most of the times the missing underscore is not an error. Also, using plugins such as vim-surround it is impossible that you forget the closing underscore when you want it.

STOWouters commented 10 years ago

Had this issue too, but I then switched to using the escape character \:

There was no problem on rendering HTML's when using the escape character. However, I don't know whether that's also the case with a LaTex renderer..

zeis commented 10 years ago

Escaping underscores doesn't work in LaTex. Anyway, in my opinion, the plugin should not assume that after an underscore (or asterisk) there will be only italic text and a closing underscore. Indeed, that doesn't seem to be a problem for markdown converters.

coreyhaines commented 10 years ago

Nothing real to add, but I just wanted to say that "Inappropriate Italic" would make a fantastic band name.

jrhorn424 commented 10 years ago

:+1: @coreyhaines

luiz commented 10 years ago

I think the issue I'm having stems from the same root. I want to have _bold italic_, but when I type:

***some text here*** yet more

The "yet more" part (and all the remaining text) gets italicized (and, inside the asterisk, does not).

The problems seems to be on line 61-62 of syntax/markdown.vim, but I just don't get what those regexes mean (I don't know vim regex peculiarities well...) :(

luiz commented 10 years ago

Oh, sorry. My issue was already fixed. I just hadn't the most recent version...

benatkin commented 9 years ago

Same issue.

carbonscott commented 9 years ago

try this one. http://superuser.com/questions/795513/how-to-stop-vim-markdown-syntax-from-highlighting-as-italic-asterisks-used-to-de and comment the line where there is syn region markdownItalic start="\S\@<=_\|_\S\@=" end="\S\@<=_\|_\S\@=" keepend contains=markdownLineStart

bohrshaw commented 8 years ago

To ignore the imperfect syntax:

autocmd FileType markdown |
      \hi def link markdownItalic              NONE |
      \hi def link markdownItalicDelimiter     NONE |
      \hi def link markdownBold                NONE |
      \hi def link markdownBoldDelimiter       NONE |
      \hi def link markdownBoldItalic          NONE |
      \hi def link markdownBoldItalicDelimiter NONE
zeis commented 8 years ago

I agree @BohrShaw, at moment the this is the best solution.

bohrshaw commented 8 years ago

Can we have an option to exclude _ as a character to indicate italic. Because _ is a common intra-word character.

The markdown processor "Blackfriday" has an extension(Intra-word emphasis suppression) for this.

chutsu commented 8 years ago

+1 @BohrShaw 's solution at this very moment. It would be nice if somehow we find a fix for this.

lifepillar commented 8 years ago

I have taken a look at how, ehm, Atom does it. It turns out that it seems to follow a simple rule: an underscore starts italics only if it is followed by non-whitespace and if it is at the start of the line or it is preceded by a non-alphanumeric character different from { or } (braces are excluded because they are used in math formulas). Similarly, an underscore ends italics only if it is preceded by non-whitespace and it is followed by a space non-alphanumeric character or eol.

That means that strings like a_a_a are not italicized at all. But, considering that italicizing inside a word is a relatively infrequent use case, it seems to me an acceptable compromise between simplicity and accuracy. Also, note that things like $[x]_1$ may be rewritten as ${[x]}_1$, so it should be possible to write math formulas without triggering italics in most cases.

So, my proposal to solve the issue is to use the following syntax rules:

exe 'syn region markdownItalic matchgroup=markdownItalicDelimiter         start="\(^\|[^0-9A-Za-z_{}]\)\@<=\*\S\@=" end="\S\@<=\*\(\W\|$\)\@=" keepend contains=markdownLineStart' . s:concealends
exe 'syn region markdownItalic matchgroup=markdownItalicDelimiter         start="\(^\|[^0-9A-Za-z_{}]\)\@<=_\S\@=" end="\S\@<=_\(\W\|$\)\@=" keepend contains=markdownLineStart' . s:concealends
exe 'syn region markdownBold matchgroup=markdownBoldDelimiter             start="\(^\|[^0-9A-Za-z_{}]\)\@<=\*\*\S\@=" end="\S\@<=\*\*\(\W\|$\)\@=" keepend contains=markdownLineStart,markdownItalic' . s:concealends
exe 'syn region markdownBold matchgroup=markdownBoldDelimiter             start="\(^\|[^0-9A-Za-z_{}]\)\@<=__\S\@=" end="\S\@<=__\(\W\|$\)\@=" keepend contains=markdownLineStart,markdownItalic' . s:concealends
exe 'syn region markdownBoldItalic matchgroup=markdownBoldItalicDelimiter start="\(^\|[^0-9A-Za-z_{}]\)\@<=\*\*\*\S\@=" end="\S\@<=\*\*\*\(\W\|$\)\@=" keepend contains=markdownLineStart' . s:concealends
exe 'syn region markdownBoldItalic matchgroup=markdownBoldItalicDelimiter start="\(^\|[^0-9A-Za-z_{}]\)\@<=___\S\@=" end="\S\@<=___\(\W\|$\)\@=" keepend contains=markdownLineStart' . s:concealends

Please experiment with them. I may open a pull request if necessary. An explanation of the first rule follows (the others being analogous):

StaticNoiseLog commented 7 years ago

Can we not use http://dillinger.io/ as a reference? This converter does what one would naturally expect: Underscore and asterisk are only considered when they form pairs. I don't know the details of how it is done and it may be impossible in VIM, but the following example sentence just comes out right:

Convert *all* *.txt files to *.md, and do _not_ change the XPC_FLAGS environment variable.

I thought the converter of the Markdown inventor (John Gruber) did something different, but indeed the above example sentence comes out the same with it: https://daringfireball.net/projects/markdown/dingus

So these two converters should be the reference for this VIM plugin. I hope it is possible to teach VIM the same tricks.

CNG commented 2 years ago

Edit: Please ignore this comment. It's now come to my attention I had switched from vim-markdown to vimwiki at some point and forgot. I'll leave the text here in case it's useful because it's still true it's a different example than above.

I have another example that is somewhat different from those above. My Markdown contains references to files where some segments begin with an underscore. Then everything following that underscore is italicized in Vim.

Example:

<img src="/images/thumbnails/_x500/angels.jpg" height="250" />

Everything after and including _x500 appears italicized, and that underscore also disappears when the line is not in focus. I ended up on this page while searching "vim markdown italics within HTML", hoping there was an easy fix. I either don't understand @bohrshaw's workaround above or it's not working for this case.

tomtomjhj commented 2 years ago

@CNG Can you try the latest version of vim and/or vim-markdown? With the latest vim & vim-markdown, everything in " is highlighted correctly as htmlString and nothing is italicized.

CNG commented 2 years ago

Hi @tomtomjhj, thank you for taking the time to test. I apologize for the confusion, I edited my post with a disclaimer once I realized Vimwiki was interfering here. Once I deleted Vimwiki, I no longer have the problem.