core.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  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, sublanguage?: boolean, children: Node[]} | string} Node */
  175. /** @typedef {{scope?: string, language?: string, sublanguage?: boolean, 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. // this outer rule makes sure we actually have a WHOLE regex and not simply
  594. // an expression such as:
  595. //
  596. // 3 / something
  597. //
  598. // (which will then blow up when regex's `illegal` sees the newline)
  599. begin: /(?=\/[^/\n]*\/)/,
  600. contains: [{
  601. scope: 'regexp',
  602. begin: /\//,
  603. end: /\/[gimuy]*/,
  604. illegal: /\n/,
  605. contains: [
  606. BACKSLASH_ESCAPE,
  607. {
  608. begin: /\[/,
  609. end: /\]/,
  610. relevance: 0,
  611. contains: [BACKSLASH_ESCAPE]
  612. }
  613. ]
  614. }]
  615. };
  616. const TITLE_MODE = {
  617. scope: 'title',
  618. begin: IDENT_RE,
  619. relevance: 0
  620. };
  621. const UNDERSCORE_TITLE_MODE = {
  622. scope: 'title',
  623. begin: UNDERSCORE_IDENT_RE,
  624. relevance: 0
  625. };
  626. const METHOD_GUARD = {
  627. // excludes method names from keyword processing
  628. begin: '\\.\\s*' + UNDERSCORE_IDENT_RE,
  629. relevance: 0
  630. };
  631. /**
  632. * Adds end same as begin mechanics to a mode
  633. *
  634. * Your mode must include at least a single () match group as that first match
  635. * group is what is used for comparison
  636. * @param {Partial<Mode>} mode
  637. */
  638. const END_SAME_AS_BEGIN = function(mode) {
  639. return Object.assign(mode,
  640. {
  641. /** @type {ModeCallback} */
  642. 'on:begin': (m, resp) => { resp.data._beginMatch = m[1]; },
  643. /** @type {ModeCallback} */
  644. 'on:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch(); }
  645. });
  646. };
  647. var MODES = /*#__PURE__*/Object.freeze({
  648. __proto__: null,
  649. MATCH_NOTHING_RE: MATCH_NOTHING_RE,
  650. IDENT_RE: IDENT_RE,
  651. UNDERSCORE_IDENT_RE: UNDERSCORE_IDENT_RE,
  652. NUMBER_RE: NUMBER_RE,
  653. C_NUMBER_RE: C_NUMBER_RE,
  654. BINARY_NUMBER_RE: BINARY_NUMBER_RE,
  655. RE_STARTERS_RE: RE_STARTERS_RE,
  656. SHEBANG: SHEBANG,
  657. BACKSLASH_ESCAPE: BACKSLASH_ESCAPE,
  658. APOS_STRING_MODE: APOS_STRING_MODE,
  659. QUOTE_STRING_MODE: QUOTE_STRING_MODE,
  660. PHRASAL_WORDS_MODE: PHRASAL_WORDS_MODE,
  661. COMMENT: COMMENT,
  662. C_LINE_COMMENT_MODE: C_LINE_COMMENT_MODE,
  663. C_BLOCK_COMMENT_MODE: C_BLOCK_COMMENT_MODE,
  664. HASH_COMMENT_MODE: HASH_COMMENT_MODE,
  665. NUMBER_MODE: NUMBER_MODE,
  666. C_NUMBER_MODE: C_NUMBER_MODE,
  667. BINARY_NUMBER_MODE: BINARY_NUMBER_MODE,
  668. REGEXP_MODE: REGEXP_MODE,
  669. TITLE_MODE: TITLE_MODE,
  670. UNDERSCORE_TITLE_MODE: UNDERSCORE_TITLE_MODE,
  671. METHOD_GUARD: METHOD_GUARD,
  672. END_SAME_AS_BEGIN: END_SAME_AS_BEGIN
  673. });
  674. /**
  675. @typedef {import('highlight.js').CallbackResponse} CallbackResponse
  676. @typedef {import('highlight.js').CompilerExt} CompilerExt
  677. */
  678. // Grammar extensions / plugins
  679. // See: https://github.com/highlightjs/highlight.js/issues/2833
  680. // Grammar extensions allow "syntactic sugar" to be added to the grammar modes
  681. // without requiring any underlying changes to the compiler internals.
  682. // `compileMatch` being the perfect small example of now allowing a grammar
  683. // author to write `match` when they desire to match a single expression rather
  684. // than being forced to use `begin`. The extension then just moves `match` into
  685. // `begin` when it runs. Ie, no features have been added, but we've just made
  686. // the experience of writing (and reading grammars) a little bit nicer.
  687. // ------
  688. // TODO: We need negative look-behind support to do this properly
  689. /**
  690. * Skip a match if it has a preceding dot
  691. *
  692. * This is used for `beginKeywords` to prevent matching expressions such as
  693. * `bob.keyword.do()`. The mode compiler automatically wires this up as a
  694. * special _internal_ 'on:begin' callback for modes with `beginKeywords`
  695. * @param {RegExpMatchArray} match
  696. * @param {CallbackResponse} response
  697. */
  698. function skipIfHasPrecedingDot(match, response) {
  699. const before = match.input[match.index - 1];
  700. if (before === ".") {
  701. response.ignoreMatch();
  702. }
  703. }
  704. /**
  705. *
  706. * @type {CompilerExt}
  707. */
  708. function scopeClassName(mode, _parent) {
  709. // eslint-disable-next-line no-undefined
  710. if (mode.className !== undefined) {
  711. mode.scope = mode.className;
  712. delete mode.className;
  713. }
  714. }
  715. /**
  716. * `beginKeywords` syntactic sugar
  717. * @type {CompilerExt}
  718. */
  719. function beginKeywords(mode, parent) {
  720. if (!parent) return;
  721. if (!mode.beginKeywords) return;
  722. // for languages with keywords that include non-word characters checking for
  723. // a word boundary is not sufficient, so instead we check for a word boundary
  724. // or whitespace - this does no harm in any case since our keyword engine
  725. // doesn't allow spaces in keywords anyways and we still check for the boundary
  726. // first
  727. mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')(?!\\.)(?=\\b|\\s)';
  728. mode.__beforeBegin = skipIfHasPrecedingDot;
  729. mode.keywords = mode.keywords || mode.beginKeywords;
  730. delete mode.beginKeywords;
  731. // prevents double relevance, the keywords themselves provide
  732. // relevance, the mode doesn't need to double it
  733. // eslint-disable-next-line no-undefined
  734. if (mode.relevance === undefined) mode.relevance = 0;
  735. }
  736. /**
  737. * Allow `illegal` to contain an array of illegal values
  738. * @type {CompilerExt}
  739. */
  740. function compileIllegal(mode, _parent) {
  741. if (!Array.isArray(mode.illegal)) return;
  742. mode.illegal = either(...mode.illegal);
  743. }
  744. /**
  745. * `match` to match a single expression for readability
  746. * @type {CompilerExt}
  747. */
  748. function compileMatch(mode, _parent) {
  749. if (!mode.match) return;
  750. if (mode.begin || mode.end) throw new Error("begin & end are not supported with match");
  751. mode.begin = mode.match;
  752. delete mode.match;
  753. }
  754. /**
  755. * provides the default 1 relevance to all modes
  756. * @type {CompilerExt}
  757. */
  758. function compileRelevance(mode, _parent) {
  759. // eslint-disable-next-line no-undefined
  760. if (mode.relevance === undefined) mode.relevance = 1;
  761. }
  762. // allow beforeMatch to act as a "qualifier" for the match
  763. // the full match begin must be [beforeMatch][begin]
  764. const beforeMatchExt = (mode, parent) => {
  765. if (!mode.beforeMatch) return;
  766. // starts conflicts with endsParent which we need to make sure the child
  767. // rule is not matched multiple times
  768. if (mode.starts) throw new Error("beforeMatch cannot be used with starts");
  769. const originalMode = Object.assign({}, mode);
  770. Object.keys(mode).forEach((key) => { delete mode[key]; });
  771. mode.keywords = originalMode.keywords;
  772. mode.begin = concat(originalMode.beforeMatch, lookahead(originalMode.begin));
  773. mode.starts = {
  774. relevance: 0,
  775. contains: [
  776. Object.assign(originalMode, { endsParent: true })
  777. ]
  778. };
  779. mode.relevance = 0;
  780. delete originalMode.beforeMatch;
  781. };
  782. // keywords that should have no default relevance value
  783. const COMMON_KEYWORDS = [
  784. 'of',
  785. 'and',
  786. 'for',
  787. 'in',
  788. 'not',
  789. 'or',
  790. 'if',
  791. 'then',
  792. 'parent', // common variable name
  793. 'list', // common variable name
  794. 'value' // common variable name
  795. ];
  796. const DEFAULT_KEYWORD_SCOPE = "keyword";
  797. /**
  798. * Given raw keywords from a language definition, compile them.
  799. *
  800. * @param {string | Record<string,string|string[]> | Array<string>} rawKeywords
  801. * @param {boolean} caseInsensitive
  802. */
  803. function compileKeywords(rawKeywords, caseInsensitive, scopeName = DEFAULT_KEYWORD_SCOPE) {
  804. /** @type {import("highlight.js/private").KeywordDict} */
  805. const compiledKeywords = Object.create(null);
  806. // input can be a string of keywords, an array of keywords, or a object with
  807. // named keys representing scopeName (which can then point to a string or array)
  808. if (typeof rawKeywords === 'string') {
  809. compileList(scopeName, rawKeywords.split(" "));
  810. } else if (Array.isArray(rawKeywords)) {
  811. compileList(scopeName, rawKeywords);
  812. } else {
  813. Object.keys(rawKeywords).forEach(function(scopeName) {
  814. // collapse all our objects back into the parent object
  815. Object.assign(
  816. compiledKeywords,
  817. compileKeywords(rawKeywords[scopeName], caseInsensitive, scopeName)
  818. );
  819. });
  820. }
  821. return compiledKeywords;
  822. // ---
  823. /**
  824. * Compiles an individual list of keywords
  825. *
  826. * Ex: "for if when while|5"
  827. *
  828. * @param {string} scopeName
  829. * @param {Array<string>} keywordList
  830. */
  831. function compileList(scopeName, keywordList) {
  832. if (caseInsensitive) {
  833. keywordList = keywordList.map(x => x.toLowerCase());
  834. }
  835. keywordList.forEach(function(keyword) {
  836. const pair = keyword.split('|');
  837. compiledKeywords[pair[0]] = [scopeName, scoreForKeyword(pair[0], pair[1])];
  838. });
  839. }
  840. }
  841. /**
  842. * Returns the proper score for a given keyword
  843. *
  844. * Also takes into account comment keywords, which will be scored 0 UNLESS
  845. * another score has been manually assigned.
  846. * @param {string} keyword
  847. * @param {string} [providedScore]
  848. */
  849. function scoreForKeyword(keyword, providedScore) {
  850. // manual scores always win over common keywords
  851. // so you can force a score of 1 if you really insist
  852. if (providedScore) {
  853. return Number(providedScore);
  854. }
  855. return commonKeyword(keyword) ? 0 : 1;
  856. }
  857. /**
  858. * Determines if a given keyword is common or not
  859. *
  860. * @param {string} keyword */
  861. function commonKeyword(keyword) {
  862. return COMMON_KEYWORDS.includes(keyword.toLowerCase());
  863. }
  864. /*
  865. For the reasoning behind this please see:
  866. https://github.com/highlightjs/highlight.js/issues/2880#issuecomment-747275419
  867. */
  868. /**
  869. * @type {Record<string, boolean>}
  870. */
  871. const seenDeprecations = {};
  872. /**
  873. * @param {string} message
  874. */
  875. const error = (message) => {
  876. console.error(message);
  877. };
  878. /**
  879. * @param {string} message
  880. * @param {any} args
  881. */
  882. const warn = (message, ...args) => {
  883. console.log(`WARN: ${message}`, ...args);
  884. };
  885. /**
  886. * @param {string} version
  887. * @param {string} message
  888. */
  889. const deprecated = (version, message) => {
  890. if (seenDeprecations[`${version}/${message}`]) return;
  891. console.log(`Deprecated as of ${version}. ${message}`);
  892. seenDeprecations[`${version}/${message}`] = true;
  893. };
  894. /* eslint-disable no-throw-literal */
  895. /**
  896. @typedef {import('highlight.js').CompiledMode} CompiledMode
  897. */
  898. const MultiClassError = new Error();
  899. /**
  900. * Renumbers labeled scope names to account for additional inner match
  901. * groups that otherwise would break everything.
  902. *
  903. * Lets say we 3 match scopes:
  904. *
  905. * { 1 => ..., 2 => ..., 3 => ... }
  906. *
  907. * So what we need is a clean match like this:
  908. *
  909. * (a)(b)(c) => [ "a", "b", "c" ]
  910. *
  911. * But this falls apart with inner match groups:
  912. *
  913. * (a)(((b)))(c) => ["a", "b", "b", "b", "c" ]
  914. *
  915. * Our scopes are now "out of alignment" and we're repeating `b` 3 times.
  916. * What needs to happen is the numbers are remapped:
  917. *
  918. * { 1 => ..., 2 => ..., 5 => ... }
  919. *
  920. * We also need to know that the ONLY groups that should be output
  921. * are 1, 2, and 5. This function handles this behavior.
  922. *
  923. * @param {CompiledMode} mode
  924. * @param {Array<RegExp | string>} regexes
  925. * @param {{key: "beginScope"|"endScope"}} opts
  926. */
  927. function remapScopeNames(mode, regexes, { key }) {
  928. let offset = 0;
  929. const scopeNames = mode[key];
  930. /** @type Record<number,boolean> */
  931. const emit = {};
  932. /** @type Record<number,string> */
  933. const positions = {};
  934. for (let i = 1; i <= regexes.length; i++) {
  935. positions[i + offset] = scopeNames[i];
  936. emit[i + offset] = true;
  937. offset += countMatchGroups(regexes[i - 1]);
  938. }
  939. // we use _emit to keep track of which match groups are "top-level" to avoid double
  940. // output from inside match groups
  941. mode[key] = positions;
  942. mode[key]._emit = emit;
  943. mode[key]._multi = true;
  944. }
  945. /**
  946. * @param {CompiledMode} mode
  947. */
  948. function beginMultiClass(mode) {
  949. if (!Array.isArray(mode.begin)) return;
  950. if (mode.skip || mode.excludeBegin || mode.returnBegin) {
  951. error("skip, excludeBegin, returnBegin not compatible with beginScope: {}");
  952. throw MultiClassError;
  953. }
  954. if (typeof mode.beginScope !== "object" || mode.beginScope === null) {
  955. error("beginScope must be object");
  956. throw MultiClassError;
  957. }
  958. remapScopeNames(mode, mode.begin, { key: "beginScope" });
  959. mode.begin = _rewriteBackreferences(mode.begin, { joinWith: "" });
  960. }
  961. /**
  962. * @param {CompiledMode} mode
  963. */
  964. function endMultiClass(mode) {
  965. if (!Array.isArray(mode.end)) return;
  966. if (mode.skip || mode.excludeEnd || mode.returnEnd) {
  967. error("skip, excludeEnd, returnEnd not compatible with endScope: {}");
  968. throw MultiClassError;
  969. }
  970. if (typeof mode.endScope !== "object" || mode.endScope === null) {
  971. error("endScope must be object");
  972. throw MultiClassError;
  973. }
  974. remapScopeNames(mode, mode.end, { key: "endScope" });
  975. mode.end = _rewriteBackreferences(mode.end, { joinWith: "" });
  976. }
  977. /**
  978. * this exists only to allow `scope: {}` to be used beside `match:`
  979. * Otherwise `beginScope` would necessary and that would look weird
  980. {
  981. match: [ /def/, /\w+/ ]
  982. scope: { 1: "keyword" , 2: "title" }
  983. }
  984. * @param {CompiledMode} mode
  985. */
  986. function scopeSugar(mode) {
  987. if (mode.scope && typeof mode.scope === "object" && mode.scope !== null) {
  988. mode.beginScope = mode.scope;
  989. delete mode.scope;
  990. }
  991. }
  992. /**
  993. * @param {CompiledMode} mode
  994. */
  995. function MultiClass(mode) {
  996. scopeSugar(mode);
  997. if (typeof mode.beginScope === "string") {
  998. mode.beginScope = { _wrap: mode.beginScope };
  999. }
  1000. if (typeof mode.endScope === "string") {
  1001. mode.endScope = { _wrap: mode.endScope };
  1002. }
  1003. beginMultiClass(mode);
  1004. endMultiClass(mode);
  1005. }
  1006. /**
  1007. @typedef {import('highlight.js').Mode} Mode
  1008. @typedef {import('highlight.js').CompiledMode} CompiledMode
  1009. @typedef {import('highlight.js').Language} Language
  1010. @typedef {import('highlight.js').HLJSPlugin} HLJSPlugin
  1011. @typedef {import('highlight.js').CompiledLanguage} CompiledLanguage
  1012. */
  1013. // compilation
  1014. /**
  1015. * Compiles a language definition result
  1016. *
  1017. * Given the raw result of a language definition (Language), compiles this so
  1018. * that it is ready for highlighting code.
  1019. * @param {Language} language
  1020. * @returns {CompiledLanguage}
  1021. */
  1022. function compileLanguage(language) {
  1023. /**
  1024. * Builds a regex with the case sensitivity of the current language
  1025. *
  1026. * @param {RegExp | string} value
  1027. * @param {boolean} [global]
  1028. */
  1029. function langRe(value, global) {
  1030. return new RegExp(
  1031. source(value),
  1032. 'm'
  1033. + (language.case_insensitive ? 'i' : '')
  1034. + (language.unicodeRegex ? 'u' : '')
  1035. + (global ? 'g' : '')
  1036. );
  1037. }
  1038. /**
  1039. Stores multiple regular expressions and allows you to quickly search for
  1040. them all in a string simultaneously - returning the first match. It does
  1041. this by creating a huge (a|b|c) regex - each individual item wrapped with ()
  1042. and joined by `|` - using match groups to track position. When a match is
  1043. found checking which position in the array has content allows us to figure
  1044. out which of the original regexes / match groups triggered the match.
  1045. The match object itself (the result of `Regex.exec`) is returned but also
  1046. enhanced by merging in any meta-data that was registered with the regex.
  1047. This is how we keep track of which mode matched, and what type of rule
  1048. (`illegal`, `begin`, end, etc).
  1049. */
  1050. class MultiRegex {
  1051. constructor() {
  1052. this.matchIndexes = {};
  1053. // @ts-ignore
  1054. this.regexes = [];
  1055. this.matchAt = 1;
  1056. this.position = 0;
  1057. }
  1058. // @ts-ignore
  1059. addRule(re, opts) {
  1060. opts.position = this.position++;
  1061. // @ts-ignore
  1062. this.matchIndexes[this.matchAt] = opts;
  1063. this.regexes.push([opts, re]);
  1064. this.matchAt += countMatchGroups(re) + 1;
  1065. }
  1066. compile() {
  1067. if (this.regexes.length === 0) {
  1068. // avoids the need to check length every time exec is called
  1069. // @ts-ignore
  1070. this.exec = () => null;
  1071. }
  1072. const terminators = this.regexes.map(el => el[1]);
  1073. this.matcherRe = langRe(_rewriteBackreferences(terminators, { joinWith: '|' }), true);
  1074. this.lastIndex = 0;
  1075. }
  1076. /** @param {string} s */
  1077. exec(s) {
  1078. this.matcherRe.lastIndex = this.lastIndex;
  1079. const match = this.matcherRe.exec(s);
  1080. if (!match) { return null; }
  1081. // eslint-disable-next-line no-undefined
  1082. const i = match.findIndex((el, i) => i > 0 && el !== undefined);
  1083. // @ts-ignore
  1084. const matchData = this.matchIndexes[i];
  1085. // trim off any earlier non-relevant match groups (ie, the other regex
  1086. // match groups that make up the multi-matcher)
  1087. match.splice(0, i);
  1088. return Object.assign(match, matchData);
  1089. }
  1090. }
  1091. /*
  1092. Created to solve the key deficiently with MultiRegex - there is no way to
  1093. test for multiple matches at a single location. Why would we need to do
  1094. that? In the future a more dynamic engine will allow certain matches to be
  1095. ignored. An example: if we matched say the 3rd regex in a large group but
  1096. decided to ignore it - we'd need to started testing again at the 4th
  1097. regex... but MultiRegex itself gives us no real way to do that.
  1098. So what this class creates MultiRegexs on the fly for whatever search
  1099. position they are needed.
  1100. NOTE: These additional MultiRegex objects are created dynamically. For most
  1101. grammars most of the time we will never actually need anything more than the
  1102. first MultiRegex - so this shouldn't have too much overhead.
  1103. Say this is our search group, and we match regex3, but wish to ignore it.
  1104. regex1 | regex2 | regex3 | regex4 | regex5 ' ie, startAt = 0
  1105. What we need is a new MultiRegex that only includes the remaining
  1106. possibilities:
  1107. regex4 | regex5 ' ie, startAt = 3
  1108. This class wraps all that complexity up in a simple API... `startAt` decides
  1109. where in the array of expressions to start doing the matching. It
  1110. auto-increments, so if a match is found at position 2, then startAt will be
  1111. set to 3. If the end is reached startAt will return to 0.
  1112. MOST of the time the parser will be setting startAt manually to 0.
  1113. */
  1114. class ResumableMultiRegex {
  1115. constructor() {
  1116. // @ts-ignore
  1117. this.rules = [];
  1118. // @ts-ignore
  1119. this.multiRegexes = [];
  1120. this.count = 0;
  1121. this.lastIndex = 0;
  1122. this.regexIndex = 0;
  1123. }
  1124. // @ts-ignore
  1125. getMatcher(index) {
  1126. if (this.multiRegexes[index]) return this.multiRegexes[index];
  1127. const matcher = new MultiRegex();
  1128. this.rules.slice(index).forEach(([re, opts]) => matcher.addRule(re, opts));
  1129. matcher.compile();
  1130. this.multiRegexes[index] = matcher;
  1131. return matcher;
  1132. }
  1133. resumingScanAtSamePosition() {
  1134. return this.regexIndex !== 0;
  1135. }
  1136. considerAll() {
  1137. this.regexIndex = 0;
  1138. }
  1139. // @ts-ignore
  1140. addRule(re, opts) {
  1141. this.rules.push([re, opts]);
  1142. if (opts.type === "begin") this.count++;
  1143. }
  1144. /** @param {string} s */
  1145. exec(s) {
  1146. const m = this.getMatcher(this.regexIndex);
  1147. m.lastIndex = this.lastIndex;
  1148. let result = m.exec(s);
  1149. // The following is because we have no easy way to say "resume scanning at the
  1150. // existing position but also skip the current rule ONLY". What happens is
  1151. // all prior rules are also skipped which can result in matching the wrong
  1152. // thing. Example of matching "booger":
  1153. // our matcher is [string, "booger", number]
  1154. //
  1155. // ....booger....
  1156. // if "booger" is ignored then we'd really need a regex to scan from the
  1157. // SAME position for only: [string, number] but ignoring "booger" (if it
  1158. // was the first match), a simple resume would scan ahead who knows how
  1159. // far looking only for "number", ignoring potential string matches (or
  1160. // future "booger" matches that might be valid.)
  1161. // So what we do: We execute two matchers, one resuming at the same
  1162. // position, but the second full matcher starting at the position after:
  1163. // /--- resume first regex match here (for [number])
  1164. // |/---- full match here for [string, "booger", number]
  1165. // vv
  1166. // ....booger....
  1167. // Which ever results in a match first is then used. So this 3-4 step
  1168. // process essentially allows us to say "match at this position, excluding
  1169. // a prior rule that was ignored".
  1170. //
  1171. // 1. Match "booger" first, ignore. Also proves that [string] does non match.
  1172. // 2. Resume matching for [number]
  1173. // 3. Match at index + 1 for [string, "booger", number]
  1174. // 4. If #2 and #3 result in matches, which came first?
  1175. if (this.resumingScanAtSamePosition()) {
  1176. if (result && result.index === this.lastIndex) ; else { // use the second matcher result
  1177. const m2 = this.getMatcher(0);
  1178. m2.lastIndex = this.lastIndex + 1;
  1179. result = m2.exec(s);
  1180. }
  1181. }
  1182. if (result) {
  1183. this.regexIndex += result.position + 1;
  1184. if (this.regexIndex === this.count) {
  1185. // wrap-around to considering all matches again
  1186. this.considerAll();
  1187. }
  1188. }
  1189. return result;
  1190. }
  1191. }
  1192. /**
  1193. * Given a mode, builds a huge ResumableMultiRegex that can be used to walk
  1194. * the content and find matches.
  1195. *
  1196. * @param {CompiledMode} mode
  1197. * @returns {ResumableMultiRegex}
  1198. */
  1199. function buildModeRegex(mode) {
  1200. const mm = new ResumableMultiRegex();
  1201. mode.contains.forEach(term => mm.addRule(term.begin, { rule: term, type: "begin" }));
  1202. if (mode.terminatorEnd) {
  1203. mm.addRule(mode.terminatorEnd, { type: "end" });
  1204. }
  1205. if (mode.illegal) {
  1206. mm.addRule(mode.illegal, { type: "illegal" });
  1207. }
  1208. return mm;
  1209. }
  1210. /** skip vs abort vs ignore
  1211. *
  1212. * @skip - The mode is still entered and exited normally (and contains rules apply),
  1213. * but all content is held and added to the parent buffer rather than being
  1214. * output when the mode ends. Mostly used with `sublanguage` to build up
  1215. * a single large buffer than can be parsed by sublanguage.
  1216. *
  1217. * - The mode begin ands ends normally.
  1218. * - Content matched is added to the parent mode buffer.
  1219. * - The parser cursor is moved forward normally.
  1220. *
  1221. * @abort - A hack placeholder until we have ignore. Aborts the mode (as if it
  1222. * never matched) but DOES NOT continue to match subsequent `contains`
  1223. * modes. Abort is bad/suboptimal because it can result in modes
  1224. * farther down not getting applied because an earlier rule eats the
  1225. * content but then aborts.
  1226. *
  1227. * - The mode does not begin.
  1228. * - Content matched by `begin` is added to the mode buffer.
  1229. * - The parser cursor is moved forward accordingly.
  1230. *
  1231. * @ignore - Ignores the mode (as if it never matched) and continues to match any
  1232. * subsequent `contains` modes. Ignore isn't technically possible with
  1233. * the current parser implementation.
  1234. *
  1235. * - The mode does not begin.
  1236. * - Content matched by `begin` is ignored.
  1237. * - The parser cursor is not moved forward.
  1238. */
  1239. /**
  1240. * Compiles an individual mode
  1241. *
  1242. * This can raise an error if the mode contains certain detectable known logic
  1243. * issues.
  1244. * @param {Mode} mode
  1245. * @param {CompiledMode | null} [parent]
  1246. * @returns {CompiledMode | never}
  1247. */
  1248. function compileMode(mode, parent) {
  1249. const cmode = /** @type CompiledMode */ (mode);
  1250. if (mode.isCompiled) return cmode;
  1251. [
  1252. scopeClassName,
  1253. // do this early so compiler extensions generally don't have to worry about
  1254. // the distinction between match/begin
  1255. compileMatch,
  1256. MultiClass,
  1257. beforeMatchExt
  1258. ].forEach(ext => ext(mode, parent));
  1259. language.compilerExtensions.forEach(ext => ext(mode, parent));
  1260. // __beforeBegin is considered private API, internal use only
  1261. mode.__beforeBegin = null;
  1262. [
  1263. beginKeywords,
  1264. // do this later so compiler extensions that come earlier have access to the
  1265. // raw array if they wanted to perhaps manipulate it, etc.
  1266. compileIllegal,
  1267. // default to 1 relevance if not specified
  1268. compileRelevance
  1269. ].forEach(ext => ext(mode, parent));
  1270. mode.isCompiled = true;
  1271. let keywordPattern = null;
  1272. if (typeof mode.keywords === "object" && mode.keywords.$pattern) {
  1273. // we need a copy because keywords might be compiled multiple times
  1274. // so we can't go deleting $pattern from the original on the first
  1275. // pass
  1276. mode.keywords = Object.assign({}, mode.keywords);
  1277. keywordPattern = mode.keywords.$pattern;
  1278. delete mode.keywords.$pattern;
  1279. }
  1280. keywordPattern = keywordPattern || /\w+/;
  1281. if (mode.keywords) {
  1282. mode.keywords = compileKeywords(mode.keywords, language.case_insensitive);
  1283. }
  1284. cmode.keywordPatternRe = langRe(keywordPattern, true);
  1285. if (parent) {
  1286. if (!mode.begin) mode.begin = /\B|\b/;
  1287. cmode.beginRe = langRe(cmode.begin);
  1288. if (!mode.end && !mode.endsWithParent) mode.end = /\B|\b/;
  1289. if (mode.end) cmode.endRe = langRe(cmode.end);
  1290. cmode.terminatorEnd = source(cmode.end) || '';
  1291. if (mode.endsWithParent && parent.terminatorEnd) {
  1292. cmode.terminatorEnd += (mode.end ? '|' : '') + parent.terminatorEnd;
  1293. }
  1294. }
  1295. if (mode.illegal) cmode.illegalRe = langRe(/** @type {RegExp | string} */ (mode.illegal));
  1296. if (!mode.contains) mode.contains = [];
  1297. mode.contains = [].concat(...mode.contains.map(function(c) {
  1298. return expandOrCloneMode(c === 'self' ? mode : c);
  1299. }));
  1300. mode.contains.forEach(function(c) { compileMode(/** @type Mode */ (c), cmode); });
  1301. if (mode.starts) {
  1302. compileMode(mode.starts, parent);
  1303. }
  1304. cmode.matcher = buildModeRegex(cmode);
  1305. return cmode;
  1306. }
  1307. if (!language.compilerExtensions) language.compilerExtensions = [];
  1308. // self is not valid at the top-level
  1309. if (language.contains && language.contains.includes('self')) {
  1310. throw new Error("ERR: contains `self` is not supported at the top-level of a language. See documentation.");
  1311. }
  1312. // we need a null object, which inherit will guarantee
  1313. language.classNameAliases = inherit$1(language.classNameAliases || {});
  1314. return compileMode(/** @type Mode */ (language));
  1315. }
  1316. /**
  1317. * Determines if a mode has a dependency on it's parent or not
  1318. *
  1319. * If a mode does have a parent dependency then often we need to clone it if
  1320. * it's used in multiple places so that each copy points to the correct parent,
  1321. * where-as modes without a parent can often safely be re-used at the bottom of
  1322. * a mode chain.
  1323. *
  1324. * @param {Mode | null} mode
  1325. * @returns {boolean} - is there a dependency on the parent?
  1326. * */
  1327. function dependencyOnParent(mode) {
  1328. if (!mode) return false;
  1329. return mode.endsWithParent || dependencyOnParent(mode.starts);
  1330. }
  1331. /**
  1332. * Expands a mode or clones it if necessary
  1333. *
  1334. * This is necessary for modes with parental dependenceis (see notes on
  1335. * `dependencyOnParent`) and for nodes that have `variants` - which must then be
  1336. * exploded into their own individual modes at compile time.
  1337. *
  1338. * @param {Mode} mode
  1339. * @returns {Mode | Mode[]}
  1340. * */
  1341. function expandOrCloneMode(mode) {
  1342. if (mode.variants && !mode.cachedVariants) {
  1343. mode.cachedVariants = mode.variants.map(function(variant) {
  1344. return inherit$1(mode, { variants: null }, variant);
  1345. });
  1346. }
  1347. // EXPAND
  1348. // if we have variants then essentially "replace" the mode with the variants
  1349. // this happens in compileMode, where this function is called from
  1350. if (mode.cachedVariants) {
  1351. return mode.cachedVariants;
  1352. }
  1353. // CLONE
  1354. // if we have dependencies on parents then we need a unique
  1355. // instance of ourselves, so we can be reused with many
  1356. // different parents without issue
  1357. if (dependencyOnParent(mode)) {
  1358. return inherit$1(mode, { starts: mode.starts ? inherit$1(mode.starts) : null });
  1359. }
  1360. if (Object.isFrozen(mode)) {
  1361. return inherit$1(mode);
  1362. }
  1363. // no special dependency issues, just return ourselves
  1364. return mode;
  1365. }
  1366. var version = "11.8.0";
  1367. class HTMLInjectionError extends Error {
  1368. constructor(reason, html) {
  1369. super(reason);
  1370. this.name = "HTMLInjectionError";
  1371. this.html = html;
  1372. }
  1373. }
  1374. /*
  1375. Syntax highlighting with language autodetection.
  1376. https://highlightjs.org/
  1377. */
  1378. /**
  1379. @typedef {import('highlight.js').Mode} Mode
  1380. @typedef {import('highlight.js').CompiledMode} CompiledMode
  1381. @typedef {import('highlight.js').CompiledScope} CompiledScope
  1382. @typedef {import('highlight.js').Language} Language
  1383. @typedef {import('highlight.js').HLJSApi} HLJSApi
  1384. @typedef {import('highlight.js').HLJSPlugin} HLJSPlugin
  1385. @typedef {import('highlight.js').PluginEvent} PluginEvent
  1386. @typedef {import('highlight.js').HLJSOptions} HLJSOptions
  1387. @typedef {import('highlight.js').LanguageFn} LanguageFn
  1388. @typedef {import('highlight.js').HighlightedHTMLElement} HighlightedHTMLElement
  1389. @typedef {import('highlight.js').BeforeHighlightContext} BeforeHighlightContext
  1390. @typedef {import('highlight.js/private').MatchType} MatchType
  1391. @typedef {import('highlight.js/private').KeywordData} KeywordData
  1392. @typedef {import('highlight.js/private').EnhancedMatch} EnhancedMatch
  1393. @typedef {import('highlight.js/private').AnnotatedError} AnnotatedError
  1394. @typedef {import('highlight.js').AutoHighlightResult} AutoHighlightResult
  1395. @typedef {import('highlight.js').HighlightOptions} HighlightOptions
  1396. @typedef {import('highlight.js').HighlightResult} HighlightResult
  1397. */
  1398. const escape = escapeHTML;
  1399. const inherit = inherit$1;
  1400. const NO_MATCH = Symbol("nomatch");
  1401. const MAX_KEYWORD_HITS = 7;
  1402. /**
  1403. * @param {any} hljs - object that is extended (legacy)
  1404. * @returns {HLJSApi}
  1405. */
  1406. const HLJS = function(hljs) {
  1407. // Global internal variables used within the highlight.js library.
  1408. /** @type {Record<string, Language>} */
  1409. const languages = Object.create(null);
  1410. /** @type {Record<string, string>} */
  1411. const aliases = Object.create(null);
  1412. /** @type {HLJSPlugin[]} */
  1413. const plugins = [];
  1414. // safe/production mode - swallows more errors, tries to keep running
  1415. // even if a single syntax or parse hits a fatal error
  1416. let SAFE_MODE = true;
  1417. const LANGUAGE_NOT_FOUND = "Could not find the language '{}', did you forget to load/include a language module?";
  1418. /** @type {Language} */
  1419. const PLAINTEXT_LANGUAGE = { disableAutodetect: true, name: 'Plain text', contains: [] };
  1420. // Global options used when within external APIs. This is modified when
  1421. // calling the `hljs.configure` function.
  1422. /** @type HLJSOptions */
  1423. let options = {
  1424. ignoreUnescapedHTML: false,
  1425. throwUnescapedHTML: false,
  1426. noHighlightRe: /^(no-?highlight)$/i,
  1427. languageDetectRe: /\blang(?:uage)?-([\w-]+)\b/i,
  1428. classPrefix: 'hljs-',
  1429. cssSelector: 'pre code',
  1430. languages: null,
  1431. // beta configuration options, subject to change, welcome to discuss
  1432. // https://github.com/highlightjs/highlight.js/issues/1086
  1433. __emitter: TokenTreeEmitter
  1434. };
  1435. /* Utility functions */
  1436. /**
  1437. * Tests a language name to see if highlighting should be skipped
  1438. * @param {string} languageName
  1439. */
  1440. function shouldNotHighlight(languageName) {
  1441. return options.noHighlightRe.test(languageName);
  1442. }
  1443. /**
  1444. * @param {HighlightedHTMLElement} block - the HTML element to determine language for
  1445. */
  1446. function blockLanguage(block) {
  1447. let classes = block.className + ' ';
  1448. classes += block.parentNode ? block.parentNode.className : '';
  1449. // language-* takes precedence over non-prefixed class names.
  1450. const match = options.languageDetectRe.exec(classes);
  1451. if (match) {
  1452. const language = getLanguage(match[1]);
  1453. if (!language) {
  1454. warn(LANGUAGE_NOT_FOUND.replace("{}", match[1]));
  1455. warn("Falling back to no-highlight mode for this block.", block);
  1456. }
  1457. return language ? match[1] : 'no-highlight';
  1458. }
  1459. return classes
  1460. .split(/\s+/)
  1461. .find((_class) => shouldNotHighlight(_class) || getLanguage(_class));
  1462. }
  1463. /**
  1464. * Core highlighting function.
  1465. *
  1466. * OLD API
  1467. * highlight(lang, code, ignoreIllegals, continuation)
  1468. *
  1469. * NEW API
  1470. * highlight(code, {lang, ignoreIllegals})
  1471. *
  1472. * @param {string} codeOrLanguageName - the language to use for highlighting
  1473. * @param {string | HighlightOptions} optionsOrCode - the code to highlight
  1474. * @param {boolean} [ignoreIllegals] - whether to ignore illegal matches, default is to bail
  1475. *
  1476. * @returns {HighlightResult} Result - an object that represents the result
  1477. * @property {string} language - the language name
  1478. * @property {number} relevance - the relevance score
  1479. * @property {string} value - the highlighted HTML code
  1480. * @property {string} code - the original raw code
  1481. * @property {CompiledMode} top - top of the current mode stack
  1482. * @property {boolean} illegal - indicates whether any illegal matches were found
  1483. */
  1484. function highlight(codeOrLanguageName, optionsOrCode, ignoreIllegals) {
  1485. let code = "";
  1486. let languageName = "";
  1487. if (typeof optionsOrCode === "object") {
  1488. code = codeOrLanguageName;
  1489. ignoreIllegals = optionsOrCode.ignoreIllegals;
  1490. languageName = optionsOrCode.language;
  1491. } else {
  1492. // old API
  1493. deprecated("10.7.0", "highlight(lang, code, ...args) has been deprecated.");
  1494. deprecated("10.7.0", "Please use highlight(code, options) instead.\nhttps://github.com/highlightjs/highlight.js/issues/2277");
  1495. languageName = codeOrLanguageName;
  1496. code = optionsOrCode;
  1497. }
  1498. // https://github.com/highlightjs/highlight.js/issues/3149
  1499. // eslint-disable-next-line no-undefined
  1500. if (ignoreIllegals === undefined) { ignoreIllegals = true; }
  1501. /** @type {BeforeHighlightContext} */
  1502. const context = {
  1503. code,
  1504. language: languageName
  1505. };
  1506. // the plugin can change the desired language or the code to be highlighted
  1507. // just be changing the object it was passed
  1508. fire("before:highlight", context);
  1509. // a before plugin can usurp the result completely by providing it's own
  1510. // in which case we don't even need to call highlight
  1511. const result = context.result
  1512. ? context.result
  1513. : _highlight(context.language, context.code, ignoreIllegals);
  1514. result.code = context.code;
  1515. // the plugin can change anything in result to suite it
  1516. fire("after:highlight", result);
  1517. return result;
  1518. }
  1519. /**
  1520. * private highlight that's used internally and does not fire callbacks
  1521. *
  1522. * @param {string} languageName - the language to use for highlighting
  1523. * @param {string} codeToHighlight - the code to highlight
  1524. * @param {boolean?} [ignoreIllegals] - whether to ignore illegal matches, default is to bail
  1525. * @param {CompiledMode?} [continuation] - current continuation mode, if any
  1526. * @returns {HighlightResult} - result of the highlight operation
  1527. */
  1528. function _highlight(languageName, codeToHighlight, ignoreIllegals, continuation) {
  1529. const keywordHits = Object.create(null);
  1530. /**
  1531. * Return keyword data if a match is a keyword
  1532. * @param {CompiledMode} mode - current mode
  1533. * @param {string} matchText - the textual match
  1534. * @returns {KeywordData | false}
  1535. */
  1536. function keywordData(mode, matchText) {
  1537. return mode.keywords[matchText];
  1538. }
  1539. function processKeywords() {
  1540. if (!top.keywords) {
  1541. emitter.addText(modeBuffer);
  1542. return;
  1543. }
  1544. let lastIndex = 0;
  1545. top.keywordPatternRe.lastIndex = 0;
  1546. let match = top.keywordPatternRe.exec(modeBuffer);
  1547. let buf = "";
  1548. while (match) {
  1549. buf += modeBuffer.substring(lastIndex, match.index);
  1550. const word = language.case_insensitive ? match[0].toLowerCase() : match[0];
  1551. const data = keywordData(top, word);
  1552. if (data) {
  1553. const [kind, keywordRelevance] = data;
  1554. emitter.addText(buf);
  1555. buf = "";
  1556. keywordHits[word] = (keywordHits[word] || 0) + 1;
  1557. if (keywordHits[word] <= MAX_KEYWORD_HITS) relevance += keywordRelevance;
  1558. if (kind.startsWith("_")) {
  1559. // _ implied for relevance only, do not highlight
  1560. // by applying a class name
  1561. buf += match[0];
  1562. } else {
  1563. const cssClass = language.classNameAliases[kind] || kind;
  1564. emitKeyword(match[0], cssClass);
  1565. }
  1566. } else {
  1567. buf += match[0];
  1568. }
  1569. lastIndex = top.keywordPatternRe.lastIndex;
  1570. match = top.keywordPatternRe.exec(modeBuffer);
  1571. }
  1572. buf += modeBuffer.substring(lastIndex);
  1573. emitter.addText(buf);
  1574. }
  1575. function processSubLanguage() {
  1576. if (modeBuffer === "") return;
  1577. /** @type HighlightResult */
  1578. let result = null;
  1579. if (typeof top.subLanguage === 'string') {
  1580. if (!languages[top.subLanguage]) {
  1581. emitter.addText(modeBuffer);
  1582. return;
  1583. }
  1584. result = _highlight(top.subLanguage, modeBuffer, true, continuations[top.subLanguage]);
  1585. continuations[top.subLanguage] = /** @type {CompiledMode} */ (result._top);
  1586. } else {
  1587. result = highlightAuto(modeBuffer, top.subLanguage.length ? top.subLanguage : null);
  1588. }
  1589. // Counting embedded language score towards the host language may be disabled
  1590. // with zeroing the containing mode relevance. Use case in point is Markdown that
  1591. // allows XML everywhere and makes every XML snippet to have a much larger Markdown
  1592. // score.
  1593. if (top.relevance > 0) {
  1594. relevance += result.relevance;
  1595. }
  1596. emitter.__addSublanguage(result._emitter, result.language);
  1597. }
  1598. function processBuffer() {
  1599. if (top.subLanguage != null) {
  1600. processSubLanguage();
  1601. } else {
  1602. processKeywords();
  1603. }
  1604. modeBuffer = '';
  1605. }
  1606. /**
  1607. * @param {string} text
  1608. * @param {string} scope
  1609. */
  1610. function emitKeyword(keyword, scope) {
  1611. if (keyword === "") return;
  1612. emitter.startScope(scope);
  1613. emitter.addText(keyword);
  1614. emitter.endScope();
  1615. }
  1616. /**
  1617. * @param {CompiledScope} scope
  1618. * @param {RegExpMatchArray} match
  1619. */
  1620. function emitMultiClass(scope, match) {
  1621. let i = 1;
  1622. const max = match.length - 1;
  1623. while (i <= max) {
  1624. if (!scope._emit[i]) { i++; continue; }
  1625. const klass = language.classNameAliases[scope[i]] || scope[i];
  1626. const text = match[i];
  1627. if (klass) {
  1628. emitKeyword(text, klass);
  1629. } else {
  1630. modeBuffer = text;
  1631. processKeywords();
  1632. modeBuffer = "";
  1633. }
  1634. i++;
  1635. }
  1636. }
  1637. /**
  1638. * @param {CompiledMode} mode - new mode to start
  1639. * @param {RegExpMatchArray} match
  1640. */
  1641. function startNewMode(mode, match) {
  1642. if (mode.scope && typeof mode.scope === "string") {
  1643. emitter.openNode(language.classNameAliases[mode.scope] || mode.scope);
  1644. }
  1645. if (mode.beginScope) {
  1646. // beginScope just wraps the begin match itself in a scope
  1647. if (mode.beginScope._wrap) {
  1648. emitKeyword(modeBuffer, language.classNameAliases[mode.beginScope._wrap] || mode.beginScope._wrap);
  1649. modeBuffer = "";
  1650. } else if (mode.beginScope._multi) {
  1651. // at this point modeBuffer should just be the match
  1652. emitMultiClass(mode.beginScope, match);
  1653. modeBuffer = "";
  1654. }
  1655. }
  1656. top = Object.create(mode, { parent: { value: top } });
  1657. return top;
  1658. }
  1659. /**
  1660. * @param {CompiledMode } mode - the mode to potentially end
  1661. * @param {RegExpMatchArray} match - the latest match
  1662. * @param {string} matchPlusRemainder - match plus remainder of content
  1663. * @returns {CompiledMode | void} - the next mode, or if void continue on in current mode
  1664. */
  1665. function endOfMode(mode, match, matchPlusRemainder) {
  1666. let matched = startsWith(mode.endRe, matchPlusRemainder);
  1667. if (matched) {
  1668. if (mode["on:end"]) {
  1669. const resp = new Response(mode);
  1670. mode["on:end"](match, resp);
  1671. if (resp.isMatchIgnored) matched = false;
  1672. }
  1673. if (matched) {
  1674. while (mode.endsParent && mode.parent) {
  1675. mode = mode.parent;
  1676. }
  1677. return mode;
  1678. }
  1679. }
  1680. // even if on:end fires an `ignore` it's still possible
  1681. // that we might trigger the end node because of a parent mode
  1682. if (mode.endsWithParent) {
  1683. return endOfMode(mode.parent, match, matchPlusRemainder);
  1684. }
  1685. }
  1686. /**
  1687. * Handle matching but then ignoring a sequence of text
  1688. *
  1689. * @param {string} lexeme - string containing full match text
  1690. */
  1691. function doIgnore(lexeme) {
  1692. if (top.matcher.regexIndex === 0) {
  1693. // no more regexes to potentially match here, so we move the cursor forward one
  1694. // space
  1695. modeBuffer += lexeme[0];
  1696. return 1;
  1697. } else {
  1698. // no need to move the cursor, we still have additional regexes to try and
  1699. // match at this very spot
  1700. resumeScanAtSamePosition = true;
  1701. return 0;
  1702. }
  1703. }
  1704. /**
  1705. * Handle the start of a new potential mode match
  1706. *
  1707. * @param {EnhancedMatch} match - the current match
  1708. * @returns {number} how far to advance the parse cursor
  1709. */
  1710. function doBeginMatch(match) {
  1711. const lexeme = match[0];
  1712. const newMode = match.rule;
  1713. const resp = new Response(newMode);
  1714. // first internal before callbacks, then the public ones
  1715. const beforeCallbacks = [newMode.__beforeBegin, newMode["on:begin"]];
  1716. for (const cb of beforeCallbacks) {
  1717. if (!cb) continue;
  1718. cb(match, resp);
  1719. if (resp.isMatchIgnored) return doIgnore(lexeme);
  1720. }
  1721. if (newMode.skip) {
  1722. modeBuffer += lexeme;
  1723. } else {
  1724. if (newMode.excludeBegin) {
  1725. modeBuffer += lexeme;
  1726. }
  1727. processBuffer();
  1728. if (!newMode.returnBegin && !newMode.excludeBegin) {
  1729. modeBuffer = lexeme;
  1730. }
  1731. }
  1732. startNewMode(newMode, match);
  1733. return newMode.returnBegin ? 0 : lexeme.length;
  1734. }
  1735. /**
  1736. * Handle the potential end of mode
  1737. *
  1738. * @param {RegExpMatchArray} match - the current match
  1739. */
  1740. function doEndMatch(match) {
  1741. const lexeme = match[0];
  1742. const matchPlusRemainder = codeToHighlight.substring(match.index);
  1743. const endMode = endOfMode(top, match, matchPlusRemainder);
  1744. if (!endMode) { return NO_MATCH; }
  1745. const origin = top;
  1746. if (top.endScope && top.endScope._wrap) {
  1747. processBuffer();
  1748. emitKeyword(lexeme, top.endScope._wrap);
  1749. } else if (top.endScope && top.endScope._multi) {
  1750. processBuffer();
  1751. emitMultiClass(top.endScope, match);
  1752. } else if (origin.skip) {
  1753. modeBuffer += lexeme;
  1754. } else {
  1755. if (!(origin.returnEnd || origin.excludeEnd)) {
  1756. modeBuffer += lexeme;
  1757. }
  1758. processBuffer();
  1759. if (origin.excludeEnd) {
  1760. modeBuffer = lexeme;
  1761. }
  1762. }
  1763. do {
  1764. if (top.scope) {
  1765. emitter.closeNode();
  1766. }
  1767. if (!top.skip && !top.subLanguage) {
  1768. relevance += top.relevance;
  1769. }
  1770. top = top.parent;
  1771. } while (top !== endMode.parent);
  1772. if (endMode.starts) {
  1773. startNewMode(endMode.starts, match);
  1774. }
  1775. return origin.returnEnd ? 0 : lexeme.length;
  1776. }
  1777. function processContinuations() {
  1778. const list = [];
  1779. for (let current = top; current !== language; current = current.parent) {
  1780. if (current.scope) {
  1781. list.unshift(current.scope);
  1782. }
  1783. }
  1784. list.forEach(item => emitter.openNode(item));
  1785. }
  1786. /** @type {{type?: MatchType, index?: number, rule?: Mode}}} */
  1787. let lastMatch = {};
  1788. /**
  1789. * Process an individual match
  1790. *
  1791. * @param {string} textBeforeMatch - text preceding the match (since the last match)
  1792. * @param {EnhancedMatch} [match] - the match itself
  1793. */
  1794. function processLexeme(textBeforeMatch, match) {
  1795. const lexeme = match && match[0];
  1796. // add non-matched text to the current mode buffer
  1797. modeBuffer += textBeforeMatch;
  1798. if (lexeme == null) {
  1799. processBuffer();
  1800. return 0;
  1801. }
  1802. // we've found a 0 width match and we're stuck, so we need to advance
  1803. // this happens when we have badly behaved rules that have optional matchers to the degree that
  1804. // sometimes they can end up matching nothing at all
  1805. // Ref: https://github.com/highlightjs/highlight.js/issues/2140
  1806. if (lastMatch.type === "begin" && match.type === "end" && lastMatch.index === match.index && lexeme === "") {
  1807. // spit the "skipped" character that our regex choked on back into the output sequence
  1808. modeBuffer += codeToHighlight.slice(match.index, match.index + 1);
  1809. if (!SAFE_MODE) {
  1810. /** @type {AnnotatedError} */
  1811. const err = new Error(`0 width match regex (${languageName})`);
  1812. err.languageName = languageName;
  1813. err.badRule = lastMatch.rule;
  1814. throw err;
  1815. }
  1816. return 1;
  1817. }
  1818. lastMatch = match;
  1819. if (match.type === "begin") {
  1820. return doBeginMatch(match);
  1821. } else if (match.type === "illegal" && !ignoreIllegals) {
  1822. // illegal match, we do not continue processing
  1823. /** @type {AnnotatedError} */
  1824. const err = new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.scope || '<unnamed>') + '"');
  1825. err.mode = top;
  1826. throw err;
  1827. } else if (match.type === "end") {
  1828. const processed = doEndMatch(match);
  1829. if (processed !== NO_MATCH) {
  1830. return processed;
  1831. }
  1832. }
  1833. // edge case for when illegal matches $ (end of line) which is technically
  1834. // a 0 width match but not a begin/end match so it's not caught by the
  1835. // first handler (when ignoreIllegals is true)
  1836. if (match.type === "illegal" && lexeme === "") {
  1837. // advance so we aren't stuck in an infinite loop
  1838. return 1;
  1839. }
  1840. // infinite loops are BAD, this is a last ditch catch all. if we have a
  1841. // decent number of iterations yet our index (cursor position in our
  1842. // parsing) still 3x behind our index then something is very wrong
  1843. // so we bail
  1844. if (iterations > 100000 && iterations > match.index * 3) {
  1845. const err = new Error('potential infinite loop, way more iterations than matches');
  1846. throw err;
  1847. }
  1848. /*
  1849. Why might be find ourselves here? An potential end match that was
  1850. triggered but could not be completed. IE, `doEndMatch` returned NO_MATCH.
  1851. (this could be because a callback requests the match be ignored, etc)
  1852. This causes no real harm other than stopping a few times too many.
  1853. */
  1854. modeBuffer += lexeme;
  1855. return lexeme.length;
  1856. }
  1857. const language = getLanguage(languageName);
  1858. if (!language) {
  1859. error(LANGUAGE_NOT_FOUND.replace("{}", languageName));
  1860. throw new Error('Unknown language: "' + languageName + '"');
  1861. }
  1862. const md = compileLanguage(language);
  1863. let result = '';
  1864. /** @type {CompiledMode} */
  1865. let top = continuation || md;
  1866. /** @type Record<string,CompiledMode> */
  1867. const continuations = {}; // keep continuations for sub-languages
  1868. const emitter = new options.__emitter(options);
  1869. processContinuations();
  1870. let modeBuffer = '';
  1871. let relevance = 0;
  1872. let index = 0;
  1873. let iterations = 0;
  1874. let resumeScanAtSamePosition = false;
  1875. try {
  1876. if (!language.__emitTokens) {
  1877. top.matcher.considerAll();
  1878. for (;;) {
  1879. iterations++;
  1880. if (resumeScanAtSamePosition) {
  1881. // only regexes not matched previously will now be
  1882. // considered for a potential match
  1883. resumeScanAtSamePosition = false;
  1884. } else {
  1885. top.matcher.considerAll();
  1886. }
  1887. top.matcher.lastIndex = index;
  1888. const match = top.matcher.exec(codeToHighlight);
  1889. // console.log("match", match[0], match.rule && match.rule.begin)
  1890. if (!match) break;
  1891. const beforeMatch = codeToHighlight.substring(index, match.index);
  1892. const processedCount = processLexeme(beforeMatch, match);
  1893. index = match.index + processedCount;
  1894. }
  1895. processLexeme(codeToHighlight.substring(index));
  1896. } else {
  1897. language.__emitTokens(codeToHighlight, emitter);
  1898. }
  1899. emitter.finalize();
  1900. result = emitter.toHTML();
  1901. return {
  1902. language: languageName,
  1903. value: result,
  1904. relevance,
  1905. illegal: false,
  1906. _emitter: emitter,
  1907. _top: top
  1908. };
  1909. } catch (err) {
  1910. if (err.message && err.message.includes('Illegal')) {
  1911. return {
  1912. language: languageName,
  1913. value: escape(codeToHighlight),
  1914. illegal: true,
  1915. relevance: 0,
  1916. _illegalBy: {
  1917. message: err.message,
  1918. index,
  1919. context: codeToHighlight.slice(index - 100, index + 100),
  1920. mode: err.mode,
  1921. resultSoFar: result
  1922. },
  1923. _emitter: emitter
  1924. };
  1925. } else if (SAFE_MODE) {
  1926. return {
  1927. language: languageName,
  1928. value: escape(codeToHighlight),
  1929. illegal: false,
  1930. relevance: 0,
  1931. errorRaised: err,
  1932. _emitter: emitter,
  1933. _top: top
  1934. };
  1935. } else {
  1936. throw err;
  1937. }
  1938. }
  1939. }
  1940. /**
  1941. * returns a valid highlight result, without actually doing any actual work,
  1942. * auto highlight starts with this and it's possible for small snippets that
  1943. * auto-detection may not find a better match
  1944. * @param {string} code
  1945. * @returns {HighlightResult}
  1946. */
  1947. function justTextHighlightResult(code) {
  1948. const result = {
  1949. value: escape(code),
  1950. illegal: false,
  1951. relevance: 0,
  1952. _top: PLAINTEXT_LANGUAGE,
  1953. _emitter: new options.__emitter(options)
  1954. };
  1955. result._emitter.addText(code);
  1956. return result;
  1957. }
  1958. /**
  1959. Highlighting with language detection. Accepts a string with the code to
  1960. highlight. Returns an object with the following properties:
  1961. - language (detected language)
  1962. - relevance (int)
  1963. - value (an HTML string with highlighting markup)
  1964. - secondBest (object with the same structure for second-best heuristically
  1965. detected language, may be absent)
  1966. @param {string} code
  1967. @param {Array<string>} [languageSubset]
  1968. @returns {AutoHighlightResult}
  1969. */
  1970. function highlightAuto(code, languageSubset) {
  1971. languageSubset = languageSubset || options.languages || Object.keys(languages);
  1972. const plaintext = justTextHighlightResult(code);
  1973. const results = languageSubset.filter(getLanguage).filter(autoDetection).map(name =>
  1974. _highlight(name, code, false)
  1975. );
  1976. results.unshift(plaintext); // plaintext is always an option
  1977. const sorted = results.sort((a, b) => {
  1978. // sort base on relevance
  1979. if (a.relevance !== b.relevance) return b.relevance - a.relevance;
  1980. // always award the tie to the base language
  1981. // ie if C++ and Arduino are tied, it's more likely to be C++
  1982. if (a.language && b.language) {
  1983. if (getLanguage(a.language).supersetOf === b.language) {
  1984. return 1;
  1985. } else if (getLanguage(b.language).supersetOf === a.language) {
  1986. return -1;
  1987. }
  1988. }
  1989. // otherwise say they are equal, which has the effect of sorting on
  1990. // relevance while preserving the original ordering - which is how ties
  1991. // have historically been settled, ie the language that comes first always
  1992. // wins in the case of a tie
  1993. return 0;
  1994. });
  1995. const [best, secondBest] = sorted;
  1996. /** @type {AutoHighlightResult} */
  1997. const result = best;
  1998. result.secondBest = secondBest;
  1999. return result;
  2000. }
  2001. /**
  2002. * Builds new class name for block given the language name
  2003. *
  2004. * @param {HTMLElement} element
  2005. * @param {string} [currentLang]
  2006. * @param {string} [resultLang]
  2007. */
  2008. function updateClassName(element, currentLang, resultLang) {
  2009. const language = (currentLang && aliases[currentLang]) || resultLang;
  2010. element.classList.add("hljs");
  2011. element.classList.add(`language-${language}`);
  2012. }
  2013. /**
  2014. * Applies highlighting to a DOM node containing code.
  2015. *
  2016. * @param {HighlightedHTMLElement} element - the HTML element to highlight
  2017. */
  2018. function highlightElement(element) {
  2019. /** @type HTMLElement */
  2020. let node = null;
  2021. const language = blockLanguage(element);
  2022. if (shouldNotHighlight(language)) return;
  2023. fire("before:highlightElement",
  2024. { el: element, language });
  2025. // we should be all text, no child nodes (unescaped HTML) - this is possibly
  2026. // an HTML injection attack - it's likely too late if this is already in
  2027. // production (the code has likely already done its damage by the time
  2028. // we're seeing it)... but we yell loudly about this so that hopefully it's
  2029. // more likely to be caught in development before making it to production
  2030. if (element.children.length > 0) {
  2031. if (!options.ignoreUnescapedHTML) {
  2032. console.warn("One of your code blocks includes unescaped HTML. This is a potentially serious security risk.");
  2033. console.warn("https://github.com/highlightjs/highlight.js/wiki/security");
  2034. console.warn("The element with unescaped HTML:");
  2035. console.warn(element);
  2036. }
  2037. if (options.throwUnescapedHTML) {
  2038. const err = new HTMLInjectionError(
  2039. "One of your code blocks includes unescaped HTML.",
  2040. element.innerHTML
  2041. );
  2042. throw err;
  2043. }
  2044. }
  2045. node = element;
  2046. const text = node.textContent;
  2047. const result = language ? highlight(text, { language, ignoreIllegals: true }) : highlightAuto(text);
  2048. element.innerHTML = result.value;
  2049. updateClassName(element, language, result.language);
  2050. element.result = {
  2051. language: result.language,
  2052. // TODO: remove with version 11.0
  2053. re: result.relevance,
  2054. relevance: result.relevance
  2055. };
  2056. if (result.secondBest) {
  2057. element.secondBest = {
  2058. language: result.secondBest.language,
  2059. relevance: result.secondBest.relevance
  2060. };
  2061. }
  2062. fire("after:highlightElement", { el: element, result, text });
  2063. }
  2064. /**
  2065. * Updates highlight.js global options with the passed options
  2066. *
  2067. * @param {Partial<HLJSOptions>} userOptions
  2068. */
  2069. function configure(userOptions) {
  2070. options = inherit(options, userOptions);
  2071. }
  2072. // TODO: remove v12, deprecated
  2073. const initHighlighting = () => {
  2074. highlightAll();
  2075. deprecated("10.6.0", "initHighlighting() deprecated. Use highlightAll() now.");
  2076. };
  2077. // TODO: remove v12, deprecated
  2078. function initHighlightingOnLoad() {
  2079. highlightAll();
  2080. deprecated("10.6.0", "initHighlightingOnLoad() deprecated. Use highlightAll() now.");
  2081. }
  2082. let wantsHighlight = false;
  2083. /**
  2084. * auto-highlights all pre>code elements on the page
  2085. */
  2086. function highlightAll() {
  2087. // if we are called too early in the loading process
  2088. if (document.readyState === "loading") {
  2089. wantsHighlight = true;
  2090. return;
  2091. }
  2092. const blocks = document.querySelectorAll(options.cssSelector);
  2093. blocks.forEach(highlightElement);
  2094. }
  2095. function boot() {
  2096. // if a highlight was requested before DOM was loaded, do now
  2097. if (wantsHighlight) highlightAll();
  2098. }
  2099. // make sure we are in the browser environment
  2100. if (typeof window !== 'undefined' && window.addEventListener) {
  2101. window.addEventListener('DOMContentLoaded', boot, false);
  2102. }
  2103. /**
  2104. * Register a language grammar module
  2105. *
  2106. * @param {string} languageName
  2107. * @param {LanguageFn} languageDefinition
  2108. */
  2109. function registerLanguage(languageName, languageDefinition) {
  2110. let lang = null;
  2111. try {
  2112. lang = languageDefinition(hljs);
  2113. } catch (error$1) {
  2114. error("Language definition for '{}' could not be registered.".replace("{}", languageName));
  2115. // hard or soft error
  2116. if (!SAFE_MODE) { throw error$1; } else { error(error$1); }
  2117. // languages that have serious errors are replaced with essentially a
  2118. // "plaintext" stand-in so that the code blocks will still get normal
  2119. // css classes applied to them - and one bad language won't break the
  2120. // entire highlighter
  2121. lang = PLAINTEXT_LANGUAGE;
  2122. }
  2123. // give it a temporary name if it doesn't have one in the meta-data
  2124. if (!lang.name) lang.name = languageName;
  2125. languages[languageName] = lang;
  2126. lang.rawDefinition = languageDefinition.bind(null, hljs);
  2127. if (lang.aliases) {
  2128. registerAliases(lang.aliases, { languageName });
  2129. }
  2130. }
  2131. /**
  2132. * Remove a language grammar module
  2133. *
  2134. * @param {string} languageName
  2135. */
  2136. function unregisterLanguage(languageName) {
  2137. delete languages[languageName];
  2138. for (const alias of Object.keys(aliases)) {
  2139. if (aliases[alias] === languageName) {
  2140. delete aliases[alias];
  2141. }
  2142. }
  2143. }
  2144. /**
  2145. * @returns {string[]} List of language internal names
  2146. */
  2147. function listLanguages() {
  2148. return Object.keys(languages);
  2149. }
  2150. /**
  2151. * @param {string} name - name of the language to retrieve
  2152. * @returns {Language | undefined}
  2153. */
  2154. function getLanguage(name) {
  2155. name = (name || '').toLowerCase();
  2156. return languages[name] || languages[aliases[name]];
  2157. }
  2158. /**
  2159. *
  2160. * @param {string|string[]} aliasList - single alias or list of aliases
  2161. * @param {{languageName: string}} opts
  2162. */
  2163. function registerAliases(aliasList, { languageName }) {
  2164. if (typeof aliasList === 'string') {
  2165. aliasList = [aliasList];
  2166. }
  2167. aliasList.forEach(alias => { aliases[alias.toLowerCase()] = languageName; });
  2168. }
  2169. /**
  2170. * Determines if a given language has auto-detection enabled
  2171. * @param {string} name - name of the language
  2172. */
  2173. function autoDetection(name) {
  2174. const lang = getLanguage(name);
  2175. return lang && !lang.disableAutodetect;
  2176. }
  2177. /**
  2178. * Upgrades the old highlightBlock plugins to the new
  2179. * highlightElement API
  2180. * @param {HLJSPlugin} plugin
  2181. */
  2182. function upgradePluginAPI(plugin) {
  2183. // TODO: remove with v12
  2184. if (plugin["before:highlightBlock"] && !plugin["before:highlightElement"]) {
  2185. plugin["before:highlightElement"] = (data) => {
  2186. plugin["before:highlightBlock"](
  2187. Object.assign({ block: data.el }, data)
  2188. );
  2189. };
  2190. }
  2191. if (plugin["after:highlightBlock"] && !plugin["after:highlightElement"]) {
  2192. plugin["after:highlightElement"] = (data) => {
  2193. plugin["after:highlightBlock"](
  2194. Object.assign({ block: data.el }, data)
  2195. );
  2196. };
  2197. }
  2198. }
  2199. /**
  2200. * @param {HLJSPlugin} plugin
  2201. */
  2202. function addPlugin(plugin) {
  2203. upgradePluginAPI(plugin);
  2204. plugins.push(plugin);
  2205. }
  2206. /**
  2207. * @param {HLJSPlugin} plugin
  2208. */
  2209. function removePlugin(plugin) {
  2210. const index = plugins.indexOf(plugin);
  2211. if (index !== -1) {
  2212. plugins.splice(index, 1);
  2213. }
  2214. }
  2215. /**
  2216. *
  2217. * @param {PluginEvent} event
  2218. * @param {any} args
  2219. */
  2220. function fire(event, args) {
  2221. const cb = event;
  2222. plugins.forEach(function(plugin) {
  2223. if (plugin[cb]) {
  2224. plugin[cb](args);
  2225. }
  2226. });
  2227. }
  2228. /**
  2229. * DEPRECATED
  2230. * @param {HighlightedHTMLElement} el
  2231. */
  2232. function deprecateHighlightBlock(el) {
  2233. deprecated("10.7.0", "highlightBlock will be removed entirely in v12.0");
  2234. deprecated("10.7.0", "Please use highlightElement now.");
  2235. return highlightElement(el);
  2236. }
  2237. /* Interface definition */
  2238. Object.assign(hljs, {
  2239. highlight,
  2240. highlightAuto,
  2241. highlightAll,
  2242. highlightElement,
  2243. // TODO: Remove with v12 API
  2244. highlightBlock: deprecateHighlightBlock,
  2245. configure,
  2246. initHighlighting,
  2247. initHighlightingOnLoad,
  2248. registerLanguage,
  2249. unregisterLanguage,
  2250. listLanguages,
  2251. getLanguage,
  2252. registerAliases,
  2253. autoDetection,
  2254. inherit,
  2255. addPlugin,
  2256. removePlugin
  2257. });
  2258. hljs.debugMode = function() { SAFE_MODE = false; };
  2259. hljs.safeMode = function() { SAFE_MODE = true; };
  2260. hljs.versionString = version;
  2261. hljs.regex = {
  2262. concat: concat,
  2263. lookahead: lookahead,
  2264. either: either,
  2265. optional: optional,
  2266. anyNumberOfTimes: anyNumberOfTimes
  2267. };
  2268. for (const key in MODES) {
  2269. // @ts-ignore
  2270. if (typeof MODES[key] === "object") {
  2271. // @ts-ignore
  2272. deepFreeze(MODES[key]);
  2273. }
  2274. }
  2275. // merge all the modes/regexes into our main object
  2276. Object.assign(hljs, MODES);
  2277. return hljs;
  2278. };
  2279. // Other names for the variable may break build script
  2280. const highlight = HLJS({});
  2281. // returns a new instance of the highlighter to be used for extensions
  2282. // check https://github.com/wooorm/lowlight/issues/47
  2283. highlight.newInstance = () => HLJS({});
  2284. module.exports = highlight;
  2285. highlight.HighlightJS = highlight;
  2286. highlight.default = highlight;