@lexical/code 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1933 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ 'use strict';
8
+
9
+ var utils = require('@lexical/utils');
10
+ var lexical = require('lexical');
11
+
12
+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
13
+
14
+ var prismCore = {exports: {}};
15
+
16
+ (function (module) {
17
+ /// <reference lib="WebWorker"/>
18
+
19
+ var _self = (typeof window !== 'undefined')
20
+ ? window // if in browser
21
+ : (
22
+ (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
23
+ ? self // if in worker
24
+ : {} // if in node js
25
+ );
26
+
27
+ /**
28
+ * Prism: Lightweight, robust, elegant syntax highlighting
29
+ *
30
+ * @license MIT <https://opensource.org/licenses/MIT>
31
+ * @author Lea Verou <https://lea.verou.me>
32
+ * @namespace
33
+ * @public
34
+ */
35
+ var Prism = (function (_self) {
36
+
37
+ // Private helper vars
38
+ var lang = /\blang(?:uage)?-([\w-]+)\b/i;
39
+ var uniqueId = 0;
40
+
41
+ // The grammar object for plaintext
42
+ var plainTextGrammar = {};
43
+
44
+
45
+ var _ = {
46
+ /**
47
+ * By default, Prism will attempt to highlight all code elements (by calling {@link Prism.highlightAll}) on the
48
+ * current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load
49
+ * additional languages or plugins yourself.
50
+ *
51
+ * By setting this value to `true`, Prism will not automatically highlight all code elements on the page.
52
+ *
53
+ * You obviously have to change this value before the automatic highlighting started. To do this, you can add an
54
+ * empty Prism object into the global scope before loading the Prism script like this:
55
+ *
56
+ * ```js
57
+ * window.Prism = window.Prism || {};
58
+ * Prism.manual = true;
59
+ * // add a new <script> to load Prism's script
60
+ * ```
61
+ *
62
+ * @default false
63
+ * @type {boolean}
64
+ * @memberof Prism
65
+ * @public
66
+ */
67
+ manual: _self.Prism && _self.Prism.manual,
68
+ disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
69
+
70
+ /**
71
+ * A namespace for utility methods.
72
+ *
73
+ * All function in this namespace that are not explicitly marked as _public_ are for __internal use only__ and may
74
+ * change or disappear at any time.
75
+ *
76
+ * @namespace
77
+ * @memberof Prism
78
+ */
79
+ util: {
80
+ encode: function encode(tokens) {
81
+ if (tokens instanceof Token) {
82
+ return new Token(tokens.type, encode(tokens.content), tokens.alias);
83
+ } else if (Array.isArray(tokens)) {
84
+ return tokens.map(encode);
85
+ } else {
86
+ return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
87
+ }
88
+ },
89
+
90
+ /**
91
+ * Returns the name of the type of the given value.
92
+ *
93
+ * @param {any} o
94
+ * @returns {string}
95
+ * @example
96
+ * type(null) === 'Null'
97
+ * type(undefined) === 'Undefined'
98
+ * type(123) === 'Number'
99
+ * type('foo') === 'String'
100
+ * type(true) === 'Boolean'
101
+ * type([1, 2]) === 'Array'
102
+ * type({}) === 'Object'
103
+ * type(String) === 'Function'
104
+ * type(/abc+/) === 'RegExp'
105
+ */
106
+ type: function (o) {
107
+ return Object.prototype.toString.call(o).slice(8, -1);
108
+ },
109
+
110
+ /**
111
+ * Returns a unique number for the given object. Later calls will still return the same number.
112
+ *
113
+ * @param {Object} obj
114
+ * @returns {number}
115
+ */
116
+ objId: function (obj) {
117
+ if (!obj['__id']) {
118
+ Object.defineProperty(obj, '__id', { value: ++uniqueId });
119
+ }
120
+ return obj['__id'];
121
+ },
122
+
123
+ /**
124
+ * Creates a deep clone of the given object.
125
+ *
126
+ * The main intended use of this function is to clone language definitions.
127
+ *
128
+ * @param {T} o
129
+ * @param {Record<number, any>} [visited]
130
+ * @returns {T}
131
+ * @template T
132
+ */
133
+ clone: function deepClone(o, visited) {
134
+ visited = visited || {};
135
+
136
+ var clone; var id;
137
+ switch (_.util.type(o)) {
138
+ case 'Object':
139
+ id = _.util.objId(o);
140
+ if (visited[id]) {
141
+ return visited[id];
142
+ }
143
+ clone = /** @type {Record<string, any>} */ ({});
144
+ visited[id] = clone;
145
+
146
+ for (var key in o) {
147
+ if (o.hasOwnProperty(key)) {
148
+ clone[key] = deepClone(o[key], visited);
149
+ }
150
+ }
151
+
152
+ return /** @type {any} */ (clone);
153
+
154
+ case 'Array':
155
+ id = _.util.objId(o);
156
+ if (visited[id]) {
157
+ return visited[id];
158
+ }
159
+ clone = [];
160
+ visited[id] = clone;
161
+
162
+ (/** @type {Array} */(/** @type {any} */(o))).forEach(function (v, i) {
163
+ clone[i] = deepClone(v, visited);
164
+ });
165
+
166
+ return /** @type {any} */ (clone);
167
+
168
+ default:
169
+ return o;
170
+ }
171
+ },
172
+
173
+ /**
174
+ * Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
175
+ *
176
+ * If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
177
+ *
178
+ * @param {Element} element
179
+ * @returns {string}
180
+ */
181
+ getLanguage: function (element) {
182
+ while (element && !lang.test(element.className)) {
183
+ element = element.parentElement;
184
+ }
185
+ if (element) {
186
+ return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
187
+ }
188
+ return 'none';
189
+ },
190
+
191
+ /**
192
+ * Returns the script element that is currently executing.
193
+ *
194
+ * This does __not__ work for line script element.
195
+ *
196
+ * @returns {HTMLScriptElement | null}
197
+ */
198
+ currentScript: function () {
199
+ if (typeof document === 'undefined') {
200
+ return null;
201
+ }
202
+ if ('currentScript' in document && 1 < 2 /* hack to trip TS' flow analysis */) {
203
+ return /** @type {any} */ (document.currentScript);
204
+ }
205
+
206
+ // IE11 workaround
207
+ // we'll get the src of the current script by parsing IE11's error stack trace
208
+ // this will not work for inline scripts
209
+
210
+ try {
211
+ throw new Error();
212
+ } catch (err) {
213
+ // Get file src url from stack. Specifically works with the format of stack traces in IE.
214
+ // A stack will look like this:
215
+ //
216
+ // Error
217
+ // at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
218
+ // at Global code (http://localhost/components/prism-core.js:606:1)
219
+
220
+ var src = (/at [^(\r\n]*\((.*):[^:]+:[^:]+\)$/i.exec(err.stack) || [])[1];
221
+ if (src) {
222
+ var scripts = document.getElementsByTagName('script');
223
+ for (var i in scripts) {
224
+ if (scripts[i].src == src) {
225
+ return scripts[i];
226
+ }
227
+ }
228
+ }
229
+ return null;
230
+ }
231
+ },
232
+
233
+ /**
234
+ * Returns whether a given class is active for `element`.
235
+ *
236
+ * The class can be activated if `element` or one of its ancestors has the given class and it can be deactivated
237
+ * if `element` or one of its ancestors has the negated version of the given class. The _negated version_ of the
238
+ * given class is just the given class with a `no-` prefix.
239
+ *
240
+ * Whether the class is active is determined by the closest ancestor of `element` (where `element` itself is
241
+ * closest ancestor) that has the given class or the negated version of it. If neither `element` nor any of its
242
+ * ancestors have the given class or the negated version of it, then the default activation will be returned.
243
+ *
244
+ * In the paradoxical situation where the closest ancestor contains __both__ the given class and the negated
245
+ * version of it, the class is considered active.
246
+ *
247
+ * @param {Element} element
248
+ * @param {string} className
249
+ * @param {boolean} [defaultActivation=false]
250
+ * @returns {boolean}
251
+ */
252
+ isActive: function (element, className, defaultActivation) {
253
+ var no = 'no-' + className;
254
+
255
+ while (element) {
256
+ var classList = element.classList;
257
+ if (classList.contains(className)) {
258
+ return true;
259
+ }
260
+ if (classList.contains(no)) {
261
+ return false;
262
+ }
263
+ element = element.parentElement;
264
+ }
265
+ return !!defaultActivation;
266
+ }
267
+ },
268
+
269
+ /**
270
+ * This namespace contains all currently loaded languages and the some helper functions to create and modify languages.
271
+ *
272
+ * @namespace
273
+ * @memberof Prism
274
+ * @public
275
+ */
276
+ languages: {
277
+ /**
278
+ * The grammar for plain, unformatted text.
279
+ */
280
+ plain: plainTextGrammar,
281
+ plaintext: plainTextGrammar,
282
+ text: plainTextGrammar,
283
+ txt: plainTextGrammar,
284
+
285
+ /**
286
+ * Creates a deep copy of the language with the given id and appends the given tokens.
287
+ *
288
+ * If a token in `redef` also appears in the copied language, then the existing token in the copied language
289
+ * will be overwritten at its original position.
290
+ *
291
+ * ## Best practices
292
+ *
293
+ * Since the position of overwriting tokens (token in `redef` that overwrite tokens in the copied language)
294
+ * doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to
295
+ * understand the language definition because, normally, the order of tokens matters in Prism grammars.
296
+ *
297
+ * Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
298
+ * Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
299
+ *
300
+ * @param {string} id The id of the language to extend. This has to be a key in `Prism.languages`.
301
+ * @param {Grammar} redef The new tokens to append.
302
+ * @returns {Grammar} The new language created.
303
+ * @public
304
+ * @example
305
+ * Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
306
+ * // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
307
+ * // at its original position
308
+ * 'comment': { ... },
309
+ * // CSS doesn't have a 'color' token, so this token will be appended
310
+ * 'color': /\b(?:red|green|blue)\b/
311
+ * });
312
+ */
313
+ extend: function (id, redef) {
314
+ var lang = _.util.clone(_.languages[id]);
315
+
316
+ for (var key in redef) {
317
+ lang[key] = redef[key];
318
+ }
319
+
320
+ return lang;
321
+ },
322
+
323
+ /**
324
+ * Inserts tokens _before_ another token in a language definition or any other grammar.
325
+ *
326
+ * ## Usage
327
+ *
328
+ * This helper method makes it easy to modify existing languages. For example, the CSS language definition
329
+ * not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded
330
+ * in HTML through `<style>` elements. To do this, it needs to modify `Prism.languages.markup` and add the
331
+ * appropriate tokens. However, `Prism.languages.markup` is a regular JavaScript object literal, so if you do
332
+ * this:
333
+ *
334
+ * ```js
335
+ * Prism.languages.markup.style = {
336
+ * // token
337
+ * };
338
+ * ```
339
+ *
340
+ * then the `style` token will be added (and processed) at the end. `insertBefore` allows you to insert tokens
341
+ * before existing tokens. For the CSS example above, you would use it like this:
342
+ *
343
+ * ```js
344
+ * Prism.languages.insertBefore('markup', 'cdata', {
345
+ * 'style': {
346
+ * // token
347
+ * }
348
+ * });
349
+ * ```
350
+ *
351
+ * ## Special cases
352
+ *
353
+ * If the grammars of `inside` and `insert` have tokens with the same name, the tokens in `inside`'s grammar
354
+ * will be ignored.
355
+ *
356
+ * This behavior can be used to insert tokens after `before`:
357
+ *
358
+ * ```js
359
+ * Prism.languages.insertBefore('markup', 'comment', {
360
+ * 'comment': Prism.languages.markup.comment,
361
+ * // tokens after 'comment'
362
+ * });
363
+ * ```
364
+ *
365
+ * ## Limitations
366
+ *
367
+ * The main problem `insertBefore` has to solve is iteration order. Since ES2015, the iteration order for object
368
+ * properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
369
+ * differently when keys are deleted and re-inserted. So `insertBefore` can't be implemented by temporarily
370
+ * deleting properties which is necessary to insert at arbitrary positions.
371
+ *
372
+ * To solve this problem, `insertBefore` doesn't actually insert the given tokens into the target object.
373
+ * Instead, it will create a new object and replace all references to the target object with the new one. This
374
+ * can be done without temporarily deleting properties, so the iteration order is well-defined.
375
+ *
376
+ * However, only references that can be reached from `Prism.languages` or `insert` will be replaced. I.e. if
377
+ * you hold the target object in a variable, then the value of the variable will not change.
378
+ *
379
+ * ```js
380
+ * var oldMarkup = Prism.languages.markup;
381
+ * var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... });
382
+ *
383
+ * assert(oldMarkup !== Prism.languages.markup);
384
+ * assert(newMarkup === Prism.languages.markup);
385
+ * ```
386
+ *
387
+ * @param {string} inside The property of `root` (e.g. a language id in `Prism.languages`) that contains the
388
+ * object to be modified.
389
+ * @param {string} before The key to insert before.
390
+ * @param {Grammar} insert An object containing the key-value pairs to be inserted.
391
+ * @param {Object<string, any>} [root] The object containing `inside`, i.e. the object that contains the
392
+ * object to be modified.
393
+ *
394
+ * Defaults to `Prism.languages`.
395
+ * @returns {Grammar} The new grammar object.
396
+ * @public
397
+ */
398
+ insertBefore: function (inside, before, insert, root) {
399
+ root = root || /** @type {any} */ (_.languages);
400
+ var grammar = root[inside];
401
+ /** @type {Grammar} */
402
+ var ret = {};
403
+
404
+ for (var token in grammar) {
405
+ if (grammar.hasOwnProperty(token)) {
406
+
407
+ if (token == before) {
408
+ for (var newToken in insert) {
409
+ if (insert.hasOwnProperty(newToken)) {
410
+ ret[newToken] = insert[newToken];
411
+ }
412
+ }
413
+ }
414
+
415
+ // Do not insert token which also occur in insert. See #1525
416
+ if (!insert.hasOwnProperty(token)) {
417
+ ret[token] = grammar[token];
418
+ }
419
+ }
420
+ }
421
+
422
+ var old = root[inside];
423
+ root[inside] = ret;
424
+
425
+ // Update references in other language definitions
426
+ _.languages.DFS(_.languages, function (key, value) {
427
+ if (value === old && key != inside) {
428
+ this[key] = ret;
429
+ }
430
+ });
431
+
432
+ return ret;
433
+ },
434
+
435
+ // Traverse a language definition with Depth First Search
436
+ DFS: function DFS(o, callback, type, visited) {
437
+ visited = visited || {};
438
+
439
+ var objId = _.util.objId;
440
+
441
+ for (var i in o) {
442
+ if (o.hasOwnProperty(i)) {
443
+ callback.call(o, i, o[i], type || i);
444
+
445
+ var property = o[i];
446
+ var propertyType = _.util.type(property);
447
+
448
+ if (propertyType === 'Object' && !visited[objId(property)]) {
449
+ visited[objId(property)] = true;
450
+ DFS(property, callback, null, visited);
451
+ } else if (propertyType === 'Array' && !visited[objId(property)]) {
452
+ visited[objId(property)] = true;
453
+ DFS(property, callback, i, visited);
454
+ }
455
+ }
456
+ }
457
+ }
458
+ },
459
+
460
+ plugins: {},
461
+
462
+ /**
463
+ * This is the most high-level function in Prism’s API.
464
+ * It fetches all the elements that have a `.language-xxxx` class and then calls {@link Prism.highlightElement} on
465
+ * each one of them.
466
+ *
467
+ * This is equivalent to `Prism.highlightAllUnder(document, async, callback)`.
468
+ *
469
+ * @param {boolean} [async=false] Same as in {@link Prism.highlightAllUnder}.
470
+ * @param {HighlightCallback} [callback] Same as in {@link Prism.highlightAllUnder}.
471
+ * @memberof Prism
472
+ * @public
473
+ */
474
+ highlightAll: function (async, callback) {
475
+ _.highlightAllUnder(document, async, callback);
476
+ },
477
+
478
+ /**
479
+ * Fetches all the descendants of `container` that have a `.language-xxxx` class and then calls
480
+ * {@link Prism.highlightElement} on each one of them.
481
+ *
482
+ * The following hooks will be run:
483
+ * 1. `before-highlightall`
484
+ * 2. `before-all-elements-highlight`
485
+ * 3. All hooks of {@link Prism.highlightElement} for each element.
486
+ *
487
+ * @param {ParentNode} container The root element, whose descendants that have a `.language-xxxx` class will be highlighted.
488
+ * @param {boolean} [async=false] Whether each element is to be highlighted asynchronously using Web Workers.
489
+ * @param {HighlightCallback} [callback] An optional callback to be invoked on each element after its highlighting is done.
490
+ * @memberof Prism
491
+ * @public
492
+ */
493
+ highlightAllUnder: function (container, async, callback) {
494
+ var env = {
495
+ callback: callback,
496
+ container: container,
497
+ selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
498
+ };
499
+
500
+ _.hooks.run('before-highlightall', env);
501
+
502
+ env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
503
+
504
+ _.hooks.run('before-all-elements-highlight', env);
505
+
506
+ for (var i = 0, element; (element = env.elements[i++]);) {
507
+ _.highlightElement(element, async === true, env.callback);
508
+ }
509
+ },
510
+
511
+ /**
512
+ * Highlights the code inside a single element.
513
+ *
514
+ * The following hooks will be run:
515
+ * 1. `before-sanity-check`
516
+ * 2. `before-highlight`
517
+ * 3. All hooks of {@link Prism.highlight}. These hooks will be run by an asynchronous worker if `async` is `true`.
518
+ * 4. `before-insert`
519
+ * 5. `after-highlight`
520
+ * 6. `complete`
521
+ *
522
+ * Some the above hooks will be skipped if the element doesn't contain any text or there is no grammar loaded for
523
+ * the element's language.
524
+ *
525
+ * @param {Element} element The element containing the code.
526
+ * It must have a class of `language-xxxx` to be processed, where `xxxx` is a valid language identifier.
527
+ * @param {boolean} [async=false] Whether the element is to be highlighted asynchronously using Web Workers
528
+ * to improve performance and avoid blocking the UI when highlighting very large chunks of code. This option is
529
+ * [disabled by default](https://prismjs.com/faq.html#why-is-asynchronous-highlighting-disabled-by-default).
530
+ *
531
+ * Note: All language definitions required to highlight the code must be included in the main `prism.js` file for
532
+ * asynchronous highlighting to work. You can build your own bundle on the
533
+ * [Download page](https://prismjs.com/download.html).
534
+ * @param {HighlightCallback} [callback] An optional callback to be invoked after the highlighting is done.
535
+ * Mostly useful when `async` is `true`, since in that case, the highlighting is done asynchronously.
536
+ * @memberof Prism
537
+ * @public
538
+ */
539
+ highlightElement: function (element, async, callback) {
540
+ // Find language
541
+ var language = _.util.getLanguage(element);
542
+ var grammar = _.languages[language];
543
+
544
+ // Set language on the element, if not present
545
+ element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
546
+
547
+ // Set language on the parent, for styling
548
+ var parent = element.parentElement;
549
+ if (parent && parent.nodeName.toLowerCase() === 'pre') {
550
+ parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
551
+ }
552
+
553
+ var code = element.textContent;
554
+
555
+ var env = {
556
+ element: element,
557
+ language: language,
558
+ grammar: grammar,
559
+ code: code
560
+ };
561
+
562
+ function insertHighlightedCode(highlightedCode) {
563
+ env.highlightedCode = highlightedCode;
564
+
565
+ _.hooks.run('before-insert', env);
566
+
567
+ env.element.innerHTML = env.highlightedCode;
568
+
569
+ _.hooks.run('after-highlight', env);
570
+ _.hooks.run('complete', env);
571
+ callback && callback.call(env.element);
572
+ }
573
+
574
+ _.hooks.run('before-sanity-check', env);
575
+
576
+ // plugins may change/add the parent/element
577
+ parent = env.element.parentElement;
578
+ if (parent && parent.nodeName.toLowerCase() === 'pre' && !parent.hasAttribute('tabindex')) {
579
+ parent.setAttribute('tabindex', '0');
580
+ }
581
+
582
+ if (!env.code) {
583
+ _.hooks.run('complete', env);
584
+ callback && callback.call(env.element);
585
+ return;
586
+ }
587
+
588
+ _.hooks.run('before-highlight', env);
589
+
590
+ if (!env.grammar) {
591
+ insertHighlightedCode(_.util.encode(env.code));
592
+ return;
593
+ }
594
+
595
+ if (async && _self.Worker) {
596
+ var worker = new Worker(_.filename);
597
+
598
+ worker.onmessage = function (evt) {
599
+ insertHighlightedCode(evt.data);
600
+ };
601
+
602
+ worker.postMessage(JSON.stringify({
603
+ language: env.language,
604
+ code: env.code,
605
+ immediateClose: true
606
+ }));
607
+ } else {
608
+ insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
609
+ }
610
+ },
611
+
612
+ /**
613
+ * Low-level function, only use if you know what you’re doing. It accepts a string of text as input
614
+ * and the language definitions to use, and returns a string with the HTML produced.
615
+ *
616
+ * The following hooks will be run:
617
+ * 1. `before-tokenize`
618
+ * 2. `after-tokenize`
619
+ * 3. `wrap`: On each {@link Token}.
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
+ * @param {string} language The name of the language definition passed to `grammar`.
626
+ * @returns {string} The highlighted HTML.
627
+ * @memberof Prism
628
+ * @public
629
+ * @example
630
+ * Prism.highlight('var foo = true;', Prism.languages.javascript, 'javascript');
631
+ */
632
+ highlight: function (text, grammar, language) {
633
+ var env = {
634
+ code: text,
635
+ grammar: grammar,
636
+ language: language
637
+ };
638
+ _.hooks.run('before-tokenize', env);
639
+ env.tokens = _.tokenize(env.code, env.grammar);
640
+ _.hooks.run('after-tokenize', env);
641
+ return Token.stringify(_.util.encode(env.tokens), env.language);
642
+ },
643
+
644
+ /**
645
+ * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input
646
+ * and the language definitions to use, and returns an array with the tokenized code.
647
+ *
648
+ * When the language definition includes nested tokens, the function is called recursively on each of these tokens.
649
+ *
650
+ * This method could be useful in other contexts as well, as a very crude parser.
651
+ *
652
+ * @param {string} text A string with the code to be highlighted.
653
+ * @param {Grammar} grammar An object containing the tokens to use.
654
+ *
655
+ * Usually a language definition like `Prism.languages.markup`.
656
+ * @returns {TokenStream} An array of strings and tokens, a token stream.
657
+ * @memberof Prism
658
+ * @public
659
+ * @example
660
+ * let code = `var foo = 0;`;
661
+ * let tokens = Prism.tokenize(code, Prism.languages.javascript);
662
+ * tokens.forEach(token => {
663
+ * if (token instanceof Prism.Token && token.type === 'number') {
664
+ * console.log(`Found numeric literal: ${token.content}`);
665
+ * }
666
+ * });
667
+ */
668
+ tokenize: function (text, grammar) {
669
+ var rest = grammar.rest;
670
+ if (rest) {
671
+ for (var token in rest) {
672
+ grammar[token] = rest[token];
673
+ }
674
+
675
+ delete grammar.rest;
676
+ }
677
+
678
+ var tokenList = new LinkedList();
679
+ addAfter(tokenList, tokenList.head, text);
680
+
681
+ matchGrammar(text, tokenList, grammar, tokenList.head, 0);
682
+
683
+ return toArray(tokenList);
684
+ },
685
+
686
+ /**
687
+ * @namespace
688
+ * @memberof Prism
689
+ * @public
690
+ */
691
+ hooks: {
692
+ all: {},
693
+
694
+ /**
695
+ * Adds the given callback to the list of callbacks for the given hook.
696
+ *
697
+ * The callback will be invoked when the hook it is registered for is run.
698
+ * Hooks are usually directly run by a highlight function but you can also run hooks yourself.
699
+ *
700
+ * One callback function can be registered to multiple hooks and the same hook multiple times.
701
+ *
702
+ * @param {string} name The name of the hook.
703
+ * @param {HookCallback} callback The callback function which is given environment variables.
704
+ * @public
705
+ */
706
+ add: function (name, callback) {
707
+ var hooks = _.hooks.all;
708
+
709
+ hooks[name] = hooks[name] || [];
710
+
711
+ hooks[name].push(callback);
712
+ },
713
+
714
+ /**
715
+ * Runs a hook invoking all registered callbacks with the given environment variables.
716
+ *
717
+ * Callbacks will be invoked synchronously and in the order in which they were registered.
718
+ *
719
+ * @param {string} name The name of the hook.
720
+ * @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
721
+ * @public
722
+ */
723
+ run: function (name, env) {
724
+ var callbacks = _.hooks.all[name];
725
+
726
+ if (!callbacks || !callbacks.length) {
727
+ return;
728
+ }
729
+
730
+ for (var i = 0, callback; (callback = callbacks[i++]);) {
731
+ callback(env);
732
+ }
733
+ }
734
+ },
735
+
736
+ Token: Token
737
+ };
738
+ _self.Prism = _;
739
+
740
+
741
+ // Typescript note:
742
+ // The following can be used to import the Token type in JSDoc:
743
+ //
744
+ // @typedef {InstanceType<import("./prism-core")["Token"]>} Token
745
+
746
+ /**
747
+ * Creates a new token.
748
+ *
749
+ * @param {string} type See {@link Token#type type}
750
+ * @param {string | TokenStream} content See {@link Token#content content}
751
+ * @param {string|string[]} [alias] The alias(es) of the token.
752
+ * @param {string} [matchedStr=""] A copy of the full string this token was created from.
753
+ * @class
754
+ * @global
755
+ * @public
756
+ */
757
+ function Token(type, content, alias, matchedStr) {
758
+ /**
759
+ * The type of the token.
760
+ *
761
+ * This is usually the key of a pattern in a {@link Grammar}.
762
+ *
763
+ * @type {string}
764
+ * @see GrammarToken
765
+ * @public
766
+ */
767
+ this.type = type;
768
+ /**
769
+ * The strings or tokens contained by this token.
770
+ *
771
+ * This will be a token stream if the pattern matched also defined an `inside` grammar.
772
+ *
773
+ * @type {string | TokenStream}
774
+ * @public
775
+ */
776
+ this.content = content;
777
+ /**
778
+ * The alias(es) of the token.
779
+ *
780
+ * @type {string|string[]}
781
+ * @see GrammarToken
782
+ * @public
783
+ */
784
+ this.alias = alias;
785
+ // Copy of the full string this token was created from
786
+ this.length = (matchedStr || '').length | 0;
787
+ }
788
+
789
+ /**
790
+ * A token stream is an array of strings and {@link Token Token} objects.
791
+ *
792
+ * Token streams have to fulfill a few properties that are assumed by most functions (mostly internal ones) that process
793
+ * them.
794
+ *
795
+ * 1. No adjacent strings.
796
+ * 2. No empty strings.
797
+ *
798
+ * The only exception here is the token stream that only contains the empty string and nothing else.
799
+ *
800
+ * @typedef {Array<string | Token>} TokenStream
801
+ * @global
802
+ * @public
803
+ */
804
+
805
+ /**
806
+ * Converts the given token or token stream to an HTML representation.
807
+ *
808
+ * The following hooks will be run:
809
+ * 1. `wrap`: On each {@link Token}.
810
+ *
811
+ * @param {string | Token | TokenStream} o The token or token stream to be converted.
812
+ * @param {string} language The name of current language.
813
+ * @returns {string} The HTML representation of the token or token stream.
814
+ * @memberof Token
815
+ * @static
816
+ */
817
+ Token.stringify = function stringify(o, language) {
818
+ if (typeof o == 'string') {
819
+ return o;
820
+ }
821
+ if (Array.isArray(o)) {
822
+ var s = '';
823
+ o.forEach(function (e) {
824
+ s += stringify(e, language);
825
+ });
826
+ return s;
827
+ }
828
+
829
+ var env = {
830
+ type: o.type,
831
+ content: stringify(o.content, language),
832
+ tag: 'span',
833
+ classes: ['token', o.type],
834
+ attributes: {},
835
+ language: language
836
+ };
837
+
838
+ var aliases = o.alias;
839
+ if (aliases) {
840
+ if (Array.isArray(aliases)) {
841
+ Array.prototype.push.apply(env.classes, aliases);
842
+ } else {
843
+ env.classes.push(aliases);
844
+ }
845
+ }
846
+
847
+ _.hooks.run('wrap', env);
848
+
849
+ var attributes = '';
850
+ for (var name in env.attributes) {
851
+ attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '&quot;') + '"';
852
+ }
853
+
854
+ return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
855
+ };
856
+
857
+ /**
858
+ * @param {RegExp} pattern
859
+ * @param {number} pos
860
+ * @param {string} text
861
+ * @param {boolean} lookbehind
862
+ * @returns {RegExpExecArray | null}
863
+ */
864
+ function matchPattern(pattern, pos, text, lookbehind) {
865
+ pattern.lastIndex = pos;
866
+ var match = pattern.exec(text);
867
+ if (match && lookbehind && match[1]) {
868
+ // change the match to remove the text matched by the Prism lookbehind group
869
+ var lookbehindLength = match[1].length;
870
+ match.index += lookbehindLength;
871
+ match[0] = match[0].slice(lookbehindLength);
872
+ }
873
+ return match;
874
+ }
875
+
876
+ /**
877
+ * @param {string} text
878
+ * @param {LinkedList<string | Token>} tokenList
879
+ * @param {any} grammar
880
+ * @param {LinkedListNode<string | Token>} startNode
881
+ * @param {number} startPos
882
+ * @param {RematchOptions} [rematch]
883
+ * @returns {void}
884
+ * @private
885
+ *
886
+ * @typedef RematchOptions
887
+ * @property {string} cause
888
+ * @property {number} reach
889
+ */
890
+ function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
891
+ for (var token in grammar) {
892
+ if (!grammar.hasOwnProperty(token) || !grammar[token]) {
893
+ continue;
894
+ }
895
+
896
+ var patterns = grammar[token];
897
+ patterns = Array.isArray(patterns) ? patterns : [patterns];
898
+
899
+ for (var j = 0; j < patterns.length; ++j) {
900
+ if (rematch && rematch.cause == token + ',' + j) {
901
+ return;
902
+ }
903
+
904
+ var patternObj = patterns[j];
905
+ var inside = patternObj.inside;
906
+ var lookbehind = !!patternObj.lookbehind;
907
+ var greedy = !!patternObj.greedy;
908
+ var alias = patternObj.alias;
909
+
910
+ if (greedy && !patternObj.pattern.global) {
911
+ // Without the global flag, lastIndex won't work
912
+ var flags = patternObj.pattern.toString().match(/[imsuy]*$/)[0];
913
+ patternObj.pattern = RegExp(patternObj.pattern.source, flags + 'g');
914
+ }
915
+
916
+ /** @type {RegExp} */
917
+ var pattern = patternObj.pattern || patternObj;
918
+
919
+ for ( // iterate the token list and keep track of the current token/string position
920
+ var currentNode = startNode.next, pos = startPos;
921
+ currentNode !== tokenList.tail;
922
+ pos += currentNode.value.length, currentNode = currentNode.next
923
+ ) {
924
+
925
+ if (rematch && pos >= rematch.reach) {
926
+ break;
927
+ }
928
+
929
+ var str = currentNode.value;
930
+
931
+ if (tokenList.length > text.length) {
932
+ // Something went terribly wrong, ABORT, ABORT!
933
+ return;
934
+ }
935
+
936
+ if (str instanceof Token) {
937
+ continue;
938
+ }
939
+
940
+ var removeCount = 1; // this is the to parameter of removeBetween
941
+ var match;
942
+
943
+ if (greedy) {
944
+ match = matchPattern(pattern, pos, text, lookbehind);
945
+ if (!match) {
946
+ break;
947
+ }
948
+
949
+ var from = match.index;
950
+ var to = match.index + match[0].length;
951
+ var p = pos;
952
+
953
+ // find the node that contains the match
954
+ p += currentNode.value.length;
955
+ while (from >= p) {
956
+ currentNode = currentNode.next;
957
+ p += currentNode.value.length;
958
+ }
959
+ // adjust pos (and p)
960
+ p -= currentNode.value.length;
961
+ pos = p;
962
+
963
+ // the current node is a Token, then the match starts inside another Token, which is invalid
964
+ if (currentNode.value instanceof Token) {
965
+ continue;
966
+ }
967
+
968
+ // find the last node which is affected by this match
969
+ for (
970
+ var k = currentNode;
971
+ k !== tokenList.tail && (p < to || typeof k.value === 'string');
972
+ k = k.next
973
+ ) {
974
+ removeCount++;
975
+ p += k.value.length;
976
+ }
977
+ removeCount--;
978
+
979
+ // replace with the new match
980
+ str = text.slice(pos, p);
981
+ match.index -= pos;
982
+ } else {
983
+ match = matchPattern(pattern, 0, str, lookbehind);
984
+ if (!match) {
985
+ continue;
986
+ }
987
+ }
988
+
989
+ // eslint-disable-next-line no-redeclare
990
+ var from = match.index;
991
+ var matchStr = match[0];
992
+ var before = str.slice(0, from);
993
+ var after = str.slice(from + matchStr.length);
994
+
995
+ var reach = pos + str.length;
996
+ if (rematch && reach > rematch.reach) {
997
+ rematch.reach = reach;
998
+ }
999
+
1000
+ var removeFrom = currentNode.prev;
1001
+
1002
+ if (before) {
1003
+ removeFrom = addAfter(tokenList, removeFrom, before);
1004
+ pos += before.length;
1005
+ }
1006
+
1007
+ removeRange(tokenList, removeFrom, removeCount);
1008
+
1009
+ var wrapped = new Token(token, inside ? _.tokenize(matchStr, inside) : matchStr, alias, matchStr);
1010
+ currentNode = addAfter(tokenList, removeFrom, wrapped);
1011
+
1012
+ if (after) {
1013
+ addAfter(tokenList, currentNode, after);
1014
+ }
1015
+
1016
+ if (removeCount > 1) {
1017
+ // at least one Token object was removed, so we have to do some rematching
1018
+ // this can only happen if the current pattern is greedy
1019
+
1020
+ /** @type {RematchOptions} */
1021
+ var nestedRematch = {
1022
+ cause: token + ',' + j,
1023
+ reach: reach
1024
+ };
1025
+ matchGrammar(text, tokenList, grammar, currentNode.prev, pos, nestedRematch);
1026
+
1027
+ // the reach might have been extended because of the rematching
1028
+ if (rematch && nestedRematch.reach > rematch.reach) {
1029
+ rematch.reach = nestedRematch.reach;
1030
+ }
1031
+ }
1032
+ }
1033
+ }
1034
+ }
1035
+ }
1036
+
1037
+ /**
1038
+ * @typedef LinkedListNode
1039
+ * @property {T} value
1040
+ * @property {LinkedListNode<T> | null} prev The previous node.
1041
+ * @property {LinkedListNode<T> | null} next The next node.
1042
+ * @template T
1043
+ * @private
1044
+ */
1045
+
1046
+ /**
1047
+ * @template T
1048
+ * @private
1049
+ */
1050
+ function LinkedList() {
1051
+ /** @type {LinkedListNode<T>} */
1052
+ var head = { value: null, prev: null, next: null };
1053
+ /** @type {LinkedListNode<T>} */
1054
+ var tail = { value: null, prev: head, next: null };
1055
+ head.next = tail;
1056
+
1057
+ /** @type {LinkedListNode<T>} */
1058
+ this.head = head;
1059
+ /** @type {LinkedListNode<T>} */
1060
+ this.tail = tail;
1061
+ this.length = 0;
1062
+ }
1063
+
1064
+ /**
1065
+ * Adds a new node with the given value to the list.
1066
+ *
1067
+ * @param {LinkedList<T>} list
1068
+ * @param {LinkedListNode<T>} node
1069
+ * @param {T} value
1070
+ * @returns {LinkedListNode<T>} The added node.
1071
+ * @template T
1072
+ */
1073
+ function addAfter(list, node, value) {
1074
+ // assumes that node != list.tail && values.length >= 0
1075
+ var next = node.next;
1076
+
1077
+ var newNode = { value: value, prev: node, next: next };
1078
+ node.next = newNode;
1079
+ next.prev = newNode;
1080
+ list.length++;
1081
+
1082
+ return newNode;
1083
+ }
1084
+ /**
1085
+ * Removes `count` nodes after the given node. The given node will not be removed.
1086
+ *
1087
+ * @param {LinkedList<T>} list
1088
+ * @param {LinkedListNode<T>} node
1089
+ * @param {number} count
1090
+ * @template T
1091
+ */
1092
+ function removeRange(list, node, count) {
1093
+ var next = node.next;
1094
+ for (var i = 0; i < count && next !== list.tail; i++) {
1095
+ next = next.next;
1096
+ }
1097
+ node.next = next;
1098
+ next.prev = node;
1099
+ list.length -= i;
1100
+ }
1101
+ /**
1102
+ * @param {LinkedList<T>} list
1103
+ * @returns {T[]}
1104
+ * @template T
1105
+ */
1106
+ function toArray(list) {
1107
+ var array = [];
1108
+ var node = list.head.next;
1109
+ while (node !== list.tail) {
1110
+ array.push(node.value);
1111
+ node = node.next;
1112
+ }
1113
+ return array;
1114
+ }
1115
+
1116
+
1117
+ if (!_self.document) {
1118
+ if (!_self.addEventListener) {
1119
+ // in Node.js
1120
+ return _;
1121
+ }
1122
+
1123
+ if (!_.disableWorkerMessageHandler) {
1124
+ // In worker
1125
+ _self.addEventListener('message', function (evt) {
1126
+ var message = JSON.parse(evt.data);
1127
+ var lang = message.language;
1128
+ var code = message.code;
1129
+ var immediateClose = message.immediateClose;
1130
+
1131
+ _self.postMessage(_.highlight(code, _.languages[lang], lang));
1132
+ if (immediateClose) {
1133
+ _self.close();
1134
+ }
1135
+ }, false);
1136
+ }
1137
+
1138
+ return _;
1139
+ }
1140
+
1141
+ // Get current script and highlight
1142
+ var script = _.util.currentScript();
1143
+
1144
+ if (script) {
1145
+ _.filename = script.src;
1146
+
1147
+ if (script.hasAttribute('data-manual')) {
1148
+ _.manual = true;
1149
+ }
1150
+ }
1151
+
1152
+ function highlightAutomaticallyCallback() {
1153
+ if (!_.manual) {
1154
+ _.highlightAll();
1155
+ }
1156
+ }
1157
+
1158
+ if (!_.manual) {
1159
+ // If the document state is "loading", then we'll use DOMContentLoaded.
1160
+ // If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
1161
+ // DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
1162
+ // might take longer one animation frame to execute which can create a race condition where only some plugins have
1163
+ // been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
1164
+ // See https://github.com/PrismJS/prism/issues/2102
1165
+ var readyState = document.readyState;
1166
+ if (readyState === 'loading' || readyState === 'interactive' && script && script.defer) {
1167
+ document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
1168
+ } else {
1169
+ if (window.requestAnimationFrame) {
1170
+ window.requestAnimationFrame(highlightAutomaticallyCallback);
1171
+ } else {
1172
+ window.setTimeout(highlightAutomaticallyCallback, 16);
1173
+ }
1174
+ }
1175
+ }
1176
+
1177
+ return _;
1178
+
1179
+ }(_self));
1180
+
1181
+ if (module.exports) {
1182
+ module.exports = Prism;
1183
+ }
1184
+
1185
+ // hack for components to work correctly in node.js
1186
+ if (typeof commonjsGlobal !== 'undefined') {
1187
+ commonjsGlobal.Prism = Prism;
1188
+ }
1189
+
1190
+ // some additional documentation/types
1191
+
1192
+ /**
1193
+ * The expansion of a simple `RegExp` literal to support additional properties.
1194
+ *
1195
+ * @typedef GrammarToken
1196
+ * @property {RegExp} pattern The regular expression of the token.
1197
+ * @property {boolean} [lookbehind=false] If `true`, then the first capturing group of `pattern` will (effectively)
1198
+ * behave as a lookbehind group meaning that the captured text will not be part of the matched text of the new token.
1199
+ * @property {boolean} [greedy=false] Whether the token is greedy.
1200
+ * @property {string|string[]} [alias] An optional alias or list of aliases.
1201
+ * @property {Grammar} [inside] The nested grammar of this token.
1202
+ *
1203
+ * The `inside` grammar will be used to tokenize the text value of each token of this kind.
1204
+ *
1205
+ * This can be used to make nested and even recursive language definitions.
1206
+ *
1207
+ * Note: This can cause infinite recursion. Be careful when you embed different languages or even the same language into
1208
+ * each another.
1209
+ * @global
1210
+ * @public
1211
+ */
1212
+
1213
+ /**
1214
+ * @typedef Grammar
1215
+ * @type {Object<string, RegExp | GrammarToken | Array<RegExp | GrammarToken>>}
1216
+ * @property {Grammar} [rest] An optional grammar object that will be appended to this grammar.
1217
+ * @global
1218
+ * @public
1219
+ */
1220
+
1221
+ /**
1222
+ * A function which will invoked after an element was successfully highlighted.
1223
+ *
1224
+ * @callback HighlightCallback
1225
+ * @param {Element} element The element successfully highlighted.
1226
+ * @returns {void}
1227
+ * @global
1228
+ * @public
1229
+ */
1230
+
1231
+ /**
1232
+ * @callback HookCallback
1233
+ * @param {Object<string, any>} env The environment variables of the hook.
1234
+ * @returns {void}
1235
+ * @global
1236
+ * @public
1237
+ */
1238
+ }(prismCore));
1239
+
1240
+ var Prism = prismCore.exports;
1241
+
1242
+ /**
1243
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
1244
+ *
1245
+ * This source code is licensed under the MIT license found in the
1246
+ * LICENSE file in the root directory of this source tree.
1247
+ *
1248
+ *
1249
+ */
1250
+ const DEFAULT_CODE_LANGUAGE = 'javascript';
1251
+ const getDefaultCodeLanguage = () => DEFAULT_CODE_LANGUAGE;
1252
+ const getCodeLanguages = () => Object.keys(Prism.languages).filter( // Prism has several language helpers mixed into languages object
1253
+ // so filtering them out here to get langs list
1254
+ language => typeof Prism.languages[language] !== 'function').sort();
1255
+ class CodeHighlightNode extends lexical.TextNode {
1256
+ constructor(text, highlightType, key) {
1257
+ super(text, key);
1258
+ this.__highlightType = highlightType;
1259
+ }
1260
+
1261
+ static getType() {
1262
+ return 'code-highlight';
1263
+ }
1264
+
1265
+ static clone(node) {
1266
+ return new CodeHighlightNode(node.__text, node.__highlightType || undefined, node.__key);
1267
+ }
1268
+
1269
+ createDOM(config) {
1270
+ const element = super.createDOM(config);
1271
+ const className = getHighlightThemeClass(config.theme, this.__highlightType);
1272
+ utils.addClassNamesToElement(element, className);
1273
+ return element;
1274
+ }
1275
+
1276
+ updateDOM( // $FlowFixMe
1277
+ prevNode, dom, config) {
1278
+ const update = super.updateDOM(prevNode, dom, config);
1279
+ const prevClassName = getHighlightThemeClass(config.theme, prevNode.__highlightType);
1280
+ const nextClassName = getHighlightThemeClass(config.theme, this.__highlightType);
1281
+
1282
+ if (prevClassName !== nextClassName) {
1283
+ if (prevClassName) {
1284
+ utils.removeClassNamesFromElement(dom, prevClassName);
1285
+ }
1286
+
1287
+ if (nextClassName) {
1288
+ utils.addClassNamesToElement(dom, nextClassName);
1289
+ }
1290
+ }
1291
+
1292
+ return update;
1293
+ } // Prevent formatting (bold, underline, etc)
1294
+
1295
+
1296
+ setFormat(format) {
1297
+ return this.getWritable();
1298
+ }
1299
+
1300
+ }
1301
+
1302
+ function getHighlightThemeClass(theme, highlightType) {
1303
+ return highlightType && theme && theme.codeHighlight && theme.codeHighlight[highlightType];
1304
+ }
1305
+
1306
+ function $createCodeHighlightNode(text, highlightType) {
1307
+ return new CodeHighlightNode(text, highlightType);
1308
+ }
1309
+ function $isCodeHighlightNode(node) {
1310
+ return node instanceof CodeHighlightNode;
1311
+ }
1312
+ class CodeNode extends lexical.ElementNode {
1313
+ static getType() {
1314
+ return 'code';
1315
+ }
1316
+
1317
+ static clone(node) {
1318
+ return new CodeNode(node.__language, node.__key);
1319
+ }
1320
+
1321
+ constructor(language, key) {
1322
+ super(key);
1323
+ this.__language = language;
1324
+ } // View
1325
+
1326
+
1327
+ createDOM(config) {
1328
+ const element = document.createElement('code');
1329
+ utils.addClassNamesToElement(element, config.theme.code);
1330
+ element.setAttribute('spellcheck', 'false');
1331
+ return element;
1332
+ }
1333
+
1334
+ updateDOM(prevNode, dom) {
1335
+ return false;
1336
+ }
1337
+
1338
+ static convertDOM() {
1339
+ return {
1340
+ div: node => ({
1341
+ conversion: convertDivElement,
1342
+ priority: 1
1343
+ }),
1344
+ pre: node => ({
1345
+ conversion: convertPreElement,
1346
+ priority: 0
1347
+ }),
1348
+ table: node => {
1349
+ // $FlowFixMe[incompatible-type] domNode is a <table> since we matched it by nodeName
1350
+ const table = node;
1351
+
1352
+ if (isGitHubCodeTable(table)) {
1353
+ return {
1354
+ conversion: convertTableElement,
1355
+ priority: 4
1356
+ };
1357
+ }
1358
+
1359
+ return null;
1360
+ },
1361
+ td: node => {
1362
+ // $FlowFixMe[incompatible-type] element is a <td> since we matched it by nodeName
1363
+ const td = node; // $FlowFixMe[incompatible-type] we know this will be a table, or null.
1364
+
1365
+ const table = td.closest('table');
1366
+
1367
+ if (isGitHubCodeCell(td)) {
1368
+ return {
1369
+ conversion: convertTableCellElement,
1370
+ priority: 4
1371
+ };
1372
+ }
1373
+
1374
+ if (table && isGitHubCodeTable(table)) {
1375
+ // Return a no-op if it's a table cell in a code table, but not a code line.
1376
+ // Otherwise it'll fall back to the T
1377
+ return {
1378
+ conversion: convertCodeNoop,
1379
+ priority: 4
1380
+ };
1381
+ }
1382
+
1383
+ return null;
1384
+ },
1385
+ tr: node => {
1386
+ // $FlowFixMe[incompatible-type] element is a <tr> since we matched it by nodeName
1387
+ const tr = node; // $FlowFixMe[incompatible-type] we know this will be a table, or null.
1388
+
1389
+ const table = tr.closest('table');
1390
+
1391
+ if (table && isGitHubCodeTable(table)) {
1392
+ return {
1393
+ conversion: convertCodeNoop,
1394
+ priority: 4
1395
+ };
1396
+ }
1397
+
1398
+ return null;
1399
+ }
1400
+ };
1401
+ } // Mutation
1402
+
1403
+
1404
+ insertNewAfter(selection) {
1405
+ const children = this.getChildren();
1406
+ const childrenLength = children.length;
1407
+
1408
+ if (childrenLength >= 2 && children[childrenLength - 1].getTextContent() === '\n' && children[childrenLength - 2].getTextContent() === '\n' && selection.isCollapsed() && selection.anchor.key === this.__key && selection.anchor.offset === childrenLength) {
1409
+ children[childrenLength - 1].remove();
1410
+ children[childrenLength - 2].remove();
1411
+ const newElement = lexical.$createParagraphNode();
1412
+ this.insertAfter(newElement);
1413
+ return newElement;
1414
+ } // If the selection is within the codeblock, find all leading tabs and
1415
+ // spaces of the current line. Create a new line that has all those
1416
+ // tabs and spaces, such that leading indentation is preserved.
1417
+
1418
+
1419
+ const anchor = selection.anchor.getNode();
1420
+ const firstNode = getFirstCodeHighlightNodeOfLine(anchor);
1421
+
1422
+ if (firstNode != null) {
1423
+ let leadingWhitespace = 0;
1424
+ const firstNodeText = firstNode.getTextContent();
1425
+
1426
+ while (leadingWhitespace < firstNodeText.length && /[\t ]/.test(firstNodeText[leadingWhitespace])) {
1427
+ leadingWhitespace += 1;
1428
+ }
1429
+
1430
+ if (leadingWhitespace > 0) {
1431
+ const whitespace = firstNodeText.substring(0, leadingWhitespace);
1432
+ const indentedChild = $createCodeHighlightNode(whitespace);
1433
+ anchor.insertAfter(indentedChild);
1434
+ selection.insertNodes([lexical.$createLineBreakNode()]);
1435
+ indentedChild.select();
1436
+ return indentedChild;
1437
+ }
1438
+ }
1439
+
1440
+ return null;
1441
+ }
1442
+
1443
+ canInsertTab() {
1444
+ return true;
1445
+ }
1446
+
1447
+ collapseAtStart() {
1448
+ const paragraph = lexical.$createParagraphNode();
1449
+ const children = this.getChildren();
1450
+ children.forEach(child => paragraph.append(child));
1451
+ this.replace(paragraph);
1452
+ return true;
1453
+ }
1454
+
1455
+ setLanguage(language) {
1456
+ const writable = this.getWritable();
1457
+ writable.__language = language;
1458
+ }
1459
+
1460
+ getLanguage() {
1461
+ return this.getLatest().__language;
1462
+ }
1463
+
1464
+ }
1465
+ function $createCodeNode(language) {
1466
+ return new CodeNode(language);
1467
+ }
1468
+ function $isCodeNode(node) {
1469
+ return node instanceof CodeNode;
1470
+ }
1471
+ function getFirstCodeHighlightNodeOfLine(anchor) {
1472
+ let currentNode = null;
1473
+ const previousSiblings = anchor.getPreviousSiblings();
1474
+ previousSiblings.push(anchor);
1475
+
1476
+ while (previousSiblings.length > 0) {
1477
+ const node = previousSiblings.pop();
1478
+
1479
+ if ($isCodeHighlightNode(node)) {
1480
+ currentNode = node;
1481
+ }
1482
+
1483
+ if (lexical.$isLineBreakNode(node)) {
1484
+ break;
1485
+ }
1486
+ }
1487
+
1488
+ return currentNode;
1489
+ }
1490
+ function getLastCodeHighlightNodeOfLine(anchor) {
1491
+ let currentNode = null;
1492
+ const nextSiblings = anchor.getNextSiblings();
1493
+ nextSiblings.unshift(anchor);
1494
+
1495
+ while (nextSiblings.length > 0) {
1496
+ const node = nextSiblings.shift();
1497
+
1498
+ if ($isCodeHighlightNode(node)) {
1499
+ currentNode = node;
1500
+ }
1501
+
1502
+ if (lexical.$isLineBreakNode(node)) {
1503
+ break;
1504
+ }
1505
+ }
1506
+
1507
+ return currentNode;
1508
+ }
1509
+
1510
+ function convertPreElement(domNode) {
1511
+ return {
1512
+ node: $createCodeNode()
1513
+ };
1514
+ }
1515
+
1516
+ function convertDivElement(domNode) {
1517
+ // $FlowFixMe[incompatible-type] domNode is a <div> since we matched it by nodeName
1518
+ const div = domNode;
1519
+ return {
1520
+ after: childLexicalNodes => {
1521
+ const domParent = domNode.parentNode;
1522
+
1523
+ if (domParent != null && domNode !== domParent.lastChild) {
1524
+ childLexicalNodes.push(lexical.$createLineBreakNode());
1525
+ }
1526
+
1527
+ return childLexicalNodes;
1528
+ },
1529
+ node: isCodeElement(div) ? $createCodeNode() : null
1530
+ };
1531
+ }
1532
+
1533
+ function convertTableElement() {
1534
+ return {
1535
+ node: $createCodeNode()
1536
+ };
1537
+ }
1538
+
1539
+ function convertCodeNoop() {
1540
+ return {
1541
+ node: null
1542
+ };
1543
+ }
1544
+
1545
+ function convertTableCellElement(domNode) {
1546
+ // $FlowFixMe[incompatible-type] domNode is a <td> since we matched it by nodeName
1547
+ const cell = domNode;
1548
+ return {
1549
+ after: childLexicalNodes => {
1550
+ if (cell.parentNode && cell.parentNode.nextSibling) {
1551
+ // Append newline between code lines
1552
+ childLexicalNodes.push(lexical.$createLineBreakNode());
1553
+ }
1554
+
1555
+ return childLexicalNodes;
1556
+ },
1557
+ node: null
1558
+ };
1559
+ }
1560
+
1561
+ function isCodeElement(div) {
1562
+ return div.style.fontFamily.match('monospace') !== null;
1563
+ }
1564
+
1565
+ function isGitHubCodeCell(cell) {
1566
+ return cell.classList.contains('js-file-line');
1567
+ }
1568
+
1569
+ function isGitHubCodeTable(table) {
1570
+ return table.classList.contains('js-file-line-container');
1571
+ }
1572
+
1573
+ function textNodeTransform(node, editor) {
1574
+ // Since CodeNode has flat children structure we only need to check
1575
+ // if node's parent is a code node and run highlighting if so
1576
+ const parentNode = node.getParent();
1577
+
1578
+ if ($isCodeNode(parentNode)) {
1579
+ codeNodeTransform(parentNode, editor);
1580
+ } else if ($isCodeHighlightNode(node)) {
1581
+ // When code block converted into paragraph or other element
1582
+ // code highlight nodes converted back to normal text
1583
+ node.replace(lexical.$createTextNode(node.__text));
1584
+ }
1585
+ } // Using `skipTransforms` to prevent extra transforms since reformatting the code
1586
+ // will not affect code block content itself.
1587
+ //
1588
+ // Using extra flag (`isHighlighting`) since both CodeNode and CodeHighlightNode
1589
+ // trasnforms might be called at the same time (e.g. new CodeHighlight node inserted) and
1590
+ // in both cases we'll rerun whole reformatting over CodeNode, which is redundant.
1591
+ // Especially when pasting code into CodeBlock.
1592
+
1593
+
1594
+ let isHighlighting = false;
1595
+
1596
+ function codeNodeTransform(node, editor) {
1597
+ if (isHighlighting) {
1598
+ return;
1599
+ }
1600
+
1601
+ isHighlighting = true;
1602
+ editor.update(() => {
1603
+ // When new code block inserted it might not have language selected
1604
+ if (node.getLanguage() === undefined) {
1605
+ node.setLanguage(DEFAULT_CODE_LANGUAGE);
1606
+ }
1607
+
1608
+ updateAndRetainSelection(node, () => {
1609
+ const code = node.getTextContent();
1610
+ const tokens = Prism.tokenize(code, Prism.languages[node.getLanguage() || ''] || Prism.languages[DEFAULT_CODE_LANGUAGE]);
1611
+ const highlightNodes = getHighlightNodes(tokens);
1612
+ const diffRange = getDiffRange(node.getChildren(), highlightNodes);
1613
+ const {
1614
+ from,
1615
+ to,
1616
+ nodesForReplacement
1617
+ } = diffRange;
1618
+
1619
+ if (from !== to || nodesForReplacement.length) {
1620
+ node.splice(from, to - from, nodesForReplacement);
1621
+ return true;
1622
+ }
1623
+
1624
+ return false;
1625
+ });
1626
+ }, {
1627
+ onUpdate: () => {
1628
+ isHighlighting = false;
1629
+ },
1630
+ skipTransforms: true
1631
+ });
1632
+ }
1633
+
1634
+ function getHighlightNodes(tokens) {
1635
+ const nodes = [];
1636
+ tokens.forEach(token => {
1637
+ if (typeof token === 'string') {
1638
+ const partials = token.split('\n');
1639
+
1640
+ for (let i = 0; i < partials.length; i++) {
1641
+ const text = partials[i];
1642
+
1643
+ if (text.length) {
1644
+ nodes.push($createCodeHighlightNode(text));
1645
+ }
1646
+
1647
+ if (i < partials.length - 1) {
1648
+ nodes.push(lexical.$createLineBreakNode());
1649
+ }
1650
+ }
1651
+ } else {
1652
+ const {
1653
+ content
1654
+ } = token;
1655
+
1656
+ if (typeof content === 'string') {
1657
+ nodes.push($createCodeHighlightNode(content, token.type));
1658
+ } else if (content.length === 1 && typeof content[0] === 'string') {
1659
+ nodes.push($createCodeHighlightNode(content[0], token.type));
1660
+ } else {
1661
+ nodes.push(...getHighlightNodes(content));
1662
+ }
1663
+ }
1664
+ });
1665
+ return nodes;
1666
+ } // Wrapping update function into selection retainer, that tries to keep cursor at the same
1667
+ // position as before.
1668
+
1669
+
1670
+ function updateAndRetainSelection(node, updateFn) {
1671
+ const selection = lexical.$getSelection();
1672
+
1673
+ if (!lexical.$isRangeSelection(selection) || !selection.anchor) {
1674
+ return;
1675
+ }
1676
+
1677
+ const anchor = selection.anchor;
1678
+ const anchorOffset = anchor.offset;
1679
+ const isNewLineAnchor = anchor.type === 'element' && lexical.$isLineBreakNode(node.getChildAtIndex(anchor.offset - 1));
1680
+ let textOffset = 0; // Calculating previous text offset (all text node prior to anchor + anchor own text offset)
1681
+
1682
+ if (!isNewLineAnchor) {
1683
+ const anchorNode = anchor.getNode();
1684
+ textOffset = anchorOffset + anchorNode.getPreviousSiblings().reduce((offset, _node) => {
1685
+ return offset + (lexical.$isLineBreakNode(_node) ? 0 : _node.getTextContentSize());
1686
+ }, 0);
1687
+ }
1688
+
1689
+ const hasChanges = updateFn();
1690
+
1691
+ if (!hasChanges) {
1692
+ return;
1693
+ } // Non-text anchors only happen for line breaks, otherwise
1694
+ // selection will be within text node (code highlight node)
1695
+
1696
+
1697
+ if (isNewLineAnchor) {
1698
+ anchor.getNode().select(anchorOffset, anchorOffset);
1699
+ return;
1700
+ } // If it was non-element anchor then we walk through child nodes
1701
+ // and looking for a position of original text offset
1702
+
1703
+
1704
+ node.getChildren().some(_node => {
1705
+ if (lexical.$isTextNode(_node)) {
1706
+ const textContentSize = _node.getTextContentSize();
1707
+
1708
+ if (textContentSize >= textOffset) {
1709
+ _node.select(textOffset, textOffset);
1710
+
1711
+ return true;
1712
+ }
1713
+
1714
+ textOffset -= textContentSize;
1715
+ }
1716
+
1717
+ return false;
1718
+ });
1719
+ } // Finds minimal diff range between two nodes lists. It returns from/to range boundaries of prevNodes
1720
+ // that needs to be replaced with `nodes` (subset of nextNodes) to make prevNodes equal to nextNodes.
1721
+
1722
+
1723
+ function getDiffRange(prevNodes, nextNodes) {
1724
+ let leadingMatch = 0;
1725
+
1726
+ while (leadingMatch < prevNodes.length) {
1727
+ if (!isEqual(prevNodes[leadingMatch], nextNodes[leadingMatch])) {
1728
+ break;
1729
+ }
1730
+
1731
+ leadingMatch++;
1732
+ }
1733
+
1734
+ const prevNodesLength = prevNodes.length;
1735
+ const nextNodesLength = nextNodes.length;
1736
+ const maxTrailingMatch = Math.min(prevNodesLength, nextNodesLength) - leadingMatch;
1737
+ let trailingMatch = 0;
1738
+
1739
+ while (trailingMatch < maxTrailingMatch) {
1740
+ trailingMatch++;
1741
+
1742
+ if (!isEqual(prevNodes[prevNodesLength - trailingMatch], nextNodes[nextNodesLength - trailingMatch])) {
1743
+ trailingMatch--;
1744
+ break;
1745
+ }
1746
+ }
1747
+
1748
+ const from = leadingMatch;
1749
+ const to = prevNodesLength - trailingMatch;
1750
+ const nodesForReplacement = nextNodes.slice(leadingMatch, nextNodesLength - trailingMatch);
1751
+ return {
1752
+ from,
1753
+ nodesForReplacement,
1754
+ to
1755
+ };
1756
+ }
1757
+
1758
+ function isEqual(nodeA, nodeB) {
1759
+ // Only checking for code higlight nodes and linebreaks. If it's regular text node
1760
+ // returning false so that it's transformed into code highlight node
1761
+ if ($isCodeHighlightNode(nodeA) && $isCodeHighlightNode(nodeB)) {
1762
+ return nodeA.__text === nodeB.__text && nodeA.__highlightType === nodeB.__highlightType;
1763
+ }
1764
+
1765
+ if (lexical.$isLineBreakNode(nodeA) && lexical.$isLineBreakNode(nodeB)) {
1766
+ return true;
1767
+ }
1768
+
1769
+ return false;
1770
+ }
1771
+
1772
+ function handleMultilineIndent(type) {
1773
+ const selection = lexical.$getSelection();
1774
+
1775
+ if (!lexical.$isRangeSelection(selection) || selection.isCollapsed()) {
1776
+ return false;
1777
+ } // Only run multiline indent logic on selections exclusively composed of code highlights and linebreaks
1778
+
1779
+
1780
+ const nodes = selection.getNodes();
1781
+
1782
+ for (let i = 0; i < nodes.length; i++) {
1783
+ const node = nodes[i];
1784
+
1785
+ if (!$isCodeHighlightNode(node) && !lexical.$isLineBreakNode(node)) {
1786
+ return false;
1787
+ }
1788
+ }
1789
+
1790
+ const startOfLine = getFirstCodeHighlightNodeOfLine(nodes[0]);
1791
+
1792
+ if (startOfLine != null) {
1793
+ doIndent(startOfLine, type);
1794
+ }
1795
+
1796
+ for (let i = 1; i < nodes.length; i++) {
1797
+ const node = nodes[i];
1798
+
1799
+ if (lexical.$isLineBreakNode(nodes[i - 1]) && $isCodeHighlightNode(node)) {
1800
+ doIndent(node, type);
1801
+ }
1802
+ }
1803
+
1804
+ return true;
1805
+ }
1806
+
1807
+ function doIndent(node, type) {
1808
+ const text = node.getTextContent();
1809
+
1810
+ if (type === lexical.INDENT_CONTENT_COMMAND) {
1811
+ // If the codeblock node doesn't start with whitespace, we don't want to
1812
+ // naively prepend a '\t'; Prism will then mangle all of our nodes when
1813
+ // it separates the whitespace from the first non-whitespace node. This
1814
+ // will lead to selection bugs when indenting lines that previously
1815
+ // didn't start with a whitespace character
1816
+ if (text.length > 0 && /\s/.test(text[0])) {
1817
+ node.setTextContent('\t' + text);
1818
+ } else {
1819
+ const indentNode = $createCodeHighlightNode('\t');
1820
+ node.insertBefore(indentNode);
1821
+ }
1822
+ } else {
1823
+ if (text.indexOf('\t') === 0) {
1824
+ // Same as above - if we leave empty text nodes lying around, the resulting
1825
+ // selection will be mangled
1826
+ if (text.length === 1) {
1827
+ node.remove();
1828
+ } else {
1829
+ node.setTextContent(text.substring(1));
1830
+ }
1831
+ }
1832
+ }
1833
+ }
1834
+
1835
+ function handleShiftLines(type, event) {
1836
+ // We only care about the alt+arrow keys
1837
+ const selection = lexical.$getSelection();
1838
+
1839
+ if (!event.altKey || !lexical.$isRangeSelection(selection)) {
1840
+ return false;
1841
+ } // I'm not quite sure why, but it seems like calling anchor.getNode() collapses the selection here
1842
+ // So first, get the anchor and the focus, then get their nodes
1843
+
1844
+
1845
+ const {
1846
+ anchor,
1847
+ focus
1848
+ } = selection;
1849
+ const anchorOffset = anchor.offset;
1850
+ const focusOffset = focus.offset;
1851
+ const anchorNode = anchor.getNode();
1852
+ const focusNode = focus.getNode(); // Ensure the selection is within the codeblock
1853
+
1854
+ if (!$isCodeHighlightNode(anchorNode) || !$isCodeHighlightNode(focusNode)) {
1855
+ return false;
1856
+ }
1857
+
1858
+ const start = getFirstCodeHighlightNodeOfLine(anchorNode);
1859
+ const end = getLastCodeHighlightNodeOfLine(focusNode);
1860
+
1861
+ if (start == null || end == null) {
1862
+ return false;
1863
+ }
1864
+
1865
+ const range = start.getNodesBetween(end);
1866
+
1867
+ for (let i = 0; i < range.length; i++) {
1868
+ const node = range[i];
1869
+
1870
+ if (!$isCodeHighlightNode(node) && !lexical.$isLineBreakNode(node)) {
1871
+ return false;
1872
+ }
1873
+ } // After this point, we know the selection is within the codeblock. We may not be able to
1874
+ // actually move the lines around, but we want to return true either way to prevent
1875
+ // the event's default behavior
1876
+
1877
+
1878
+ event.preventDefault();
1879
+ event.stopPropagation(); // required to stop cursor movement under Firefox
1880
+
1881
+ const arrowIsUp = type === lexical.KEY_ARROW_UP_COMMAND;
1882
+ const linebreak = arrowIsUp ? start.getPreviousSibling() : end.getNextSibling();
1883
+
1884
+ if (!lexical.$isLineBreakNode(linebreak)) {
1885
+ return true;
1886
+ }
1887
+
1888
+ const sibling = arrowIsUp ? linebreak.getPreviousSibling() : linebreak.getNextSibling();
1889
+
1890
+ if (sibling == null) {
1891
+ return true;
1892
+ }
1893
+
1894
+ const maybeInsertionPoint = arrowIsUp ? getFirstCodeHighlightNodeOfLine(sibling) : getLastCodeHighlightNodeOfLine(sibling);
1895
+ let insertionPoint = maybeInsertionPoint != null ? maybeInsertionPoint : sibling;
1896
+ linebreak.remove();
1897
+ range.forEach(node => node.remove());
1898
+
1899
+ if (type === lexical.KEY_ARROW_UP_COMMAND) {
1900
+ range.forEach(node => insertionPoint.insertBefore(node));
1901
+ insertionPoint.insertBefore(linebreak);
1902
+ } else {
1903
+ insertionPoint.insertAfter(linebreak);
1904
+ insertionPoint = linebreak;
1905
+ range.forEach(node => {
1906
+ insertionPoint.insertAfter(node);
1907
+ insertionPoint = node;
1908
+ });
1909
+ }
1910
+
1911
+ selection.setTextNodeRange(anchorNode, anchorOffset, focusNode, focusOffset);
1912
+ return true;
1913
+ }
1914
+
1915
+ function registerCodeHighlighting(editor) {
1916
+ if (!editor.hasNodes([CodeNode, CodeHighlightNode])) {
1917
+ throw new Error('CodeHighlightPlugin: CodeNode or CodeHighlightNode not registered on editor');
1918
+ }
1919
+
1920
+ return utils.mergeRegister(editor.registerNodeTransform(CodeNode, node => codeNodeTransform(node, editor)), editor.registerNodeTransform(lexical.TextNode, node => textNodeTransform(node, editor)), editor.registerNodeTransform(CodeHighlightNode, node => textNodeTransform(node, editor)), editor.registerCommand(lexical.INDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.INDENT_CONTENT_COMMAND), 1), editor.registerCommand(lexical.OUTDENT_CONTENT_COMMAND, payload => handleMultilineIndent(lexical.OUTDENT_CONTENT_COMMAND), 1), editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_UP_COMMAND, payload), 1), editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, payload => handleShiftLines(lexical.KEY_ARROW_DOWN_COMMAND, payload), 1));
1921
+ }
1922
+
1923
+ exports.$createCodeHighlightNode = $createCodeHighlightNode;
1924
+ exports.$createCodeNode = $createCodeNode;
1925
+ exports.$isCodeHighlightNode = $isCodeHighlightNode;
1926
+ exports.$isCodeNode = $isCodeNode;
1927
+ exports.CodeHighlightNode = CodeHighlightNode;
1928
+ exports.CodeNode = CodeNode;
1929
+ exports.getCodeLanguages = getCodeLanguages;
1930
+ exports.getDefaultCodeLanguage = getDefaultCodeLanguage;
1931
+ exports.getFirstCodeHighlightNodeOfLine = getFirstCodeHighlightNodeOfLine;
1932
+ exports.getLastCodeHighlightNodeOfLine = getLastCodeHighlightNodeOfLine;
1933
+ exports.registerCodeHighlighting = registerCodeHighlighting;