Tao*_*hyn 10 regex syntax vim markdown highlight
我决定更多地了解vim及其语法突出显示.使用其他人的示例,我正在为Markdown创建自己的语法文件.我见过mkd.vim,它也有这个问题.我的问题是列表项和代码块突出显示之间.
代码块定义:
例:
Regular text
this is code, monospaced and left untouched by markdown
another line of code
Regular Text
Run Code Online (Sandbox Code Playgroud)
我的代码块的Vim语法:
syn match mkdCodeBlock /\(\s\{4,}\|\t\{1,}\).*\n/ contained nextgroup=mkdCodeBlock
hi link mkdCodeBlock comment
Run Code Online (Sandbox Code Playgroud)
Unorder List项目定义:
例:
Regular text
- item 1
- sub item 1
- sub item 2
- item 2
this is part of item 2
so is this
- item 3, still in the same list
- sub item 1
- sub item 2
Regular text, list ends above
Run Code Online (Sandbox Code Playgroud)
我的Vim语法为unorder列表项定义(我只突出显示[-+*]
):
syn region mkdListItem start=/\s*[-*+]\s\+/ matchgroup=pdcListText end=".*" contained nextgroup=mkdListItem,mkdListSkipNL contains=@Spell skipnl
syn match mkdListSkipNL /\s*\n/ contained nextgroup=mkdListItem,mkdListSkipNL skipnl
hi link mkdListItem operator
Run Code Online (Sandbox Code Playgroud)
我无法突出显示使用列表的最后两个规则和代码块.
这是一个破坏我的语法突出显示的示例:
Regular text
- Item 1
- Item 2
part of item 2
- these 2 line should be highlighted as a list item
- but they are highlighted as a code block
Run Code Online (Sandbox Code Playgroud)
我目前无法弄清楚如何让突出显示以我想要的方式工作
忘记添加下面列出的两个规则中使用的"全局"语法规则.这是为了确保他们以空行开头.
syn match mkdBlankLine /^\s*\n/ nextgroup=mkdCodeBlock,mkdListItem transparent
Run Code Online (Sandbox Code Playgroud)
另一个注意:我应该更清楚.在我的语法文件中,列表规则出现在Blockquote规则之前
只需确保mkdListItem的定义在mkdCodeBlock的定义之后,如下所示:
syn match mkdCodeBlock /\(\s\{4,}\|\t\{1,}\).*\n/ contained nextgroup=mkdCodeBlock
hi link mkdCodeBlock comment
syn region mkdListItem start=/\s*[-*+]\s\+/ matchgroup=pdcListText end=".*" contained nextgroup=mkdListItem,mkdListSkipNL contains=@Spell skipnl
syn match mkdListSkipNL /\s*\n/ contained nextgroup=mkdListItem,mkdListSkipNL skipnl
hi link mkdListItem operator
syn match mkdBlankLine /^\s*\n/ nextgroup=mkdCodeBlock,mkdListItem transparent
Run Code Online (Sandbox Code Playgroud)
Vim文档说:help :syn-define
:
"如果多个项目在同一位置匹配,则最后定义的项目将获胜.因此,您可以使用与相同文本匹配的项目覆盖以前定义的语法项目.但关键字始终位于匹配项或区域之前.带有匹配大小写的关键字总是在忽略大小写的关键字之前."