Locally add prismjs
This commit is contained in:
parent
35af792c6a
commit
6aa044cf9b
701 changed files with 35787 additions and 0 deletions
195
node_modules/prismjs/plugins/file-highlight/prism-file-highlight.js
generated
vendored
Normal file
195
node_modules/prismjs/plugins/file-highlight/prism-file-highlight.js
generated
vendored
Normal file
|
@ -0,0 +1,195 @@
|
|||
(function () {
|
||||
|
||||
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
|
||||
if (!Element.prototype.matches) {
|
||||
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
|
||||
}
|
||||
|
||||
var LOADING_MESSAGE = 'Loading…';
|
||||
var FAILURE_MESSAGE = function (status, message) {
|
||||
return '✖ Error ' + status + ' while fetching file: ' + message;
|
||||
};
|
||||
var FAILURE_EMPTY_MESSAGE = '✖ Error: File does not exist or is empty';
|
||||
|
||||
var EXTENSIONS = {
|
||||
'js': 'javascript',
|
||||
'py': 'python',
|
||||
'rb': 'ruby',
|
||||
'ps1': 'powershell',
|
||||
'psm1': 'powershell',
|
||||
'sh': 'bash',
|
||||
'bat': 'batch',
|
||||
'h': 'c',
|
||||
'tex': 'latex'
|
||||
};
|
||||
|
||||
var STATUS_ATTR = 'data-src-status';
|
||||
var STATUS_LOADING = 'loading';
|
||||
var STATUS_LOADED = 'loaded';
|
||||
var STATUS_FAILED = 'failed';
|
||||
|
||||
var SELECTOR = 'pre[data-src]:not([' + STATUS_ATTR + '="' + STATUS_LOADED + '"])'
|
||||
+ ':not([' + STATUS_ATTR + '="' + STATUS_LOADING + '"])';
|
||||
|
||||
/**
|
||||
* Loads the given file.
|
||||
*
|
||||
* @param {string} src The URL or path of the source file to load.
|
||||
* @param {(result: string) => void} success
|
||||
* @param {(reason: string) => void} error
|
||||
*/
|
||||
function loadFile(src, success, error) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', src, true);
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4) {
|
||||
if (xhr.status < 400 && xhr.responseText) {
|
||||
success(xhr.responseText);
|
||||
} else {
|
||||
if (xhr.status >= 400) {
|
||||
error(FAILURE_MESSAGE(xhr.status, xhr.statusText));
|
||||
} else {
|
||||
error(FAILURE_EMPTY_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.send(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given range.
|
||||
*
|
||||
* This returns a range with inclusive ends.
|
||||
*
|
||||
* @param {string | null | undefined} range
|
||||
* @returns {[number, number | undefined] | undefined}
|
||||
*/
|
||||
function parseRange(range) {
|
||||
var m = /^\s*(\d+)\s*(?:(,)\s*(?:(\d+)\s*)?)?$/.exec(range || '');
|
||||
if (m) {
|
||||
var start = Number(m[1]);
|
||||
var comma = m[2];
|
||||
var end = m[3];
|
||||
|
||||
if (!comma) {
|
||||
return [start, start];
|
||||
}
|
||||
if (!end) {
|
||||
return [start, undefined];
|
||||
}
|
||||
return [start, Number(end)];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Prism.hooks.add('before-highlightall', function (env) {
|
||||
env.selector += ', ' + SELECTOR;
|
||||
});
|
||||
|
||||
Prism.hooks.add('before-sanity-check', function (env) {
|
||||
var pre = /** @type {HTMLPreElement} */ (env.element);
|
||||
if (pre.matches(SELECTOR)) {
|
||||
env.code = ''; // fast-path the whole thing and go to complete
|
||||
|
||||
pre.setAttribute(STATUS_ATTR, STATUS_LOADING); // mark as loading
|
||||
|
||||
// add code element with loading message
|
||||
var code = pre.appendChild(document.createElement('CODE'));
|
||||
code.textContent = LOADING_MESSAGE;
|
||||
|
||||
var src = pre.getAttribute('data-src');
|
||||
|
||||
var language = env.language;
|
||||
if (language === 'none') {
|
||||
// the language might be 'none' because there is no language set;
|
||||
// in this case, we want to use the extension as the language
|
||||
var extension = (/\.(\w+)$/.exec(src) || [, 'none'])[1];
|
||||
language = EXTENSIONS[extension] || extension;
|
||||
}
|
||||
|
||||
// set language classes
|
||||
Prism.util.setLanguage(code, language);
|
||||
Prism.util.setLanguage(pre, language);
|
||||
|
||||
// preload the language
|
||||
var autoloader = Prism.plugins.autoloader;
|
||||
if (autoloader) {
|
||||
autoloader.loadLanguages(language);
|
||||
}
|
||||
|
||||
// load file
|
||||
loadFile(
|
||||
src,
|
||||
function (text) {
|
||||
// mark as loaded
|
||||
pre.setAttribute(STATUS_ATTR, STATUS_LOADED);
|
||||
|
||||
// handle data-range
|
||||
var range = parseRange(pre.getAttribute('data-range'));
|
||||
if (range) {
|
||||
var lines = text.split(/\r\n?|\n/g);
|
||||
|
||||
// the range is one-based and inclusive on both ends
|
||||
var start = range[0];
|
||||
var end = range[1] == null ? lines.length : range[1];
|
||||
|
||||
if (start < 0) { start += lines.length; }
|
||||
start = Math.max(0, Math.min(start - 1, lines.length));
|
||||
if (end < 0) { end += lines.length; }
|
||||
end = Math.max(0, Math.min(end, lines.length));
|
||||
|
||||
text = lines.slice(start, end).join('\n');
|
||||
|
||||
// add data-start for line numbers
|
||||
if (!pre.hasAttribute('data-start')) {
|
||||
pre.setAttribute('data-start', String(start + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// highlight code
|
||||
code.textContent = text;
|
||||
Prism.highlightElement(code);
|
||||
},
|
||||
function (error) {
|
||||
// mark as failed
|
||||
pre.setAttribute(STATUS_ATTR, STATUS_FAILED);
|
||||
|
||||
code.textContent = error;
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Prism.plugins.fileHighlight = {
|
||||
/**
|
||||
* Executes the File Highlight plugin for all matching `pre` elements under the given container.
|
||||
*
|
||||
* Note: Elements which are already loaded or currently loading will not be touched by this method.
|
||||
*
|
||||
* @param {ParentNode} [container=document]
|
||||
*/
|
||||
highlight: function highlight(container) {
|
||||
var elements = (container || document).querySelectorAll(SELECTOR);
|
||||
|
||||
for (var i = 0, element; (element = elements[i++]);) {
|
||||
Prism.highlightElement(element);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var logged = false;
|
||||
/** @deprecated Use `Prism.plugins.fileHighlight.highlight` instead. */
|
||||
Prism.fileHighlight = function () {
|
||||
if (!logged) {
|
||||
console.warn('Prism.fileHighlight is deprecated. Use `Prism.plugins.fileHighlight.highlight` instead.');
|
||||
logged = true;
|
||||
}
|
||||
Prism.plugins.fileHighlight.highlight.apply(this, arguments);
|
||||
};
|
||||
|
||||
}());
|
1
node_modules/prismjs/plugins/file-highlight/prism-file-highlight.min.js
generated
vendored
Normal file
1
node_modules/prismjs/plugins/file-highlight/prism-file-highlight.min.js
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
!function(){if("undefined"!=typeof Prism&&"undefined"!=typeof document){Element.prototype.matches||(Element.prototype.matches=Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector);var t={js:"javascript",py:"python",rb:"ruby",ps1:"powershell",psm1:"powershell",sh:"bash",bat:"batch",h:"c",tex:"latex"},e="data-src-status",i='pre[data-src]:not([data-src-status="loaded"]):not([data-src-status="loading"])';Prism.hooks.add("before-highlightall",(function(t){t.selector+=", "+i})),Prism.hooks.add("before-sanity-check",(function(a){var n=a.element;if(n.matches(i)){a.code="",n.setAttribute(e,"loading");var s=n.appendChild(document.createElement("CODE"));s.textContent="Loading…";var r=n.getAttribute("data-src"),l=a.language;if("none"===l){var o=(/\.(\w+)$/.exec(r)||[,"none"])[1];l=t[o]||o}Prism.util.setLanguage(s,l),Prism.util.setLanguage(n,l);var h=Prism.plugins.autoloader;h&&h.loadLanguages(l),function(t,i,a){var r=new XMLHttpRequest;r.open("GET",t,!0),r.onreadystatechange=function(){4==r.readyState&&(r.status<400&&r.responseText?function(t){n.setAttribute(e,"loaded");var i=function(t){var e=/^\s*(\d+)\s*(?:(,)\s*(?:(\d+)\s*)?)?$/.exec(t||"");if(e){var i=Number(e[1]),a=e[2],n=e[3];return a?n?[i,Number(n)]:[i,void 0]:[i,i]}}(n.getAttribute("data-range"));if(i){var a=t.split(/\r\n?|\n/g),r=i[0],l=null==i[1]?a.length:i[1];r<0&&(r+=a.length),r=Math.max(0,Math.min(r-1,a.length)),l<0&&(l+=a.length),l=Math.max(0,Math.min(l,a.length)),t=a.slice(r,l).join("\n"),n.hasAttribute("data-start")||n.setAttribute("data-start",String(r+1))}s.textContent=t,Prism.highlightElement(s)}(r.responseText):r.status>=400?a("✖ Error "+r.status+" while fetching file: "+r.statusText):a("✖ Error: File does not exist or is empty"))},r.send(null)}(r,0,(function(t){n.setAttribute(e,"failed"),s.textContent=t}))}})),Prism.plugins.fileHighlight={highlight:function(t){for(var e,a=(t||document).querySelectorAll(i),n=0;e=a[n++];)Prism.highlightElement(e)}};var a=!1;Prism.fileHighlight=function(){a||(console.warn("Prism.fileHighlight is deprecated. Use `Prism.plugins.fileHighlight.highlight` instead."),a=!0),Prism.plugins.fileHighlight.highlight.apply(this,arguments)}}}();
|
Loading…
Add table
Add a link
Reference in a new issue