3 " Maintainer: Ben Williams <benw@plasticboy.com>
4 " URL: http://plasticboy.com/markdown-vim-mode/
6 " Last Change: 2009 May 18
7 " Remark: Uses HTML syntax file
8 " Remark: I don't do anything with angle brackets (<>) because that would too easily
9 " easily conflict with HTML syntax
10 " TODO: Handle stuff contained within stuff (e.g. headings within blockquotes)
13 " Read the HTML syntax to start with
15 so <sfile>:p:h/html.vim
17 runtime! syntax/html.vim
18 unlet b:current_syntax
23 elseif exists("b:current_syntax")
27 " don't use standard HiLink, it will not work with included syntax files
29 command! -nargs=+ HtmlHiLink hi link <args>
31 command! -nargs=+ HtmlHiLink hi def link <args>
38 "additions to HTML groups
39 syn region htmlBold start=/\\\@<!\(^\|\A\)\@=\*\@<!\*\*\*\@!/ end=/\\\@<!\*\@<!\*\*\*\@!\($\|\A\)\@=/ contains=@Spell,htmlItalic
40 syn region htmlItalic start=/\\\@<!\(^\|\A\)\@=\*\@<!\*\*\@!/ end=/\\\@<!\*\@<!\*\*\@!\($\|\A\)\@=/ contains=htmlBold,@Spell
41 syn region htmlBold start=/\\\@<!\(^\|\A\)\@=_\@<!___\@!/ end=/\\\@<!_\@<!___\@!\($\|\A\)\@=/ contains=htmlItalic,@Spell
42 syn region htmlItalic start=/\\\@<!\(^\|\A\)\@=_\@<!__\@!/ end=/\\\@<!_\@<!__\@!\($\|\A\)\@=/ contains=htmlBold,@Spell
44 " [link](URL) | [link][id] | [link][]
45 syn region mkdLink matchgroup=mkdDelimiter start="\!\?\[" end="\]\ze\s*[[(]" contains=@Spell nextgroup=mkdURL,mkdID skipwhite
46 syn region mkdID matchgroup=mkdDelimiter start="\[" end="\]" contained
47 syn region mkdURL matchgroup=mkdDelimiter start="(" end=")" contained
48 " mkd inline links: protocol optional user:pass@ sub/domain .com, .co.uk, etc optional port path/querystring/hash fragment
49 " ------------ _____________________ --------------------------- ________________________ ----------------- __
50 syntax match mkdInlineURL /https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?\S*/
52 " Link definitions: [id]: URL (Optional Title)
53 " TODO handle automatic links without colliding with htmlTag (<URL>)
54 syn region mkdLinkDef matchgroup=mkdDelimiter start="^ \{,3}\zs\[" end="]:" oneline nextgroup=mkdLinkDefTarget skipwhite
55 syn region mkdLinkDefTarget start="<\?\zs\S" excludenl end="\ze[>[:space:]\n]" contained nextgroup=mkdLinkTitle,mkdLinkDef skipwhite skipnl oneline
56 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+"+ end=+"+ contained
57 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+'+ end=+'+ contained
58 syn region mkdLinkTitle matchgroup=mkdDelimiter start=+(+ end=+)+ contained
60 "define Markdown groups
61 syn match mkdLineContinue ".$" contained
62 syn match mkdRule /^\s*\*\s\{0,1}\*\s\{0,1}\*$/
63 syn match mkdRule /^\s*-\s\{0,1}-\s\{0,1}-$/
64 syn match mkdRule /^\s*_\s\{0,1}_\s\{0,1}_$/
65 syn match mkdRule /^\s*-\{3,}$/
66 syn match mkdRule /^\s*\*\{3,5}$/
67 syn match mkdListItem "^\s*[-*+]\s\+"
68 syn match mkdListItem "^\s*\d\+\.\s\+"
69 syn match mkdCode /^\s*\n\(\(\s\{4,}[^ ]\|\t\+[^\t]\).*\n\)\+/
70 syn match mkdLineBreak / \+$/
71 syn region mkdCode start=/\\\@<!`/ end=/\\\@<!`/
72 syn region mkdCode start=/\s*``[^`]*/ end=/[^`]*``\s*/
73 syn region mkdBlockquote start=/^\s*>/ end=/$/ contains=mkdLineBreak,mkdLineContinue,@Spell
74 syn region mkdCode start="<pre[^>]*>" end="</pre>"
75 syn region mkdCode start="<code[^>]*>" end="</code>"
78 syn region htmlH1 start="^\s*#" end="\($\|#\+\)" contains=@Spell
79 syn region htmlH2 start="^\s*##" end="\($\|#\+\)" contains=@Spell
80 syn region htmlH3 start="^\s*###" end="\($\|#\+\)" contains=@Spell
81 syn region htmlH4 start="^\s*####" end="\($\|#\+\)" contains=@Spell
82 syn region htmlH5 start="^\s*#####" end="\($\|#\+\)" contains=@Spell
83 syn region htmlH6 start="^\s*######" end="\($\|#\+\)" contains=@Spell
84 syn match htmlH1 /^.\+\n=\+$/ contains=@Spell
85 syn match htmlH2 /^.\+\n-\+$/ contains=@Spell
89 " fold region for headings
90 syn region mkdHeaderFold
91 \ start="^\s*\z(#\+\)"
96 " fold region for lists
97 syn region mkdListFold
98 \ start="^\z(\s*\)\*\z(\s*\)"
99 \ skip="^\z1 \z2\s*[^#]"
104 setlocal foldmethod=syntax
108 "highlighting for Markdown groups
109 HtmlHiLink mkdString String
110 HtmlHiLink mkdCode String
111 HtmlHiLink mkdBlockquote Comment
112 HtmlHiLink mkdLineContinue Comment
113 HtmlHiLink mkdListItem Identifier
114 HtmlHiLink mkdRule Identifier
115 HtmlHiLink mkdLineBreak Todo
116 HtmlHiLink mkdLink htmlLink
117 HtmlHiLink mkdURL htmlString
118 HtmlHiLink mkdInlineURL htmlLink
119 HtmlHiLink mkdID Identifier
120 HtmlHiLink mkdLinkDef mkdID
121 HtmlHiLink mkdLinkDefTarget mkdURL
122 HtmlHiLink mkdLinkTitle htmlString
124 HtmlHiLink mkdDelimiter Delimiter
126 let b:current_syntax = "mkd"
128 delcommand HtmlHiLink