vim-markdown icon indicating copy to clipboard operation
vim-markdown copied to clipboard

Inappropriate Italic

Open zeis opened this issue 11 years ago • 17 comments

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.

zeis avatar Feb 03 '14 15:02 zeis

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

  • foo\_bar
  • foo\_<any non-alphanumeric character>bar
  • $ x\_{3} $

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..

STOWouters avatar Feb 03 '14 18:02 STOWouters

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.

zeis avatar Feb 03 '14 19:02 zeis

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

coreyhaines avatar Mar 15 '14 19:03 coreyhaines

:+1: @coreyhaines

jrhorn424 avatar Apr 03 '14 02:04 jrhorn424

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 avatar May 22 '14 17:05 luiz

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

luiz avatar May 23 '14 20:05 luiz

Same issue.

benatkin avatar Oct 25 '14 03:10 benatkin

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

carbonscott avatar Mar 10 '15 14:03 carbonscott

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

bohrshaw avatar Aug 03 '15 08:08 bohrshaw

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

zeis avatar Aug 03 '15 09:08 zeis

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.

bohrshaw avatar Oct 08 '15 06:10 bohrshaw

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

chutsu avatar Jan 20 '16 14:01 chutsu

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):

  • \(^\|[^0-9A-Za-z_{}]\)\@<=\*\S\@= matches an asterisk (\*) immediately preceded (\@<=) by either the beginning of the line (^) or by any character different from a digit, a letter, an underscore, or a brace ([^0-9A-Za-z_{}]); the asterisk must be followed by a non-whitespace character (\S), which is not included as part of the match (\@=).
  • \S\@<=\*\(\W\|$\)\@= matches an asterisk (\*) immediately preceded (\@<=) by a non-whitespace character (\S) and followed by either a ~~white space~~ non-alphanumeric character (\W) or by the end of the line ($), which are not part of the match (\@=).

lifepillar avatar Jan 30 '16 18:01 lifepillar

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.

StaticNoiseLog avatar Aug 29 '16 10:08 StaticNoiseLog

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.

CNG avatar Feb 26 '22 02:02 CNG

@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.

tomtomjhj avatar Feb 26 '22 03:02 tomtomjhj

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.

CNG avatar Feb 26 '22 04:02 CNG