UNPKG

4.7 kBJavaScriptView Raw
1/*
2Language: Markdown
3Requires: xml.js
4Author: John Crepezzi <john.crepezzi@gmail.com>
5Website: https://daringfireball.net/projects/markdown/
6Category: common, markup
7*/
8
9function markdown(hljs) {
10 const regex = hljs.regex;
11 const INLINE_HTML = {
12 begin: /<\/?[A-Za-z_]/,
13 end: '>',
14 subLanguage: 'xml',
15 relevance: 0
16 };
17 const HORIZONTAL_RULE = {
18 begin: '^[-\\*]{3,}',
19 end: '$'
20 };
21 const CODE = {
22 className: 'code',
23 variants: [
24 // TODO: fix to allow these to work with sublanguage also
25 {
26 begin: '(`{3,})[^`](.|\\n)*?\\1`*[ ]*'
27 },
28 {
29 begin: '(~{3,})[^~](.|\\n)*?\\1~*[ ]*'
30 },
31 // needed to allow markdown as a sublanguage to work
32 {
33 begin: '```',
34 end: '```+[ ]*$'
35 },
36 {
37 begin: '~~~',
38 end: '~~~+[ ]*$'
39 },
40 {
41 begin: '`.+?`'
42 },
43 {
44 begin: '(?=^( {4}|\\t))',
45 // use contains to gobble up multiple lines to allow the block to be whatever size
46 // but only have a single open/close tag vs one per line
47 contains: [
48 {
49 begin: '^( {4}|\\t)',
50 end: '(\\n)$'
51 }
52 ],
53 relevance: 0
54 }
55 ]
56 };
57 const LIST = {
58 className: 'bullet',
59 begin: '^[ \t]*([*+-]|(\\d+\\.))(?=\\s+)',
60 end: '\\s+',
61 excludeEnd: true
62 };
63 const LINK_REFERENCE = {
64 begin: /^\[[^\n]+\]:/,
65 returnBegin: true,
66 contains: [
67 {
68 className: 'symbol',
69 begin: /\[/,
70 end: /\]/,
71 excludeBegin: true,
72 excludeEnd: true
73 },
74 {
75 className: 'link',
76 begin: /:\s*/,
77 end: /$/,
78 excludeBegin: true
79 }
80 ]
81 };
82 const URL_SCHEME = /[A-Za-z][A-Za-z0-9+.-]*/;
83 const LINK = {
84 variants: [
85 // too much like nested array access in so many languages
86 // to have any real relevance
87 {
88 begin: /\[.+?\]\[.*?\]/,
89 relevance: 0
90 },
91 // popular internet URLs
92 {
93 begin: /\[.+?\]\(((data|javascript|mailto):|(?:http|ftp)s?:\/\/).*?\)/,
94 relevance: 2
95 },
96 {
97 begin: regex.concat(/\[.+?\]\(/, URL_SCHEME, /:\/\/.*?\)/),
98 relevance: 2
99 },
100 // relative urls
101 {
102 begin: /\[.+?\]\([./?&#].*?\)/,
103 relevance: 1
104 },
105 // whatever else, lower relevance (might not be a link at all)
106 {
107 begin: /\[.*?\]\(.*?\)/,
108 relevance: 0
109 }
110 ],
111 returnBegin: true,
112 contains: [
113 {
114 // empty strings for alt or link text
115 match: /\[(?=\])/
116 },
117 {
118 className: 'string',
119 relevance: 0,
120 begin: '\\[',
121 end: '\\]',
122 excludeBegin: true,
123 returnEnd: true
124 },
125 {
126 className: 'link',
127 relevance: 0,
128 begin: '\\]\\(',
129 end: '\\)',
130 excludeBegin: true,
131 excludeEnd: true
132 },
133 {
134 className: 'symbol',
135 relevance: 0,
136 begin: '\\]\\[',
137 end: '\\]',
138 excludeBegin: true,
139 excludeEnd: true
140 }
141 ]
142 };
143 const BOLD = {
144 className: 'strong',
145 contains: [], // defined later
146 variants: [
147 {
148 begin: /_{2}/,
149 end: /_{2}/
150 },
151 {
152 begin: /\*{2}/,
153 end: /\*{2}/
154 }
155 ]
156 };
157 const ITALIC = {
158 className: 'emphasis',
159 contains: [], // defined later
160 variants: [
161 {
162 begin: /\*(?!\*)/,
163 end: /\*/
164 },
165 {
166 begin: /_(?!_)/,
167 end: /_/,
168 relevance: 0
169 }
170 ]
171 };
172 BOLD.contains.push(ITALIC);
173 ITALIC.contains.push(BOLD);
174
175 let CONTAINABLE = [
176 INLINE_HTML,
177 LINK
178 ];
179
180 BOLD.contains = BOLD.contains.concat(CONTAINABLE);
181 ITALIC.contains = ITALIC.contains.concat(CONTAINABLE);
182
183 CONTAINABLE = CONTAINABLE.concat(BOLD, ITALIC);
184
185 const HEADER = {
186 className: 'section',
187 variants: [
188 {
189 begin: '^#{1,6}',
190 end: '$',
191 contains: CONTAINABLE
192 },
193 {
194 begin: '(?=^.+?\\n[=-]{2,}$)',
195 contains: [
196 {
197 begin: '^[=-]*$'
198 },
199 {
200 begin: '^',
201 end: "\\n",
202 contains: CONTAINABLE
203 }
204 ]
205 }
206 ]
207 };
208
209 const BLOCKQUOTE = {
210 className: 'quote',
211 begin: '^>\\s+',
212 contains: CONTAINABLE,
213 end: '$'
214 };
215
216 return {
217 name: 'Markdown',
218 aliases: [
219 'md',
220 'mkdown',
221 'mkd'
222 ],
223 contains: [
224 HEADER,
225 INLINE_HTML,
226 LIST,
227 BOLD,
228 ITALIC,
229 BLOCKQUOTE,
230 CODE,
231 HORIZONTAL_RULE,
232 LINK,
233 LINK_REFERENCE
234 ]
235 };
236}
237
238export { markdown as default };