core.js 74 KB

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