UNPKG

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