core.js 72 KB

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