]> git.madduck.net Git - etc/vim.git/blob - syntax/markdown.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix single-line emphasis dealing with escapes
[etc/vim.git] / syntax / markdown.vim
1 " Vim syntax file
2 " Language:     Markdown
3 " Maintainer:   Ben Williams <benw@plasticboy.com>
4 " URL:          http://plasticboy.com/markdown-vim-mode/
5 " Remark:       Uses HTML syntax file
6 " TODO:         Handle stuff contained within stuff (e.g. headings within blockquotes)
7
8
9 " Read the HTML syntax to start with
10 if version < 600
11   so <sfile>:p:h/html.vim
12 else
13   runtime! syntax/html.vim
14
15   if exists('b:current_syntax')
16     unlet b:current_syntax
17   endif
18 endif
19
20 if version < 600
21   syntax clear
22 elseif exists("b:current_syntax")
23   finish
24 endif
25
26 " don't use standard HiLink, it will not work with included syntax files
27 if version < 508
28   command! -nargs=+ HtmlHiLink hi link <args>
29 else
30   command! -nargs=+ HtmlHiLink hi def link <args>
31 endif
32
33 syn spell toplevel
34 syn case ignore
35 syn sync linebreaks=1
36
37 let s:conceal = ''
38 let s:concealends = ''
39 if has('conceal')
40   let s:conceal = ' conceal'
41   let s:concealends = ' concealends'
42 endif
43
44 "additions to HTML groups
45 if get(g:, "vim_markdown_emphasis_multiline", 1)
46     syn region htmlItalic start="\%(^\|\s\)\zs\*\ze[^\\\*\t ]" end="[^\\\*\t ]\zs\*\ze\_W" keepend
47     syn region htmlItalic start="\%(^\|\s\)\zs_\ze[^\\_\t ]" end="[^\\_\t ]\zs_\ze\_W" keepend
48     syn region htmlBold start="\*\*\ze\S" end="\S\zs\*\*" keepend
49     syn region htmlBold start="__\ze\S" end="\S\zs__" keepend
50     syn region htmlBoldItalic start="\*\*\*\ze\S" end="\S\zs\*\*\*" keepend
51     syn region htmlBoldItalic start="___\ze\S" end="\S\zs___" keepend
52 else
53     " single-line emphasis (emphasis only works with closing token on the same line)
54     syn region htmlItalic start="\%(^\|\s\)\zs\*\ze[^\\\*\t ]\([^\n]*[^\\\*\t ]\)\?\*" end="[^\\\*\t ]\zs\*\ze\_W" keepend
55     syn region htmlItalic start="\%(^\|\s\)\zs_\ze[^\\_\t ]\([^\n]*[^\\_\t ]\)\?_" end="[^\\_\t ]\zs_\ze\_W" keepend
56     syn region htmlBold start="\*\*\ze\S\([^\n]*[^\\\*\t ]\)\?\*\*" end="\S\zs\*\*" keepend
57     syn region htmlBold start="__\ze\S\([^\n]*[^\\_\t ]\)\?__" end="\S\zs__" keepend
58     syn region htmlBold start="\*\*\*\ze\S\([^\n]*[^\\\*\t ]\)\?\*\*\*" end="\S\zs\*\*\*" keepend
59     syn region htmlBold start="___\ze\S\([^\n]*[^\\_\t ]\)\?___" end="\S\zs___" keepend
60 endif
61
62 " [link](URL) | [link][id] | [link][] | ![image](URL)
63 syn region mkdFootnotes matchgroup=mkdDelimiter start="\[^"    end="\]"
64 execute 'syn region mkdID matchgroup=mkdDelimiter    start="\["    end="\]" contained oneline' . s:conceal
65 execute 'syn region mkdURL matchgroup=mkdDelimiter   start="("     end=")"  contained oneline' . s:conceal
66 execute 'syn region mkdLink matchgroup=mkdDelimiter  start="\\\@<!!\?\[" end="\n\{-,1}[^]]\{-}\zs\]\ze[[(]" contains=@mkdNonListItem,@Spell nextgroup=mkdURL,mkdID skipwhite oneline' . s:concealends
67
68 " Autolink without angle brackets.
69 " mkd  inline links:           protocol   optional  user:pass@       sub/domain                 .com, .co.uk, etc      optional port   path/querystring/hash fragment
70 "                            ------------ _____________________ --------------------------- ________________________ ----------------- __
71 syntax match   mkdInlineURL /https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?\S*/
72
73 " Autolink with parenthesis.
74 syntax region  mkdInlineURL matchgroup=mkdDelimiter start="(\(https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?\S*)\)\@=" end=")"
75
76 " Autolink with angle brackets.
77 syn region mkdInlineURL matchgroup=mkdDelimiter start="\\\@<!<\ze[a-z][a-z0-9,.-]\{1,22}:\/\/[^> ]*>" end=">"
78
79 " Link definitions: [id]: URL (Optional Title)
80 syn region mkdLinkDef matchgroup=mkdDelimiter   start="^ \{,3}\zs\[" end="]:" oneline nextgroup=mkdLinkDefTarget skipwhite
81 syn region mkdLinkDefTarget start="<\?\zs\S" excludenl end="\ze[>[:space:]\n]"   contained nextgroup=mkdLinkTitle,mkdLinkDef skipwhite skipnl oneline
82 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+"+     end=+"+  contained
83 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+'+     end=+'+  contained
84 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+(+     end=+)+  contained
85
86 "HTML headings
87 syn region htmlH1       start="^\s*#"                   end="$" contains=@Spell
88 syn region htmlH2       start="^\s*##"                  end="$" contains=@Spell
89 syn region htmlH3       start="^\s*###"                 end="$" contains=@Spell
90 syn region htmlH4       start="^\s*####"                end="$" contains=@Spell
91 syn region htmlH5       start="^\s*#####"               end="$" contains=@Spell
92 syn region htmlH6       start="^\s*######"              end="$" contains=@Spell
93 syn match  htmlH1       /^.\+\n=\+$/ contains=@Spell
94 syn match  htmlH2       /^.\+\n-\+$/ contains=@Spell
95
96 "define Markdown groups
97 syn match  mkdLineBreak    /  \+$/
98 syn region mkdBlockquote   start=/^\s*>/                   end=/$/ contains=mkdLineBreak,@Spell
99 syn region mkdCode         start=/\(\([^\\]\|^\)\\\)\@<!`/ end=/\(\([^\\]\|^\)\\\)\@<!`/
100 syn region mkdCode         start=/\s*``[^`]*/              end=/[^`]*``\s*/
101 syn region mkdCode         start=/^\s*\z(`\{3,}\)[^`]*$/   end=/^\s*\z1`*\s*$/
102 syn region mkdCode         start=/\s*\~\~[^\~]*/           end=/[^\~]*\~\~\s*/
103 syn region mkdCode         start=/^\s*\z(\~\{3,}\)\s*[0-9A-Za-z_+-]*\s*$/         end=/^\s*\z1\~*\s*$/
104 syn region mkdCode         start="<pre[^>]*\\\@<!>"        end="</pre>"
105 syn region mkdCode         start="<code[^>]*\\\@<!>"       end="</code>"
106 syn region mkdFootnote     start="\[^"                     end="\]"
107 syn match  mkdCode         /^\s*\n\(\(\s\{8,}[^ ]\|\t\t\+[^\t]\).*\n\)\+/
108 syn match  mkdCode         /\%^\(\(\s\{4,}[^ ]\|\t\+[^\t]\).*\n\)\+/
109 syn match  mkdCode         /^\s*\n\(\(\s\{4,}[^ ]\|\t\+[^\t]\).*\n\)\+/ contained
110 syn match  mkdListItem     /^\s*\%([-*+]\|\d\+\.\)\s\+/ contained
111 syn region mkdListItemLine start="^\s*\%([-*+]\|\d\+\.\)\s\+" end="$" oneline contains=@mkdNonListItem,mkdListItem,@Spell
112 syn region mkdNonListItemBlock start="\(\%^\(\s*\([-*+]\|\d\+\.\)\s\+\)\@!\|\n\(\_^\_$\|\s\{4,}[^ ]\|\t+[^\t]\)\@!\)" end="^\(\s*\([-*+]\|\d\+\.\)\s\+\)\@=" contains=@mkdNonListItem,@Spell
113 syn match  mkdRule         /^\s*\*\s\{0,1}\*\s\{0,1}\*$/
114 syn match  mkdRule         /^\s*-\s\{0,1}-\s\{0,1}-$/
115 syn match  mkdRule         /^\s*_\s\{0,1}_\s\{0,1}_$/
116 syn match  mkdRule         /^\s*-\{3,}$/
117 syn match  mkdRule         /^\s*\*\{3,5}$/
118
119 " YAML frontmatter
120 if get(g:, 'vim_markdown_frontmatter', 0)
121   syn include @yamlTop syntax/yaml.vim
122   syn region Comment matchgroup=mkdDelimiter start="\%^---$" end="^---$" contains=@yamlTop
123   unlet! b:current_syntax
124 endif
125
126 if get(g:, 'vim_markdown_toml_frontmatter', 0)
127   try
128     syn include @tomlTop syntax/toml.vim
129     syn region Comment matchgroup=mkdDelimiter start="\%^+++$" end="^+++$" transparent contains=@tomlTop
130     unlet! b:current_syntax
131   catch /E484/
132     syn region Comment matchgroup=mkdDelimiter start="\%^+++$" end="^+++$"
133   endtry
134 endif
135
136 if get(g:, 'vim_markdown_json_frontmatter', 0)
137   try
138     syn include @jsonTop syntax/json.vim
139     syn region Comment matchgroup=mkdDelimiter start="\%^{$" end="^}$" contains=@jsonTop
140     unlet! b:current_syntax
141   catch /E484/
142     syn region Comment matchgroup=mkdDelimiter start="\%^{$" end="^}$"
143   endtry
144 endif
145
146 if get(g:, 'vim_markdown_math', 0)
147   syn include @tex syntax/tex.vim
148   syn region mkdMath matchgroup=mkdDelimiter start="\\\@<!\$" end="\$" contains=@tex
149   syn region mkdMath matchgroup=mkdDelimiter start="\\\@<!\$\$" end="\$\$" contains=@tex
150 endif
151
152 syn cluster mkdNonListItem contains=@htmlTop,htmlItalic,htmlBold,htmlBoldItalic,mkdFootnotes,mkdInlineURL,mkdLink,mkdLinkDef,mkdLineBreak,mkdBlockquote,mkdCode,mkdRule,htmlH1,htmlH2,htmlH3,htmlH4,htmlH5,htmlH6,mkdMath
153
154 "highlighting for Markdown groups
155 HtmlHiLink mkdString        String
156 HtmlHiLink mkdCode          String
157 HtmlHiLink mkdCodeStart     String
158 HtmlHiLink mkdCodeEnd       String
159 HtmlHiLink mkdFootnote      Comment
160 HtmlHiLink mkdBlockquote    Comment
161 HtmlHiLink mkdListItem      Identifier
162 HtmlHiLink mkdRule          Identifier
163 HtmlHiLink mkdLineBreak     Todo
164 HtmlHiLink mkdFootnotes     htmlLink
165 HtmlHiLink mkdLink          htmlLink
166 HtmlHiLink mkdURL           htmlString
167 HtmlHiLink mkdInlineURL     htmlLink
168 HtmlHiLink mkdID            Identifier
169 HtmlHiLink mkdLinkDef       mkdID
170 HtmlHiLink mkdLinkDefTarget mkdURL
171 HtmlHiLink mkdLinkTitle     htmlString
172 HtmlHiLink mkdDelimiter     Delimiter
173
174 let b:current_syntax = "mkd"
175
176 delcommand HtmlHiLink
177 " vim: ts=8