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,29 @@
.token.punctuation.brace-hover,
.token.punctuation.brace-selected {
outline: solid 1px;
}
.rainbow-braces .token.punctuation.brace-level-1,
.rainbow-braces .token.punctuation.brace-level-5,
.rainbow-braces .token.punctuation.brace-level-9 {
color: #E50;
opacity: 1;
}
.rainbow-braces .token.punctuation.brace-level-2,
.rainbow-braces .token.punctuation.brace-level-6,
.rainbow-braces .token.punctuation.brace-level-10 {
color: #0B3;
opacity: 1;
}
.rainbow-braces .token.punctuation.brace-level-3,
.rainbow-braces .token.punctuation.brace-level-7,
.rainbow-braces .token.punctuation.brace-level-11 {
color: #26F;
opacity: 1;
}
.rainbow-braces .token.punctuation.brace-level-4,
.rainbow-braces .token.punctuation.brace-level-8,
.rainbow-braces .token.punctuation.brace-level-12 {
color: #E0E;
opacity: 1;
}

View file

@ -0,0 +1,190 @@
(function () {
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
return;
}
function mapClassName(name) {
var customClass = Prism.plugins.customClass;
if (customClass) {
return customClass.apply(name, 'none');
} else {
return name;
}
}
var PARTNER = {
'(': ')',
'[': ']',
'{': '}',
};
// The names for brace types.
// These names have two purposes: 1) they can be used for styling and 2) they are used to pair braces. Only braces
// of the same type are paired.
var NAMES = {
'(': 'brace-round',
'[': 'brace-square',
'{': 'brace-curly',
};
// A map for brace aliases.
// This is useful for when some braces have a prefix/suffix as part of the punctuation token.
var BRACE_ALIAS_MAP = {
'${': '{', // JS template punctuation (e.g. `foo ${bar + 1}`)
};
var LEVEL_WARP = 12;
var pairIdCounter = 0;
var BRACE_ID_PATTERN = /^(pair-\d+-)(close|open)$/;
/**
* Returns the brace partner given one brace of a brace pair.
*
* @param {HTMLElement} brace
* @returns {HTMLElement}
*/
function getPartnerBrace(brace) {
var match = BRACE_ID_PATTERN.exec(brace.id);
return document.querySelector('#' + match[1] + (match[2] == 'open' ? 'close' : 'open'));
}
/**
* @this {HTMLElement}
*/
function hoverBrace() {
if (!Prism.util.isActive(this, 'brace-hover', true)) {
return;
}
[this, getPartnerBrace(this)].forEach(function (e) {
e.classList.add(mapClassName('brace-hover'));
});
}
/**
* @this {HTMLElement}
*/
function leaveBrace() {
[this, getPartnerBrace(this)].forEach(function (e) {
e.classList.remove(mapClassName('brace-hover'));
});
}
/**
* @this {HTMLElement}
*/
function clickBrace() {
if (!Prism.util.isActive(this, 'brace-select', true)) {
return;
}
[this, getPartnerBrace(this)].forEach(function (e) {
e.classList.add(mapClassName('brace-selected'));
});
}
Prism.hooks.add('complete', function (env) {
/** @type {HTMLElement} */
var code = env.element;
var pre = code.parentElement;
if (!pre || pre.tagName != 'PRE') {
return;
}
// find the braces to match
/** @type {string[]} */
var toMatch = [];
if (Prism.util.isActive(code, 'match-braces')) {
toMatch.push('(', '[', '{');
}
if (toMatch.length == 0) {
// nothing to match
return;
}
if (!pre.__listenerAdded) {
// code blocks might be highlighted more than once
pre.addEventListener('mousedown', function removeBraceSelected() {
// the code element might have been replaced
var code = pre.querySelector('code');
var className = mapClassName('brace-selected');
Array.prototype.slice.call(code.querySelectorAll('.' + className)).forEach(function (e) {
e.classList.remove(className);
});
});
Object.defineProperty(pre, '__listenerAdded', { value: true });
}
/** @type {HTMLSpanElement[]} */
var punctuation = Array.prototype.slice.call(
code.querySelectorAll('span.' + mapClassName('token') + '.' + mapClassName('punctuation'))
);
/** @type {{ index: number, open: boolean, element: HTMLElement }[]} */
var allBraces = [];
toMatch.forEach(function (open) {
var close = PARTNER[open];
var name = mapClassName(NAMES[open]);
/** @type {[number, number][]} */
var pairs = [];
/** @type {number[]} */
var openStack = [];
for (var i = 0; i < punctuation.length; i++) {
var element = punctuation[i];
if (element.childElementCount == 0) {
var text = element.textContent;
text = BRACE_ALIAS_MAP[text] || text;
if (text === open) {
allBraces.push({ index: i, open: true, element: element });
element.classList.add(name);
element.classList.add(mapClassName('brace-open'));
openStack.push(i);
} else if (text === close) {
allBraces.push({ index: i, open: false, element: element });
element.classList.add(name);
element.classList.add(mapClassName('brace-close'));
if (openStack.length) {
pairs.push([i, openStack.pop()]);
}
}
}
}
pairs.forEach(function (pair) {
var pairId = 'pair-' + (pairIdCounter++) + '-';
var opening = punctuation[pair[0]];
var closing = punctuation[pair[1]];
opening.id = pairId + 'open';
closing.id = pairId + 'close';
[opening, closing].forEach(function (e) {
e.addEventListener('mouseenter', hoverBrace);
e.addEventListener('mouseleave', leaveBrace);
e.addEventListener('click', clickBrace);
});
});
});
var level = 0;
allBraces.sort(function (a, b) { return a.index - b.index; });
allBraces.forEach(function (brace) {
if (brace.open) {
brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
level++;
} else {
level = Math.max(0, level - 1);
brace.element.classList.add(mapClassName('brace-level-' + (level % LEVEL_WARP + 1)));
}
});
});
}());

