core.js 67 KB

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