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

Add syntax concealing for italic/bold/italic bold
[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') && get(g:, 'vim_markdown_conceal', 1)
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     let s:oneline = ''
47 else
48     let s:oneline = ' oneline'
49 endif
50 syn region mkdItalic matchgroup=mkdItalic start="\%(\*\|_\)"    end="\%(\*\|_\)"
51 syn region mkdBold matchgroup=mkdBold start="\%(\*\*\|__\)"    end="\%(\*\*\|__\)"
52 syn region mkdBoldItalic matchgroup=mkdBoldItalic start="\%(\*\*\*\|___\)"    end="\%(\*\*\*\|___\)"
53 execute 'syn region htmlItalic matchgroup=mkdItalic start="\%(^\|\s\)\zs\*\ze[^\\\*\t ]\%(\%([^*]\|\\\*\|\n\)*[^\\\*\t ]\)\?\*\_W" end="[^\\\*\t ]\zs\*\ze\_W" keepend' . s:oneline . s:concealends
54 execute 'syn region htmlItalic matchgroup=mkdItalic start="\%(^\|\s\)\zs_\ze[^\\_\t ]" end="[^\\_\t ]\zs_\ze\_W" keepend' . s:oneline . s:concealends
55 execute 'syn region htmlBold matchgroup=mkdBold start="\%(^\|\s\)\*\*\ze\S" end="\S\zs\*\*" keepend' . s:oneline . s:concealends
56 execute 'syn region htmlBold matchgroup=mkdBold start="\%(^\|\s\)\zs__\ze\S" end="\S\zs__" keepend' . s:oneline . s:concealends
57 execute 'syn region htmlBoldItalic matchgroup=mkdBoldItalic start="\%(^\|\s\)\zs\*\*\*\ze\S" end="\S\zs\*\*\*" keepend' . s:oneline . s:concealends
58 execute 'syn region htmlBoldItalic matchgroup=mkdBoldItalic start="\%(^\|\s\)\zs___\ze\S" end="\S\zs___" keepend' . s:oneline . s:concealends
59
60 " [link](URL) | [link][id] | [link][] | ![image](URL)
61 syn region mkdFootnotes matchgroup=mkdDelimiter start="\[^"    end="\]"
62 execute 'syn region mkdID matchgroup=mkdDelimiter    start="\["    end="\]" contained oneline' . s:conceal
63 execute 'syn region mkdURL matchgroup=mkdDelimiter   start="("     end=")"  contained oneline' . s:conceal
64 execute 'syn region mkdLink matchgroup=mkdDelimiter  start="\\\@<!!\?\[" end="\n\{-,1}[^]]\{-}\zs\]\ze[[(]" contains=@mkdNonListItem,@Spell nextgroup=mkdURL,mkdID skipwhite oneline' . s:concealends
65
66 " Autolink without angle brackets.
67 " mkd  inline links:      protocol     optional  user:pass@  sub/domain                    .com, .co.uk, etc         optional port   path/querystring/hash fragment
68 "                         ------------ _____________________ ----------------------------- _________________________ ----------------- __
69 syn match   mkdInlineURL /https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z0-9][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?\S*/
70
71 " Autolink with parenthesis.
72 syn region  mkdInlineURL matchgroup=mkdDelimiter start="(\(https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z0-9][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?\S*)\)\@=" end=")"
73
74 " Autolink with angle brackets.
75 syn region mkdInlineURL matchgroup=mkdDelimiter start="\\\@<!<\ze[a-z][a-z0-9,.-]\{1,22}:\/\/[^> ]*>" end=">"
76
77 " Link definitions: [id]: URL (Optional Title)
78 syn region mkdLinkDef matchgroup=mkdDelimiter   start="^ \{,3}\zs\[" end="]:" oneline nextgroup=mkdLinkDefTarget skipwhite
79 syn region mkdLinkDefTarget start="<\?\zs\S" excludenl end="\ze[>[:space:]\n]"   contained nextgroup=mkdLinkTitle,mkdLinkDef skipwhite skipnl oneline
80 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+"+     end=+"+  contained
81 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+'+     end=+'+  contained
82 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+(+     end=+)+  contained
83
84 "HTML headings
85 syn region htmlH1       start="^\s*#"                   end="$" contains=mkdLink,mkdInlineURL,@Spell
86 syn region htmlH2       start="^\s*##"                  end="$" contains=mkdLink,mkdInlineURL,@Spell
87 syn region htmlH3       start="^\s*###"                 end="$" contains=mkdLink,mkdInlineURL,@Spell
88 syn region htmlH4       start="^\s*####"                end="$" contains=mkdLink,mkdInlineURL,@Spell
89 syn region htmlH5       start="^\s*#####"               end="$" contains=mkdLink,mkdInlineURL,@Spell
90 syn region htmlH6       start="^\s*######"              end="$" contains=mkdLink,mkdInlineURL,@Spell
91 syn match  htmlH1       /^.\+\n=\+$/ contains=mkdLink,mkdInlineURL,@Spell
92 syn match  htmlH2       /^.\+\n-\+$/ contains=mkdLink,mkdInlineURL,@Spell
93
94 "define Markdown groups
95 syn match  mkdLineBreak    /  \+$/
96 syn region mkdBlockquote   start=/^\s*>/                   end=/$/ contains=mkdLink,mkdInlineURL,mkdLineBreak,@Spell
97 syn region mkdCode         start=/\(\([^\\]\|^\)\\\)\@<!`/ end=/\(\([^\\]\|^\)\\\)\@<!`/
98 syn region mkdCode         start=/\s*``[^`]*/              end=/[^`]*``\s*/
99 syn region mkdCode         start=/^\s*\z(`\{3,}\)[^`]*$/   end=/^\s*\z1`*\s*$/
100 syn region mkdCode         start=/\s*\~\~[^\~]*/           end=/[^\~]*\~\~\s*/
101 syn region mkdCode         start=/^\s*\z(\~\{3,}\)\s*[0-9A-Za-z_+-]*\s*$/         end=/^\s*\z1\~*\s*$/
102 syn region mkdCode         start="<pre[^>]*\\\@<!>"        end="</pre>"
103 syn region mkdCode         start="<code[^>]*\\\@<!>"       end="</code>"
104 syn region mkdFootnote     start="\[^"                     end="\]"
105 syn match  mkdCode         /^\s*\n\(\(\s\{8,}[^ ]\|\t\t\+[^\t]\).*\n\)\+/
106 syn match  mkdCode         /\%^\(\(\s\{4,}[^ ]\|\t\+[^\t]\).*\n\)\+/
107 syn match  mkdCode         /^\s*\n\(\(\s\{4,}[^ ]\|\t\+[^\t]\).*\n\)\+/ contained
108 syn match  mkdListItem     /^\s*\%([-*+]\|\d\+\.\)\s\+/ contained
109 syn region mkdListItemLine start="^\s*\%([-*+]\|\d\+\.\)\s\+" end="$" oneline contains=@mkdNonListItem,mkdListItem,@Spell
110 syn region mkdNonListItemBlock start="\(\%^\(\s*\([-*+]\|\d\+\.\)\s\+\)\@!\|\n\(\_^\_$\|\s\{4,}[^ ]\|\t+[^\t]\)\@!\)" end="^\(\s*\([-*+]\|\d\+\.\)\s\+\)\@=" contains=@mkdNonListItem,@Spell
111 syn match  mkdRule         /^\s*\*\s\{0,1}\*\s\{0,1}\*$/
112 syn match  mkdRule         /^\s*-\s\{0,1}-\s\{0,1}-$/
113 syn match  mkdRule         /^\s*_\s\{0,1}_\s\{0,1}_$/
114 syn match  mkdRule         /^\s*-\{3,}$/
115 syn match  mkdRule         /^\s*\*\{3,5}$/
116
117 " YAML frontmatter
118 if get(g:, 'vim_markdown_frontmatter', 0)
119   syn include @yamlTop syntax/yaml.vim
120   syn region Comment matchgroup=mkdDelimiter start="\%^---$" end="^---$" contains=@yamlTop keepend
121   unlet! b:current_syntax
122 endif
123
124 if get(g:, 'vim_markdown_toml_frontmatter', 0)
125   try
126     syn include @tomlTop syntax/toml.vim
127     syn region Comment matchgroup=mkdDelimiter start="\%^+++$" end="^+++$" transparent contains=@tomlTop keepend
128     unlet! b:current_syntax
129   catch /E484/
130     syn region Comment matchgroup=mkdDelimiter start="\%^+++$" end="^+++$"
131   endtry
132 endif
133
134 if get(g:, 'vim_markdown_json_frontmatter', 0)
135   try
136     syn include @jsonTop syntax/json.vim
137     syn region Comment matchgroup=mkdDelimiter start="\%^{$" end="^}$" contains=@jsonTop keepend
138     unlet! b:current_syntax
139   catch /E484/
140     syn region Comment matchgroup=mkdDelimiter start="\%^{$" end="^}$"
141   endtry
142 endif
143
144 if get(g:, 'vim_markdown_math', 0)
145   syn include @tex syntax/tex.vim
146   syn region mkdMath start="\\\@<!\$" end="\$" contains=@tex keepend
147   syn region mkdMath start="\\\@<!\$\$" end="\$\$" contains=@tex keepend
148 endif
149
150 syn cluster mkdNonListItem contains=@htmlTop,htmlItalic,htmlBold,htmlBoldItalic,mkdFootnotes,mkdInlineURL,mkdLink,mkdLinkDef,mkdLineBreak,mkdBlockquote,mkdCode,mkdRule,htmlH1,htmlH2,htmlH3,htmlH4,htmlH5,htmlH6,mkdMath
151
152 "highlighting for Markdown groups
153 HtmlHiLink mkdString        String
154 HtmlHiLink mkdCode          String
155 HtmlHiLink mkdCodeStart     String
156 HtmlHiLink mkdCodeEnd       String
157 HtmlHiLink mkdFootnote      Comment
158 HtmlHiLink mkdBlockquote    Comment
159 HtmlHiLink mkdListItem      Identifier
160 HtmlHiLink mkdRule          Identifier
161 HtmlHiLink mkdLineBreak     Visual
162 HtmlHiLink mkdFootnotes     htmlLink
163 HtmlHiLink mkdLink          htmlLink
164 HtmlHiLink mkdURL           htmlString
165 HtmlHiLink mkdInlineURL     htmlLink
166 HtmlHiLink mkdID            Identifier
167 HtmlHiLink mkdLinkDef       mkdID
168 HtmlHiLink mkdLinkDefTarget mkdURL
169 HtmlHiLink mkdLinkTitle     htmlString
170 HtmlHiLink mkdDelimiter     Delimiter
171
172 let b:current_syntax = "mkd"
173
174 delcommand HtmlHiLink
175 " vim: ts=8