UNPKG

8.86 kBJavaScriptView Raw
1(function (Prism) {
2
3 // Allow only one line break
4 var inner = /(?:\\.|[^\\\n\r]|(?:\n|\r\n?)(?!\n|\r\n?))/.source;
5
6 /**
7 * This function is intended for the creation of the bold or italic pattern.
8 *
9 * This also adds a lookbehind group to the given pattern to ensure that the pattern is not backslash-escaped.
10 *
11 * _Note:_ Keep in mind that this adds a capturing group.
12 *
13 * @param {string} pattern
14 * @returns {RegExp}
15 */
16 function createInline(pattern) {
17 pattern = pattern.replace(/<inner>/g, function () { return inner; });
18 return RegExp(/((?:^|[^\\])(?:\\{2})*)/.source + '(?:' + pattern + ')');
19 }
20
21
22 var tableCell = /(?:\\.|``(?:[^`\r\n]|`(?!`))+``|`[^`\r\n]+`|[^\\|\r\n`])+/.source;
23 var tableRow = /\|?__(?:\|__)+\|?(?:(?:\n|\r\n?)|$)/.source.replace(/__/g, function () { return tableCell; });
24 var tableLine = /\|?[ \t]*:?-{3,}:?[ \t]*(?:\|[ \t]*:?-{3,}:?[ \t]*)+\|?(?:\n|\r\n?)/.source;
25
26
27 Prism.languages.markdown = Prism.languages.extend('markup', {});
28 Prism.languages.insertBefore('markdown', 'prolog', {
29 'blockquote': {
30 // > ...
31 pattern: /^>(?:[\t ]*>)*/m,
32 alias: 'punctuation'
33 },
34 'table': {
35 pattern: RegExp('^' + tableRow + tableLine + '(?:' + tableRow + ')*', 'm'),
36 inside: {
37 'table-data-rows': {
38 pattern: RegExp('^(' + tableRow + tableLine + ')(?:' + tableRow + ')*$'),
39 lookbehind: true,
40 inside: {
41 'table-data': {
42 pattern: RegExp(tableCell),
43 inside: Prism.languages.markdown
44 },
45 'punctuation': /\|/
46 }
47 },
48 'table-line': {
49 pattern: RegExp('^(' + tableRow + ')' + tableLine + '$'),
50 lookbehind: true,
51 inside: {
52 'punctuation': /\||:?-{3,}:?/
53 }
54 },
55 'table-header-row': {
56 pattern: RegExp('^' + tableRow + '$'),
57 inside: {
58 'table-header': {
59 pattern: RegExp(tableCell),
60 alias: 'important',
61 inside: Prism.languages.markdown
62 },
63 'punctuation': /\|/
64 }
65 }
66 }
67 },
68 'code': [
69 {
70 // Prefixed by 4 spaces or 1 tab and preceded by an empty line
71 pattern: /((?:^|\n)[ \t]*\n|(?:^|\r\n?)[ \t]*\r\n?)(?: {4}|\t).+(?:(?:\n|\r\n?)(?: {4}|\t).+)*/,
72 lookbehind: true,
73 alias: 'keyword'
74 },
75 {
76 // `code`
77 // ``code``
78 pattern: /``.+?``|`[^`\r\n]+`/,
79 alias: 'keyword'
80 },
81 {
82 // ```optional language
83 // code block
84 // ```
85 pattern: /^```[\s\S]*?^```$/m,
86 greedy: true,
87 inside: {
88 'code-block': {
89 pattern: /^(```.*(?:\n|\r\n?))[\s\S]+?(?=(?:\n|\r\n?)^```$)/m,
90 lookbehind: true
91 },
92 'code-language': {
93 pattern: /^(```).+/,
94 lookbehind: true
95 },
96 'punctuation': /```/
97 }
98 }
99 ],
100 'title': [
101 {
102 // title 1
103 // =======
104
105 // title 2
106 // -------
107 pattern: /\S.*(?:\n|\r\n?)(?:==+|--+)(?=[ \t]*$)/m,
108 alias: 'important',
109 inside: {
110 punctuation: /==+$|--+$/
111 }
112 },
113 {
114 // # title 1
115 // ###### title 6
116 pattern: /(^\s*)#+.+/m,
117 lookbehind: true,
118 alias: 'important',
119 inside: {
120 punctuation: /^#+|#+$/
121 }
122 }
123 ],
124 'hr': {
125 // ***
126 // ---
127 // * * *
128 // -----------
129 pattern: /(^\s*)([*-])(?:[\t ]*\2){2,}(?=\s*$)/m,
130 lookbehind: true,
131 alias: 'punctuation'
132 },
133 'list': {
134 // * item
135 // + item
136 // - item
137 // 1. item
138 pattern: /(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,
139 lookbehind: true,
140 alias: 'punctuation'
141 },
142 'url-reference': {
143 // [id]: http://example.com "Optional title"
144 // [id]: http://example.com 'Optional title'
145 // [id]: http://example.com (Optional title)
146 // [id]: <http://example.com> "Optional title"
147 pattern: /!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,
148 inside: {
149 'variable': {
150 pattern: /^(!?\[)[^\]]+/,
151 lookbehind: true
152 },
153 'string': /(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,
154 'punctuation': /^[\[\]!:]|[<>]/
155 },
156 alias: 'url'
157 },
158 'bold': {
159 // **strong**
160 // __strong__
161
162 // allow one nested instance of italic text using the same delimiter
163 pattern: createInline(/\b__(?:(?!_)<inner>|_(?:(?!_)<inner>)+_)+__\b|\*\*(?:(?!\*)<inner>|\*(?:(?!\*)<inner>)+\*)+\*\*/.source),
164 lookbehind: true,
165 greedy: true,
166 inside: {
167 'content': {
168 pattern: /(^..)[\s\S]+(?=..$)/,
169 lookbehind: true,
170 inside: {} // see below
171 },
172 'punctuation': /\*\*|__/
173 }
174 },
175 'italic': {
176 // *em*
177 // _em_
178
179 // allow one nested instance of bold text using the same delimiter
180 pattern: createInline(/\b_(?:(?!_)<inner>|__(?:(?!_)<inner>)+__)+_\b|\*(?:(?!\*)<inner>|\*\*(?:(?!\*)<inner>)+\*\*)+\*/.source),
181 lookbehind: true,
182 greedy: true,
183 inside: {
184 'content': {
185 pattern: /(^.)[\s\S]+(?=.$)/,
186 lookbehind: true,
187 inside: {} // see below
188 },
189 'punctuation': /[*_]/
190 }
191 },
192 'strike': {
193 // ~~strike through~~
194 // ~strike~
195 pattern: createInline(/(~~?)(?:(?!~)<inner>)+?\2/.source),
196 lookbehind: true,
197 greedy: true,
198 inside: {
199 'content': {
200 pattern: /(^~~?)[\s\S]+(?=\1$)/,
201 lookbehind: true,
202 inside: {} // see below
203 },
204 'punctuation': /~~?/
205 }
206 },
207 'url': {
208 // [example](http://example.com "Optional title")
209 // [example][id]
210 // [example] [id]
211 pattern: createInline(/!?\[(?:(?!\])<inner>)+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[(?:(?!\])<inner>)+\])/.source),
212 lookbehind: true,
213 greedy: true,
214 inside: {
215 'variable': {
216 pattern: /(\[)[^\]]+(?=\]$)/,
217 lookbehind: true
218 },
219 'content': {
220 pattern: /(^!?\[)[^\]]+(?=\])/,
221 lookbehind: true,
222 inside: {} // see below
223 },
224 'string': {
225 pattern: /"(?:\\.|[^"\\])*"(?=\)$)/
226 }
227 }
228 }
229 });
230
231 ['url', 'bold', 'italic', 'strike'].forEach(function (token) {
232 ['url', 'bold', 'italic', 'strike'].forEach(function (inside) {
233 if (token !== inside) {
234 Prism.languages.markdown[token].inside.content.inside[inside] = Prism.languages.markdown[inside];
235 }
236 });
237 });
238
239 Prism.hooks.add('after-tokenize', function (env) {
240 if (env.language !== 'markdown' && env.language !== 'md') {
241 return;
242 }
243
244 function walkTokens(tokens) {
245 if (!tokens || typeof tokens === 'string') {
246 return;
247 }
248
249 for (var i = 0, l = tokens.length; i < l; i++) {
250 var token = tokens[i];
251
252 if (token.type !== 'code') {
253 walkTokens(token.content);
254 continue;
255 }
256
257 /*
258 * Add the correct `language-xxxx` class to this code block. Keep in mind that the `code-language` token
259 * is optional. But the grammar is defined so that there is only one case we have to handle:
260 *
261 * token.content = [
262 * <span class="punctuation">```</span>,
263 * <span class="code-language">xxxx</span>,
264 * '\n', // exactly one new lines (\r or \n or \r\n)
265 * <span class="code-block">...</span>,
266 * '\n', // exactly one new lines again
267 * <span class="punctuation">```</span>
268 * ];
269 */
270
271 var codeLang = token.content[1];
272 var codeBlock = token.content[3];
273
274 if (codeLang && codeBlock &&
275 codeLang.type === 'code-language' && codeBlock.type === 'code-block' &&
276 typeof codeLang.content === 'string') {
277
278 // this might be a language that Prism does not support
279
280 // do some replacements to support C++, C#, and F#
281 var lang = codeLang.content.replace(/\b#/g, 'sharp').replace(/\b\+\+/g, 'pp')
282 // only use the first word
283 lang = (/[a-z][\w-]*/i.exec(lang) || [''])[0].toLowerCase();
284 var alias = 'language-' + lang;
285
286 // add alias
287 if (!codeBlock.alias) {
288 codeBlock.alias = [alias];
289 } else if (typeof codeBlock.alias === 'string') {
290 codeBlock.alias = [codeBlock.alias, alias];
291 } else {
292 codeBlock.alias.push(alias);
293 }
294 }
295 }
296 }
297
298 walkTokens(env.tokens);
299 });
300
301 Prism.hooks.add('wrap', function (env) {
302 if (env.type !== 'code-block') {
303 return;
304 }
305
306 var codeLang = '';
307 for (var i = 0, l = env.classes.length; i < l; i++) {
308 var cls = env.classes[i];
309 var match = /language-(.+)/.exec(cls);
310 if (match) {
311 codeLang = match[1];
312 break;
313 }
314 }
315
316 var grammar = Prism.languages[codeLang];
317
318 if (!grammar) {
319 if (codeLang && codeLang !== 'none' && Prism.plugins.autoloader) {
320 var id = 'md-' + new Date().valueOf() + '-' + Math.floor(Math.random() * 1e16);
321 env.attributes['id'] = id;
322
323 Prism.plugins.autoloader.loadLanguages(codeLang, function () {
324 var ele = document.getElementById(id);
325 if (ele) {
326 ele.innerHTML = Prism.highlight(ele.textContent, Prism.languages[codeLang], codeLang);
327 }
328 });
329 }
330 } else {
331 // reverse Prism.util.encode
332 var code = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
333
334 env.content = Prism.highlight(code, grammar, codeLang);
335 }
336 });
337
338 Prism.languages.md = Prism.languages.markdown;
339
340}(Prism));