Highlight.js CSS Less SCSS Stylus

v11.7.0 Syntax highlighting for the Web

As of 2023-06-25. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/469422/1210675/Highlightjs%20CSS%20Less%20SCSS%20Stylus.js

  1. /*!
  2. Highlight.js v11.7.0 (git: 82688fad18)
  3. (c) 2006-2022 undefined and other contributors
  4. License: BSD-3-Clause
  5. */
  6. var hljs = (function () {
  7. 'use strict';
  8.  
  9. var deepFreezeEs6 = {exports: {}};
  10.  
  11. function deepFreeze(obj) {
  12. if (obj instanceof Map) {
  13. obj.clear = obj.delete = obj.set = function () {
  14. throw new Error('map is read-only');
  15. };
  16. } else if (obj instanceof Set) {
  17. obj.add = obj.clear = obj.delete = function () {
  18. throw new Error('set is read-only');
  19. };
  20. }
  21.  
  22. // Freeze self
  23. Object.freeze(obj);
  24.  
  25. Object.getOwnPropertyNames(obj).forEach(function (name) {
  26. var prop = obj[name];
  27.  
  28. // Freeze prop if it is an object
  29. if (typeof prop == 'object' && !Object.isFrozen(prop)) {
  30. deepFreeze(prop);
  31. }
  32. });
  33.  
  34. return obj;
  35. }
  36.  
  37. deepFreezeEs6.exports = deepFreeze;
  38. deepFreezeEs6.exports.default = deepFreeze;
  39.  
  40. /** @typedef {import('highlight.js').CallbackResponse} CallbackResponse */
  41. /** @typedef {import('highlight.js').CompiledMode} CompiledMode */
  42. /** @implements CallbackResponse */
  43.  
  44. class Response {
  45. /**
  46. * @param {CompiledMode} mode
  47. */
  48. constructor(mode) {
  49. // eslint-disable-next-line no-undefined
  50. if (mode.data === undefined) mode.data = {};
  51.  
  52. this.data = mode.data;
  53. this.isMatchIgnored = false;
  54. }
  55.  
  56. ignoreMatch() {
  57. this.isMatchIgnored = true;
  58. }
  59. }
  60.  
  61. /**
  62. * @param {string} value
  63. * @returns {string}
  64. */
  65. function escapeHTML(value) {
  66. return value
  67. .replace(/&/g, '&')
  68. .replace(/</g, '&lt;')
  69. .replace(/>/g, '&gt;')
  70. .replace(/"/g, '&quot;')
  71. .replace(/'/g, '&#x27;');
  72. }
  73.  
  74. /**
  75. * performs a shallow merge of multiple objects into one
  76. *
  77. * @template T
  78. * @param {T} original
  79. * @param {Record<string,any>[]} objects
  80. * @returns {T} a single new object
  81. */
  82. function inherit$1(original, ...objects) {
  83. /** @type Record<string,any> */
  84. const result = Object.create(null);
  85.  
  86. for (const key in original) {
  87. result[key] = original[key];
  88. }
  89. objects.forEach(function(obj) {
  90. for (const key in obj) {
  91. result[key] = obj[key];
  92. }
  93. });
  94. return /** @type {T} */ (result);
  95. }
  96.  
  97. /**
  98. * @typedef {object} Renderer
  99. * @property {(text: string) => void} addText
  100. * @property {(node: Node) => void} openNode
  101. * @property {(node: Node) => void} closeNode
  102. * @property {() => string} value
  103. */
  104.  
  105. /** @typedef {{scope?: string, language?: string, sublanguage?: boolean}} Node */
  106. /** @typedef {{walk: (r: Renderer) => void}} Tree */
  107. /** */
  108.  
  109. const SPAN_CLOSE = '</span>';
  110.  
  111. /**
  112. * Determines if a node needs to be wrapped in <span>
  113. *
  114. * @param {Node} node */
  115. const emitsWrappingTags = (node) => {
  116. // rarely we can have a sublanguage where language is undefined
  117. // TODO: track down why
  118. return !!node.scope || (node.sublanguage && node.language);
  119. };
  120.  
  121. /**
  122. *
  123. * @param {string} name
  124. * @param {{prefix:string}} options
  125. */
  126. const scopeToCSSClass = (name, { prefix }) => {
  127. if (name.includes(".")) {
  128. const pieces = name.split(".");
  129. return [
  130. `${prefix}${pieces.shift()}`,
  131. ...(pieces.map((x, i) => `${x}${"_".repeat(i + 1)}`))
  132. ].join(" ");
  133. }
  134. return `${prefix}${name}`;
  135. };
  136.  
  137. /** @type {Renderer} */
  138. class HTMLRenderer {
  139. /**
  140. * Creates a new HTMLRenderer
  141. *
  142. * @param {Tree} parseTree - the parse tree (must support `walk` API)
  143. * @param {{classPrefix: string}} options
  144. */
  145. constructor(parseTree, options) {
  146. this.buffer = "";
  147. this.classPrefix = options.classPrefix;
  148. parseTree.walk(this);
  149. }
  150.  
  151. /**
  152. * Adds texts to the output stream
  153. *
  154. * @param {string} text */
  155. addText(text) {
  156. this.buffer += escapeHTML(text);
  157. }
  158.  
  159. /**
  160. * Adds a node open to the output stream (if needed)
  161. *
  162. * @param {Node} node */
  163. openNode(node) {
  164. if (!emitsWrappingTags(node)) return;
  165.  
  166. let className = "";
  167. if (node.sublanguage) {
  168. className = `language-${node.language}`;
  169. } else {
  170. className = scopeToCSSClass(node.scope, { prefix: this.classPrefix });
  171. }
  172. this.span(className);
  173. }
  174.  
  175. /**
  176. * Adds a node close to the output stream (if needed)
  177. *
  178. * @param {Node} node */
  179. closeNode(node) {
  180. if (!emitsWrappingTags(node)) return;
  181.  
  182. this.buffer += SPAN_CLOSE;
  183. }
  184.  
  185. /**
  186. * returns the accumulated buffer
  187. */
  188. value() {
  189. return this.buffer;
  190. }
  191.  
  192. // helpers
  193.  
  194. /**
  195. * Builds a span element
  196. *
  197. * @param {string} className */
  198. span(className) {
  199. this.buffer += `<span class="${className}">`;
  200. }
  201. }
  202.  
  203. /** @typedef {{scope?: string, language?: string, sublanguage?: boolean, children: Node[]} | string} Node */
  204. /** @typedef {{scope?: string, language?: string, sublanguage?: boolean, children: Node[]} } DataNode */
  205. /** @typedef {import('highlight.js').Emitter} Emitter */
  206. /** */
  207.  
  208. /** @returns {DataNode} */
  209. const newNode = (opts = {}) => {
  210. /** @type DataNode */
  211. const result = { children: [] };
  212. Object.assign(result, opts);
  213. return result;
  214. };
  215.  
  216. class TokenTree {
  217. constructor() {
  218. /** @type DataNode */
  219. this.rootNode = newNode();
  220. this.stack = [this.rootNode];
  221. }
  222.  
  223. get top() {
  224. return this.stack[this.stack.length - 1];
  225. }
  226.  
  227. get root() { return this.rootNode; }
  228.  
  229. /** @param {Node} node */
  230. add(node) {
  231. this.top.children.push(node);
  232. }
  233.  
  234. /** @param {string} scope */
  235. openNode(scope) {
  236. /** @type Node */
  237. const node = newNode({ scope });
  238. this.add(node);
  239. this.stack.push(node);
  240. }
  241.  
  242. closeNode() {
  243. if (this.stack.length > 1) {
  244. return this.stack.pop();
  245. }
  246. // eslint-disable-next-line no-undefined
  247. return undefined;
  248. }
  249.  
  250. closeAllNodes() {
  251. while (this.closeNode());
  252. }
  253.  
  254. toJSON() {
  255. return JSON.stringify(this.rootNode, null, 4);
  256. }
  257.  
  258. /**
  259. * @typedef { import("./html_renderer").Renderer } Renderer
  260. * @param {Renderer} builder
  261. */
  262. walk(builder) {
  263. // this does not
  264. return this.constructor._walk(builder, this.rootNode);
  265. // this works
  266. // return TokenTree._walk(builder, this.rootNode);
  267. }
  268.  
  269. /**
  270. * @param {Renderer} builder
  271. * @param {Node} node
  272. */
  273. static _walk(builder, node) {
  274. if (typeof node === "string") {
  275. builder.addText(node);
  276. } else if (node.children) {
  277. builder.openNode(node);
  278. node.children.forEach((child) => this._walk(builder, child));
  279. builder.closeNode(node);
  280. }
  281. return builder;
  282. }
  283.  
  284. /**
  285. * @param {Node} node
  286. */
  287. static _collapse(node) {
  288. if (typeof node === "string") return;
  289. if (!node.children) return;
  290.  
  291. if (node.children.every(el => typeof el === "string")) {
  292. // node.text = node.children.join("");
  293. // delete node.children;
  294. node.children = [node.children.join("")];
  295. } else {
  296. node.children.forEach((child) => {
  297. TokenTree._collapse(child);
  298. });
  299. }
  300. }
  301. }
  302.  
  303. /**
  304. Currently this is all private API, but this is the minimal API necessary
  305. that an Emitter must implement to fully support the parser.
  306.  
  307. Minimal interface:
  308.  
  309. - addKeyword(text, scope)
  310. - addText(text)
  311. - addSublanguage(emitter, subLanguageName)
  312. - finalize()
  313. - openNode(scope)
  314. - closeNode()
  315. - closeAllNodes()
  316. - toHTML()
  317.  
  318. */
  319.  
  320. /**
  321. * @implements {Emitter}
  322. */
  323. class TokenTreeEmitter extends TokenTree {
  324. /**
  325. * @param {*} options
  326. */
  327. constructor(options) {
  328. super();
  329. this.options = options;
  330. }
  331.  
  332. /**
  333. * @param {string} text
  334. * @param {string} scope
  335. */
  336. addKeyword(text, scope) {
  337. if (text === "") { return; }
  338.  
  339. this.openNode(scope);
  340. this.addText(text);
  341. this.closeNode();
  342. }
  343.  
  344. /**
  345. * @param {string} text
  346. */
  347. addText(text) {
  348. if (text === "") { return; }
  349.  
  350. this.add(text);
  351. }
  352.  
  353. /**
  354. * @param {Emitter & {root: DataNode}} emitter
  355. * @param {string} name
  356. */
  357. addSublanguage(emitter, name) {
  358. /** @type DataNode */
  359. const node = emitter.root;
  360. node.sublanguage = true;
  361. node.language = name;
  362. this.add(node);
  363. }
  364.  
  365. toHTML() {
  366. const renderer = new HTMLRenderer(this, this.options);
  367. return renderer.value();
  368. }
  369.  
  370. finalize() {
  371. return true;
  372. }
  373. }
  374.  
  375. /**
  376. * @param {string} value
  377. * @returns {RegExp}
  378. * */
  379.  
  380. /**
  381. * @param {RegExp | string } re
  382. * @returns {string}
  383. */
  384. function source(re) {
  385. if (!re) return null;
  386. if (typeof re === "string") return re;
  387.  
  388. return re.source;
  389. }
  390.  
  391. /**
  392. * @param {RegExp | string } re
  393. * @returns {string}
  394. */
  395. function lookahead(re) {
  396. return concat('(?=', re, ')');
  397. }
  398.  
  399. /**
  400. * @param {RegExp | string } re
  401. * @returns {string}
  402. */
  403. function anyNumberOfTimes(re) {
  404. return concat('(?:', re, ')*');
  405. }
  406.  
  407. /**
  408. * @param {RegExp | string } re
  409. * @returns {string}
  410. */
  411. function optional(re) {
  412. return concat('(?:', re, ')?');
  413. }
  414.  
  415. /**
  416. * @param {...(RegExp | string) } args
  417. * @returns {string}
  418. */
  419. function concat(...args) {
  420. const joined = args.map((x) => source(x)).join("");
  421. return joined;
  422. }
  423.  
  424. /**
  425. * @param { Array<string | RegExp | Object> } args
  426. * @returns {object}
  427. */
  428. function stripOptionsFromArgs(args) {
  429. const opts = args[args.length - 1];
  430.  
  431. if (typeof opts === 'object' && opts.constructor === Object) {
  432. args.splice(args.length - 1, 1);
  433. return opts;
  434. } else {
  435. return {};
  436. }
  437. }
  438.  
  439. /** @typedef { {capture?: boolean} } RegexEitherOptions */
  440.  
  441. /**
  442. * Any of the passed expresssions may match
  443. *
  444. * Creates a huge this | this | that | that match
  445. * @param {(RegExp | string)[] | [...(RegExp | string)[], RegexEitherOptions]} args
  446. * @returns {string}
  447. */
  448. function either(...args) {
  449. /** @type { object & {capture?: boolean} } */
  450. const opts = stripOptionsFromArgs(args);
  451. const joined = '('
  452. + (opts.capture ? "" : "?:")
  453. + args.map((x) => source(x)).join("|") + ")";
  454. return joined;
  455. }
  456.  
  457. /**
  458. * @param {RegExp | string} re
  459. * @returns {number}
  460. */
  461. function countMatchGroups(re) {
  462. return (new RegExp(re.toString() + '|')).exec('').length - 1;
  463. }
  464.  
  465. /**
  466. * Does lexeme start with a regular expression match at the beginning
  467. * @param {RegExp} re
  468. * @param {string} lexeme
  469. */
  470. function startsWith(re, lexeme) {
  471. const match = re && re.exec(lexeme);
  472. return match && match.index === 0;
  473. }
  474.  
  475. // BACKREF_RE matches an open parenthesis or backreference. To avoid
  476. // an incorrect parse, it additionally matches the following:
  477. // - [...] elements, where the meaning of parentheses and escapes change
  478. // - other escape sequences, so we do not misparse escape sequences as
  479. // interesting elements
  480. // - non-matching or lookahead parentheses, which do not capture. These
  481. // follow the '(' with a '?'.
  482. const BACKREF_RE = /\[(?:[^\\\]]|\\.)*\]|\(\??|\\([1-9][0-9]*)|\\./;
  483.  
  484. // **INTERNAL** Not intended for outside usage
  485. // join logically computes regexps.join(separator), but fixes the
  486. // backreferences so they continue to match.
  487. // it also places each individual regular expression into it's own
  488. // match group, keeping track of the sequencing of those match groups
  489. // is currently an exercise for the caller. :-)
  490. /**
  491. * @param {(string | RegExp)[]} regexps
  492. * @param {{joinWith: string}} opts
  493. * @returns {string}
  494. */
  495. function _rewriteBackreferences(regexps, { joinWith }) {
  496. let numCaptures = 0;
  497.  
  498. return regexps.map((regex) => {
  499. numCaptures += 1;
  500. const offset = numCaptures;
  501. let re = source(regex);
  502. let out = '';
  503.  
  504. while (re.length > 0) {
  505. const match = BACKREF_RE.exec(re);
  506. if (!match) {
  507. out += re;
  508. break;
  509. }
  510. out += re.substring(0, match.index);
  511. re = re.substring(match.index + match[0].length);
  512. if (match[0][0] === '\\' && match[1]) {
  513. // Adjust the backreference.
  514. out += '\\' + String(Number(match[1]) + offset);
  515. } else {
  516. out += match[0];
  517. if (match[0] === '(') {
  518. numCaptures++;
  519. }
  520. }
  521. }
  522. return out;
  523. }).map(re => `(${re})`).join(joinWith);
  524. }
  525.  
  526. /** @typedef {import('highlight.js').Mode} Mode */
  527. /** @typedef {import('highlight.js').ModeCallback} ModeCallback */
  528.  
  529. // Common regexps
  530. const MATCH_NOTHING_RE = /\b\B/;
  531. const IDENT_RE = '[a-zA-Z]\\w*';
  532. const UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*';
  533. const NUMBER_RE = '\\b\\d+(\\.\\d+)?';
  534. const C_NUMBER_RE = '(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)'; // 0x..., 0..., decimal, float
  535. const BINARY_NUMBER_RE = '\\b(0b[01]+)'; // 0b...
  536. const RE_STARTERS_RE = '!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~';
  537.  
  538. /**
  539. * @param { Partial<Mode> & {binary?: string | RegExp} } opts
  540. */
  541. const SHEBANG = (opts = {}) => {
  542. const beginShebang = /^#![ ]*\//;
  543. if (opts.binary) {
  544. opts.begin = concat(
  545. beginShebang,
  546. /.*\b/,
  547. opts.binary,
  548. /\b.*/);
  549. }
  550. return inherit$1({
  551. scope: 'meta',
  552. begin: beginShebang,
  553. end: /$/,
  554. relevance: 0,
  555. /** @type {ModeCallback} */
  556. "on:begin": (m, resp) => {
  557. if (m.index !== 0) resp.ignoreMatch();
  558. }
  559. }, opts);
  560. };
  561.  
  562. // Common modes
  563. const BACKSLASH_ESCAPE = {
  564. begin: '\\\\[\\s\\S]', relevance: 0
  565. };
  566. const APOS_STRING_MODE = {
  567. scope: 'string',
  568. begin: '\'',
  569. end: '\'',
  570. illegal: '\\n',
  571. contains: [BACKSLASH_ESCAPE]
  572. };
  573. const QUOTE_STRING_MODE = {
  574. scope: 'string',
  575. begin: '"',
  576. end: '"',
  577. illegal: '\\n',
  578. contains: [BACKSLASH_ESCAPE]
  579. };
  580. const PHRASAL_WORDS_MODE = {
  581. begin: /\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/
  582. };
  583. /**
  584. * Creates a comment mode
  585. *
  586. * @param {string | RegExp} begin
  587. * @param {string | RegExp} end
  588. * @param {Mode | {}} [modeOptions]
  589. * @returns {Partial<Mode>}
  590. */
  591. const COMMENT = function(begin, end, modeOptions = {}) {
  592. const mode = inherit$1(
  593. {
  594. scope: 'comment',
  595. begin,
  596. end,
  597. contains: []
  598. },
  599. modeOptions
  600. );
  601. mode.contains.push({
  602. scope: 'doctag',
  603. // hack to avoid the space from being included. the space is necessary to
  604. // match here to prevent the plain text rule below from gobbling up doctags
  605. begin: '[ ]*(?=(TODO|FIXME|NOTE|BUG|OPTIMIZE|HACK|XXX):)',
  606. end: /(TODO|FIXME|NOTE|BUG|OPTIMIZE|HACK|XXX):/,
  607. excludeBegin: true,
  608. relevance: 0
  609. });
  610. const ENGLISH_WORD = either(
  611. // list of common 1 and 2 letter words in English
  612. "I",
  613. "a",
  614. "is",
  615. "so",
  616. "us",
  617. "to",
  618. "at",
  619. "if",
  620. "in",
  621. "it",
  622. "on",
  623. // note: this is not an exhaustive list of contractions, just popular ones
  624. /[A-Za-z]+['](d|ve|re|ll|t|s|n)/, // contractions - can't we'd they're let's, etc
  625. /[A-Za-z]+[-][a-z]+/, // `no-way`, etc.
  626. /[A-Za-z][a-z]{2,}/ // allow capitalized words at beginning of sentences
  627. );
  628. // looking like plain text, more likely to be a comment
  629. mode.contains.push(
  630. {
  631. // TODO: how to include ", (, ) without breaking grammars that use these for
  632. // comment delimiters?
  633. // begin: /[ ]+([()"]?([A-Za-z'-]{3,}|is|a|I|so|us|[tT][oO]|at|if|in|it|on)[.]?[()":]?([.][ ]|[ ]|\))){3}/
  634. // ---
  635.  
  636. // this tries to find sequences of 3 english words in a row (without any
  637. // "programming" type syntax) this gives us a strong signal that we've
  638. // TRULY found a comment - vs perhaps scanning with the wrong language.
  639. // It's possible to find something that LOOKS like the start of the
  640. // comment - but then if there is no readable text - good chance it is a
  641. // false match and not a comment.
  642. //
  643. // for a visual example please see:
  644. // https://github.com/highlightjs/highlight.js/issues/2827
  645.  
  646. begin: concat(
  647. /[ ]+/, // necessary to prevent us gobbling up doctags like /* @author Bob Mcgill */
  648. '(',
  649. ENGLISH_WORD,
  650. /[.]?[:]?([.][ ]|[ ])/,
  651. '){3}') // look for 3 words in a row
  652. }
  653. );
  654. return mode;
  655. };
  656. const C_LINE_COMMENT_MODE = COMMENT('//', '$');
  657. const C_BLOCK_COMMENT_MODE = COMMENT('/\\*', '\\*/');
  658. const HASH_COMMENT_MODE = COMMENT('#', '$');
  659. const NUMBER_MODE = {
  660. scope: 'number',
  661. begin: NUMBER_RE,
  662. relevance: 0
  663. };
  664. const C_NUMBER_MODE = {
  665. scope: 'number',
  666. begin: C_NUMBER_RE,
  667. relevance: 0
  668. };
  669. const BINARY_NUMBER_MODE = {
  670. scope: 'number',
  671. begin: BINARY_NUMBER_RE,
  672. relevance: 0
  673. };
  674. const REGEXP_MODE = {
  675. // this outer rule makes sure we actually have a WHOLE regex and not simply
  676. // an expression such as:
  677. //
  678. // 3 / something
  679. //
  680. // (which will then blow up when regex's `illegal` sees the newline)
  681. begin: /(?=\/[^/\n]*\/)/,
  682. contains: [{
  683. scope: 'regexp',
  684. begin: /\//,
  685. end: /\/[gimuy]*/,
  686. illegal: /\n/,
  687. contains: [
  688. BACKSLASH_ESCAPE,
  689. {
  690. begin: /\[/,
  691. end: /\]/,
  692. relevance: 0,
  693. contains: [BACKSLASH_ESCAPE]
  694. }
  695. ]
  696. }]
  697. };
  698. const TITLE_MODE = {
  699. scope: 'title',
  700. begin: IDENT_RE,
  701. relevance: 0
  702. };
  703. const UNDERSCORE_TITLE_MODE = {
  704. scope: 'title',
  705. begin: UNDERSCORE_IDENT_RE,
  706. relevance: 0
  707. };
  708. const METHOD_GUARD = {
  709. // excludes method names from keyword processing
  710. begin: '\\.\\s*' + UNDERSCORE_IDENT_RE,
  711. relevance: 0
  712. };
  713.  
  714. /**
  715. * Adds end same as begin mechanics to a mode
  716. *
  717. * Your mode must include at least a single () match group as that first match
  718. * group is what is used for comparison
  719. * @param {Partial<Mode>} mode
  720. */
  721. const END_SAME_AS_BEGIN = function(mode) {
  722. return Object.assign(mode,
  723. {
  724. /** @type {ModeCallback} */
  725. 'on:begin': (m, resp) => { resp.data._beginMatch = m[1]; },
  726. /** @type {ModeCallback} */
  727. 'on:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch(); }
  728. });
  729. };
  730.  
  731. var MODES = /*#__PURE__*/Object.freeze({
  732. __proto__: null,
  733. MATCH_NOTHING_RE: MATCH_NOTHING_RE,
  734. IDENT_RE: IDENT_RE,
  735. UNDERSCORE_IDENT_RE: UNDERSCORE_IDENT_RE,
  736. NUMBER_RE: NUMBER_RE,
  737. C_NUMBER_RE: C_NUMBER_RE,
  738. BINARY_NUMBER_RE: BINARY_NUMBER_RE,
  739. RE_STARTERS_RE: RE_STARTERS_RE,
  740. SHEBANG: SHEBANG,
  741. BACKSLASH_ESCAPE: BACKSLASH_ESCAPE,
  742. APOS_STRING_MODE: APOS_STRING_MODE,
  743. QUOTE_STRING_MODE: QUOTE_STRING_MODE,
  744. PHRASAL_WORDS_MODE: PHRASAL_WORDS_MODE,
  745. COMMENT: COMMENT,
  746. C_LINE_COMMENT_MODE: C_LINE_COMMENT_MODE,
  747. C_BLOCK_COMMENT_MODE: C_BLOCK_COMMENT_MODE,
  748. HASH_COMMENT_MODE: HASH_COMMENT_MODE,
  749. NUMBER_MODE: NUMBER_MODE,
  750. C_NUMBER_MODE: C_NUMBER_MODE,
  751. BINARY_NUMBER_MODE: BINARY_NUMBER_MODE,
  752. REGEXP_MODE: REGEXP_MODE,
  753. TITLE_MODE: TITLE_MODE,
  754. UNDERSCORE_TITLE_MODE: UNDERSCORE_TITLE_MODE,
  755. METHOD_GUARD: METHOD_GUARD,
  756. END_SAME_AS_BEGIN: END_SAME_AS_BEGIN
  757. });
  758.  
  759. /**
  760. @typedef {import('highlight.js').CallbackResponse} CallbackResponse
  761. @typedef {import('highlight.js').CompilerExt} CompilerExt
  762. */
  763.  
  764. // Grammar extensions / plugins
  765. // See: https://github.com/highlightjs/highlight.js/issues/2833
  766.  
  767. // Grammar extensions allow "syntactic sugar" to be added to the grammar modes
  768. // without requiring any underlying changes to the compiler internals.
  769.  
  770. // `compileMatch` being the perfect small example of now allowing a grammar
  771. // author to write `match` when they desire to match a single expression rather
  772. // than being forced to use `begin`. The extension then just moves `match` into
  773. // `begin` when it runs. Ie, no features have been added, but we've just made
  774. // the experience of writing (and reading grammars) a little bit nicer.
  775.  
  776. // ------
  777.  
  778. // TODO: We need negative look-behind support to do this properly
  779. /**
  780. * Skip a match if it has a preceding dot
  781. *
  782. * This is used for `beginKeywords` to prevent matching expressions such as
  783. * `bob.keyword.do()`. The mode compiler automatically wires this up as a
  784. * special _internal_ 'on:begin' callback for modes with `beginKeywords`
  785. * @param {RegExpMatchArray} match
  786. * @param {CallbackResponse} response
  787. */
  788. function skipIfHasPrecedingDot(match, response) {
  789. const before = match.input[match.index - 1];
  790. if (before === ".") {
  791. response.ignoreMatch();
  792. }
  793. }
  794.  
  795. /**
  796. *
  797. * @type {CompilerExt}
  798. */
  799. function scopeClassName(mode, _parent) {
  800. // eslint-disable-next-line no-undefined
  801. if (mode.className !== undefined) {
  802. mode.scope = mode.className;
  803. delete mode.className;
  804. }
  805. }
  806.  
  807. /**
  808. * `beginKeywords` syntactic sugar
  809. * @type {CompilerExt}
  810. */
  811. function beginKeywords(mode, parent) {
  812. if (!parent) return;
  813. if (!mode.beginKeywords) return;
  814.  
  815. // for languages with keywords that include non-word characters checking for
  816. // a word boundary is not sufficient, so instead we check for a word boundary
  817. // or whitespace - this does no harm in any case since our keyword engine
  818. // doesn't allow spaces in keywords anyways and we still check for the boundary
  819. // first
  820. mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')(?!\\.)(?=\\b|\\s)';
  821. mode.__beforeBegin = skipIfHasPrecedingDot;
  822. mode.keywords = mode.keywords || mode.beginKeywords;
  823. delete mode.beginKeywords;
  824.  
  825. // prevents double relevance, the keywords themselves provide
  826. // relevance, the mode doesn't need to double it
  827. // eslint-disable-next-line no-undefined
  828. if (mode.relevance === undefined) mode.relevance = 0;
  829. }
  830.  
  831. /**
  832. * Allow `illegal` to contain an array of illegal values
  833. * @type {CompilerExt}
  834. */
  835. function compileIllegal(mode, _parent) {
  836. if (!Array.isArray(mode.illegal)) return;
  837.  
  838. mode.illegal = either(...mode.illegal);
  839. }
  840.  
  841. /**
  842. * `match` to match a single expression for readability
  843. * @type {CompilerExt}
  844. */
  845. function compileMatch(mode, _parent) {
  846. if (!mode.match) return;
  847. if (mode.begin || mode.end) throw new Error("begin & end are not supported with match");
  848.  
  849. mode.begin = mode.match;
  850. delete mode.match;
  851. }
  852.  
  853. /**
  854. * provides the default 1 relevance to all modes
  855. * @type {CompilerExt}
  856. */
  857. function compileRelevance(mode, _parent) {
  858. // eslint-disable-next-line no-undefined
  859. if (mode.relevance === undefined) mode.relevance = 1;
  860. }
  861.  
  862. // allow beforeMatch to act as a "qualifier" for the match
  863. // the full match begin must be [beforeMatch][begin]
  864. const beforeMatchExt = (mode, parent) => {
  865. if (!mode.beforeMatch) return;
  866. // starts conflicts with endsParent which we need to make sure the child
  867. // rule is not matched multiple times
  868. if (mode.starts) throw new Error("beforeMatch cannot be used with starts");
  869.  
  870. const originalMode = Object.assign({}, mode);
  871. Object.keys(mode).forEach((key) => { delete mode[key]; });
  872.  
  873. mode.keywords = originalMode.keywords;
  874. mode.begin = concat(originalMode.beforeMatch, lookahead(originalMode.begin));
  875. mode.starts = {
  876. relevance: 0,
  877. contains: [
  878. Object.assign(originalMode, { endsParent: true })
  879. ]
  880. };
  881. mode.relevance = 0;
  882.  
  883. delete originalMode.beforeMatch;
  884. };
  885.  
  886. // keywords that should have no default relevance value
  887. const COMMON_KEYWORDS = [
  888. 'of',
  889. 'and',
  890. 'for',
  891. 'in',
  892. 'not',
  893. 'or',
  894. 'if',
  895. 'then',
  896. 'parent', // common variable name
  897. 'list', // common variable name
  898. 'value' // common variable name
  899. ];
  900.  
  901. const DEFAULT_KEYWORD_SCOPE = "keyword";
  902.  
  903. /**
  904. * Given raw keywords from a language definition, compile them.
  905. *
  906. * @param {string | Record<string,string|string[]> | Array<string>} rawKeywords
  907. * @param {boolean} caseInsensitive
  908. */
  909. function compileKeywords(rawKeywords, caseInsensitive, scopeName = DEFAULT_KEYWORD_SCOPE) {
  910. /** @type {import("highlight.js/private").KeywordDict} */
  911. const compiledKeywords = Object.create(null);
  912.  
  913. // input can be a string of keywords, an array of keywords, or a object with
  914. // named keys representing scopeName (which can then point to a string or array)
  915. if (typeof rawKeywords === 'string') {
  916. compileList(scopeName, rawKeywords.split(" "));
  917. } else if (Array.isArray(rawKeywords)) {
  918. compileList(scopeName, rawKeywords);
  919. } else {
  920. Object.keys(rawKeywords).forEach(function(scopeName) {
  921. // collapse all our objects back into the parent object
  922. Object.assign(
  923. compiledKeywords,
  924. compileKeywords(rawKeywords[scopeName], caseInsensitive, scopeName)
  925. );
  926. });
  927. }
  928. return compiledKeywords;
  929.  
  930. // ---
  931.  
  932. /**
  933. * Compiles an individual list of keywords
  934. *
  935. * Ex: "for if when while|5"
  936. *
  937. * @param {string} scopeName
  938. * @param {Array<string>} keywordList
  939. */
  940. function compileList(scopeName, keywordList) {
  941. if (caseInsensitive) {
  942. keywordList = keywordList.map(x => x.toLowerCase());
  943. }
  944. keywordList.forEach(function(keyword) {
  945. const pair = keyword.split('|');
  946. compiledKeywords[pair[0]] = [scopeName, scoreForKeyword(pair[0], pair[1])];
  947. });
  948. }
  949. }
  950.  
  951. /**
  952. * Returns the proper score for a given keyword
  953. *
  954. * Also takes into account comment keywords, which will be scored 0 UNLESS
  955. * another score has been manually assigned.
  956. * @param {string} keyword
  957. * @param {string} [providedScore]
  958. */
  959. function scoreForKeyword(keyword, providedScore) {
  960. // manual scores always win over common keywords
  961. // so you can force a score of 1 if you really insist
  962. if (providedScore) {
  963. return Number(providedScore);
  964. }
  965.  
  966. return commonKeyword(keyword) ? 0 : 1;
  967. }
  968.  
  969. /**
  970. * Determines if a given keyword is common or not
  971. *
  972. * @param {string} keyword */
  973. function commonKeyword(keyword) {
  974. return COMMON_KEYWORDS.includes(keyword.toLowerCase());
  975. }
  976.  
  977. /*
  978.  
  979. For the reasoning behind this please see:
  980. https://github.com/highlightjs/highlight.js/issues/2880#issuecomment-747275419
  981.  
  982. */
  983.  
  984. /**
  985. * @type {Record<string, boolean>}
  986. */
  987. const seenDeprecations = {};
  988.  
  989. /**
  990. * @param {string} message
  991. */
  992. const error = (message) => {
  993. console.error(message);
  994. };
  995.  
  996. /**
  997. * @param {string} message
  998. * @param {any} args
  999. */
  1000. const warn = (message, ...args) => {
  1001. console.log(`WARN: ${message}`, ...args);
  1002. };
  1003.  
  1004. /**
  1005. * @param {string} version
  1006. * @param {string} message
  1007. */
  1008. const deprecated = (version, message) => {
  1009. if (seenDeprecations[`${version}/${message}`]) return;
  1010.  
  1011. console.log(`Deprecated as of ${version}. ${message}`);
  1012. seenDeprecations[`${version}/${message}`] = true;
  1013. };
  1014.  
  1015. /* eslint-disable no-throw-literal */
  1016.  
  1017. /**
  1018. @typedef {import('highlight.js').CompiledMode} CompiledMode
  1019. */
  1020.  
  1021. const MultiClassError = new Error();
  1022.  
  1023. /**
  1024. * Renumbers labeled scope names to account for additional inner match
  1025. * groups that otherwise would break everything.
  1026. *
  1027. * Lets say we 3 match scopes:
  1028. *
  1029. * { 1 => ..., 2 => ..., 3 => ... }
  1030. *
  1031. * So what we need is a clean match like this:
  1032. *
  1033. * (a)(b)(c) => [ "a", "b", "c" ]
  1034. *
  1035. * But this falls apart with inner match groups:
  1036. *
  1037. * (a)(((b)))(c) => ["a", "b", "b", "b", "c" ]
  1038. *
  1039. * Our scopes are now "out of alignment" and we're repeating `b` 3 times.
  1040. * What needs to happen is the numbers are remapped:
  1041. *
  1042. * { 1 => ..., 2 => ..., 5 => ... }
  1043. *
  1044. * We also need to know that the ONLY groups that should be output
  1045. * are 1, 2, and 5. This function handles this behavior.
  1046. *
  1047. * @param {CompiledMode} mode
  1048. * @param {Array<RegExp | string>} regexes
  1049. * @param {{key: "beginScope"|"endScope"}} opts
  1050. */
  1051. function remapScopeNames(mode, regexes, { key }) {
  1052. let offset = 0;
  1053. const scopeNames = mode[key];
  1054. /** @type Record<number,boolean> */
  1055. const emit = {};
  1056. /** @type Record<number,string> */
  1057. const positions = {};
  1058.  
  1059. for (let i = 1; i <= regexes.length; i++) {
  1060. positions[i + offset] = scopeNames[i];
  1061. emit[i + offset] = true;
  1062. offset += countMatchGroups(regexes[i - 1]);
  1063. }
  1064. // we use _emit to keep track of which match groups are "top-level" to avoid double
  1065. // output from inside match groups
  1066. mode[key] = positions;
  1067. mode[key]._emit = emit;
  1068. mode[key]._multi = true;
  1069. }
  1070.  
  1071. /**
  1072. * @param {CompiledMode} mode
  1073. */
  1074. function beginMultiClass(mode) {
  1075. if (!Array.isArray(mode.begin)) return;
  1076.  
  1077. if (mode.skip || mode.excludeBegin || mode.returnBegin) {
  1078. error("skip, excludeBegin, returnBegin not compatible with beginScope: {}");
  1079. throw MultiClassError;
  1080. }
  1081.  
  1082. if (typeof mode.beginScope !== "object" || mode.beginScope === null) {
  1083. error("beginScope must be object");
  1084. throw MultiClassError;
  1085. }
  1086.  
  1087. remapScopeNames(mode, mode.begin, { key: "beginScope" });
  1088. mode.begin = _rewriteBackreferences(mode.begin, { joinWith: "" });
  1089. }
  1090.  
  1091. /**
  1092. * @param {CompiledMode} mode
  1093. */
  1094. function endMultiClass(mode) {
  1095. if (!Array.isArray(mode.end)) return;
  1096.  
  1097. if (mode.skip || mode.excludeEnd || mode.returnEnd) {
  1098. error("skip, excludeEnd, returnEnd not compatible with endScope: {}");
  1099. throw MultiClassError;
  1100. }
  1101.  
  1102. if (typeof mode.endScope !== "object" || mode.endScope === null) {
  1103. error("endScope must be object");
  1104. throw MultiClassError;
  1105. }
  1106.  
  1107. remapScopeNames(mode, mode.end, { key: "endScope" });
  1108. mode.end = _rewriteBackreferences(mode.end, { joinWith: "" });
  1109. }
  1110.  
  1111. /**
  1112. * this exists only to allow `scope: {}` to be used beside `match:`
  1113. * Otherwise `beginScope` would necessary and that would look weird
  1114.  
  1115. {
  1116. match: [ /def/, /\w+/ ]
  1117. scope: { 1: "keyword" , 2: "title" }
  1118. }
  1119.  
  1120. * @param {CompiledMode} mode
  1121. */
  1122. function scopeSugar(mode) {
  1123. if (mode.scope && typeof mode.scope === "object" && mode.scope !== null) {
  1124. mode.beginScope = mode.scope;
  1125. delete mode.scope;
  1126. }
  1127. }
  1128.  
  1129. /**
  1130. * @param {CompiledMode} mode
  1131. */
  1132. function MultiClass(mode) {
  1133. scopeSugar(mode);
  1134.  
  1135. if (typeof mode.beginScope === "string") {
  1136. mode.beginScope = { _wrap: mode.beginScope };
  1137. }
  1138. if (typeof mode.endScope === "string") {
  1139. mode.endScope = { _wrap: mode.endScope };
  1140. }
  1141.  
  1142. beginMultiClass(mode);
  1143. endMultiClass(mode);
  1144. }
  1145.  
  1146. /**
  1147. @typedef {import('highlight.js').Mode} Mode
  1148. @typedef {import('highlight.js').CompiledMode} CompiledMode
  1149. @typedef {import('highlight.js').Language} Language
  1150. @typedef {import('highlight.js').HLJSPlugin} HLJSPlugin
  1151. @typedef {import('highlight.js').CompiledLanguage} CompiledLanguage
  1152. */
  1153.  
  1154. // compilation
  1155.  
  1156. /**
  1157. * Compiles a language definition result
  1158. *
  1159. * Given the raw result of a language definition (Language), compiles this so
  1160. * that it is ready for highlighting code.
  1161. * @param {Language} language
  1162. * @returns {CompiledLanguage}
  1163. */
  1164. function compileLanguage(language) {
  1165. /**
  1166. * Builds a regex with the case sensitivity of the current language
  1167. *
  1168. * @param {RegExp | string} value
  1169. * @param {boolean} [global]
  1170. */
  1171. function langRe(value, global) {
  1172. return new RegExp(
  1173. source(value),
  1174. 'm'
  1175. + (language.case_insensitive ? 'i' : '')
  1176. + (language.unicodeRegex ? 'u' : '')
  1177. + (global ? 'g' : '')
  1178. );
  1179. }
  1180.  
  1181. /**
  1182. Stores multiple regular expressions and allows you to quickly search for
  1183. them all in a string simultaneously - returning the first match. It does
  1184. this by creating a huge (a|b|c) regex - each individual item wrapped with ()
  1185. and joined by `|` - using match groups to track position. When a match is
  1186. found checking which position in the array has content allows us to figure
  1187. out which of the original regexes / match groups triggered the match.
  1188.  
  1189. The match object itself (the result of `Regex.exec`) is returned but also
  1190. enhanced by merging in any meta-data that was registered with the regex.
  1191. This is how we keep track of which mode matched, and what type of rule
  1192. (`illegal`, `begin`, end, etc).
  1193. */
  1194. class MultiRegex {
  1195. constructor() {
  1196. this.matchIndexes = {};
  1197. // @ts-ignore
  1198. this.regexes = [];
  1199. this.matchAt = 1;
  1200. this.position = 0;
  1201. }
  1202.  
  1203. // @ts-ignore
  1204. addRule(re, opts) {
  1205. opts.position = this.position++;
  1206. // @ts-ignore
  1207. this.matchIndexes[this.matchAt] = opts;
  1208. this.regexes.push([opts, re]);
  1209. this.matchAt += countMatchGroups(re) + 1;
  1210. }
  1211.  
  1212. compile() {
  1213. if (this.regexes.length === 0) {
  1214. // avoids the need to check length every time exec is called
  1215. // @ts-ignore
  1216. this.exec = () => null;
  1217. }
  1218. const terminators = this.regexes.map(el => el[1]);
  1219. this.matcherRe = langRe(_rewriteBackreferences(terminators, { joinWith: '|' }), true);
  1220. this.lastIndex = 0;
  1221. }
  1222.  
  1223. /** @param {string} s */
  1224. exec(s) {
  1225. this.matcherRe.lastIndex = this.lastIndex;
  1226. const match = this.matcherRe.exec(s);
  1227. if (!match) { return null; }
  1228.  
  1229. // eslint-disable-next-line no-undefined
  1230. const i = match.findIndex((el, i) => i > 0 && el !== undefined);
  1231. // @ts-ignore
  1232. const matchData = this.matchIndexes[i];
  1233. // trim off any earlier non-relevant match groups (ie, the other regex
  1234. // match groups that make up the multi-matcher)
  1235. match.splice(0, i);
  1236.  
  1237. return Object.assign(match, matchData);
  1238. }
  1239. }
  1240.  
  1241. /*
  1242. Created to solve the key deficiently with MultiRegex - there is no way to
  1243. test for multiple matches at a single location. Why would we need to do
  1244. that? In the future a more dynamic engine will allow certain matches to be
  1245. ignored. An example: if we matched say the 3rd regex in a large group but
  1246. decided to ignore it - we'd need to started testing again at the 4th
  1247. regex... but MultiRegex itself gives us no real way to do that.
  1248.  
  1249. So what this class creates MultiRegexs on the fly for whatever search
  1250. position they are needed.
  1251.  
  1252. NOTE: These additional MultiRegex objects are created dynamically. For most
  1253. grammars most of the time we will never actually need anything more than the
  1254. first MultiRegex - so this shouldn't have too much overhead.
  1255.  
  1256. Say this is our search group, and we match regex3, but wish to ignore it.
  1257.  
  1258. regex1 | regex2 | regex3 | regex4 | regex5 ' ie, startAt = 0
  1259.  
  1260. What we need is a new MultiRegex that only includes the remaining
  1261. possibilities:
  1262.  
  1263. regex4 | regex5 ' ie, startAt = 3
  1264.  
  1265. This class wraps all that complexity up in a simple API... `startAt` decides
  1266. where in the array of expressions to start doing the matching. It
  1267. auto-increments, so if a match is found at position 2, then startAt will be
  1268. set to 3. If the end is reached startAt will return to 0.
  1269.  
  1270. MOST of the time the parser will be setting startAt manually to 0.
  1271. */
  1272. class ResumableMultiRegex {
  1273. constructor() {
  1274. // @ts-ignore
  1275. this.rules = [];
  1276. // @ts-ignore
  1277. this.multiRegexes = [];
  1278. this.count = 0;
  1279.  
  1280. this.lastIndex = 0;
  1281. this.regexIndex = 0;
  1282. }
  1283.  
  1284. // @ts-ignore
  1285. getMatcher(index) {
  1286. if (this.multiRegexes[index]) return this.multiRegexes[index];
  1287.  
  1288. const matcher = new MultiRegex();
  1289. this.rules.slice(index).forEach(([re, opts]) => matcher.addRule(re, opts));
  1290. matcher.compile();
  1291. this.multiRegexes[index] = matcher;
  1292. return matcher;
  1293. }
  1294.  
  1295. resumingScanAtSamePosition() {
  1296. return this.regexIndex !== 0;
  1297. }
  1298.  
  1299. considerAll() {
  1300. this.regexIndex = 0;
  1301. }
  1302.  
  1303. // @ts-ignore
  1304. addRule(re, opts) {
  1305. this.rules.push([re, opts]);
  1306. if (opts.type === "begin") this.count++;
  1307. }
  1308.  
  1309. /** @param {string} s */
  1310. exec(s) {
  1311. const m = this.getMatcher(this.regexIndex);
  1312. m.lastIndex = this.lastIndex;
  1313. let result = m.exec(s);
  1314.  
  1315. // The following is because we have no easy way to say "resume scanning at the
  1316. // existing position but also skip the current rule ONLY". What happens is
  1317. // all prior rules are also skipped which can result in matching the wrong
  1318. // thing. Example of matching "booger":
  1319.  
  1320. // our matcher is [string, "booger", number]
  1321. //
  1322. // ....booger....
  1323.  
  1324. // if "booger" is ignored then we'd really need a regex to scan from the
  1325. // SAME position for only: [string, number] but ignoring "booger" (if it
  1326. // was the first match), a simple resume would scan ahead who knows how
  1327. // far looking only for "number", ignoring potential string matches (or
  1328. // future "booger" matches that might be valid.)
  1329.  
  1330. // So what we do: We execute two matchers, one resuming at the same
  1331. // position, but the second full matcher starting at the position after:
  1332.  
  1333. // /--- resume first regex match here (for [number])
  1334. // |/---- full match here for [string, "booger", number]
  1335. // vv
  1336. // ....booger....
  1337.  
  1338. // Which ever results in a match first is then used. So this 3-4 step
  1339. // process essentially allows us to say "match at this position, excluding
  1340. // a prior rule that was ignored".
  1341. //
  1342. // 1. Match "booger" first, ignore. Also proves that [string] does non match.
  1343. // 2. Resume matching for [number]
  1344. // 3. Match at index + 1 for [string, "booger", number]
  1345. // 4. If #2 and #3 result in matches, which came first?
  1346. if (this.resumingScanAtSamePosition()) {
  1347. if (result && result.index === this.lastIndex) ; else { // use the second matcher result
  1348. const m2 = this.getMatcher(0);
  1349. m2.lastIndex = this.lastIndex + 1;
  1350. result = m2.exec(s);
  1351. }
  1352. }
  1353.  
  1354. if (result) {
  1355. this.regexIndex += result.position + 1;
  1356. if (this.regexIndex === this.count) {
  1357. // wrap-around to considering all matches again
  1358. this.considerAll();
  1359. }
  1360. }
  1361.  
  1362. return result;
  1363. }
  1364. }
  1365.  
  1366. /**
  1367. * Given a mode, builds a huge ResumableMultiRegex that can be used to walk
  1368. * the content and find matches.
  1369. *
  1370. * @param {CompiledMode} mode
  1371. * @returns {ResumableMultiRegex}
  1372. */
  1373. function buildModeRegex(mode) {
  1374. const mm = new ResumableMultiRegex();
  1375.  
  1376. mode.contains.forEach(term => mm.addRule(term.begin, { rule: term, type: "begin" }));
  1377.  
  1378. if (mode.terminatorEnd) {
  1379. mm.addRule(mode.terminatorEnd, { type: "end" });
  1380. }
  1381. if (mode.illegal) {
  1382. mm.addRule(mode.illegal, { type: "illegal" });
  1383. }
  1384.  
  1385. return mm;
  1386. }
  1387.  
  1388. /** skip vs abort vs ignore
  1389. *
  1390. * @skip - The mode is still entered and exited normally (and contains rules apply),
  1391. * but all content is held and added to the parent buffer rather than being
  1392. * output when the mode ends. Mostly used with `sublanguage` to build up
  1393. * a single large buffer than can be parsed by sublanguage.
  1394. *
  1395. * - The mode begin ands ends normally.
  1396. * - Content matched is added to the parent mode buffer.
  1397. * - The parser cursor is moved forward normally.
  1398. *
  1399. * @abort - A hack placeholder until we have ignore. Aborts the mode (as if it
  1400. * never matched) but DOES NOT continue to match subsequent `contains`
  1401. * modes. Abort is bad/suboptimal because it can result in modes
  1402. * farther down not getting applied because an earlier rule eats the
  1403. * content but then aborts.
  1404. *
  1405. * - The mode does not begin.
  1406. * - Content matched by `begin` is added to the mode buffer.
  1407. * - The parser cursor is moved forward accordingly.
  1408. *
  1409. * @ignore - Ignores the mode (as if it never matched) and continues to match any
  1410. * subsequent `contains` modes. Ignore isn't technically possible with
  1411. * the current parser implementation.
  1412. *
  1413. * - The mode does not begin.
  1414. * - Content matched by `begin` is ignored.
  1415. * - The parser cursor is not moved forward.
  1416. */
  1417.  
  1418. /**
  1419. * Compiles an individual mode
  1420. *
  1421. * This can raise an error if the mode contains certain detectable known logic
  1422. * issues.
  1423. * @param {Mode} mode
  1424. * @param {CompiledMode | null} [parent]
  1425. * @returns {CompiledMode | never}
  1426. */
  1427. function compileMode(mode, parent) {
  1428. const cmode = /** @type CompiledMode */ (mode);
  1429. if (mode.isCompiled) return cmode;
  1430.  
  1431. [
  1432. scopeClassName,
  1433. // do this early so compiler extensions generally don't have to worry about
  1434. // the distinction between match/begin
  1435. compileMatch,
  1436. MultiClass,
  1437. beforeMatchExt
  1438. ].forEach(ext => ext(mode, parent));
  1439.  
  1440. language.compilerExtensions.forEach(ext => ext(mode, parent));
  1441.  
  1442. // __beforeBegin is considered private API, internal use only
  1443. mode.__beforeBegin = null;
  1444.  
  1445. [
  1446. beginKeywords,
  1447. // do this later so compiler extensions that come earlier have access to the
  1448. // raw array if they wanted to perhaps manipulate it, etc.
  1449. compileIllegal,
  1450. // default to 1 relevance if not specified
  1451. compileRelevance
  1452. ].forEach(ext => ext(mode, parent));
  1453.  
  1454. mode.isCompiled = true;
  1455.  
  1456. let keywordPattern = null;
  1457. if (typeof mode.keywords === "object" && mode.keywords.$pattern) {
  1458. // we need a copy because keywords might be compiled multiple times
  1459. // so we can't go deleting $pattern from the original on the first
  1460. // pass
  1461. mode.keywords = Object.assign({}, mode.keywords);
  1462. keywordPattern = mode.keywords.$pattern;
  1463. delete mode.keywords.$pattern;
  1464. }
  1465. keywordPattern = keywordPattern || /\w+/;
  1466.  
  1467. if (mode.keywords) {
  1468. mode.keywords = compileKeywords(mode.keywords, language.case_insensitive);
  1469. }
  1470.  
  1471. cmode.keywordPatternRe = langRe(keywordPattern, true);
  1472.  
  1473. if (parent) {
  1474. if (!mode.begin) mode.begin = /\B|\b/;
  1475. cmode.beginRe = langRe(cmode.begin);
  1476. if (!mode.end && !mode.endsWithParent) mode.end = /\B|\b/;
  1477. if (mode.end) cmode.endRe = langRe(cmode.end);
  1478. cmode.terminatorEnd = source(cmode.end) || '';
  1479. if (mode.endsWithParent && parent.terminatorEnd) {
  1480. cmode.terminatorEnd += (mode.end ? '|' : '') + parent.terminatorEnd;
  1481. }
  1482. }
  1483. if (mode.illegal) cmode.illegalRe = langRe(/** @type {RegExp | string} */ (mode.illegal));
  1484. if (!mode.contains) mode.contains = [];
  1485.  
  1486. mode.contains = [].concat(...mode.contains.map(function(c) {
  1487. return expandOrCloneMode(c === 'self' ? mode : c);
  1488. }));
  1489. mode.contains.forEach(function(c) { compileMode(/** @type Mode */ (c), cmode); });
  1490.  
  1491. if (mode.starts) {
  1492. compileMode(mode.starts, parent);
  1493. }
  1494.  
  1495. cmode.matcher = buildModeRegex(cmode);
  1496. return cmode;
  1497. }
  1498.  
  1499. if (!language.compilerExtensions) language.compilerExtensions = [];
  1500.  
  1501. // self is not valid at the top-level
  1502. if (language.contains && language.contains.includes('self')) {
  1503. throw new Error("ERR: contains `self` is not supported at the top-level of a language. See documentation.");
  1504. }
  1505.  
  1506. // we need a null object, which inherit will guarantee
  1507. language.classNameAliases = inherit$1(language.classNameAliases || {});
  1508.  
  1509. return compileMode(/** @type Mode */ (language));
  1510. }
  1511.  
  1512. /**
  1513. * Determines if a mode has a dependency on it's parent or not
  1514. *
  1515. * If a mode does have a parent dependency then often we need to clone it if
  1516. * it's used in multiple places so that each copy points to the correct parent,
  1517. * where-as modes without a parent can often safely be re-used at the bottom of
  1518. * a mode chain.
  1519. *
  1520. * @param {Mode | null} mode
  1521. * @returns {boolean} - is there a dependency on the parent?
  1522. * */
  1523. function dependencyOnParent(mode) {
  1524. if (!mode) return false;
  1525.  
  1526. return mode.endsWithParent || dependencyOnParent(mode.starts);
  1527. }
  1528.  
  1529. /**
  1530. * Expands a mode or clones it if necessary
  1531. *
  1532. * This is necessary for modes with parental dependenceis (see notes on
  1533. * `dependencyOnParent`) and for nodes that have `variants` - which must then be
  1534. * exploded into their own individual modes at compile time.
  1535. *
  1536. * @param {Mode} mode
  1537. * @returns {Mode | Mode[]}
  1538. * */
  1539. function expandOrCloneMode(mode) {
  1540. if (mode.variants && !mode.cachedVariants) {
  1541. mode.cachedVariants = mode.variants.map(function(variant) {
  1542. return inherit$1(mode, { variants: null }, variant);
  1543. });
  1544. }
  1545.  
  1546. // EXPAND
  1547. // if we have variants then essentially "replace" the mode with the variants
  1548. // this happens in compileMode, where this function is called from
  1549. if (mode.cachedVariants) {
  1550. return mode.cachedVariants;
  1551. }
  1552.  
  1553. // CLONE
  1554. // if we have dependencies on parents then we need a unique
  1555. // instance of ourselves, so we can be reused with many
  1556. // different parents without issue
  1557. if (dependencyOnParent(mode)) {
  1558. return inherit$1(mode, { starts: mode.starts ? inherit$1(mode.starts) : null });
  1559. }
  1560.  
  1561. if (Object.isFrozen(mode)) {
  1562. return inherit$1(mode);
  1563. }
  1564.  
  1565. // no special dependency issues, just return ourselves
  1566. return mode;
  1567. }
  1568.  
  1569. var version = "11.7.0";
  1570.  
  1571. class HTMLInjectionError extends Error {
  1572. constructor(reason, html) {
  1573. super(reason);
  1574. this.name = "HTMLInjectionError";
  1575. this.html = html;
  1576. }
  1577. }
  1578.  
  1579. /*
  1580. Syntax highlighting with language autodetection.
  1581. https://highlightjs.org/
  1582. */
  1583.  
  1584. /**
  1585. @typedef {import('highlight.js').Mode} Mode
  1586. @typedef {import('highlight.js').CompiledMode} CompiledMode
  1587. @typedef {import('highlight.js').CompiledScope} CompiledScope
  1588. @typedef {import('highlight.js').Language} Language
  1589. @typedef {import('highlight.js').HLJSApi} HLJSApi
  1590. @typedef {import('highlight.js').HLJSPlugin} HLJSPlugin
  1591. @typedef {import('highlight.js').PluginEvent} PluginEvent
  1592. @typedef {import('highlight.js').HLJSOptions} HLJSOptions
  1593. @typedef {import('highlight.js').LanguageFn} LanguageFn
  1594. @typedef {import('highlight.js').HighlightedHTMLElement} HighlightedHTMLElement
  1595. @typedef {import('highlight.js').BeforeHighlightContext} BeforeHighlightContext
  1596. @typedef {import('highlight.js/private').MatchType} MatchType
  1597. @typedef {import('highlight.js/private').KeywordData} KeywordData
  1598. @typedef {import('highlight.js/private').EnhancedMatch} EnhancedMatch
  1599. @typedef {import('highlight.js/private').AnnotatedError} AnnotatedError
  1600. @typedef {import('highlight.js').AutoHighlightResult} AutoHighlightResult
  1601. @typedef {import('highlight.js').HighlightOptions} HighlightOptions
  1602. @typedef {import('highlight.js').HighlightResult} HighlightResult
  1603. */
  1604.  
  1605.  
  1606. const escape = escapeHTML;
  1607. const inherit = inherit$1;
  1608. const NO_MATCH = Symbol("nomatch");
  1609. const MAX_KEYWORD_HITS = 7;
  1610.  
  1611. /**
  1612. * @param {any} hljs - object that is extended (legacy)
  1613. * @returns {HLJSApi}
  1614. */
  1615. const HLJS = function(hljs) {
  1616. // Global internal variables used within the highlight.js library.
  1617. /** @type {Record<string, Language>} */
  1618. const languages = Object.create(null);
  1619. /** @type {Record<string, string>} */
  1620. const aliases = Object.create(null);
  1621. /** @type {HLJSPlugin[]} */
  1622. const plugins = [];
  1623.  
  1624. // safe/production mode - swallows more errors, tries to keep running
  1625. // even if a single syntax or parse hits a fatal error
  1626. let SAFE_MODE = true;
  1627. const LANGUAGE_NOT_FOUND = "Could not find the language '{}', did you forget to load/include a language module?";
  1628. /** @type {Language} */
  1629. const PLAINTEXT_LANGUAGE = { disableAutodetect: true, name: 'Plain text', contains: [] };
  1630.  
  1631. // Global options used when within external APIs. This is modified when
  1632. // calling the `hljs.configure` function.
  1633. /** @type HLJSOptions */
  1634. let options = {
  1635. ignoreUnescapedHTML: false,
  1636. throwUnescapedHTML: false,
  1637. noHighlightRe: /^(no-?highlight)$/i,
  1638. languageDetectRe: /\blang(?:uage)?-([\w-]+)\b/i,
  1639. classPrefix: 'hljs-',
  1640. cssSelector: 'pre code',
  1641. languages: null,
  1642. // beta configuration options, subject to change, welcome to discuss
  1643. // https://github.com/highlightjs/highlight.js/issues/1086
  1644. __emitter: TokenTreeEmitter
  1645. };
  1646.  
  1647. /* Utility functions */
  1648.  
  1649. /**
  1650. * Tests a language name to see if highlighting should be skipped
  1651. * @param {string} languageName
  1652. */
  1653. function shouldNotHighlight(languageName) {
  1654. return options.noHighlightRe.test(languageName);
  1655. }
  1656.  
  1657. /**
  1658. * @param {HighlightedHTMLElement} block - the HTML element to determine language for
  1659. */
  1660. function blockLanguage(block) {
  1661. let classes = block.className + ' ';
  1662.  
  1663. classes += block.parentNode ? block.parentNode.className : '';
  1664.  
  1665. // language-* takes precedence over non-prefixed class names.
  1666. const match = options.languageDetectRe.exec(classes);
  1667. if (match) {
  1668. const language = getLanguage(match[1]);
  1669. if (!language) {
  1670. warn(LANGUAGE_NOT_FOUND.replace("{}", match[1]));
  1671. warn("Falling back to no-highlight mode for this block.", block);
  1672. }
  1673. return language ? match[1] : 'no-highlight';
  1674. }
  1675.  
  1676. return classes
  1677. .split(/\s+/)
  1678. .find((_class) => shouldNotHighlight(_class) || getLanguage(_class));
  1679. }
  1680.  
  1681. /**
  1682. * Core highlighting function.
  1683. *
  1684. * OLD API
  1685. * highlight(lang, code, ignoreIllegals, continuation)
  1686. *
  1687. * NEW API
  1688. * highlight(code, {lang, ignoreIllegals})
  1689. *
  1690. * @param {string} codeOrLanguageName - the language to use for highlighting
  1691. * @param {string | HighlightOptions} optionsOrCode - the code to highlight
  1692. * @param {boolean} [ignoreIllegals] - whether to ignore illegal matches, default is to bail
  1693. *
  1694. * @returns {HighlightResult} Result - an object that represents the result
  1695. * @property {string} language - the language name
  1696. * @property {number} relevance - the relevance score
  1697. * @property {string} value - the highlighted HTML code
  1698. * @property {string} code - the original raw code
  1699. * @property {CompiledMode} top - top of the current mode stack
  1700. * @property {boolean} illegal - indicates whether any illegal matches were found
  1701. */
  1702. function highlight(codeOrLanguageName, optionsOrCode, ignoreIllegals) {
  1703. let code = "";
  1704. let languageName = "";
  1705. if (typeof optionsOrCode === "object") {
  1706. code = codeOrLanguageName;
  1707. ignoreIllegals = optionsOrCode.ignoreIllegals;
  1708. languageName = optionsOrCode.language;
  1709. } else {
  1710. // old API
  1711. deprecated("10.7.0", "highlight(lang, code, ...args) has been deprecated.");
  1712. deprecated("10.7.0", "Please use highlight(code, options) instead.\nhttps://github.com/highlightjs/highlight.js/issues/2277");
  1713. languageName = codeOrLanguageName;
  1714. code = optionsOrCode;
  1715. }
  1716.  
  1717. // https://github.com/highlightjs/highlight.js/issues/3149
  1718. // eslint-disable-next-line no-undefined
  1719. if (ignoreIllegals === undefined) { ignoreIllegals = true; }
  1720.  
  1721. /** @type {BeforeHighlightContext} */
  1722. const context = {
  1723. code,
  1724. language: languageName
  1725. };
  1726. // the plugin can change the desired language or the code to be highlighted
  1727. // just be changing the object it was passed
  1728. fire("before:highlight", context);
  1729.  
  1730. // a before plugin can usurp the result completely by providing it's own
  1731. // in which case we don't even need to call highlight
  1732. const result = context.result
  1733. ? context.result
  1734. : _highlight(context.language, context.code, ignoreIllegals);
  1735.  
  1736. result.code = context.code;
  1737. // the plugin can change anything in result to suite it
  1738. fire("after:highlight", result);
  1739.  
  1740. return result;
  1741. }
  1742.  
  1743. /**
  1744. * private highlight that's used internally and does not fire callbacks
  1745. *
  1746. * @param {string} languageName - the language to use for highlighting
  1747. * @param {string} codeToHighlight - the code to highlight
  1748. * @param {boolean?} [ignoreIllegals] - whether to ignore illegal matches, default is to bail
  1749. * @param {CompiledMode?} [continuation] - current continuation mode, if any
  1750. * @returns {HighlightResult} - result of the highlight operation
  1751. */
  1752. function _highlight(languageName, codeToHighlight, ignoreIllegals, continuation) {
  1753. const keywordHits = Object.create(null);
  1754.  
  1755. /**
  1756. * Return keyword data if a match is a keyword
  1757. * @param {CompiledMode} mode - current mode
  1758. * @param {string} matchText - the textual match
  1759. * @returns {KeywordData | false}
  1760. */
  1761. function keywordData(mode, matchText) {
  1762. return mode.keywords[matchText];
  1763. }
  1764.  
  1765. function processKeywords() {
  1766. if (!top.keywords) {
  1767. emitter.addText(modeBuffer);
  1768. return;
  1769. }
  1770.  
  1771. let lastIndex = 0;
  1772. top.keywordPatternRe.lastIndex = 0;
  1773. let match = top.keywordPatternRe.exec(modeBuffer);
  1774. let buf = "";
  1775.  
  1776. while (match) {
  1777. buf += modeBuffer.substring(lastIndex, match.index);
  1778. const word = language.case_insensitive ? match[0].toLowerCase() : match[0];
  1779. const data = keywordData(top, word);
  1780. if (data) {
  1781. const [kind, keywordRelevance] = data;
  1782. emitter.addText(buf);
  1783. buf = "";
  1784.  
  1785. keywordHits[word] = (keywordHits[word] || 0) + 1;
  1786. if (keywordHits[word] <= MAX_KEYWORD_HITS) relevance += keywordRelevance;
  1787. if (kind.startsWith("_")) {
  1788. // _ implied for relevance only, do not highlight
  1789. // by applying a class name
  1790. buf += match[0];
  1791. } else {
  1792. const cssClass = language.classNameAliases[kind] || kind;
  1793. emitter.addKeyword(match[0], cssClass);
  1794. }
  1795. } else {
  1796. buf += match[0];
  1797. }
  1798. lastIndex = top.keywordPatternRe.lastIndex;
  1799. match = top.keywordPatternRe.exec(modeBuffer);
  1800. }
  1801. buf += modeBuffer.substring(lastIndex);
  1802. emitter.addText(buf);
  1803. }
  1804.  
  1805. function processSubLanguage() {
  1806. if (modeBuffer === "") return;
  1807. /** @type HighlightResult */
  1808. let result = null;
  1809.  
  1810. if (typeof top.subLanguage === 'string') {
  1811. if (!languages[top.subLanguage]) {
  1812. emitter.addText(modeBuffer);
  1813. return;
  1814. }
  1815. result = _highlight(top.subLanguage, modeBuffer, true, continuations[top.subLanguage]);
  1816. continuations[top.subLanguage] = /** @type {CompiledMode} */ (result._top);
  1817. } else {
  1818. result = highlightAuto(modeBuffer, top.subLanguage.length ? top.subLanguage : null);
  1819. }
  1820.  
  1821. // Counting embedded language score towards the host language may be disabled
  1822. // with zeroing the containing mode relevance. Use case in point is Markdown that
  1823. // allows XML everywhere and makes every XML snippet to have a much larger Markdown
  1824. // score.
  1825. if (top.relevance > 0) {
  1826. relevance += result.relevance;
  1827. }
  1828. emitter.addSublanguage(result._emitter, result.language);
  1829. }
  1830.  
  1831. function processBuffer() {
  1832. if (top.subLanguage != null) {
  1833. processSubLanguage();
  1834. } else {
  1835. processKeywords();
  1836. }
  1837. modeBuffer = '';
  1838. }
  1839.  
  1840. /**
  1841. * @param {CompiledScope} scope
  1842. * @param {RegExpMatchArray} match
  1843. */
  1844. function emitMultiClass(scope, match) {
  1845. let i = 1;
  1846. const max = match.length - 1;
  1847. while (i <= max) {
  1848. if (!scope._emit[i]) { i++; continue; }
  1849. const klass = language.classNameAliases[scope[i]] || scope[i];
  1850. const text = match[i];
  1851. if (klass) {
  1852. emitter.addKeyword(text, klass);
  1853. } else {
  1854. modeBuffer = text;
  1855. processKeywords();
  1856. modeBuffer = "";
  1857. }
  1858. i++;
  1859. }
  1860. }
  1861.  
  1862. /**
  1863. * @param {CompiledMode} mode - new mode to start
  1864. * @param {RegExpMatchArray} match
  1865. */
  1866. function startNewMode(mode, match) {
  1867. if (mode.scope && typeof mode.scope === "string") {
  1868. emitter.openNode(language.classNameAliases[mode.scope] || mode.scope);
  1869. }
  1870. if (mode.beginScope) {
  1871. // beginScope just wraps the begin match itself in a scope
  1872. if (mode.beginScope._wrap) {
  1873. emitter.addKeyword(modeBuffer, language.classNameAliases[mode.beginScope._wrap] || mode.beginScope._wrap);
  1874. modeBuffer = "";
  1875. } else if (mode.beginScope._multi) {
  1876. // at this point modeBuffer should just be the match
  1877. emitMultiClass(mode.beginScope, match);
  1878. modeBuffer = "";
  1879. }
  1880. }
  1881.  
  1882. top = Object.create(mode, { parent: { value: top } });
  1883. return top;
  1884. }
  1885.  
  1886. /**
  1887. * @param {CompiledMode } mode - the mode to potentially end
  1888. * @param {RegExpMatchArray} match - the latest match
  1889. * @param {string} matchPlusRemainder - match plus remainder of content
  1890. * @returns {CompiledMode | void} - the next mode, or if void continue on in current mode
  1891. */
  1892. function endOfMode(mode, match, matchPlusRemainder) {
  1893. let matched = startsWith(mode.endRe, matchPlusRemainder);
  1894.  
  1895. if (matched) {
  1896. if (mode["on:end"]) {
  1897. const resp = new Response(mode);
  1898. mode["on:end"](match, resp);
  1899. if (resp.isMatchIgnored) matched = false;
  1900. }
  1901.  
  1902. if (matched) {
  1903. while (mode.endsParent && mode.parent) {
  1904. mode = mode.parent;
  1905. }
  1906. return mode;
  1907. }
  1908. }
  1909. // even if on:end fires an `ignore` it's still possible
  1910. // that we might trigger the end node because of a parent mode
  1911. if (mode.endsWithParent) {
  1912. return endOfMode(mode.parent, match, matchPlusRemainder);
  1913. }
  1914. }
  1915.  
  1916. /**
  1917. * Handle matching but then ignoring a sequence of text
  1918. *
  1919. * @param {string} lexeme - string containing full match text
  1920. */
  1921. function doIgnore(lexeme) {
  1922. if (top.matcher.regexIndex === 0) {
  1923. // no more regexes to potentially match here, so we move the cursor forward one
  1924. // space
  1925. modeBuffer += lexeme[0];
  1926. return 1;
  1927. } else {
  1928. // no need to move the cursor, we still have additional regexes to try and
  1929. // match at this very spot
  1930. resumeScanAtSamePosition = true;
  1931. return 0;
  1932. }
  1933. }
  1934.  
  1935. /**
  1936. * Handle the start of a new potential mode match
  1937. *
  1938. * @param {EnhancedMatch} match - the current match
  1939. * @returns {number} how far to advance the parse cursor
  1940. */
  1941. function doBeginMatch(match) {
  1942. const lexeme = match[0];
  1943. const newMode = match.rule;
  1944.  
  1945. const resp = new Response(newMode);
  1946. // first internal before callbacks, then the public ones
  1947. const beforeCallbacks = [newMode.__beforeBegin, newMode["on:begin"]];
  1948. for (const cb of beforeCallbacks) {
  1949. if (!cb) continue;
  1950. cb(match, resp);
  1951. if (resp.isMatchIgnored) return doIgnore(lexeme);
  1952. }
  1953.  
  1954. if (newMode.skip) {
  1955. modeBuffer += lexeme;
  1956. } else {
  1957. if (newMode.excludeBegin) {
  1958. modeBuffer += lexeme;
  1959. }
  1960. processBuffer();
  1961. if (!newMode.returnBegin && !newMode.excludeBegin) {
  1962. modeBuffer = lexeme;
  1963. }
  1964. }
  1965. startNewMode(newMode, match);
  1966. return newMode.returnBegin ? 0 : lexeme.length;
  1967. }
  1968.  
  1969. /**
  1970. * Handle the potential end of mode
  1971. *
  1972. * @param {RegExpMatchArray} match - the current match
  1973. */
  1974. function doEndMatch(match) {
  1975. const lexeme = match[0];
  1976. const matchPlusRemainder = codeToHighlight.substring(match.index);
  1977.  
  1978. const endMode = endOfMode(top, match, matchPlusRemainder);
  1979. if (!endMode) { return NO_MATCH; }
  1980.  
  1981. const origin = top;
  1982. if (top.endScope && top.endScope._wrap) {
  1983. processBuffer();
  1984. emitter.addKeyword(lexeme, top.endScope._wrap);
  1985. } else if (top.endScope && top.endScope._multi) {
  1986. processBuffer();
  1987. emitMultiClass(top.endScope, match);
  1988. } else if (origin.skip) {
  1989. modeBuffer += lexeme;
  1990. } else {
  1991. if (!(origin.returnEnd || origin.excludeEnd)) {
  1992. modeBuffer += lexeme;
  1993. }
  1994. processBuffer();
  1995. if (origin.excludeEnd) {
  1996. modeBuffer = lexeme;
  1997. }
  1998. }
  1999. do {
  2000. if (top.scope) {
  2001. emitter.closeNode();
  2002. }
  2003. if (!top.skip && !top.subLanguage) {
  2004. relevance += top.relevance;
  2005. }
  2006. top = top.parent;
  2007. } while (top !== endMode.parent);
  2008. if (endMode.starts) {
  2009. startNewMode(endMode.starts, match);
  2010. }
  2011. return origin.returnEnd ? 0 : lexeme.length;
  2012. }
  2013.  
  2014. function processContinuations() {
  2015. const list = [];
  2016. for (let current = top; current !== language; current = current.parent) {
  2017. if (current.scope) {
  2018. list.unshift(current.scope);
  2019. }
  2020. }
  2021. list.forEach(item => emitter.openNode(item));
  2022. }
  2023.  
  2024. /** @type {{type?: MatchType, index?: number, rule?: Mode}}} */
  2025. let lastMatch = {};
  2026.  
  2027. /**
  2028. * Process an individual match
  2029. *
  2030. * @param {string} textBeforeMatch - text preceding the match (since the last match)
  2031. * @param {EnhancedMatch} [match] - the match itself
  2032. */
  2033. function processLexeme(textBeforeMatch, match) {
  2034. const lexeme = match && match[0];
  2035.  
  2036. // add non-matched text to the current mode buffer
  2037. modeBuffer += textBeforeMatch;
  2038.  
  2039. if (lexeme == null) {
  2040. processBuffer();
  2041. return 0;
  2042. }
  2043.  
  2044. // we've found a 0 width match and we're stuck, so we need to advance
  2045. // this happens when we have badly behaved rules that have optional matchers to the degree that
  2046. // sometimes they can end up matching nothing at all
  2047. // Ref: https://github.com/highlightjs/highlight.js/issues/2140
  2048. if (lastMatch.type === "begin" && match.type === "end" && lastMatch.index === match.index && lexeme === "") {
  2049. // spit the "skipped" character that our regex choked on back into the output sequence
  2050. modeBuffer += codeToHighlight.slice(match.index, match.index + 1);
  2051. if (!SAFE_MODE) {
  2052. /** @type {AnnotatedError} */
  2053. const err = new Error(`0 width match regex (${languageName})`);
  2054. err.languageName = languageName;
  2055. err.badRule = lastMatch.rule;
  2056. throw err;
  2057. }
  2058. return 1;
  2059. }
  2060. lastMatch = match;
  2061.  
  2062. if (match.type === "begin") {
  2063. return doBeginMatch(match);
  2064. } else if (match.type === "illegal" && !ignoreIllegals) {
  2065. // illegal match, we do not continue processing
  2066. /** @type {AnnotatedError} */
  2067. const err = new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.scope || '<unnamed>') + '"');
  2068. err.mode = top;
  2069. throw err;
  2070. } else if (match.type === "end") {
  2071. const processed = doEndMatch(match);
  2072. if (processed !== NO_MATCH) {
  2073. return processed;
  2074. }
  2075. }
  2076.  
  2077. // edge case for when illegal matches $ (end of line) which is technically
  2078. // a 0 width match but not a begin/end match so it's not caught by the
  2079. // first handler (when ignoreIllegals is true)
  2080. if (match.type === "illegal" && lexeme === "") {
  2081. // advance so we aren't stuck in an infinite loop
  2082. return 1;
  2083. }
  2084.  
  2085. // infinite loops are BAD, this is a last ditch catch all. if we have a
  2086. // decent number of iterations yet our index (cursor position in our
  2087. // parsing) still 3x behind our index then something is very wrong
  2088. // so we bail
  2089. if (iterations > 100000 && iterations > match.index * 3) {
  2090. const err = new Error('potential infinite loop, way more iterations than matches');
  2091. throw err;
  2092. }
  2093.  
  2094. /*
  2095. Why might be find ourselves here? An potential end match that was
  2096. triggered but could not be completed. IE, `doEndMatch` returned NO_MATCH.
  2097. (this could be because a callback requests the match be ignored, etc)
  2098.  
  2099. This causes no real harm other than stopping a few times too many.
  2100. */
  2101.  
  2102. modeBuffer += lexeme;
  2103. return lexeme.length;
  2104. }
  2105.  
  2106. const language = getLanguage(languageName);
  2107. if (!language) {
  2108. error(LANGUAGE_NOT_FOUND.replace("{}", languageName));
  2109. throw new Error('Unknown language: "' + languageName + '"');
  2110. }
  2111.  
  2112. const md = compileLanguage(language);
  2113. let result = '';
  2114. /** @type {CompiledMode} */
  2115. let top = continuation || md;
  2116. /** @type Record<string,CompiledMode> */
  2117. const continuations = {}; // keep continuations for sub-languages
  2118. const emitter = new options.__emitter(options);
  2119. processContinuations();
  2120. let modeBuffer = '';
  2121. let relevance = 0;
  2122. let index = 0;
  2123. let iterations = 0;
  2124. let resumeScanAtSamePosition = false;
  2125.  
  2126. try {
  2127. top.matcher.considerAll();
  2128.  
  2129. for (;;) {
  2130. iterations++;
  2131. if (resumeScanAtSamePosition) {
  2132. // only regexes not matched previously will now be
  2133. // considered for a potential match
  2134. resumeScanAtSamePosition = false;
  2135. } else {
  2136. top.matcher.considerAll();
  2137. }
  2138. top.matcher.lastIndex = index;
  2139.  
  2140. const match = top.matcher.exec(codeToHighlight);
  2141. // console.log("match", match[0], match.rule && match.rule.begin)
  2142.  
  2143. if (!match) break;
  2144.  
  2145. const beforeMatch = codeToHighlight.substring(index, match.index);
  2146. const processedCount = processLexeme(beforeMatch, match);
  2147. index = match.index + processedCount;
  2148. }
  2149. processLexeme(codeToHighlight.substring(index));
  2150. emitter.closeAllNodes();
  2151. emitter.finalize();
  2152. result = emitter.toHTML();
  2153.  
  2154. return {
  2155. language: languageName,
  2156. value: result,
  2157. relevance: relevance,
  2158. illegal: false,
  2159. _emitter: emitter,
  2160. _top: top
  2161. };
  2162. } catch (err) {
  2163. if (err.message && err.message.includes('Illegal')) {
  2164. return {
  2165. language: languageName,
  2166. value: escape(codeToHighlight),
  2167. illegal: true,
  2168. relevance: 0,
  2169. _illegalBy: {
  2170. message: err.message,
  2171. index: index,
  2172. context: codeToHighlight.slice(index - 100, index + 100),
  2173. mode: err.mode,
  2174. resultSoFar: result
  2175. },
  2176. _emitter: emitter
  2177. };
  2178. } else if (SAFE_MODE) {
  2179. return {
  2180. language: languageName,
  2181. value: escape(codeToHighlight),
  2182. illegal: false,
  2183. relevance: 0,
  2184. errorRaised: err,
  2185. _emitter: emitter,
  2186. _top: top
  2187. };
  2188. } else {
  2189. throw err;
  2190. }
  2191. }
  2192. }
  2193.  
  2194. /**
  2195. * returns a valid highlight result, without actually doing any actual work,
  2196. * auto highlight starts with this and it's possible for small snippets that
  2197. * auto-detection may not find a better match
  2198. * @param {string} code
  2199. * @returns {HighlightResult}
  2200. */
  2201. function justTextHighlightResult(code) {
  2202. const result = {
  2203. value: escape(code),
  2204. illegal: false,
  2205. relevance: 0,
  2206. _top: PLAINTEXT_LANGUAGE,
  2207. _emitter: new options.__emitter(options)
  2208. };
  2209. result._emitter.addText(code);
  2210. return result;
  2211. }
  2212.  
  2213. /**
  2214. Highlighting with language detection. Accepts a string with the code to
  2215. highlight. Returns an object with the following properties:
  2216.  
  2217. - language (detected language)
  2218. - relevance (int)
  2219. - value (an HTML string with highlighting markup)
  2220. - secondBest (object with the same structure for second-best heuristically
  2221. detected language, may be absent)
  2222.  
  2223. @param {string} code
  2224. @param {Array<string>} [languageSubset]
  2225. @returns {AutoHighlightResult}
  2226. */
  2227. function highlightAuto(code, languageSubset) {
  2228. languageSubset = languageSubset || options.languages || Object.keys(languages);
  2229. const plaintext = justTextHighlightResult(code);
  2230.  
  2231. const results = languageSubset.filter(getLanguage).filter(autoDetection).map(name =>
  2232. _highlight(name, code, false)
  2233. );
  2234. results.unshift(plaintext); // plaintext is always an option
  2235.  
  2236. const sorted = results.sort((a, b) => {
  2237. // sort base on relevance
  2238. if (a.relevance !== b.relevance) return b.relevance - a.relevance;
  2239.  
  2240. // always award the tie to the base language
  2241. // ie if C++ and Arduino are tied, it's more likely to be C++
  2242. if (a.language && b.language) {
  2243. if (getLanguage(a.language).supersetOf === b.language) {
  2244. return 1;
  2245. } else if (getLanguage(b.language).supersetOf === a.language) {
  2246. return -1;
  2247. }
  2248. }
  2249.  
  2250. // otherwise say they are equal, which has the effect of sorting on
  2251. // relevance while preserving the original ordering - which is how ties
  2252. // have historically been settled, ie the language that comes first always
  2253. // wins in the case of a tie
  2254. return 0;
  2255. });
  2256.  
  2257. const [best, secondBest] = sorted;
  2258.  
  2259. /** @type {AutoHighlightResult} */
  2260. const result = best;
  2261. result.secondBest = secondBest;
  2262.  
  2263. return result;
  2264. }
  2265.  
  2266. /**
  2267. * Builds new class name for block given the language name
  2268. *
  2269. * @param {HTMLElement} element
  2270. * @param {string} [currentLang]
  2271. * @param {string} [resultLang]
  2272. */
  2273. function updateClassName(element, currentLang, resultLang) {
  2274. const language = (currentLang && aliases[currentLang]) || resultLang;
  2275.  
  2276. element.classList.add("hljs");
  2277. element.classList.add(`language-${language}`);
  2278. }
  2279.  
  2280. /**
  2281. * Applies highlighting to a DOM node containing code.
  2282. *
  2283. * @param {HighlightedHTMLElement} element - the HTML element to highlight
  2284. */
  2285. function highlightElement(element) {
  2286. /** @type HTMLElement */
  2287. let node = null;
  2288. const language = blockLanguage(element);
  2289.  
  2290. if (shouldNotHighlight(language)) return;
  2291.  
  2292. fire("before:highlightElement",
  2293. { el: element, language: language });
  2294.  
  2295. // we should be all text, no child nodes (unescaped HTML) - this is possibly
  2296. // an HTML injection attack - it's likely too late if this is already in
  2297. // production (the code has likely already done its damage by the time
  2298. // we're seeing it)... but we yell loudly about this so that hopefully it's
  2299. // more likely to be caught in development before making it to production
  2300. if (element.children.length > 0) {
  2301. if (!options.ignoreUnescapedHTML) {
  2302. console.warn("One of your code blocks includes unescaped HTML. This is a potentially serious security risk.");
  2303. console.warn("https://github.com/highlightjs/highlight.js/wiki/security");
  2304. console.warn("The element with unescaped HTML:");
  2305. console.warn(element);
  2306. }
  2307. if (options.throwUnescapedHTML) {
  2308. const err = new HTMLInjectionError(
  2309. "One of your code blocks includes unescaped HTML.",
  2310. element.innerHTML
  2311. );
  2312. throw err;
  2313. }
  2314. }
  2315.  
  2316. node = element;
  2317. const text = node.textContent;
  2318. const result = language ? highlight(text, { language, ignoreIllegals: true }) : highlightAuto(text);
  2319.  
  2320. element.innerHTML = result.value;
  2321. updateClassName(element, language, result.language);
  2322. element.result = {
  2323. language: result.language,
  2324. // TODO: remove with version 11.0
  2325. re: result.relevance,
  2326. relevance: result.relevance
  2327. };
  2328. if (result.secondBest) {
  2329. element.secondBest = {
  2330. language: result.secondBest.language,
  2331. relevance: result.secondBest.relevance
  2332. };
  2333. }
  2334.  
  2335. fire("after:highlightElement", { el: element, result, text });
  2336. }
  2337.  
  2338. /**
  2339. * Updates highlight.js global options with the passed options
  2340. *
  2341. * @param {Partial<HLJSOptions>} userOptions
  2342. */
  2343. function configure(userOptions) {
  2344. options = inherit(options, userOptions);
  2345. }
  2346.  
  2347. // TODO: remove v12, deprecated
  2348. const initHighlighting = () => {
  2349. highlightAll();
  2350. deprecated("10.6.0", "initHighlighting() deprecated. Use highlightAll() now.");
  2351. };
  2352.  
  2353. // TODO: remove v12, deprecated
  2354. function initHighlightingOnLoad() {
  2355. highlightAll();
  2356. deprecated("10.6.0", "initHighlightingOnLoad() deprecated. Use highlightAll() now.");
  2357. }
  2358.  
  2359. let wantsHighlight = false;
  2360.  
  2361. /**
  2362. * auto-highlights all pre>code elements on the page
  2363. */
  2364. function highlightAll() {
  2365. // if we are called too early in the loading process
  2366. if (document.readyState === "loading") {
  2367. wantsHighlight = true;
  2368. return;
  2369. }
  2370.  
  2371. const blocks = document.querySelectorAll(options.cssSelector);
  2372. blocks.forEach(highlightElement);
  2373. }
  2374.  
  2375. function boot() {
  2376. // if a highlight was requested before DOM was loaded, do now
  2377. if (wantsHighlight) highlightAll();
  2378. }
  2379.  
  2380. // make sure we are in the browser environment
  2381. if (typeof window !== 'undefined' && window.addEventListener) {
  2382. window.addEventListener('DOMContentLoaded', boot, false);
  2383. }
  2384.  
  2385. /**
  2386. * Register a language grammar module
  2387. *
  2388. * @param {string} languageName
  2389. * @param {LanguageFn} languageDefinition
  2390. */
  2391. function registerLanguage(languageName, languageDefinition) {
  2392. let lang = null;
  2393. try {
  2394. lang = languageDefinition(hljs);
  2395. } catch (error$1) {
  2396. error("Language definition for '{}' could not be registered.".replace("{}", languageName));
  2397. // hard or soft error
  2398. if (!SAFE_MODE) { throw error$1; } else { error(error$1); }
  2399. // languages that have serious errors are replaced with essentially a
  2400. // "plaintext" stand-in so that the code blocks will still get normal
  2401. // css classes applied to them - and one bad language won't break the
  2402. // entire highlighter
  2403. lang = PLAINTEXT_LANGUAGE;
  2404. }
  2405. // give it a temporary name if it doesn't have one in the meta-data
  2406. if (!lang.name) lang.name = languageName;
  2407. languages[languageName] = lang;
  2408. lang.rawDefinition = languageDefinition.bind(null, hljs);
  2409.  
  2410. if (lang.aliases) {
  2411. registerAliases(lang.aliases, { languageName });
  2412. }
  2413. }
  2414.  
  2415. /**
  2416. * Remove a language grammar module
  2417. *
  2418. * @param {string} languageName
  2419. */
  2420. function unregisterLanguage(languageName) {
  2421. delete languages[languageName];
  2422. for (const alias of Object.keys(aliases)) {
  2423. if (aliases[alias] === languageName) {
  2424. delete aliases[alias];
  2425. }
  2426. }
  2427. }
  2428.  
  2429. /**
  2430. * @returns {string[]} List of language internal names
  2431. */
  2432. function listLanguages() {
  2433. return Object.keys(languages);
  2434. }
  2435.  
  2436. /**
  2437. * @param {string} name - name of the language to retrieve
  2438. * @returns {Language | undefined}
  2439. */
  2440. function getLanguage(name) {
  2441. name = (name || '').toLowerCase();
  2442. return languages[name] || languages[aliases[name]];
  2443. }
  2444.  
  2445. /**
  2446. *
  2447. * @param {string|string[]} aliasList - single alias or list of aliases
  2448. * @param {{languageName: string}} opts
  2449. */
  2450. function registerAliases(aliasList, { languageName }) {
  2451. if (typeof aliasList === 'string') {
  2452. aliasList = [aliasList];
  2453. }
  2454. aliasList.forEach(alias => { aliases[alias.toLowerCase()] = languageName; });
  2455. }
  2456.  
  2457. /**
  2458. * Determines if a given language has auto-detection enabled
  2459. * @param {string} name - name of the language
  2460. */
  2461. function autoDetection(name) {
  2462. const lang = getLanguage(name);
  2463. return lang && !lang.disableAutodetect;
  2464. }
  2465.  
  2466. /**
  2467. * Upgrades the old highlightBlock plugins to the new
  2468. * highlightElement API
  2469. * @param {HLJSPlugin} plugin
  2470. */
  2471. function upgradePluginAPI(plugin) {
  2472. // TODO: remove with v12
  2473. if (plugin["before:highlightBlock"] && !plugin["before:highlightElement"]) {
  2474. plugin["before:highlightElement"] = (data) => {
  2475. plugin["before:highlightBlock"](
  2476. Object.assign({ block: data.el }, data)
  2477. );
  2478. };
  2479. }
  2480. if (plugin["after:highlightBlock"] && !plugin["after:highlightElement"]) {
  2481. plugin["after:highlightElement"] = (data) => {
  2482. plugin["after:highlightBlock"](
  2483. Object.assign({ block: data.el }, data)
  2484. );
  2485. };
  2486. }
  2487. }
  2488.  
  2489. /**
  2490. * @param {HLJSPlugin} plugin
  2491. */
  2492. function addPlugin(plugin) {
  2493. upgradePluginAPI(plugin);
  2494. plugins.push(plugin);
  2495. }
  2496.  
  2497. /**
  2498. *
  2499. * @param {PluginEvent} event
  2500. * @param {any} args
  2501. */
  2502. function fire(event, args) {
  2503. const cb = event;
  2504. plugins.forEach(function(plugin) {
  2505. if (plugin[cb]) {
  2506. plugin[cb](args);
  2507. }
  2508. });
  2509. }
  2510.  
  2511. /**
  2512. * DEPRECATED
  2513. * @param {HighlightedHTMLElement} el
  2514. */
  2515. function deprecateHighlightBlock(el) {
  2516. deprecated("10.7.0", "highlightBlock will be removed entirely in v12.0");
  2517. deprecated("10.7.0", "Please use highlightElement now.");
  2518.  
  2519. return highlightElement(el);
  2520. }
  2521.  
  2522. /* Interface definition */
  2523. Object.assign(hljs, {
  2524. highlight,
  2525. highlightAuto,
  2526. highlightAll,
  2527. highlightElement,
  2528. // TODO: Remove with v12 API
  2529. highlightBlock: deprecateHighlightBlock,
  2530. configure,
  2531. initHighlighting,
  2532. initHighlightingOnLoad,
  2533. registerLanguage,
  2534. unregisterLanguage,
  2535. listLanguages,
  2536. getLanguage,
  2537. registerAliases,
  2538. autoDetection,
  2539. inherit,
  2540. addPlugin
  2541. });
  2542.  
  2543. hljs.debugMode = function() { SAFE_MODE = false; };
  2544. hljs.safeMode = function() { SAFE_MODE = true; };
  2545. hljs.versionString = version;
  2546.  
  2547. hljs.regex = {
  2548. concat: concat,
  2549. lookahead: lookahead,
  2550. either: either,
  2551. optional: optional,
  2552. anyNumberOfTimes: anyNumberOfTimes
  2553. };
  2554.  
  2555. for (const key in MODES) {
  2556. // @ts-ignore
  2557. if (typeof MODES[key] === "object") {
  2558. // @ts-ignore
  2559. deepFreezeEs6.exports(MODES[key]);
  2560. }
  2561. }
  2562.  
  2563. // merge all the modes/regexes into our main object
  2564. Object.assign(hljs, MODES);
  2565.  
  2566. return hljs;
  2567. };
  2568.  
  2569. // export an "instance" of the highlighter
  2570. var highlight = HLJS({});
  2571.  
  2572. return highlight;
  2573.  
  2574. })();
  2575. if (typeof exports === 'object' && typeof module !== 'undefined') { module.exports = hljs; }