UNPKG

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