prism-autoloader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. (function () {
  2. if (typeof Prism === 'undefined' || typeof document === 'undefined') {
  3. return;
  4. }
  5. /* eslint-disable */
  6. /**
  7. * The dependencies map is built automatically with gulp.
  8. *
  9. * @type {Object<string, string | string[]>}
  10. */
  11. var lang_dependencies = /*dependencies_placeholder[*/{
  12. "javascript": "clike",
  13. "actionscript": "javascript",
  14. "apex": [
  15. "clike",
  16. "sql"
  17. ],
  18. "arduino": "cpp",
  19. "aspnet": [
  20. "markup",
  21. "csharp"
  22. ],
  23. "birb": "clike",
  24. "bison": "c",
  25. "c": "clike",
  26. "csharp": "clike",
  27. "cpp": "c",
  28. "cfscript": "clike",
  29. "chaiscript": [
  30. "clike",
  31. "cpp"
  32. ],
  33. "cilkc": "c",
  34. "cilkcpp": "cpp",
  35. "coffeescript": "javascript",
  36. "crystal": "ruby",
  37. "css-extras": "css",
  38. "d": "clike",
  39. "dart": "clike",
  40. "django": "markup-templating",
  41. "ejs": [
  42. "javascript",
  43. "markup-templating"
  44. ],
  45. "etlua": [
  46. "lua",
  47. "markup-templating"
  48. ],
  49. "erb": [
  50. "ruby",
  51. "markup-templating"
  52. ],
  53. "fsharp": "clike",
  54. "firestore-security-rules": "clike",
  55. "flow": "javascript",
  56. "ftl": "markup-templating",
  57. "gml": "clike",
  58. "glsl": "c",
  59. "go": "clike",
  60. "gradle": "clike",
  61. "groovy": "clike",
  62. "haml": "ruby",
  63. "handlebars": "markup-templating",
  64. "haxe": "clike",
  65. "hlsl": "c",
  66. "idris": "haskell",
  67. "java": "clike",
  68. "javadoc": [
  69. "markup",
  70. "java",
  71. "javadoclike"
  72. ],
  73. "jolie": "clike",
  74. "jsdoc": [
  75. "javascript",
  76. "javadoclike",
  77. "typescript"
  78. ],
  79. "js-extras": "javascript",
  80. "json5": "json",
  81. "jsonp": "json",
  82. "js-templates": "javascript",
  83. "kotlin": "clike",
  84. "latte": [
  85. "clike",
  86. "markup-templating",
  87. "php"
  88. ],
  89. "less": "css",
  90. "lilypond": "scheme",
  91. "liquid": "markup-templating",
  92. "markdown": "markup",
  93. "markup-templating": "markup",
  94. "mongodb": "javascript",
  95. "n4js": "javascript",
  96. "objectivec": "c",
  97. "opencl": "c",
  98. "parser": "markup",
  99. "php": "markup-templating",
  100. "phpdoc": [
  101. "php",
  102. "javadoclike"
  103. ],
  104. "php-extras": "php",
  105. "plsql": "sql",
  106. "processing": "clike",
  107. "protobuf": "clike",
  108. "pug": [
  109. "markup",
  110. "javascript"
  111. ],
  112. "purebasic": "clike",
  113. "purescript": "haskell",
  114. "qsharp": "clike",
  115. "qml": "javascript",
  116. "qore": "clike",
  117. "racket": "scheme",
  118. "cshtml": [
  119. "markup",
  120. "csharp"
  121. ],
  122. "jsx": [
  123. "markup",
  124. "javascript"
  125. ],
  126. "tsx": [
  127. "jsx",
  128. "typescript"
  129. ],
  130. "reason": "clike",
  131. "ruby": "clike",
  132. "sass": "css",
  133. "scss": "css",
  134. "scala": "java",
  135. "shell-session": "bash",
  136. "smarty": "markup-templating",
  137. "solidity": "clike",
  138. "soy": "markup-templating",
  139. "sparql": "turtle",
  140. "sqf": "clike",
  141. "squirrel": "clike",
  142. "stata": [
  143. "mata",
  144. "java",
  145. "python"
  146. ],
  147. "t4-cs": [
  148. "t4-templating",
  149. "csharp"
  150. ],
  151. "t4-vb": [
  152. "t4-templating",
  153. "vbnet"
  154. ],
  155. "tap": "yaml",
  156. "tt2": [
  157. "clike",
  158. "markup-templating"
  159. ],
  160. "textile": "markup",
  161. "twig": "markup-templating",
  162. "typescript": "javascript",
  163. "v": "clike",
  164. "vala": "clike",
  165. "vbnet": "basic",
  166. "velocity": "markup",
  167. "wiki": "markup",
  168. "xeora": "markup",
  169. "xml-doc": "markup",
  170. "xquery": "markup"
  171. }/*]*/;
  172. var lang_aliases = /*aliases_placeholder[*/{
  173. "html": "markup",
  174. "xml": "markup",
  175. "svg": "markup",
  176. "mathml": "markup",
  177. "ssml": "markup",
  178. "atom": "markup",
  179. "rss": "markup",
  180. "js": "javascript",
  181. "g4": "antlr4",
  182. "ino": "arduino",
  183. "arm-asm": "armasm",
  184. "art": "arturo",
  185. "adoc": "asciidoc",
  186. "avs": "avisynth",
  187. "avdl": "avro-idl",
  188. "gawk": "awk",
  189. "sh": "bash",
  190. "shell": "bash",
  191. "shortcode": "bbcode",
  192. "rbnf": "bnf",
  193. "oscript": "bsl",
  194. "cs": "csharp",
  195. "dotnet": "csharp",
  196. "cfc": "cfscript",
  197. "cilk-c": "cilkc",
  198. "cilk-cpp": "cilkcpp",
  199. "cilk": "cilkcpp",
  200. "coffee": "coffeescript",
  201. "conc": "concurnas",
  202. "jinja2": "django",
  203. "dns-zone": "dns-zone-file",
  204. "dockerfile": "docker",
  205. "gv": "dot",
  206. "eta": "ejs",
  207. "xlsx": "excel-formula",
  208. "xls": "excel-formula",
  209. "gamemakerlanguage": "gml",
  210. "po": "gettext",
  211. "gni": "gn",
  212. "ld": "linker-script",
  213. "go-mod": "go-module",
  214. "hbs": "handlebars",
  215. "mustache": "handlebars",
  216. "hs": "haskell",
  217. "idr": "idris",
  218. "gitignore": "ignore",
  219. "hgignore": "ignore",
  220. "npmignore": "ignore",
  221. "webmanifest": "json",
  222. "kt": "kotlin",
  223. "kts": "kotlin",
  224. "kum": "kumir",
  225. "tex": "latex",
  226. "context": "latex",
  227. "ly": "lilypond",
  228. "emacs": "lisp",
  229. "elisp": "lisp",
  230. "emacs-lisp": "lisp",
  231. "md": "markdown",
  232. "moon": "moonscript",
  233. "n4jsd": "n4js",
  234. "nani": "naniscript",
  235. "objc": "objectivec",
  236. "qasm": "openqasm",
  237. "objectpascal": "pascal",
  238. "px": "pcaxis",
  239. "pcode": "peoplecode",
  240. "plantuml": "plant-uml",
  241. "pq": "powerquery",
  242. "mscript": "powerquery",
  243. "pbfasm": "purebasic",
  244. "purs": "purescript",
  245. "py": "python",
  246. "qs": "qsharp",
  247. "rkt": "racket",
  248. "razor": "cshtml",
  249. "rpy": "renpy",
  250. "res": "rescript",
  251. "robot": "robotframework",
  252. "rb": "ruby",
  253. "sh-session": "shell-session",
  254. "shellsession": "shell-session",
  255. "smlnj": "sml",
  256. "sol": "solidity",
  257. "sln": "solution-file",
  258. "rq": "sparql",
  259. "sclang": "supercollider",
  260. "t4": "t4-cs",
  261. "trickle": "tremor",
  262. "troy": "tremor",
  263. "trig": "turtle",
  264. "ts": "typescript",
  265. "tsconfig": "typoscript",
  266. "uscript": "unrealscript",
  267. "uc": "unrealscript",
  268. "url": "uri",
  269. "vb": "visual-basic",
  270. "vba": "visual-basic",
  271. "webidl": "web-idl",
  272. "mathematica": "wolfram",
  273. "nb": "wolfram",
  274. "wl": "wolfram",
  275. "xeoracube": "xeora",
  276. "yml": "yaml"
  277. }/*]*/;
  278. /* eslint-enable */
  279. /**
  280. * @typedef LangDataItem
  281. * @property {{ success?: () => void, error?: () => void }[]} callbacks
  282. * @property {boolean} [error]
  283. * @property {boolean} [loading]
  284. */
  285. /** @type {Object<string, LangDataItem>} */
  286. var lang_data = {};
  287. var ignored_language = 'none';
  288. var languages_path = 'components/';
  289. var script = Prism.util.currentScript();
  290. if (script) {
  291. var autoloaderFile = /\bplugins\/autoloader\/prism-autoloader\.(?:min\.)?js(?:\?[^\r\n/]*)?$/i;
  292. var prismFile = /(^|\/)[\w-]+\.(?:min\.)?js(?:\?[^\r\n/]*)?$/i;
  293. var autoloaderPath = script.getAttribute('data-autoloader-path');
  294. if (autoloaderPath != null) {
  295. // data-autoloader-path is set, so just use it
  296. languages_path = autoloaderPath.trim().replace(/\/?$/, '/');
  297. } else {
  298. var src = script.src;
  299. if (autoloaderFile.test(src)) {
  300. // the script is the original autoloader script in the usual Prism project structure
  301. languages_path = src.replace(autoloaderFile, 'components/');
  302. } else if (prismFile.test(src)) {
  303. // the script is part of a bundle like a custom prism.js from the download page
  304. languages_path = src.replace(prismFile, '$1components/');
  305. }
  306. }
  307. }
  308. var config = Prism.plugins.autoloader = {
  309. languages_path: languages_path,
  310. use_minified: true,
  311. loadLanguages: loadLanguages
  312. };
  313. /**
  314. * Lazily loads an external script.
  315. *
  316. * @param {string} src
  317. * @param {() => void} [success]
  318. * @param {() => void} [error]
  319. */
  320. function addScript(src, success, error) {
  321. var s = document.createElement('script');
  322. s.src = src;
  323. s.async = true;
  324. s.onload = function () {
  325. document.body.removeChild(s);
  326. success && success();
  327. };
  328. s.onerror = function () {
  329. document.body.removeChild(s);
  330. error && error();
  331. };
  332. document.body.appendChild(s);
  333. }
  334. /**
  335. * Returns all additional dependencies of the given element defined by the `data-dependencies` attribute.
  336. *
  337. * @param {Element} element
  338. * @returns {string[]}
  339. */
  340. function getDependencies(element) {
  341. var deps = (element.getAttribute('data-dependencies') || '').trim();
  342. if (!deps) {
  343. var parent = element.parentElement;
  344. if (parent && parent.tagName.toLowerCase() === 'pre') {
  345. deps = (parent.getAttribute('data-dependencies') || '').trim();
  346. }
  347. }
  348. return deps ? deps.split(/\s*,\s*/g) : [];
  349. }
  350. /**
  351. * Returns whether the given language is currently loaded.
  352. *
  353. * @param {string} lang
  354. * @returns {boolean}
  355. */
  356. function isLoaded(lang) {
  357. if (lang.indexOf('!') >= 0) {
  358. // forced reload
  359. return false;
  360. }
  361. lang = lang_aliases[lang] || lang; // resolve alias
  362. if (lang in Prism.languages) {
  363. // the given language is already loaded
  364. return true;
  365. }
  366. // this will catch extensions like CSS extras that don't add a grammar to Prism.languages
  367. var data = lang_data[lang];
  368. return data && !data.error && data.loading === false;
  369. }
  370. /**
  371. * Returns the path to a grammar, using the language_path and use_minified config keys.
  372. *
  373. * @param {string} lang
  374. * @returns {string}
  375. */
  376. function getLanguagePath(lang) {
  377. return config.languages_path + 'prism-' + lang + (config.use_minified ? '.min' : '') + '.js';
  378. }
  379. /**
  380. * Loads all given grammars concurrently.
  381. *
  382. * @param {string[]|string} languages
  383. * @param {(languages: string[]) => void} [success]
  384. * @param {(language: string) => void} [error] This callback will be invoked on the first language to fail.
  385. */
  386. function loadLanguages(languages, success, error) {
  387. if (typeof languages === 'string') {
  388. languages = [languages];
  389. }
  390. var total = languages.length;
  391. var completed = 0;
  392. var failed = false;
  393. if (total === 0) {
  394. if (success) {
  395. setTimeout(success, 0);
  396. }
  397. return;
  398. }
  399. function successCallback() {
  400. if (failed) {
  401. return;
  402. }
  403. completed++;
  404. if (completed === total) {
  405. success && success(languages);
  406. }
  407. }
  408. languages.forEach(function (lang) {
  409. loadLanguage(lang, successCallback, function () {
  410. if (failed) {
  411. return;
  412. }
  413. failed = true;
  414. error && error(lang);
  415. });
  416. });
  417. }
  418. /**
  419. * Loads a grammar with its dependencies.
  420. *
  421. * @param {string} lang
  422. * @param {() => void} [success]
  423. * @param {() => void} [error]
  424. */
  425. function loadLanguage(lang, success, error) {
  426. var force = lang.indexOf('!') >= 0;
  427. lang = lang.replace('!', '');
  428. lang = lang_aliases[lang] || lang;
  429. function load() {
  430. var data = lang_data[lang];
  431. if (!data) {
  432. data = lang_data[lang] = {
  433. callbacks: []
  434. };
  435. }
  436. data.callbacks.push({
  437. success: success,
  438. error: error
  439. });
  440. if (!force && isLoaded(lang)) {
  441. // the language is already loaded and we aren't forced to reload
  442. languageCallback(lang, 'success');
  443. } else if (!force && data.error) {
  444. // the language failed to load before and we don't reload
  445. languageCallback(lang, 'error');
  446. } else if (force || !data.loading) {
  447. // the language isn't currently loading and/or we are forced to reload
  448. data.loading = true;
  449. data.error = false;
  450. addScript(getLanguagePath(lang), function () {
  451. data.loading = false;
  452. languageCallback(lang, 'success');
  453. }, function () {
  454. data.loading = false;
  455. data.error = true;
  456. languageCallback(lang, 'error');
  457. });
  458. }
  459. }
  460. var dependencies = lang_dependencies[lang];
  461. if (dependencies && dependencies.length) {
  462. loadLanguages(dependencies, load, error);
  463. } else {
  464. load();
  465. }
  466. }
  467. /**
  468. * Runs all callbacks of the given type for the given language.
  469. *
  470. * @param {string} lang
  471. * @param {"success" | "error"} type
  472. */
  473. function languageCallback(lang, type) {
  474. if (lang_data[lang]) {
  475. var callbacks = lang_data[lang].callbacks;
  476. for (var i = 0, l = callbacks.length; i < l; i++) {
  477. var callback = callbacks[i][type];
  478. if (callback) {
  479. setTimeout(callback, 0);
  480. }
  481. }
  482. callbacks.length = 0;
  483. }
  484. }
  485. Prism.hooks.add('complete', function (env) {
  486. var element = env.element;
  487. var language = env.language;
  488. if (!element || !language || language === ignored_language) {
  489. return;
  490. }
  491. var deps = getDependencies(element);
  492. if (/^diff-./i.test(language)) {
  493. // the "diff-xxxx" format is used by the Diff Highlight plugin
  494. deps.push('diff');
  495. deps.push(language.substr('diff-'.length));
  496. } else {
  497. deps.push(language);
  498. }
  499. if (!deps.every(isLoaded)) {
  500. // the language or some dependencies aren't loaded
  501. loadLanguages(deps, function () {
  502. Prism.highlightElement(element);
  503. });
  504. }
  505. });
  506. }());