View file

@ -0,0 +1 @@
.token.punctuation.brace-hover,.token.punctuation.brace-selected{outline:solid 1px}.rainbow-braces .token.punctuation.brace-level-1,.rainbow-braces .token.punctuation.brace-level-5,.rainbow-braces .token.punctuation.brace-level-9{color:#e50;opacity:1}.rainbow-braces .token.punctuation.brace-level-10,.rainbow-braces .token.punctuation.brace-level-2,.rainbow-braces .token.punctuation.brace-level-6{color:#0b3;opacity:1}.rainbow-braces .token.punctuation.brace-level-11,.rainbow-braces .token.punctuation.brace-level-3,.rainbow-braces .token.punctuation.brace-level-7{color:#26f;opacity:1}.rainbow-braces .token.punctuation.brace-level-12,.rainbow-braces .token.punctuation.brace-level-4,.rainbow-braces .token.punctuation.brace-level-8{color:#e0e;opacity:1}

View file

@ -0,0 +1 @@
!function(){if("undefined"!=typeof Prism&&"undefined"!=typeof document){var e={"(":")","[":"]","{":"}"},t={"(":"brace-round","[":"brace-square","{":"brace-curly"},n={"${":"{"},r=0,c=/^(pair-\d+-)(close|open)$/;Prism.hooks.add("complete",(function(c){var i=c.element,d=i.parentElement;if(d&&"PRE"==d.tagName){var u=[];if(Prism.util.isActive(i,"match-braces")&&u.push("(","[","{"),0!=u.length){d.__listenerAdded||(d.addEventListener("mousedown",(function(){var e=d.querySelector("code"),t=s("brace-selected");Array.prototype.slice.call(e.querySelectorAll("."+t)).forEach((function(e){e.classList.remove(t)}))})),Object.defineProperty(d,"__listenerAdded",{value:!0}));var f=Array.prototype.slice.call(i.querySelectorAll("span."+s("token")+"."+s("punctuation"))),h=[];u.forEach((function(c){for(var i=e[c],d=s(t[c]),u=[],p=[],v=0;v<f.length;v++){var m=f[v];if(0==m.childElementCount){var b=m.textContent;(b=n[b]||b)===c?(h.push({index:v,open:!0,element:m}),m.classList.add(d),m.classList.add(s("brace-open")),p.push(v)):b===i&&(h.push({index:v,open:!1,element:m}),m.classList.add(d),m.classList.add(s("brace-close")),p.length&&u.push([v,p.pop()]))}}u.forEach((function(e){var t="pair-"+r+++"-",n=f[e[0]],c=f[e[1]];n.id=t+"open",c.id=t+"close",[n,c].forEach((function(e){e.addEventListener("mouseenter",a),e.addEventListener("mouseleave",o),e.addEventListener("click",l)}))}))}));var p=0;h.sort((function(e,t){return e.index-t.index})),h.forEach((function(e){e.open?(e.element.classList.add(s("brace-level-"+(p%12+1))),p++):(p=Math.max(0,p-1),e.element.classList.add(s("brace-level-"+(p%12+1))))}))}}}))}function s(e){var t=Prism.plugins.customClass;return t?t.apply(e,"none"):e}function i(e){var t=c.exec(e.id);return document.querySelector("#"+t[1]+("open"==t[2]?"close":"open"))}function a(){Prism.util.isActive(this,"brace-hover",!0)&&[this,i(this)].forEach((function(e){e.classList.add(s("brace-hover"))}))}function o(){[this,i(this)].forEach((function(e){e.classList.remove(s("brace-hover"))}))}function l(){Prism.util.isActive(this,"brace-select",!0)&&[this,i(this)].forEach((function(e){e.classList.add(s("brace-selected"))}))}}();