Locally add prismjs

This commit is contained in:
Aroy-Art 2024-11-26 21:16:11 +01:00
parent 35af792c6a
commit 6aa044cf9b
Signed by: Aroy
GPG key ID: 583642324A1D2070
701 changed files with 35787 additions and 0 deletions

View file

@ -0,0 +1,126 @@
(function () {
if (typeof Prism === 'undefined' || typeof document === 'undefined' || !document.createRange) {
return;
}
Prism.plugins.KeepMarkup = true;
Prism.hooks.add('before-highlight', function (env) {
if (!env.element.children.length) {
return;
}
if (!Prism.util.isActive(env.element, 'keep-markup', true)) {
return;
}
var dropTokens = Prism.util.isActive(env.element, 'drop-tokens', false);
/**
* Returns whether the given element should be kept.
*
* @param {HTMLElement} element
* @returns {boolean}
*/
function shouldKeep(element) {
if (dropTokens && element.nodeName.toLowerCase() === 'span' && element.classList.contains('token')) {
return false;
}
return true;
}
var pos = 0;
var data = [];
function processElement(element) {
if (!shouldKeep(element)) {
// don't keep this element and just process its children
processChildren(element);
return;
}
var o = {
// Store original element so we can restore it after highlighting
element: element,
posOpen: pos
};
data.push(o);
processChildren(element);
o.posClose = pos;
}
function processChildren(element) {
for (var i = 0, l = element.childNodes.length; i < l; i++) {
var child = element.childNodes[i];
if (child.nodeType === 1) { // element
processElement(child);
} else if (child.nodeType === 3) { // text
pos += child.data.length;
}
}
}
processChildren(env.element);
if (data.length) {
// data is an array of all existing tags
env.keepMarkup = data;
}
});
Prism.hooks.add('after-highlight', function (env) {
if (env.keepMarkup && env.keepMarkup.length) {
var walk = function (elt, nodeState) {
for (var i = 0, l = elt.childNodes.length; i < l; i++) {
var child = elt.childNodes[i];
if (child.nodeType === 1) { // element
if (!walk(child, nodeState)) {
return false;
}
} else if (child.nodeType === 3) { // text
if (!nodeState.nodeStart && nodeState.pos + child.data.length > nodeState.node.posOpen) {
// We found the start position
nodeState.nodeStart = child;
nodeState.nodeStartPos = nodeState.node.posOpen - nodeState.pos;
}
if (nodeState.nodeStart && nodeState.pos + child.data.length >= nodeState.node.posClose) {
// We found the end position
nodeState.nodeEnd = child;
nodeState.nodeEndPos = nodeState.node.posClose - nodeState.pos;
}
nodeState.pos += child.data.length;
}
if (nodeState.nodeStart && nodeState.nodeEnd) {
// Select the range and wrap it with the element
var range = document.createRange();
range.setStart(nodeState.nodeStart, nodeState.nodeStartPos);
range.setEnd(nodeState.nodeEnd, nodeState.nodeEndPos);
nodeState.node.element.innerHTML = '';
nodeState.node.element.appendChild(range.extractContents());
range.insertNode(nodeState.node.element);
range.detach();
// Process is over
return false;
}
}
return true;
};
// For each tag, we walk the DOM to reinsert it
env.keepMarkup.forEach(function (node) {
walk(env.element, {
node: node,
pos: 0
});
});
// Store new highlightedCode for later hooks calls
env.highlightedCode = env.element.innerHTML;
}
});
}());

View file

@ -0,0 +1 @@
"undefined"!=typeof Prism&&"undefined"!=typeof document&&document.createRange&&(Prism.plugins.KeepMarkup=!0,Prism.hooks.add("before-highlight",(function(e){if(e.element.children.length&&Prism.util.isActive(e.element,"keep-markup",!0)){var n=Prism.util.isActive(e.element,"drop-tokens",!1),t=0,o=[];r(e.element),o.length&&(e.keepMarkup=o)}function d(e){if(function(e){return!n||"span"!==e.nodeName.toLowerCase()||!e.classList.contains("token")}(e)){var d={element:e,posOpen:t};o.push(d),r(e),d.posClose=t}else r(e)}function r(e){for(var n=0,o=e.childNodes.length;n<o;n++){var r=e.childNodes[n];1===r.nodeType?d(r):3===r.nodeType&&(t+=r.data.length)}}})),Prism.hooks.add("after-highlight",(function(e){if(e.keepMarkup&&e.keepMarkup.length){var n=function(e,t){for(var o=0,d=e.childNodes.length;o<d;o++){var r=e.childNodes[o];if(1===r.nodeType){if(!n(r,t))return!1}else 3===r.nodeType&&(!t.nodeStart&&t.pos+r.data.length>t.node.posOpen&&(t.nodeStart=r,t.nodeStartPos=t.node.posOpen-t.pos),t.nodeStart&&t.pos+r.data.length>=t.node.posClose&&(t.nodeEnd=r,t.nodeEndPos=t.node.posClose-t.pos),t.pos+=r.data.length);if(t.nodeStart&&t.nodeEnd){var s=document.createRange();return s.setStart(t.nodeStart,t.nodeStartPos),s.setEnd(t.nodeEnd,t.nodeEndPos),t.node.element.innerHTML="",t.node.element.appendChild(s.extractContents()),s.insertNode(t.node.element),s.detach(),!1}}return!0};e.keepMarkup.forEach((function(t){n(e.element,{node:t,pos:0})})),e.highlightedCode=e.element.innerHTML}})));