Vim Markdown突出显示(列出项目和代码块冲突)

时间:2008-09-26 05:50:17

标签: regex vim syntax markdown highlight

我决定更多地了解vim及其语法突出显示。 使用其他人的示例,我正在为Markdown创建自己的语法文件。我见过mkd.vim,它也有这个问题。 我的问题是在列表项和代码块突出显示之间。

代码块definition

  • 第一行是空白
  • 第二行以至少4个空格或1个标签
  • 开头
  • 块以空白行结束

示例:

Regular text

    this is code, monospaced and left untouched by markdown
    another line of code

Regular Text

代码块的我的Vim语法:

syn match mkdCodeBlock   /\(\s\{4,}\|\t\{1,}\).*\n/ contained nextgroup=mkdCodeBlock  

hi link mkdCodeBlock  comment

取消订单列表项definition

  • 第一行是空白
  • 第二行以[ - + *]开头,后跟空格
  • 列表以空白行结束,然后是正常(非列表)行
  • 在行项目之间
  • 可以添加任意数量的空行
  • 通过缩进(4个空格或1个制表符)
  • 指定子列表
  • 列表项之后的一行普通文本作为该列表项的延续

示例:

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

用于无序列表项定义的我的Vim语法(我只突出显示[-+*]):

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

我无法突出显示列表的最后两条规则和代码块。

这是一个突破我语法突出显示的示例:

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

我目前无法弄清楚如何让突出显示按我想要的方式工作


忘记添加下面列出的两个规则中使用的“全局”语法规则。这是为了确保他们以空行开头。

syn match mkdBlankLine   /^\s*\n/    nextgroup=mkdCodeBlock,mkdListItem transparent

另一个注意:我应该更清楚。在我的语法文件中,列表规则出现在Blockquote Rules

之前

3 个答案:

答案 0 :(得分:6)

确保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

Vim文档在:help :syn-define中说:

“如果多个项目在同一位置匹配,那么就是那个 定义了最后的胜利。因此,您可以覆盖以前定义的语法项 使用匹配相同文本的项目。但关键字总是在a之前 比赛或地区。具有匹配大小写的关键字总是在a之前 忽略大小写的关键字。“

答案 1 :(得分:1)

hcs42是正确的。我记得现在正在读这一部分,但我忘了它,直到hcs24让我想起它。

以下是我的更新语法(其他一些调整):

"""""""""""""""""""""""""""""""""""""""
" Code Blocks:

"   Indent with at least 4 space or 1 tab
"   This rule must appear for mkdListItem, or highlighting gets messed up
syn match mkdCodeBlock   /\(\s\{4,}\|\t\{1,}\).*\n/ contained nextgroup=mkdCodeBlock  

"""""""""""""""""""""""""""""""""""""""
" Lists:

"   These first two rules need to be first or the highlighting will be
"   incorrect

"   Continue a list on the current line or next line
syn match mkdListCont /\s*[^-+*].*/ contained nextgroup=mkdListCont,mkdListItem,mkdListSkipNL contains=@Spell skipnl transparent

"   Skip empty lines
syn match mkdListSkipNL /\s*\n/ contained nextgroup=mkdListItem,mkdListSkipNL 

"   Unorder list
syn match  mkdListItem /\s*[-*+]\s\+/ contained nextgroup=mkdListSkipNL,mkdListCont  skipnl 

答案 2 :(得分:0)

Tao Zhyn,可能涵盖了您的用例,但它没有涵盖Markdown语法。在Markdown中,列表项可以包含代码块。您可以查看我的解决方案here

TL; DR;问题是vim不会让你这样说:一个与其容器具有相同缩进的块+4个空格。我找到的唯一解决方案是为每种类型的块生成规则,这些块可以包含在每个缩进级别的列表项中(实际上我支持42级缩进,但它是一个任意数字)

所以我有markdownCodeBlockInListItemAtLevel1 必须包含在markdownListItemAtLevel1中它需要至少有8个前导空格,然后是markdownCodeBlockInListItemAtLevel2 必须包含在markdownListItemAtLevel2中必须包含在markdownListItemAtLevel1中,ant需要至少有10个前导空格,ecc ......

我知道几年过去了但也许有人会认为这个答案很有用,因为所有基于缩进的语法都会遇到同样的问题

相关问题