UNPKG

3.98 kBJavaScriptView Raw
1(function () {
2 if (typeof self === 'undefined' || !self.Prism || !self.document) {
3 return;
4 }
5
6 var Prism = window.Prism;
7
8 var LOADING_MESSAGE = 'Loading…';
9 var FAILURE_MESSAGE = function (status, message) {
10 return '✖ Error ' + status + ' while fetching file: ' + message;
11 };
12 var FAILURE_EMPTY_MESSAGE = '✖ Error: File does not exist or is empty';
13
14 var EXTENSIONS = {
15 'js': 'javascript',
16 'py': 'python',
17 'rb': 'ruby',
18 'ps1': 'powershell',
19 'psm1': 'powershell',
20 'sh': 'bash',
21 'bat': 'batch',
22 'h': 'c',
23 'tex': 'latex'
24 };
25
26 var STATUS_ATTR = 'data-src-status';
27 var STATUS_LOADING = 'loading';
28 var STATUS_LOADED = 'loaded';
29 var STATUS_FAILED = 'failed';
30
31 var SELECTOR = 'pre[data-src]:not([' + STATUS_ATTR + '="' + STATUS_LOADED + '"])'
32 + ':not([' + STATUS_ATTR + '="' + STATUS_LOADING + '"])';
33
34 var lang = /\blang(?:uage)?-([\w-]+)\b/i;
35
36 /**
37 * Sets the Prism `language-xxxx` or `lang-xxxx` class to the given language.
38 *
39 * @param {HTMLElement} element
40 * @param {string} language
41 * @returns {void}
42 */
43 function setLanguageClass(element, language) {
44 var className = element.className;
45 className = className.replace(lang, ' ') + ' language-' + language;
46 element.className = className.replace(/\s+/g, ' ').trim();
47 }
48
49
50 Prism.hooks.add('before-highlightall', function (env) {
51 env.selector += ', ' + SELECTOR;
52 });
53
54 Prism.hooks.add('before-sanity-check', function (env) {
55 var pre = /** @type {HTMLPreElement} */ (env.element);
56 if (pre.matches(SELECTOR)) {
57 env.code = ''; // fast-path the whole thing and go to complete
58
59 pre.setAttribute(STATUS_ATTR, STATUS_LOADING); // mark as loading
60
61 // add code element with loading message
62 var code = pre.appendChild(document.createElement('CODE'));
63 code.textContent = LOADING_MESSAGE;
64
65 var src = pre.getAttribute('data-src');
66
67 var language = env.language;
68 if (language === 'none') {
69 // the language might be 'none' because there is no language set;
70 // in this case, we want to use the extension as the language
71 var extension = (/\.(\w+)$/.exec(src) || [, 'none'])[1];
72 language = EXTENSIONS[extension] || extension;
73 }
74
75 // set language classes
76 setLanguageClass(code, language);
77 setLanguageClass(pre, language);
78
79 // preload the language
80 var autoloader = Prism.plugins.autoloader;
81 if (autoloader) {
82 autoloader.loadLanguages(language);
83 }
84
85 // load file
86 var xhr = new XMLHttpRequest();
87 xhr.open('GET', src, true);
88 xhr.onreadystatechange = function () {
89 if (xhr.readyState == 4) {
90 if (xhr.status < 400 && xhr.responseText) {
91 // mark as loaded
92 pre.setAttribute(STATUS_ATTR, STATUS_LOADED);
93
94 // highlight code
95 code.textContent = xhr.responseText;
96 Prism.highlightElement(code);
97
98 } else {
99 // mark as failed
100 pre.setAttribute(STATUS_ATTR, STATUS_FAILED);
101
102 if (xhr.status >= 400) {
103 code.textContent = FAILURE_MESSAGE(xhr.status, xhr.statusText);
104 } else {
105 code.textContent = FAILURE_EMPTY_MESSAGE;
106 }
107 }
108 }
109 };
110 xhr.send(null);
111 }
112 });
113
114 Prism.plugins.fileHighlight = {
115 /**
116 * Executes the File Highlight plugin for all matching `pre` elements under the given container.
117 *
118 * Note: Elements which are already loaded or currently loading will not be touched by this method.
119 *
120 * @param {ParentNode} [container=document]
121 */
122 highlight: function highlight(container) {
123 var elements = (container || document).querySelectorAll(SELECTOR);
124
125 for (var i = 0, element; element = elements[i++];) {
126 Prism.highlightElement(element);
127 }
128 }
129 };
130
131 var logged = false;
132 /** @deprecated Use `Prism.plugins.fileHighlight.highlight` instead. */
133 Prism.fileHighlight = function () {
134 if (!logged) {
135 console.warn('Prism.fileHighlight is deprecated. Use `Prism.plugins.fileHighlight.highlight` instead.');
136 logged = true;
137 }
138 Prism.plugins.fileHighlight.highlight.apply(this, arguments);
139 }
140
141})();