1 | /// <reference lib="WebWorker"/>
|
2 |
|
3 | var _self = (typeof window !== 'undefined')
|
4 | ? window // if in browser
|
5 | : (
|
6 | (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
|
7 | ? self // if in worker
|
8 | : {} // if in node js
|
9 | );
|
10 |
|
11 | /**
|
12 | * Prism: Lightweight, robust, elegant syntax highlighting
|
13 | *
|
14 | * @license MIT <https://opensource.org/licenses/MIT>
|
15 | * @author Lea Verou <https://lea.verou.me>
|
16 | * @namespace
|
17 | * @public
|
18 | */
|
19 | var Prism = (function (_self){
|
20 |
|
21 | // Private helper vars
|
22 | var lang = /\blang(?:uage)?-([\w-]+)\b/i;
|
23 | var uniqueId = 0;
|
24 |
|
25 |
|
26 | var _ = {
|
27 | /**
|
28 | * By default, Prism will attempt to highlight all code elements (by calling {@link Prism.highlightAll}) on the
|
29 | * current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load
|
30 | * additional languages or plugins yourself.
|
31 | *
|
32 | * By setting this value to `true`, Prism will not automatically highlight all code elements on the page.
|
33 | *
|
34 | * You obviously have to change this value before the automatic highlighting started. To do this, you can add an
|
35 | * empty Prism object into the global scope before loading the Prism script like this:
|
36 | *
|
37 | * ```js
|
38 | * window.Prism = window.Prism || {};
|
39 | * Prism.manual = true;
|
40 | * // add a new <script> to load Prism's script
|
41 | * ```
|
42 | *
|
43 | * @default false
|
44 | * @type {boolean}
|
45 | * @memberof Prism
|
46 | * @public
|
47 | */
|
48 | manual: _self.Prism && _self.Prism.manual,
|
49 | disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
|
50 |
|
51 | /**
|
52 | * A namespace for utility methods.
|
53 | *
|
54 | * All function in this namespace that are not explicitly marked as _public_ are for __internal use only__ and may
|
55 | * change or disappear at any time.
|
56 | *
|
57 | * @namespace
|
58 | * @memberof Prism
|
59 | */
|
60 | util: {
|
61 | encode: function encode(tokens) {
|
62 | if (tokens instanceof Token) {
|
63 | return new Token(tokens.type, encode(tokens.content), tokens.alias);
|
64 | } else if (Array.isArray(tokens)) {
|
65 | return tokens.map(encode);
|
66 | } else {
|
67 | return tokens.replace(/&/g, '&').replace(/</g, '<').replace(/\u00a0/g, ' ');
|
68 | }
|
69 | },
|
70 |
|
71 | /**
|
72 | * Returns the name of the type of the given value.
|
73 | *
|
74 | * @param {any} o
|
75 | * @returns {string}
|
76 | * @example
|
77 | * type(null) === 'Null'
|
78 | * type(undefined) === 'Undefined'
|
79 | * type(123) === 'Number'
|
80 | * type('foo') === 'String'
|
81 | * type(true) === 'Boolean'
|
82 | * type([1, 2]) === 'Array'
|
83 | * type({}) === 'Object'
|
84 | * type(String) === 'Function'
|
85 | * type(/abc+/) === 'RegExp'
|
86 | */
|
87 | type: function (o) {
|
88 | return Object.prototype.toString.call(o).slice(8, -1);
|
89 | },
|
90 |
|
91 | /**
|
92 | * Returns a unique number for the given object. Later calls will still return the same number.
|
93 | *
|
94 | * @param {Object} obj
|
95 | * @returns {number}
|
96 | */
|
97 | objId: function (obj) {
|
98 | if (!obj['__id']) {
|
99 | Object.defineProperty(obj, '__id', { value: ++uniqueId });
|
100 | }
|
101 | return obj['__id'];
|
102 | },
|
103 |
|
104 | /**
|
105 | * Creates a deep clone of the given object.
|
106 | *
|
107 | * The main intended use of this function is to clone language definitions.
|
108 | *
|
109 | * @param {T} o
|
110 | * @param {Record<number, any>} [visited]
|
111 | * @returns {T}
|
112 | * @template T
|
113 | */
|
114 | clone: function deepClone(o, visited) {
|
115 | visited = visited || {};
|
116 |
|
117 | var clone, id;
|
118 | switch (_.util.type(o)) {
|
119 | case 'Object':
|
120 | id = _.util.objId(o);
|
121 | if (visited[id]) {
|
122 | return visited[id];
|
123 | }
|
124 | clone = /** @type {Record<string, any>} */ ({});
|
125 | visited[id] = clone;
|
126 |
|
127 | for (var key in o) {
|
128 | if (o.hasOwnProperty(key)) {
|
129 | clone[key] = deepClone(o[key], visited);
|
130 | }
|
131 | }
|
132 |
|
133 | return /** @type {any} */ (clone);
|
134 |
|
135 | case 'Array':
|
136 | id = _.util.objId(o);
|
137 | if (visited[id]) {
|
138 | return visited[id];
|
139 | }
|
140 | clone = [];
|
141 | visited[id] = clone;
|
142 |
|
143 | (/** @type {Array} */(/** @type {any} */(o))).forEach(function (v, i) {
|
144 | clone[i] = deepClone(v, visited);
|
145 | });
|
146 |
|
147 | return /** @type {any} */ (clone);
|
148 |
|
149 | default:
|
150 | return o;
|
151 | }
|
152 | },
|
153 |
|
154 | /**
|
155 | * Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
|
156 | *
|
157 | * If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
|
158 | *
|
159 | * @param {Element} element
|
160 | * @returns {string}
|
161 | */
|
162 | getLanguage: function (element) {
|
163 | while (element && !lang.test(element.className)) {
|
164 | element = element.parentElement;
|
165 | }
|
166 | if (element) {
|
167 | return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
|
168 | }
|
169 | return 'none';
|
170 | },
|
171 |
|
172 | /**
|
173 | * Returns the script element that is currently executing.
|
174 | *
|
175 | * This does __not__ work for line script element.
|
176 | *
|
177 | * @returns {HTMLScriptElement | null}
|
178 | */
|
179 | currentScript: function () {
|
180 | if (typeof document === 'undefined') {
|
181 | return null;
|
182 | }
|
183 | if ('currentScript' in document && 1 < 2 /* hack to trip TS' flow analysis */) {
|
184 | return /** @type {any} */ (document.currentScript);
|
185 | }
|
186 |
|
187 | // IE11 workaround
|
188 | // we'll get the src of the current script by parsing IE11's error stack trace
|
189 | // this will not work for inline scripts
|
190 |
|
191 | try {
|
192 | throw new Error();
|
193 | } catch (err) {
|
194 | // Get file src url from stack. Specifically works with the format of stack traces in IE.
|
195 | // A stack will look like this:
|
196 | //
|
197 | // Error
|
198 | // at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
|
199 | // at Global code (http://localhost/components/prism-core.js:606:1)
|
200 |
|
201 | var src = (/at [^(\r\n]*\((.*):.+:.+\)$/i.exec(err.stack) || [])[1];
|
202 | if (src) {
|
203 | var scripts = document.getElementsByTagName('script');
|
204 | for (var i in scripts) {
|
205 | if (scripts[i].src == src) {
|
206 | return scripts[i];
|
207 | }
|
208 | }
|
209 | }
|
210 | return null;
|
211 | }
|
212 | },
|
213 |
|
214 | /**
|
215 | * Returns whether a given class is active for `element`.
|
216 | *
|
217 | * The class can be activated if `element` or one of its ancestors has the given class and it can be deactivated
|
218 | * if `element` or one of its ancestors has the negated version of the given class. The _negated version_ of the
|
219 | * given class is just the given class with a `no-` prefix.
|
220 | *
|
221 | * Whether the class is active is determined by the closest ancestor of `element` (where `element` itself is
|
222 | * closest ancestor) that has the given class or the negated version of it. If neither `element` nor any of its
|
223 | * ancestors have the given class or the negated version of it, then the default activation will be returned.
|
224 | *
|
225 | * In the paradoxical situation where the closest ancestor contains __both__ the given class and the negated
|
226 | * version of it, the class is considered active.
|
227 | *
|
228 | * @param {Element} element
|
229 | * @param {string} className
|
230 | * @param {boolean} [defaultActivation=false]
|
231 | * @returns {boolean}
|
232 | */
|
233 | isActive: function (element, className, defaultActivation) {
|
234 | var no = 'no-' + className;
|
235 |
|
236 | while (element) {
|
237 | var classList = element.classList;
|
238 | if (classList.contains(className)) {
|
239 | return true;
|
240 | }
|
241 | if (classList.contains(no)) {
|
242 | return false;
|
243 | }
|
244 | element = element.parentElement;
|
245 | }
|
246 | return !!defaultActivation;
|
247 | }
|
248 | },
|
249 |
|
250 | /**
|
251 | * This namespace contains all currently loaded languages and the some helper functions to create and modify languages.
|
252 | *
|
253 | * @namespace
|
254 | * @memberof Prism
|
255 | * @public
|
256 | */
|
257 | languages: {
|
258 | /**
|
259 | * Creates a deep copy of the language with the given id and appends the given tokens.
|
260 | *
|
261 | * If a token in `redef` also appears in the copied language, then the existing token in the copied language
|
262 | * will be overwritten at its original position.
|
263 | *
|
264 | * ## Best practices
|
265 | *
|
266 | * Since the position of overwriting tokens (token in `redef` that overwrite tokens in the copied language)
|
267 | * doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to
|
268 | * understand the language definition because, normally, the order of tokens matters in Prism grammars.
|
269 | *
|
270 | * Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
|
271 | * Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
|
272 | *
|
273 | * @param {string} id The id of the language to extend. This has to be a key in `Prism.languages`.
|
274 | * @param {Grammar} redef The new tokens to append.
|
275 | * @returns {Grammar} The new language created.
|
276 | * @public
|
277 | * @example
|
278 | * Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
|
279 | * // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
|
280 | * // at its original position
|
281 | * 'comment': { ... },
|
282 | * // CSS doesn't have a 'color' token, so this token will be appended
|
283 | * 'color': /\b(?:red|green|blue)\b/
|
284 | * });
|
285 | */
|
286 | extend: function (id, redef) {
|
287 | var lang = _.util.clone(_.languages[id]);
|
288 |
|
289 | for (var key in redef) {
|
290 | lang[key] = redef[key];
|
291 | }
|
292 |
|
293 | return lang;
|
294 | },
|
295 |
|
296 | /**
|
297 | * Inserts tokens _before_ another token in a language definition or any other grammar.
|
298 | *
|
299 | * ## Usage
|
300 | *
|
301 | * This helper method makes it easy to modify existing languages. For example, the CSS language definition
|
302 | * not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded
|
303 | * in HTML through `<style>` elements. To do this, it needs to modify `Prism.languages.markup` and add the
|
304 | * appropriate tokens. However, `Prism.languages.markup` is a regular JavaScript object literal, so if you do
|
305 | * this:
|
306 | *
|
307 | * ```js
|
308 | * Prism.languages.markup.style = {
|
309 | * // token
|
310 | * };
|
311 | * ```
|
312 | *
|
313 | * then the `style` token will be added (and processed) at the end. `insertBefore` allows you to insert tokens
|
314 | * before existing tokens. For the CSS example above, you would use it like this:
|
315 | *
|
316 | * ```js
|
317 | * Prism.languages.insertBefore('markup', 'cdata', {
|
318 | * 'style': {
|
319 | * // token
|
320 | * }
|
321 | * });
|
322 | * ```
|
323 | *
|
324 | * ## Special cases
|
325 | *
|
326 | * If the grammars of `inside` and `insert` have tokens with the same name, the tokens in `inside`'s grammar
|
327 | * will be ignored.
|
328 | *
|
329 | * This behavior can be used to insert tokens after `before`:
|
330 | *
|
331 | * ```js
|
332 | * Prism.languages.insertBefore('markup', 'comment', {
|
333 | * 'comment': Prism.languages.markup.comment,
|
334 | * // tokens after 'comment'
|
335 | * });
|
336 | * ```
|
337 | *
|
338 | * ## Limitations
|
339 | *
|
340 | * The main problem `insertBefore` has to solve is iteration order. Since ES2015, the iteration order for object
|
341 | * properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
|
342 | * differently when keys are deleted and re-inserted. So `insertBefore` can't be implemented by temporarily
|
343 | * deleting properties which is necessary to insert at arbitrary positions.
|
344 | *
|
345 | * To solve this problem, `insertBefore` doesn't actually insert the given tokens into the target object.
|
346 | * Instead, it will create a new object and replace all references to the target object with the new one. This
|
347 | * can be done without temporarily deleting properties, so the iteration order is well-defined.
|
348 | *
|
349 | * However, only references that can be reached from `Prism.languages` or `insert` will be replaced. I.e. if
|
350 | * you hold the target object in a variable, then the value of the variable will not change.
|
351 | *
|
352 | * ```js
|
353 | * var oldMarkup = Prism.languages.markup;
|
354 | * var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... });
|
355 | *
|
356 | * assert(oldMarkup !== Prism.languages.markup);
|
357 | * assert(newMarkup === Prism.languages.markup);
|
358 | * ```
|
359 | *
|
360 | * @param {string} inside The property of `root` (e.g. a language id in `Prism.languages`) that contains the
|
361 | * object to be modified.
|
362 | * @param {string} before The key to insert before.
|
363 | * @param {Grammar} insert An object containing the key-value pairs to be inserted.
|
364 | * @param {Object<string, any>} [root] The object containing `inside`, i.e. the object that contains the
|
365 | * object to be modified.
|
366 | *
|
367 | * Defaults to `Prism.languages`.
|
368 | * @returns {Grammar} The new grammar object.
|
369 | * @public
|
370 | */
|
371 | insertBefore: function (inside, before, insert, root) {
|
372 | root = root || /** @type {any} */ (_.languages);
|
373 | var grammar = root[inside];
|
374 | /** @type {Grammar} */
|
375 | var ret = {};
|
376 |
|
377 | for (var token in grammar) {
|
378 | if (grammar.hasOwnProperty(token)) {
|
379 |
|
380 | if (token == before) {
|
381 | for (var newToken in insert) {
|
382 | if (insert.hasOwnProperty(newToken)) {
|
383 | ret[newToken] = insert[newToken];
|
384 | }
|
385 | }
|
386 | }
|
387 |
|
388 | // Do not insert token which also occur in insert. See #1525
|
389 | if (!insert.hasOwnProperty(token)) {
|
390 | ret[token] = grammar[token];
|
391 | }
|
392 | }
|
393 | }
|
394 |
|
395 | var old = root[inside];
|
396 | root[inside] = ret;
|
397 |
|
398 | // Update references in other language definitions
|
399 | _.languages.DFS(_.languages, function(key, value) {
|
400 | if (value === old && key != inside) {
|
401 | this[key] = ret;
|
402 | }
|
403 | });
|
404 |
|
405 | return ret;
|
406 | },
|
407 |
|
408 | // Traverse a language definition with Depth First Search
|
409 | DFS: function DFS(o, callback, type, visited) {
|
410 | visited = visited || {};
|
411 |
|
412 | var objId = _.util.objId;
|
413 |
|
414 | for (var i in o) {
|
415 | if (o.hasOwnProperty(i)) {
|
416 | callback.call(o, i, o[i], type || i);
|
417 |
|
418 | var property = o[i],
|
419 | propertyType = _.util.type(property);
|
420 |
|
421 | if (propertyType === 'Object' && !visited[objId(property)]) {
|
422 | visited[objId(property)] = true;
|
423 | DFS(property, callback, null, visited);
|
424 | }
|
425 | else if (propertyType === 'Array' && !visited[objId(property)]) {
|
426 | visited[objId(property)] = true;
|
427 | DFS(property, callback, i, visited);
|
428 | }
|
429 | }
|
430 | }
|
431 | }
|
432 | },
|
433 |
|
434 | plugins: {},
|
435 |
|
436 | /**
|
437 | * This is the most high-level function in Prism’s API.
|
438 | * It fetches all the elements that have a `.language-xxxx` class and then calls {@link Prism.highlightElement} on
|
439 | * each one of them.
|
440 | *
|
441 | * This is equivalent to `Prism.highlightAllUnder(document, async, callback)`.
|
442 | *
|
443 | * @param {boolean} [async=false] Same as in {@link Prism.highlightAllUnder}.
|
444 | * @param {HighlightCallback} [callback] Same as in {@link Prism.highlightAllUnder}.
|
445 | * @memberof Prism
|
446 | * @public
|
447 | */
|
448 | highlightAll: function(async, callback) {
|
449 | _.highlightAllUnder(document, async, callback);
|
450 | },
|
451 |
|
452 | /**
|
453 | * Fetches all the descendants of `container` that have a `.language-xxxx` class and then calls
|
454 | * {@link Prism.highlightElement} on each one of them.
|
455 | *
|
456 | * The following hooks will be run:
|
457 | * 1. `before-highlightall`
|
458 | * 2. `before-all-elements-highlight`
|
459 | * 3. All hooks of {@link Prism.highlightElement} for each element.
|
460 | *
|
461 | * @param {ParentNode} container The root element, whose descendants that have a `.language-xxxx` class will be highlighted.
|
462 | * @param {boolean} [async=false] Whether each element is to be highlighted asynchronously using Web Workers.
|
463 | * @param {HighlightCallback} [callback] An optional callback to be invoked on each element after its highlighting is done.
|
464 | * @memberof Prism
|
465 | * @public
|
466 | */
|
467 | highlightAllUnder: function(container, async, callback) {
|
468 | var env = {
|
469 | callback: callback,
|
470 | container: container,
|
471 | selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
|
472 | };
|
473 |
|
474 | _.hooks.run('before-highlightall', env);
|
475 |
|
476 | env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
|
477 |
|
478 | _.hooks.run('before-all-elements-highlight', env);
|
479 |
|
480 | for (var i = 0, element; element = env.elements[i++];) {
|
481 | _.highlightElement(element, async === true, env.callback);
|
482 | }
|
483 | },
|
484 |
|
485 | /**
|
486 | * Highlights the code inside a single element.
|
487 | *
|
488 | * The following hooks will be run:
|
489 | * 1. `before-sanity-check`
|
490 | * 2. `before-highlight`
|
491 | * 3. All hooks of {@link Prism.highlight}. These hooks will be run by an asynchronous worker if `async` is `true`.
|
492 | * 4. `before-insert`
|
493 | * 5. `after-highlight`
|
494 | * 6. `complete`
|
495 | *
|
496 | * Some the above hooks will be skipped if the element doesn't contain any text or there is no grammar loaded for
|
497 | * the element's language.
|
498 | *
|
499 | * @param {Element} element The element containing the code.
|
500 | * It must have a class of `language-xxxx` to be processed, where `xxxx` is a valid language identifier.
|
501 | * @param {boolean} [async=false] Whether the element is to be highlighted asynchronously using Web Workers
|
502 | * to improve performance and avoid blocking the UI when highlighting very large chunks of code. This option is
|
503 | * [disabled by default](https://prismjs.com/faq.html#why-is-asynchronous-highlighting-disabled-by-default).
|
504 | *
|
505 | * Note: All language definitions required to highlight the code must be included in the main `prism.js` file for
|
506 | * asynchronous highlighting to work. You can build your own bundle on the
|
507 | * [Download page](https://prismjs.com/download.html).
|
508 | * @param {HighlightCallback} [callback] An optional callback to be invoked after the highlighting is done.
|
509 | * Mostly useful when `async` is `true`, since in that case, the highlighting is done asynchronously.
|
510 | * @memberof Prism
|
511 | * @public
|
512 | */
|
513 | highlightElement: function(element, async, callback) {
|
514 | // Find language
|
515 | var language = _.util.getLanguage(element);
|
516 | var grammar = _.languages[language];
|
517 |
|
518 | // Set language on the element, if not present
|
519 | element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
|
520 |
|
521 | // Set language on the parent, for styling
|
522 | var parent = element.parentElement;
|
523 | if (parent && parent.nodeName.toLowerCase() === 'pre') {
|
524 | parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
|
525 | }
|
526 |
|
527 | var code = element.textContent;
|
528 |
|
529 | var env = {
|
530 | element: element,
|
531 | language: language,
|
532 | grammar: grammar,
|
533 | code: code
|
534 | };
|
535 |
|
536 | function insertHighlightedCode(highlightedCode) {
|
537 | env.highlightedCode = highlightedCode;
|
538 |
|
539 | _.hooks.run('before-insert', env);
|
540 |
|
541 | env.element.innerHTML = env.highlightedCode;
|
542 |
|
543 | _.hooks.run('after-highlight', env);
|
544 | _.hooks.run('complete', env);
|
545 | callback && callback.call(env.element);
|
546 | }
|
547 |
|
548 | _.hooks.run('before-sanity-check', env);
|
549 |
|
550 | if (!env.code) {
|
551 | _.hooks.run('complete', env);
|
552 | callback && callback.call(env.element);
|
553 | return;
|
554 | }
|
555 |
|
556 | _.hooks.run('before-highlight', env);
|
557 |
|
558 | if (!env.grammar) {
|
559 | insertHighlightedCode(_.util.encode(env.code));
|
560 | return;
|
561 | }
|
562 |
|
563 | if (async && _self.Worker) {
|
564 | var worker = new Worker(_.filename);
|
565 |
|
566 | worker.onmessage = function(evt) {
|
567 | insertHighlightedCode(evt.data);
|
568 | };
|
569 |
|
570 | worker.postMessage(JSON.stringify({
|
571 | language: env.language,
|
572 | code: env.code,
|
573 | immediateClose: true
|
574 | }));
|
575 | }
|
576 | else {
|
577 | insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
|
578 | }
|
579 | },
|
580 |
|
581 | /**
|
582 | * Low-level function, only use if you know what you’re doing. It accepts a string of text as input
|
583 | * and the language definitions to use, and returns a string with the HTML produced.
|
584 | *
|
585 | * The following hooks will be run:
|
586 | * 1. `before-tokenize`
|
587 | * 2. `after-tokenize`
|
588 | * 3. `wrap`: On each {@link Token}.
|
589 | *
|
590 | * @param {string} text A string with the code to be highlighted.
|
591 | * @param {Grammar} grammar An object containing the tokens to use.
|
592 | *
|
593 | * Usually a language definition like `Prism.languages.markup`.
|
594 | * @param {string} language The name of the language definition passed to `grammar`.
|
595 | * @returns {string} The highlighted HTML.
|
596 | * @memberof Prism
|
597 | * @public
|
598 | * @example
|
599 | * Prism.highlight('var foo = true;', Prism.languages.javascript, 'javascript');
|
600 | */
|
601 | highlight: function (text, grammar, language) {
|
602 | var env = {
|
603 | code: text,
|
604 | grammar: grammar,
|
605 | language: language
|
606 | };
|
607 | _.hooks.run('before-tokenize', env);
|
608 | env.tokens = _.tokenize(env.code, env.grammar);
|
609 | _.hooks.run('after-tokenize', env);
|
610 | return Token.stringify(_.util.encode(env.tokens), env.language);
|
611 | },
|
612 |
|
613 | /**
|
614 | * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input
|
615 | * and the language definitions to use, and returns an array with the tokenized code.
|
616 | *
|
617 | * When the language definition includes nested tokens, the function is called recursively on each of these tokens.
|
618 | *
|
619 | * This method could be useful in other contexts as well, as a very crude parser.
|
620 | *
|
621 | * @param {string} text A string with the code to be highlighted.
|
622 | * @param {Grammar} grammar An object containing the tokens to use.
|
623 | *
|
624 | * Usually a language definition like `Prism.languages.markup`.
|
625 | * @returns {TokenStream} An array of strings and tokens, a token stream.
|
626 | * @memberof Prism
|
627 | * @public
|
628 | * @example
|
629 | * let code = `var foo = 0;`;
|
630 | * let tokens = Prism.tokenize(code, Prism.languages.javascript);
|
631 | * tokens.forEach(token => {
|
632 | * if (token instanceof Prism.Token && token.type === 'number') {
|
633 | * console.log(`Found numeric literal: ${token.content}`);
|
634 | * }
|
635 | * });
|
636 | */
|
637 | tokenize: function(text, grammar) {
|
638 | var rest = grammar.rest;
|
639 | if (rest) {
|
640 | for (var token in rest) {
|
641 | grammar[token] = rest[token];
|
642 | }
|
643 |
|
644 | delete grammar.rest;
|
645 | }
|
646 |
|
647 | var tokenList = new LinkedList();
|
648 | addAfter(tokenList, tokenList.head, text);
|
649 |
|
650 | matchGrammar(text, tokenList, grammar, tokenList.head, 0);
|
651 |
|
652 | return toArray(tokenList);
|
653 | },
|
654 |
|
655 | /**
|
656 | * @namespace
|
657 | * @memberof Prism
|
658 | * @public
|
659 | */
|
660 | hooks: {
|
661 | all: {},
|
662 |
|
663 | /**
|
664 | * Adds the given callback to the list of callbacks for the given hook.
|
665 | *
|
666 | * The callback will be invoked when the hook it is registered for is run.
|
667 | * Hooks are usually directly run by a highlight function but you can also run hooks yourself.
|
668 | *
|
669 | * One callback function can be registered to multiple hooks and the same hook multiple times.
|
670 | *
|
671 | * @param {string} name The name of the hook.
|
672 | * @param {HookCallback} callback The callback function which is given environment variables.
|
673 | * @public
|
674 | */
|
675 | add: function (name, callback) {
|
676 | var hooks = _.hooks.all;
|
677 |
|
678 | hooks[name] = hooks[name] || [];
|
679 |
|
680 | hooks[name].push(callback);
|
681 | },
|
682 |
|
683 | /**
|
684 | * Runs a hook invoking all registered callbacks with the given environment variables.
|
685 | *
|
686 | * Callbacks will be invoked synchronously and in the order in which they were registered.
|
687 | *
|
688 | * @param {string} name The name of the hook.
|
689 | * @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
|
690 | * @public
|
691 | */
|
692 | run: function (name, env) {
|
693 | var callbacks = _.hooks.all[name];
|
694 |
|
695 | if (!callbacks || !callbacks.length) {
|
696 | return;
|
697 | }
|
698 |
|
699 | for (var i=0, callback; callback = callbacks[i++];) {
|
700 | callback(env);
|
701 | }
|
702 | }
|
703 | },
|
704 |
|
705 | Token: Token
|
706 | };
|
707 | _self.Prism = _;
|
708 |
|
709 |
|
710 | // Typescript note:
|
711 | // The following can be used to import the Token type in JSDoc:
|
712 | //
|
713 | // @typedef {InstanceType<import("./prism-core")["Token"]>} Token
|
714 |
|
715 | /**
|
716 | * Creates a new token.
|
717 | *
|
718 | * @param {string} type See {@link Token#type type}
|
719 | * @param {string | TokenStream} content See {@link Token#content content}
|
720 | * @param {string|string[]} [alias] The alias(es) of the token.
|
721 | * @param {string} [matchedStr=""] A copy of the full string this token was created from.
|
722 | * @class
|
723 | * @global
|
724 | * @public
|
725 | */
|
726 | function Token(type, content, alias, matchedStr) {
|
727 | /**
|
728 | * The type of the token.
|
729 | *
|
730 | * This is usually the key of a pattern in a {@link Grammar}.
|
731 | *
|
732 | * @type {string}
|
733 | * @see GrammarToken
|
734 | * @public
|
735 | */
|
736 | this.type = type;
|
737 | /**
|
738 | * The strings or tokens contained by this token.
|
739 | *
|
740 | * This will be a token stream if the pattern matched also defined an `inside` grammar.
|
741 | *
|
742 | * @type {string | TokenStream}
|
743 | * @public
|
744 | */
|
745 | this.content = content;
|
746 | /**
|
747 | * The alias(es) of the token.
|
748 | *
|
749 | * @type {string|string[]}
|
750 | * @see GrammarToken
|
751 | * @public
|
752 | */
|
753 | this.alias = alias;
|
754 | // Copy of the full string this token was created from
|
755 | this.length = (matchedStr || '').length | 0;
|
756 | }
|
757 |
|
758 | /**
|
759 | * A token stream is an array of strings and {@link Token Token} objects.
|
760 | *
|
761 | * Token streams have to fulfill a few properties that are assumed by most functions (mostly internal ones) that process
|
762 | * them.
|
763 | *
|
764 | * 1. No adjacent strings.
|
765 | * 2. No empty strings.
|
766 | *
|
767 | * The only exception here is the token stream that only contains the empty string and nothing else.
|
768 | *
|
769 | * @typedef {Array<string | Token>} TokenStream
|
770 | * @global
|
771 | * @public
|
772 | */
|
773 |
|
774 | /**
|
775 | * Converts the given token or token stream to an HTML representation.
|
776 | *
|
777 | * The following hooks will be run:
|
778 | * 1. `wrap`: On each {@link Token}.
|
779 | *
|
780 | * @param {string | Token | TokenStream} o The token or token stream to be converted.
|
781 | * @param {string} language The name of current language.
|
782 | * @returns {string} The HTML representation of the token or token stream.
|
783 | * @memberof Token
|
784 | * @static
|
785 | */
|
786 | Token.stringify = function stringify(o, language) {
|
787 | if (typeof o == 'string') {
|
788 | return o;
|
789 | }
|
790 | if (Array.isArray(o)) {
|
791 | var s = '';
|
792 | o.forEach(function (e) {
|
793 | s += stringify(e, language);
|
794 | });
|
795 | return s;
|
796 | }
|
797 |
|
798 | var env = {
|
799 | type: o.type,
|
800 | content: stringify(o.content, language),
|
801 | tag: 'span',
|
802 | classes: ['token', o.type],
|
803 | attributes: {},
|
804 | language: language
|
805 | };
|
806 |
|
807 | var aliases = o.alias;
|
808 | if (aliases) {
|
809 | if (Array.isArray(aliases)) {
|
810 | Array.prototype.push.apply(env.classes, aliases);
|
811 | } else {
|
812 | env.classes.push(aliases);
|
813 | }
|
814 | }
|
815 |
|
816 | _.hooks.run('wrap', env);
|
817 |
|
818 | var attributes = '';
|
819 | for (var name in env.attributes) {
|
820 | attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"';
|
821 | }
|
822 |
|
823 | return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
|
824 | };
|
825 |
|
826 | /**
|
827 | * @param {RegExp} pattern
|
828 | * @param {number} pos
|
829 | * @param {string} text
|
830 | * @param {boolean} lookbehind
|
831 | * @returns {RegExpExecArray | null}
|
832 | */
|
833 | function matchPattern(pattern, pos, text, lookbehind) {
|
834 | pattern.lastIndex = pos;
|
835 | var match = pattern.exec(text);
|
836 | if (match && lookbehind && match[1]) {
|
837 | // change the match to remove the text matched by the Prism lookbehind group
|
838 | var lookbehindLength = match[1].length;
|
839 | match.index += lookbehindLength;
|
840 | match[0] = match[0].slice(lookbehindLength);
|
841 | }
|
842 | return match;
|
843 | }
|
844 |
|
845 | /**
|
846 | * @param {string} text
|
847 | * @param {LinkedList<string | Token>} tokenList
|
848 | * @param {any} grammar
|
849 | * @param {LinkedListNode<string | Token>} startNode
|
850 | * @param {number} startPos
|
851 | * @param {RematchOptions} [rematch]
|
852 | * @returns {void}
|
853 | * @private
|
854 | *
|
855 | * @typedef RematchOptions
|
856 | * @property {string} cause
|
857 | * @property {number} reach
|
858 | */
|
859 | function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
|
860 | for (var token in grammar) {
|
861 | if (!grammar.hasOwnProperty(token) || !grammar[token]) {
|
862 | continue;
|
863 | }
|
864 |
|
865 | var patterns = grammar[token];
|
866 | patterns = Array.isArray(patterns) ? patterns : [patterns];
|
867 |
|
868 | for (var j = 0; j < patterns.length; ++j) {
|
869 | if (rematch && rematch.cause == token + ',' + j) {
|
870 | return;
|
871 | }
|
872 |
|
873 | var patternObj = patterns[j],
|
874 | inside = patternObj.inside,
|
875 | lookbehind = !!patternObj.lookbehind,
|
876 | greedy = !!patternObj.greedy,
|
877 | alias = patternObj.alias;
|
878 |
|
879 | if (greedy && !patternObj.pattern.global) {
|
880 | // Without the global flag, lastIndex won't work
|
881 | var flags = patternObj.pattern.toString().match(/[imsuy]*$/)[0];
|
882 | patternObj.pattern = RegExp(patternObj.pattern.source, flags + 'g');
|
883 | }
|
884 |
|
885 | /** @type {RegExp} */
|
886 | var pattern = patternObj.pattern || patternObj;
|
887 |
|
888 | for ( // iterate the token list and keep track of the current token/string position
|
889 | var currentNode = startNode.next, pos = startPos;
|
890 | currentNode !== tokenList.tail;
|
891 | pos += currentNode.value.length, currentNode = currentNode.next
|
892 | ) {
|
893 |
|
894 | if (rematch && pos >= rematch.reach) {
|
895 | break;
|
896 | }
|
897 |
|
898 | var str = currentNode.value;
|
899 |
|
900 | if (tokenList.length > text.length) {
|
901 | // Something went terribly wrong, ABORT, ABORT!
|
902 | return;
|
903 | }
|
904 |
|
905 | if (str instanceof Token) {
|
906 | continue;
|
907 | }
|
908 |
|
909 | var removeCount = 1; // this is the to parameter of removeBetween
|
910 | var match;
|
911 |
|
912 | if (greedy) {
|
913 | match = matchPattern(pattern, pos, text, lookbehind);
|
914 | if (!match) {
|
915 | break;
|
916 | }
|
917 |
|
918 | var from = match.index;
|
919 | var to = match.index + match[0].length;
|
920 | var p = pos;
|
921 |
|
922 | // find the node that contains the match
|
923 | p += currentNode.value.length;
|
924 | while (from >= p) {
|
925 | currentNode = currentNode.next;
|
926 | p += currentNode.value.length;
|
927 | }
|
928 | // adjust pos (and p)
|
929 | p -= currentNode.value.length;
|
930 | pos = p;
|
931 |
|
932 | // the current node is a Token, then the match starts inside another Token, which is invalid
|
933 | if (currentNode.value instanceof Token) {
|
934 | continue;
|
935 | }
|
936 |
|
937 | // find the last node which is affected by this match
|
938 | for (
|
939 | var k = currentNode;
|
940 | k !== tokenList.tail && (p < to || typeof k.value === 'string');
|
941 | k = k.next
|
942 | ) {
|
943 | removeCount++;
|
944 | p += k.value.length;
|
945 | }
|
946 | removeCount--;
|
947 |
|
948 | // replace with the new match
|
949 | str = text.slice(pos, p);
|
950 | match.index -= pos;
|
951 | } else {
|
952 | match = matchPattern(pattern, 0, str, lookbehind);
|
953 | if (!match) {
|
954 | continue;
|
955 | }
|
956 | }
|
957 |
|
958 | var from = match.index,
|
959 | matchStr = match[0],
|
960 | before = str.slice(0, from),
|
961 | after = str.slice(from + matchStr.length);
|
962 |
|
963 | var reach = pos + str.length;
|
964 | if (rematch && reach > rematch.reach) {
|
965 | rematch.reach = reach;
|
966 | }
|
967 |
|
968 | var removeFrom = currentNode.prev;
|
969 |
|
970 | if (before) {
|
971 | removeFrom = addAfter(tokenList, removeFrom, before);
|
972 | pos += before.length;
|
973 | }
|
974 |
|
975 | removeRange(tokenList, removeFrom, removeCount);
|
976 |
|
977 | var wrapped = new Token(token, inside ? _.tokenize(matchStr, inside) : matchStr, alias, matchStr);
|
978 | currentNode = addAfter(tokenList, removeFrom, wrapped);
|
979 |
|
980 | if (after) {
|
981 | addAfter(tokenList, currentNode, after);
|
982 | }
|
983 |
|
984 | if (removeCount > 1) {
|
985 | // at least one Token object was removed, so we have to do some rematching
|
986 | // this can only happen if the current pattern is greedy
|
987 | matchGrammar(text, tokenList, grammar, currentNode.prev, pos, {
|
988 | cause: token + ',' + j,
|
989 | reach: reach
|
990 | });
|
991 | }
|
992 | }
|
993 | }
|
994 | }
|
995 | }
|
996 |
|
997 | /**
|
998 | * @typedef LinkedListNode
|
999 | * @property {T} value
|
1000 | * @property {LinkedListNode<T> | null} prev The previous node.
|
1001 | * @property {LinkedListNode<T> | null} next The next node.
|
1002 | * @template T
|
1003 | * @private
|
1004 | */
|
1005 |
|
1006 | /**
|
1007 | * @template T
|
1008 | * @private
|
1009 | */
|
1010 | function LinkedList() {
|
1011 | /** @type {LinkedListNode<T>} */
|
1012 | var head = { value: null, prev: null, next: null };
|
1013 | /** @type {LinkedListNode<T>} */
|
1014 | var tail = { value: null, prev: head, next: null };
|
1015 | head.next = tail;
|
1016 |
|
1017 | /** @type {LinkedListNode<T>} */
|
1018 | this.head = head;
|
1019 | /** @type {LinkedListNode<T>} */
|
1020 | this.tail = tail;
|
1021 | this.length = 0;
|
1022 | }
|
1023 |
|
1024 | /**
|
1025 | * Adds a new node with the given value to the list.
|
1026 | * @param {LinkedList<T>} list
|
1027 | * @param {LinkedListNode<T>} node
|
1028 | * @param {T} value
|
1029 | * @returns {LinkedListNode<T>} The added node.
|
1030 | * @template T
|
1031 | */
|
1032 | function addAfter(list, node, value) {
|
1033 | // assumes that node != list.tail && values.length >= 0
|
1034 | var next = node.next;
|
1035 |
|
1036 | var newNode = { value: value, prev: node, next: next };
|
1037 | node.next = newNode;
|
1038 | next.prev = newNode;
|
1039 | list.length++;
|
1040 |
|
1041 | return newNode;
|
1042 | }
|
1043 | /**
|
1044 | * Removes `count` nodes after the given node. The given node will not be removed.
|
1045 | * @param {LinkedList<T>} list
|
1046 | * @param {LinkedListNode<T>} node
|
1047 | * @param {number} count
|
1048 | * @template T
|
1049 | */
|
1050 | function removeRange(list, node, count) {
|
1051 | var next = node.next;
|
1052 | for (var i = 0; i < count && next !== list.tail; i++) {
|
1053 | next = next.next;
|
1054 | }
|
1055 | node.next = next;
|
1056 | next.prev = node;
|
1057 | list.length -= i;
|
1058 | }
|
1059 | /**
|
1060 | * @param {LinkedList<T>} list
|
1061 | * @returns {T[]}
|
1062 | * @template T
|
1063 | */
|
1064 | function toArray(list) {
|
1065 | var array = [];
|
1066 | var node = list.head.next;
|
1067 | while (node !== list.tail) {
|
1068 | array.push(node.value);
|
1069 | node = node.next;
|
1070 | }
|
1071 | return array;
|
1072 | }
|
1073 |
|
1074 |
|
1075 | if (!_self.document) {
|
1076 | if (!_self.addEventListener) {
|
1077 | // in Node.js
|
1078 | return _;
|
1079 | }
|
1080 |
|
1081 | if (!_.disableWorkerMessageHandler) {
|
1082 | // In worker
|
1083 | _self.addEventListener('message', function (evt) {
|
1084 | var message = JSON.parse(evt.data),
|
1085 | lang = message.language,
|
1086 | code = message.code,
|
1087 | immediateClose = message.immediateClose;
|
1088 |
|
1089 | _self.postMessage(_.highlight(code, _.languages[lang], lang));
|
1090 | if (immediateClose) {
|
1091 | _self.close();
|
1092 | }
|
1093 | }, false);
|
1094 | }
|
1095 |
|
1096 | return _;
|
1097 | }
|
1098 |
|
1099 | // Get current script and highlight
|
1100 | var script = _.util.currentScript();
|
1101 |
|
1102 | if (script) {
|
1103 | _.filename = script.src;
|
1104 |
|
1105 | if (script.hasAttribute('data-manual')) {
|
1106 | _.manual = true;
|
1107 | }
|
1108 | }
|
1109 |
|
1110 | function highlightAutomaticallyCallback() {
|
1111 | if (!_.manual) {
|
1112 | _.highlightAll();
|
1113 | }
|
1114 | }
|
1115 |
|
1116 | if (!_.manual) {
|
1117 | // If the document state is "loading", then we'll use DOMContentLoaded.
|
1118 | // If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
|
1119 | // DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
|
1120 | // might take longer one animation frame to execute which can create a race condition where only some plugins have
|
1121 | // been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
|
1122 | // See https://github.com/PrismJS/prism/issues/2102
|
1123 | var readyState = document.readyState;
|
1124 | if (readyState === 'loading' || readyState === 'interactive' && script && script.defer) {
|
1125 | document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
|
1126 | } else {
|
1127 | if (window.requestAnimationFrame) {
|
1128 | window.requestAnimationFrame(highlightAutomaticallyCallback);
|
1129 | } else {
|
1130 | window.setTimeout(highlightAutomaticallyCallback, 16);
|
1131 | }
|
1132 | }
|
1133 | }
|
1134 |
|
1135 | return _;
|
1136 |
|
1137 | })(_self);
|
1138 |
|
1139 | if (typeof module !== 'undefined' && module.exports) {
|
1140 | module.exports = Prism;
|
1141 | }
|
1142 |
|
1143 | // hack for components to work correctly in node.js
|
1144 | if (typeof global !== 'undefined') {
|
1145 | global.Prism = Prism;
|
1146 | }
|
1147 |
|
1148 | // some additional documentation/types
|
1149 |
|
1150 | /**
|
1151 | * The expansion of a simple `RegExp` literal to support additional properties.
|
1152 | *
|
1153 | * @typedef GrammarToken
|
1154 | * @property {RegExp} pattern The regular expression of the token.
|
1155 | * @property {boolean} [lookbehind=false] If `true`, then the first capturing group of `pattern` will (effectively)
|
1156 | * behave as a lookbehind group meaning that the captured text will not be part of the matched text of the new token.
|
1157 | * @property {boolean} [greedy=false] Whether the token is greedy.
|
1158 | * @property {string|string[]} [alias] An optional alias or list of aliases.
|
1159 | * @property {Grammar} [inside] The nested grammar of this token.
|
1160 | *
|
1161 | * The `inside` grammar will be used to tokenize the text value of each token of this kind.
|
1162 | *
|
1163 | * This can be used to make nested and even recursive language definitions.
|
1164 | *
|
1165 | * Note: This can cause infinite recursion. Be careful when you embed different languages or even the same language into
|
1166 | * each another.
|
1167 | * @global
|
1168 | * @public
|
1169 | */
|
1170 |
|
1171 | /**
|
1172 | * @typedef Grammar
|
1173 | * @type {Object<string, RegExp | GrammarToken | Array<RegExp | GrammarToken>>}
|
1174 | * @property {Grammar} [rest] An optional grammar object that will be appended to this grammar.
|
1175 | * @global
|
1176 | * @public
|
1177 | */
|
1178 |
|
1179 | /**
|
1180 | * A function which will invoked after an element was successfully highlighted.
|
1181 | *
|
1182 | * @callback HighlightCallback
|
1183 | * @param {Element} element The element successfully highlighted.
|
1184 | * @returns {void}
|
1185 | * @global
|
1186 | * @public
|
1187 | */
|
1188 |
|
1189 | /**
|
1190 | * @callback HookCallback
|
1191 | * @param {Object<string, any>} env The environment variables of the hook.
|
1192 | * @returns {void}
|
1193 | * @global
|
1194 | * @public
|
1195 | */
|