core.js 74 KB

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