docs:更新文档

This commit is contained in:
张益铭
2021-03-01 15:06:11 +08:00
parent 1542135ab0
commit 9064b372e8
5835 changed files with 904126 additions and 161722 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,178 @@
(function () {
if (typeof self === 'undefined' || !self.Prism || !self.document) {
return;
}
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+-)(open|close)$/;
/**
* 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('brace-hover');
});
}
/**
* @this {HTMLElement}
*/
function leaveBrace() {
[this, getPartnerBrace(this)].forEach(function (e) {
e.classList.remove('brace-hover');
});
}
/**
* @this {HTMLElement}
*/
function clickBrace() {
if (!Prism.util.isActive(this, 'brace-select', true)) {
return;
}
[this, getPartnerBrace(this)].forEach(function (e) {
e.classList.add('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');
Array.prototype.slice.call(code.querySelectorAll('.brace-selected')).forEach(function (e) {
e.classList.remove('brace-selected');
});
});
Object.defineProperty(pre, '__listenerAdded', { value: true });
}
/** @type {HTMLSpanElement[]} */
var punctuation = Array.prototype.slice.call(code.querySelectorAll('span.token.punctuation'));
/** @type {{ index: number, open: boolean, element: HTMLElement }[]} */
var allBraces = [];
toMatch.forEach(function (open) {
var close = PARTNER[open];
var name = 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('brace-open');
openStack.push(i);
} else if (text === close) {
allBraces.push({ index: i, open: false, element: element });
element.classList.add(name);
element.classList.add('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('brace-level-' + (level % LEVEL_WARP + 1));
level++;
} else {
level = Math.max(0, level - 1);
brace.element.classList.add('brace-level-' + (level % LEVEL_WARP + 1));
}
});
});
}());

View File

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