prism-core.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /// <reference lib="WebWorker"/>
  2. var _self = (typeof window !== 'undefined')
  3. ? window // if in browser
  4. : (
  5. (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
  6. ? self // if in worker
  7. : {} // if in node js
  8. );
  9. /**
  10. * Prism: Lightweight, robust, elegant syntax highlighting
  11. *
  12. * @license MIT <https://opensource.org/licenses/MIT>
  13. * @author Lea Verou <https://lea.verou.me>
  14. * @namespace
  15. * @public
  16. */
  17. var Prism = (function (_self) {
  18. // Private helper vars
  19. var lang = /(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i;
  20. var uniqueId = 0;
  21. // The grammar object for plaintext
  22. var plainTextGrammar = {};
  23. var _ = {
  24. /**
  25. * By default, Prism will attempt to highlight all code elements (by calling {@link Prism.highlightAll}) on the
  26. * current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load
  27. * additional languages or plugins yourself.
  28. *
  29. * By setting this value to `true`, Prism will not automatically highlight all code elements on the page.
  30. *
  31. * You obviously have to change this value before the automatic highlighting started. To do this, you can add an
  32. * empty Prism object into the global scope before loading the Prism script like this:
  33. *
  34. * ```js
  35. * window.Prism = window.Prism || {};
  36. * Prism.manual = true;
  37. * // add a new <script> to load Prism's script
  38. * ```
  39. *
  40. * @default false
  41. * @type {boolean}
  42. * @memberof Prism
  43. * @public
  44. */
  45. manual: _self.Prism && _self.Prism.manual,
  46. /**
  47. * By default, if Prism is in a web worker, it assumes that it is in a worker it created itself, so it uses
  48. * `addEventListener` to communicate with its parent instance. However, if you're using Prism manually in your
  49. * own worker, you don't want it to do this.
  50. *
  51. * By setting this value to `true`, Prism will not add its own listeners to the worker.
  52. *
  53. * You obviously have to change this value before Prism executes. To do this, you can add an
  54. * empty Prism object into the global scope before loading the Prism script like this:
  55. *
  56. * ```js
  57. * window.Prism = window.Prism || {};
  58. * Prism.disableWorkerMessageHandler = true;
  59. * // Load Prism's script
  60. * ```
  61. *
  62. * @default false
  63. * @type {boolean}
  64. * @memberof Prism
  65. * @public
  66. */
  67. disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
  68. /**
  69. * A namespace for utility methods.
  70. *
  71. * All function in this namespace that are not explicitly marked as _public_ are for __internal use only__ and may
  72. * change or disappear at any time.
  73. *
  74. * @namespace
  75. * @memberof Prism
  76. */
  77. util: {
  78. encode: function encode(tokens) {
  79. if (tokens instanceof Token) {
  80. return new Token(tokens.type, encode(tokens.content), tokens.alias);
  81. } else if (Array.isArray(tokens)) {
  82. return tokens.map(encode);
  83. } else {
  84. return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
  85. }
  86. },
  87. /**
  88. * Returns the name of the type of the given value.
  89. *
  90. * @param {any} o
  91. * @returns {string}
  92. * @example
  93. * type(null) === 'Null'
  94. * type(undefined) === 'Undefined'
  95. * type(123) === 'Number'
  96. * type('foo') === 'String'
  97. * type(true) === 'Boolean'
  98. * type([1, 2]) === 'Array'
  99. * type({}) === 'Object'
  100. * type(String) === 'Function'
  101. * type(/abc+/) === 'RegExp'
  102. */
  103. type: function (o) {
  104. return Object.prototype.toString.call(o).slice(8, -1);
  105. },
  106. /**
  107. * Returns a unique number for the given object. Later calls will still return the same number.
  108. *
  109. * @param {Object} obj
  110. * @returns {number}
  111. */
  112. objId: function (obj) {
  113. if (!obj['__id']) {
  114. Object.defineProperty(obj, '__id', { value: ++uniqueId });
  115. }
  116. return obj['__id'];
  117. },
  118. /**
  119. * Creates a deep clone of the given object.
  120. *
  121. * The main intended use of this function is to clone language definitions.
  122. *
  123. * @param {T} o
  124. * @param {Record<number, any>} [visited]
  125. * @returns {T}
  126. * @template T
  127. */
  128. clone: function deepClone(o, visited) {
  129. visited = visited || {};
  130. var clone; var id;
  131. switch (_.util.type(o)) {
  132. case 'Object':
  133. id = _.util.objId(o);
  134. if (visited[id]) {
  135. return visited[id];
  136. }
  137. clone = /** @type {Record<string, any>} */ ({});
  138. visited[id] = clone;
  139. for (var key in o) {
  140. if (o.hasOwnProperty(key)) {
  141. clone[key] = deepClone(o[key], visited);
  142. }
  143. }
  144. return /** @type {any} */ (clone);
  145. case 'Array':
  146. id = _.util.objId(o);
  147. if (visited[id]) {
  148. return visited[id];
  149. }
  150. clone = [];
  151. visited[id] = clone;
  152. (/** @type {Array} */(/** @type {any} */(o))).forEach(function (v, i) {
  153. clone[i] = deepClone(v, visited);
  154. });
  155. return /** @type {any} */ (clone);
  156. default:
  157. return o;
  158. }
  159. },
  160. /**
  161. * Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
  162. *
  163. * If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
  164. *
  165. * @param {Element} element
  166. * @returns {string}
  167. */
  168. getLanguage: function (element) {
  169. while (element) {
  170. var m = lang.exec(element.className);
  171. if (m) {
  172. return m[1].toLowerCase();
  173. }
  174. element = element.parentElement;
  175. }
  176. return 'none';
  177. },
  178. /**
  179. * Sets the Prism `language-xxxx` class of the given element.
  180. *
  181. * @param {Element} element
  182. * @param {string} language
  183. * @returns {void}
  184. */
  185. setLanguage: function (element, language) {
  186. // remove all `language-xxxx` classes
  187. // (this might leave behind a leading space)
  188. element.className = element.className.replace(RegExp(lang, 'gi'), '');
  189. // add the new `language-xxxx` class
  190. // (using `classList` will automatically clean up spaces for us)
  191. element.classList.add('language-' + language);
  192. },
  193. /**
  194. * Returns the script element that is currently executing.
  195. *
  196. * This does __not__ work for line script element.
  197. *
  198. * @returns {HTMLScriptElement | null}
  199. */
  200. currentScript: function () {
  201. if (typeof document === 'undefined') {
  202. return null;
  203. }
  204. if ('currentScript' in document && 1 < 2 /* hack to trip TS' flow analysis */) {
  205. return /** @type {any} */ (document.currentScript);
  206. }
  207. // IE11 workaround
  208. // we'll get the src of the current script by parsing IE11's error stack trace
  209. // this will not work for inline scripts
  210. try {
  211. throw new Error();
  212. } catch (err) {
  213. // Get file src url from stack. Specifically works with the format of stack traces in IE.
  214. // A stack will look like this:
  215. //
  216. // Error
  217. // at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
  218. // at Global code (http://localhost/components/prism-core.js:606:1)
  219. var src = (/at [^(\r\n]*\((.*):[^:]+:[^:]+\)$/i.exec(err.stack) || [])[1];
  220. if (src) {
  221. var scripts = document.getElementsByTagName('script');
  222. for (var i in scripts) {
  223. if (scripts[i].src == src) {
  224. return scripts[i];
  225. }
  226. }
  227. }
  228. return null;
  229. }
  230. },
  231. /**
  232. * Returns whether a given class is active for `element`.
  233. *
  234. * The class can be activated if `element` or one of its ancestors has the given class and it can be deactivated
  235. * if `element` or one of its ancestors has the negated version of the given class. The _negated version_ of the
  236. * given class is just the given class with a `no-` prefix.
  237. *
  238. * Whether the class is active is determined by the closest ancestor of `element` (where `element` itself is
  239. * closest ancestor) that has the given class or the negated version of it. If neither `element` nor any of its
  240. * ancestors have the given class or the negated version of it, then the default activation will be returned.
  241. *
  242. * In the paradoxical situation where the closest ancestor contains __both__ the given class and the negated
  243. * version of it, the class is considered active.
  244. *
  245. * @param {Element} element
  246. * @param {string} className
  247. * @param {boolean} [defaultActivation=false]
  248. * @returns {boolean}
  249. */
  250. isActive: function (element, className, defaultActivation) {
  251. var no = 'no-' + className;
  252. while (element) {
  253. var classList = element.classList;
  254. if (classList.contains(className)) {
  255. return true;
  256. }
  257. if (classList.contains(no)) {
  258. return false;
  259. }
  260. element = element.parentElement;
  261. }
  262. return !!defaultActivation;
  263. }
  264. },
  265. /**
  266. * This namespace contains all currently loaded languages and the some helper functions to create and modify languages.
  267. *
  268. * @namespace
  269. * @memberof Prism
  270. * @public
  271. */
  272. languages: {
  273. /**
  274. * The grammar for plain, unformatted text.
  275. */
  276. plain: plainTextGrammar,
  277. plaintext: plainTextGrammar,
  278. text: plainTextGrammar,
  279. txt: plainTextGrammar,
  280. /**
  281. * Creates a deep copy of the language with the given id and appends the given tokens.
  282. *
  283. * If a token in `redef` also appears in the copied language, then the existing token in the copied language
  284. * will be overwritten at its original position.
  285. *
  286. * ## Best practices
  287. *
  288. * Since the position of overwriting tokens (token in `redef` that overwrite tokens in the copied language)
  289. * doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to
  290. * understand the language definition because, normally, the order of tokens matters in Prism grammars.
  291. *
  292. * Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
  293. * Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
  294. *
  295. * @param {string} id The id of the language to extend. This has to be a key in `Prism.languages`.
  296. * @param {Grammar} redef The new tokens to append.
  297. * @returns {Grammar} The new language created.
  298. * @public
  299. * @example
  300. * Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
  301. * // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
  302. * // at its original position
  303. * 'comment': { ... },
  304. * // CSS doesn't have a 'color' token, so this token will be appended
  305. * 'color': /\b(?:red|green|blue)\b/
  306. * });
  307. */
  308. extend: function (id, redef) {
  309. var lang = _.util.clone(_.languages[id]);
  310. for (var key in redef) {
  311. lang[key] = redef[key];
  312. }
  313. return lang;
  314. },
  315. /**
  316. * Inserts tokens _before_ another token in a language definition or any other grammar.
  317. *
  318. * ## Usage
  319. *
  320. * This helper method makes it easy to modify existing languages. For example, the CSS language definition
  321. * not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded
  322. * in HTML through `<style>` elements. To do this, it needs to modify `Prism.languages.markup` and add the
  323. * appropriate tokens. However, `Prism.languages.markup` is a regular JavaScript object literal, so if you do
  324. * this:
  325. *
  326. * ```js
  327. * Prism.languages.markup.style = {
  328. * // token
  329. * };
  330. * ```
  331. *
  332. * then the `style` token will be added (and processed) at the end. `insertBefore` allows you to insert tokens
  333. * before existing tokens. For the CSS example above, you would use it like this:
  334. *
  335. * ```js
  336. * Prism.languages.insertBefore('markup', 'cdata', {
  337. * 'style': {
  338. * // token
  339. * }
  340. * });
  341. * ```
  342. *
  343. * ## Special cases
  344. *
  345. * If the grammars of `inside` and `insert` have tokens with the same name, the tokens in `inside`'s grammar
  346. * will be ignored.
  347. *
  348. * This behavior can be used to insert tokens after `before`:
  349. *
  350. * ```js
  351. * Prism.languages.insertBefore('markup', 'comment', {
  352. * 'comment': Prism.languages.markup.comment,
  353. * // tokens after 'comment'
  354. * });
  355. * ```
  356. *
  357. * ## Limitations
  358. *
  359. * The main problem `insertBefore` has to solve is iteration order. Since ES2015, the iteration order for object
  360. * properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
  361. * differently when keys are deleted and re-inserted. So `insertBefore` can't be implemented by temporarily
  362. * deleting properties which is necessary to insert at arbitrary positions.
  363. *
  364. * To solve this problem, `insertBefore` doesn't actually insert the given tokens into the target object.
  365. * Instead, it will create a new object and replace all references to the target object with the new one. This
  366. * can be done without temporarily deleting properties, so the iteration order is well-defined.
  367. *
  368. * However, only references that can be reached from `Prism.languages` or `insert` will be replaced. I.e. if
  369. * you hold the target object in a variable, then the value of the variable will not change.
  370. *
  371. * ```js
  372. * var oldMarkup = Prism.languages.markup;
  373. * var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... });
  374. *
  375. * assert(oldMarkup !== Prism.languages.markup);
  376. * assert(newMarkup === Prism.languages.markup);
  377. * ```
  378. *
  379. * @param {string} inside The property of `root` (e.g. a language id in `Prism.languages`) that contains the
  380. * object to be modified.
  381. * @param {string} before The key to insert before.
  382. * @param {Grammar} insert An object containing the key-value pairs to be inserted.
  383. * @param {Object<string, any>} [root] The object containing `inside`, i.e. the object that contains the
  384. * object to be modified.
  385. *
  386. * Defaults to `Prism.languages`.
  387. * @returns {Grammar} The new grammar object.
  388. * @public
  389. */
  390. insertBefore: function (inside, before, insert, root) {
  391. root = root || /** @type {any} */ (_.languages);
  392. var grammar = root[inside];
  393. /** @type {Grammar} */
  394. var ret = {};
  395. for (var token in grammar) {
  396. if (grammar.hasOwnProperty(token)) {
  397. if (token == before) {
  398. for (var newToken in insert) {
  399. if (insert.hasOwnProperty(newToken)) {
  400. ret[newToken] = insert[newToken];
  401. }
  402. }
  403. }
  404. // Do not insert token which also occur in insert. See #1525
  405. if (!insert.hasOwnProperty(token)) {
  406. ret[token] = grammar[token];
  407. }
  408. }
  409. }
  410. var old = root[inside];
  411. root[inside] = ret;
  412. // Update references in other language definitions
  413. _.languages.DFS(_.languages, function (key, value) {
  414. if (value === old && key != inside) {
  415. this[key] = ret;
  416. }
  417. });
  418. return ret;
  419. },
  420. // Traverse a language definition with Depth First Search
  421. DFS: function DFS(o, callback, type, visited) {
  422. visited = visited || {};
  423. var objId = _.util.objId;
  424. for (var i in o) {
  425. if (o.hasOwnProperty(i)) {
  426. callback.call(o, i, o[i], type || i);
  427. var property = o[i];
  428. var propertyType = _.util.type(property);
  429. if (propertyType === 'Object' && !visited[objId(property)]) {
  430. visited[objId(property)] = true;
  431. DFS(property, callback, null, visited);
  432. } else if (propertyType === 'Array' && !visited[objId(property)]) {
  433. visited[objId(property)] = true;
  434. DFS(property, callback, i, visited);
  435. }
  436. }
  437. }
  438. }
  439. },
  440. plugins: {},
  441. /**
  442. * This is the most high-level function in Prism’s API.
  443. * It fetches all the elements that have a `.language-xxxx` class and then calls {@link Prism.highlightElement} on
  444. * each one of them.
  445. *
  446. * This is equivalent to `Prism.highlightAllUnder(document, async, callback)`.
  447. *
  448. * @param {boolean} [async=false] Same as in {@link Prism.highlightAllUnder}.
  449. * @param {HighlightCallback} [callback] Same as in {@link Prism.highlightAllUnder}.
  450. * @memberof Prism
  451. * @public
  452. */
  453. highlightAll: function (async, callback) {
  454. _.highlightAllUnder(document, async, callback);
  455. },
  456. /**
  457. * Fetches all the descendants of `container` that have a `.language-xxxx` class and then calls
  458. * {@link Prism.highlightElement} on each one of them.
  459. *
  460. * The following hooks will be run:
  461. * 1. `before-highlightall`
  462. * 2. `before-all-elements-highlight`
  463. * 3. All hooks of {@link Prism.highlightElement} for each element.
  464. *
  465. * @param {ParentNode} container The root element, whose descendants that have a `.language-xxxx` class will be highlighted.
  466. * @param {boolean} [async=false] Whether each element is to be highlighted asynchronously using Web Workers.
  467. * @param {HighlightCallback} [callback] An optional callback to be invoked on each element after its highlighting is done.
  468. * @memberof Prism
  469. * @public
  470. */
  471. highlightAllUnder: function (container, async, callback) {
  472. var env = {
  473. callback: callback,
  474. container: container,
  475. selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
  476. };
  477. _.hooks.run('before-highlightall', env);
  478. env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
  479. _.hooks.run('before-all-elements-highlight', env);
  480. for (var i = 0, element; (element = env.elements[i++]);) {
  481. _.highlightElement(element, async === true, env.callback);
  482. }
  483. },
  484. /**
  485. * Highlights the code inside a single element.
  486. *
  487. * The following hooks will be run:
  488. * 1. `before-sanity-check`
  489. * 2. `before-highlight`
  490. * 3. All hooks of {@link Prism.highlight}. These hooks will be run by an asynchronous worker if `async` is `true`.
  491. * 4. `before-insert`
  492. * 5. `after-highlight`
  493. * 6. `complete`
  494. *
  495. * Some the above hooks will be skipped if the element doesn't contain any text or there is no grammar loaded for
  496. * the element's language.
  497. *
  498. * @param {Element} element The element containing the code.
  499. * It must have a class of `language-xxxx` to be processed, where `xxxx` is a valid language identifier.
  500. * @param {boolean} [async=false] Whether the element is to be highlighted asynchronously using Web Workers
  501. * to improve performance and avoid blocking the UI when highlighting very large chunks of code. This option is
  502. * [disabled by default](https://prismjs.com/faq.html#why-is-asynchronous-highlighting-disabled-by-default).
  503. *
  504. * Note: All language definitions required to highlight the code must be included in the main `prism.js` file for
  505. * asynchronous highlighting to work. You can build your own bundle on the
  506. * [Download page](https://prismjs.com/download.html).
  507. * @param {HighlightCallback} [callback] An optional callback to be invoked after the highlighting is done.
  508. * Mostly useful when `async` is `true`, since in that case, the highlighting is done asynchronously.
  509. * @memberof Prism
  510. * @public
  511. */
  512. highlightElement: function (element, async, callback) {
  513. // Find language
  514. var language = _.util.getLanguage(element);
  515. var grammar = _.languages[language];
  516. // Set language on the element, if not present
  517. _.util.setLanguage(element, language);
  518. // Set language on the parent, for styling
  519. var parent = element.parentElement;
  520. if (parent && parent.nodeName.toLowerCase() === 'pre') {
  521. _.util.setLanguage(parent, language);
  522. }
  523. var code = element.textContent;
  524. var env = {
  525. element: element,
  526. language: language,
  527. grammar: grammar,
  528. code: code
  529. };
  530. function insertHighlightedCode(highlightedCode) {
  531. env.highlightedCode = highlightedCode;
  532. _.hooks.run('before-insert', env);
  533. env.element.innerHTML = env.highlightedCode;
  534. _.hooks.run('after-highlight', env);
  535. _.hooks.run('complete', env);
  536. callback && callback.call(env.element);
  537. }
  538. _.hooks.run('before-sanity-check', env);
  539. // plugins may change/add the parent/element
  540. parent = env.element.parentElement;
  541. if (parent && parent.nodeName.toLowerCase() === 'pre' && !parent.hasAttribute('tabindex')) {
  542. parent.setAttribute('tabindex', '0');
  543. }
  544. if (!env.code) {
  545. _.hooks.run('complete', env);
  546. callback && callback.call(env.element);
  547. return;
  548. }
  549. _.hooks.run('before-highlight', env);
  550. if (!env.grammar) {
  551. insertHighlightedCode(_.util.encode(env.code));
  552. return;
  553. }
  554. if (async && _self.Worker) {
  555. var worker = new Worker(_.filename);
  556. worker.onmessage = function (evt) {
  557. insertHighlightedCode(evt.data);
  558. };
  559. worker.postMessage(JSON.stringify({
  560. language: env.language,
  561. code: env.code,
  562. immediateClose: true
  563. }));
  564. } else {
  565. insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
  566. }
  567. },
  568. /**
  569. * Low-level function, only use if you know what you’re doing. It accepts a string of text as input
  570. * and the language definitions to use, and returns a string with the HTML produced.
  571. *
  572. * The following hooks will be run:
  573. * 1. `before-tokenize`
  574. * 2. `after-tokenize`
  575. * 3. `wrap`: On each {@link Token}.
  576. *
  577. * @param {string} text A string with the code to be highlighted.
  578. * @param {Grammar} grammar An object containing the tokens to use.
  579. *
  580. * Usually a language definition like `Prism.languages.markup`.
  581. * @param {string} language The name of the language definition passed to `grammar`.
  582. * @returns {string} The highlighted HTML.
  583. * @memberof Prism
  584. * @public
  585. * @example
  586. * Prism.highlight('var foo = true;', Prism.languages.javascript, 'javascript');
  587. */
  588. highlight: function (text, grammar, language) {
  589. var env = {
  590. code: text,
  591. grammar: grammar,
  592. language: language
  593. };
  594. _.hooks.run('before-tokenize', env);
  595. if (!env.grammar) {
  596. throw new Error('The language "' + env.language + '" has no grammar.');
  597. }
  598. env.tokens = _.tokenize(env.code, env.grammar);
  599. _.hooks.run('after-tokenize', env);
  600. return Token.stringify(_.util.encode(env.tokens), env.language);
  601. },
  602. /**
  603. * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input
  604. * and the language definitions to use, and returns an array with the tokenized code.
  605. *
  606. * When the language definition includes nested tokens, the function is called recursively on each of these tokens.
  607. *
  608. * This method could be useful in other contexts as well, as a very crude parser.
  609. *
  610. * @param {string} text A string with the code to be highlighted.
  611. * @param {Grammar} grammar An object containing the tokens to use.
  612. *
  613. * Usually a language definition like `Prism.languages.markup`.
  614. * @returns {TokenStream} An array of strings and tokens, a token stream.
  615. * @memberof Prism
  616. * @public
  617. * @example
  618. * let code = `var foo = 0;`;
  619. * let tokens = Prism.tokenize(code, Prism.languages.javascript);
  620. * tokens.forEach(token => {
  621. * if (token instanceof Prism.Token && token.type === 'number') {
  622. * console.log(`Found numeric literal: ${token.content}`);
  623. * }
  624. * });
  625. */
  626. tokenize: function (text, grammar) {
  627. var rest = grammar.rest;
  628. if (rest) {
  629. for (var token in rest) {
  630. grammar[token] = rest[token];
  631. }
  632. delete grammar.rest;
  633. }
  634. var tokenList = new LinkedList();
  635. addAfter(tokenList, tokenList.head, text);
  636. matchGrammar(text, tokenList, grammar, tokenList.head, 0);
  637. return toArray(tokenList);
  638. },
  639. /**
  640. * @namespace
  641. * @memberof Prism
  642. * @public
  643. */
  644. hooks: {
  645. all: {},
  646. /**
  647. * Adds the given callback to the list of callbacks for the given hook.
  648. *
  649. * The callback will be invoked when the hook it is registered for is run.
  650. * Hooks are usually directly run by a highlight function but you can also run hooks yourself.
  651. *
  652. * One callback function can be registered to multiple hooks and the same hook multiple times.
  653. *
  654. * @param {string} name The name of the hook.
  655. * @param {HookCallback} callback The callback function which is given environment variables.
  656. * @public
  657. */
  658. add: function (name, callback) {
  659. var hooks = _.hooks.all;
  660. hooks[name] = hooks[name] || [];
  661. hooks[name].push(callback);
  662. },
  663. /**
  664. * Runs a hook invoking all registered callbacks with the given environment variables.
  665. *
  666. * Callbacks will be invoked synchronously and in the order in which they were registered.
  667. *
  668. * @param {string} name The name of the hook.
  669. * @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
  670. * @public
  671. */
  672. run: function (name, env) {
  673. var callbacks = _.hooks.all[name];
  674. if (!callbacks || !callbacks.length) {
  675. return;
  676. }
  677. for (var i = 0, callback; (callback = callbacks[i++]);) {
  678. callback(env);
  679. }
  680. }
  681. },
  682. Token: Token
  683. };
  684. _self.Prism = _;
  685. // Typescript note:
  686. // The following can be used to import the Token type in JSDoc:
  687. //
  688. // @typedef {InstanceType<import("./prism-core")["Token"]>} Token
  689. /**
  690. * Creates a new token.
  691. *
  692. * @param {string} type See {@link Token#type type}
  693. * @param {string | TokenStream} content See {@link Token#content content}
  694. * @param {string|string[]} [alias] The alias(es) of the token.
  695. * @param {string} [matchedStr=""] A copy of the full string this token was created from.
  696. * @class
  697. * @global
  698. * @public
  699. */
  700. function Token(type, content, alias, matchedStr) {
  701. /**
  702. * The type of the token.
  703. *
  704. * This is usually the key of a pattern in a {@link Grammar}.
  705. *
  706. * @type {string}
  707. * @see GrammarToken
  708. * @public
  709. */
  710. this.type = type;
  711. /**
  712. * The strings or tokens contained by this token.
  713. *
  714. * This will be a token stream if the pattern matched also defined an `inside` grammar.
  715. *
  716. * @type {string | TokenStream}
  717. * @public
  718. */
  719. this.content = content;
  720. /**
  721. * The alias(es) of the token.
  722. *
  723. * @type {string|string[]}
  724. * @see GrammarToken
  725. * @public
  726. */
  727. this.alias = alias;
  728. // Copy of the full string this token was created from
  729. this.length = (matchedStr || '').length | 0;
  730. }
  731. /**
  732. * A token stream is an array of strings and {@link Token Token} objects.
  733. *
  734. * Token streams have to fulfill a few properties that are assumed by most functions (mostly internal ones) that process
  735. * them.
  736. *
  737. * 1. No adjacent strings.
  738. * 2. No empty strings.
  739. *
  740. * The only exception here is the token stream that only contains the empty string and nothing else.
  741. *
  742. * @typedef {Array<string | Token>} TokenStream
  743. * @global
  744. * @public
  745. */
  746. /**
  747. * Converts the given token or token stream to an HTML representation.
  748. *
  749. * The following hooks will be run:
  750. * 1. `wrap`: On each {@link Token}.
  751. *
  752. * @param {string | Token | TokenStream} o The token or token stream to be converted.
  753. * @param {string} language The name of current language.
  754. * @returns {string} The HTML representation of the token or token stream.
  755. * @memberof Token
  756. * @static
  757. */
  758. Token.stringify = function stringify(o, language) {
  759. if (typeof o == 'string') {
  760. return o;
  761. }
  762. if (Array.isArray(o)) {
  763. var s = '';
  764. o.forEach(function (e) {
  765. s += stringify(e, language);
  766. });
  767. return s;
  768. }
  769. var env = {
  770. type: o.type,
  771. content: stringify(o.content, language),
  772. tag: 'span',
  773. classes: ['token', o.type],
  774. attributes: {},
  775. language: language
  776. };
  777. var aliases = o.alias;
  778. if (aliases) {
  779. if (Array.isArray(aliases)) {
  780. Array.prototype.push.apply(env.classes, aliases);
  781. } else {
  782. env.classes.push(aliases);
  783. }
  784. }
  785. _.hooks.run('wrap', env);
  786. var attributes = '';
  787. for (var name in env.attributes) {
  788. attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '&quot;') + '"';
  789. }
  790. return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
  791. };
  792. /**
  793. * @param {RegExp} pattern
  794. * @param {number} pos
  795. * @param {string} text
  796. * @param {boolean} lookbehind
  797. * @returns {RegExpExecArray | null}
  798. */
  799. function matchPattern(pattern, pos, text, lookbehind) {
  800. pattern.lastIndex = pos;
  801. var match = pattern.exec(text);
  802. if (match && lookbehind && match[1]) {
  803. // change the match to remove the text matched by the Prism lookbehind group
  804. var lookbehindLength = match[1].length;
  805. match.index += lookbehindLength;
  806. match[0] = match[0].slice(lookbehindLength);
  807. }
  808. return match;
  809. }
  810. /**
  811. * @param {string} text
  812. * @param {LinkedList<string | Token>} tokenList
  813. * @param {any} grammar
  814. * @param {LinkedListNode<string | Token>} startNode
  815. * @param {number} startPos
  816. * @param {RematchOptions} [rematch]
  817. * @returns {void}
  818. * @private
  819. *
  820. * @typedef RematchOptions
  821. * @property {string} cause
  822. * @property {number} reach
  823. */
  824. function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
  825. for (var token in grammar) {
  826. if (!grammar.hasOwnProperty(token) || !grammar[token]) {
  827. continue;
  828. }
  829. var patterns = grammar[token];
  830. patterns = Array.isArray(patterns) ? patterns : [patterns];
  831. for (var j = 0; j < patterns.length; ++j) {
  832. if (rematch && rematch.cause == token + ',' + j) {
  833. return;
  834. }
  835. var patternObj = patterns[j];
  836. var inside = patternObj.inside;
  837. var lookbehind = !!patternObj.lookbehind;
  838. var greedy = !!patternObj.greedy;
  839. var alias = patternObj.alias;
  840. if (greedy && !patternObj.pattern.global) {
  841. // Without the global flag, lastIndex won't work
  842. var flags = patternObj.pattern.toString().match(/[imsuy]*$/)[0];
  843. patternObj.pattern = RegExp(patternObj.pattern.source, flags + 'g');
  844. }
  845. /** @type {RegExp} */
  846. var pattern = patternObj.pattern || patternObj;
  847. for ( // iterate the token list and keep track of the current token/string position
  848. var currentNode = startNode.next, pos = startPos;
  849. currentNode !== tokenList.tail;
  850. pos += currentNode.value.length, currentNode = currentNode.next
  851. ) {
  852. if (rematch && pos >= rematch.reach) {
  853. break;
  854. }
  855. var str = currentNode.value;
  856. if (tokenList.length > text.length) {
  857. // Something went terribly wrong, ABORT, ABORT!
  858. return;
  859. }
  860. if (str instanceof Token) {
  861. continue;
  862. }
  863. var removeCount = 1; // this is the to parameter of removeBetween
  864. var match;
  865. if (greedy) {
  866. match = matchPattern(pattern, pos, text, lookbehind);
  867. if (!match || match.index >= text.length) {
  868. break;
  869. }
  870. var from = match.index;
  871. var to = match.index + match[0].length;
  872. var p = pos;
  873. // find the node that contains the match
  874. p += currentNode.value.length;
  875. while (from >= p) {
  876. currentNode = currentNode.next;
  877. p += currentNode.value.length;
  878. }
  879. // adjust pos (and p)
  880. p -= currentNode.value.length;
  881. pos = p;
  882. // the current node is a Token, then the match starts inside another Token, which is invalid
  883. if (currentNode.value instanceof Token) {
  884. continue;
  885. }
  886. // find the last node which is affected by this match
  887. for (
  888. var k = currentNode;
  889. k !== tokenList.tail && (p < to || typeof k.value === 'string');
  890. k = k.next
  891. ) {
  892. removeCount++;
  893. p += k.value.length;
  894. }
  895. removeCount--;
  896. // replace with the new match
  897. str = text.slice(pos, p);
  898. match.index -= pos;
  899. } else {
  900. match = matchPattern(pattern, 0, str, lookbehind);
  901. if (!match) {
  902. continue;
  903. }
  904. }
  905. // eslint-disable-next-line no-redeclare
  906. var from = match.index;
  907. var matchStr = match[0];
  908. var before = str.slice(0, from);
  909. var after = str.slice(from + matchStr.length);
  910. var reach = pos + str.length;
  911. if (rematch && reach > rematch.reach) {
  912. rematch.reach = reach;
  913. }
  914. var removeFrom = currentNode.prev;
  915. if (before) {
  916. removeFrom = addAfter(tokenList, removeFrom, before);
  917. pos += before.length;
  918. }
  919. removeRange(tokenList, removeFrom, removeCount);
  920. var wrapped = new Token(token, inside ? _.tokenize(matchStr, inside) : matchStr, alias, matchStr);
  921. currentNode = addAfter(tokenList, removeFrom, wrapped);
  922. if (after) {
  923. addAfter(tokenList, currentNode, after);
  924. }
  925. if (removeCount > 1) {
  926. // at least one Token object was removed, so we have to do some rematching
  927. // this can only happen if the current pattern is greedy
  928. /** @type {RematchOptions} */
  929. var nestedRematch = {
  930. cause: token + ',' + j,
  931. reach: reach
  932. };
  933. matchGrammar(text, tokenList, grammar, currentNode.prev, pos, nestedRematch);
  934. // the reach might have been extended because of the rematching
  935. if (rematch && nestedRematch.reach > rematch.reach) {
  936. rematch.reach = nestedRematch.reach;
  937. }
  938. }
  939. }
  940. }
  941. }
  942. }
  943. /**
  944. * @typedef LinkedListNode
  945. * @property {T} value
  946. * @property {LinkedListNode<T> | null} prev The previous node.
  947. * @property {LinkedListNode<T> | null} next The next node.
  948. * @template T
  949. * @private
  950. */
  951. /**
  952. * @template T
  953. * @private
  954. */
  955. function LinkedList() {
  956. /** @type {LinkedListNode<T>} */
  957. var head = { value: null, prev: null, next: null };
  958. /** @type {LinkedListNode<T>} */
  959. var tail = { value: null, prev: head, next: null };
  960. head.next = tail;
  961. /** @type {LinkedListNode<T>} */
  962. this.head = head;
  963. /** @type {LinkedListNode<T>} */
  964. this.tail = tail;
  965. this.length = 0;
  966. }
  967. /**
  968. * Adds a new node with the given value to the list.
  969. *
  970. * @param {LinkedList<T>} list
  971. * @param {LinkedListNode<T>} node
  972. * @param {T} value
  973. * @returns {LinkedListNode<T>} The added node.
  974. * @template T
  975. */
  976. function addAfter(list, node, value) {
  977. // assumes that node != list.tail && values.length >= 0
  978. var next = node.next;
  979. var newNode = { value: value, prev: node, next: next };
  980. node.next = newNode;
  981. next.prev = newNode;
  982. list.length++;
  983. return newNode;
  984. }
  985. /**
  986. * Removes `count` nodes after the given node. The given node will not be removed.
  987. *
  988. * @param {LinkedList<T>} list
  989. * @param {LinkedListNode<T>} node
  990. * @param {number} count
  991. * @template T
  992. */
  993. function removeRange(list, node, count) {
  994. var next = node.next;
  995. for (var i = 0; i < count && next !== list.tail; i++) {
  996. next = next.next;
  997. }
  998. node.next = next;
  999. next.prev = node;
  1000. list.length -= i;
  1001. }
  1002. /**
  1003. * @param {LinkedList<T>} list
  1004. * @returns {T[]}
  1005. * @template T
  1006. */
  1007. function toArray(list) {
  1008. var array = [];
  1009. var node = list.head.next;
  1010. while (node !== list.tail) {
  1011. array.push(node.value);
  1012. node = node.next;
  1013. }
  1014. return array;
  1015. }
  1016. if (!_self.document) {
  1017. if (!_self.addEventListener) {
  1018. // in Node.js
  1019. return _;
  1020. }
  1021. if (!_.disableWorkerMessageHandler) {
  1022. // In worker
  1023. _self.addEventListener('message', function (evt) {
  1024. var message = JSON.parse(evt.data);
  1025. var lang = message.language;
  1026. var code = message.code;
  1027. var immediateClose = message.immediateClose;
  1028. _self.postMessage(_.highlight(code, _.languages[lang], lang));
  1029. if (immediateClose) {
  1030. _self.close();
  1031. }
  1032. }, false);
  1033. }
  1034. return _;
  1035. }
  1036. // Get current script and highlight
  1037. var script = _.util.currentScript();
  1038. if (script) {
  1039. _.filename = script.src;
  1040. if (script.hasAttribute('data-manual')) {
  1041. _.manual = true;
  1042. }
  1043. }
  1044. function highlightAutomaticallyCallback() {
  1045. if (!_.manual) {
  1046. _.highlightAll();
  1047. }
  1048. }
  1049. if (!_.manual) {
  1050. // If the document state is "loading", then we'll use DOMContentLoaded.
  1051. // If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
  1052. // DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
  1053. // might take longer one animation frame to execute which can create a race condition where only some plugins have
  1054. // been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
  1055. // See https://github.com/PrismJS/prism/issues/2102
  1056. var readyState = document.readyState;
  1057. if (readyState === 'loading' || readyState === 'interactive' && script && script.defer) {
  1058. document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
  1059. } else {
  1060. if (window.requestAnimationFrame) {
  1061. window.requestAnimationFrame(highlightAutomaticallyCallback);
  1062. } else {
  1063. window.setTimeout(highlightAutomaticallyCallback, 16);
  1064. }
  1065. }
  1066. }
  1067. return _;
  1068. }(_self));
  1069. if (typeof module !== 'undefined' && module.exports) {
  1070. module.exports = Prism;
  1071. }
  1072. // hack for components to work correctly in node.js
  1073. if (typeof global !== 'undefined') {
  1074. global.Prism = Prism;
  1075. }
  1076. // some additional documentation/types
  1077. /**
  1078. * The expansion of a simple `RegExp` literal to support additional properties.
  1079. *
  1080. * @typedef GrammarToken
  1081. * @property {RegExp} pattern The regular expression of the token.
  1082. * @property {boolean} [lookbehind=false] If `true`, then the first capturing group of `pattern` will (effectively)
  1083. * behave as a lookbehind group meaning that the captured text will not be part of the matched text of the new token.
  1084. * @property {boolean} [greedy=false] Whether the token is greedy.
  1085. * @property {string|string[]} [alias] An optional alias or list of aliases.
  1086. * @property {Grammar} [inside] The nested grammar of this token.
  1087. *
  1088. * The `inside` grammar will be used to tokenize the text value of each token of this kind.
  1089. *
  1090. * This can be used to make nested and even recursive language definitions.
  1091. *
  1092. * Note: This can cause infinite recursion. Be careful when you embed different languages or even the same language into
  1093. * each another.
  1094. * @global
  1095. * @public
  1096. */
  1097. /**
  1098. * @typedef Grammar
  1099. * @type {Object<string, RegExp | GrammarToken | Array<RegExp | GrammarToken>>}
  1100. * @property {Grammar} [rest] An optional grammar object that will be appended to this grammar.
  1101. * @global
  1102. * @public
  1103. */
  1104. /**
  1105. * A function which will invoked after an element was successfully highlighted.
  1106. *
  1107. * @callback HighlightCallback
  1108. * @param {Element} element The element successfully highlighted.
  1109. * @returns {void}
  1110. * @global
  1111. * @public
  1112. */
  1113. /**
  1114. * @callback HookCallback
  1115. * @param {Object<string, any>} env The environment variables of the hook.
  1116. * @returns {void}
  1117. * @global
  1118. * @public
  1119. */