feat: add http-status-code.md cheatsheet.

This commit is contained in:
jaywcjlove
2022-09-29 22:46:01 +08:00
parent 43bf0d4f77
commit 5094fac68a
10 changed files with 304 additions and 42 deletions

18
scripts/utils/childs.mjs Normal file
View File

@ -0,0 +1,18 @@
export function getChilds(data = [], level, result = []) {
for (let i = 1; i <= data.length; i++) {
const titleNum = Number(data[i]?.tagName?.replace(/^h/, ''));
if (titleNum && titleNum === level) break;
result.push(data[i]);
}
return result;
}
/** 获取 Heading 到下一个 Heading 之间的内容*/
export function getHeader(data = [], level, result = []) {
for (let i = 1; i <= data.length; i++) {
if (/^h\d$/.test(data[i]?.tagName) || data[i]?.number !== level) break;
result.push(data[i]);
}
return result;
}

View File

@ -0,0 +1,18 @@
/** 标记 Number */
export function panelAddNumber(arr = [], result = []) {
let n = 0;
let level = -1;
while (n < arr.length) {
const toc = arr[n];
const titleNum = Number(toc?.tagName?.replace(/^h/, ''));
if (titleNum && titleNum > -1) {
level = titleNum;
}
if (toc) {
result.push({ ...toc, number: level })
}
n++;
}
return result
}

View File

@ -0,0 +1,6 @@
export function rehypeUrls(node) {
if (node.type === 'element' && node.properties.href && /.md/.test(node.properties.href) && !/^(https?:\/\/)/.test(node.properties.href)) {
let href = node.properties.href;
node.properties.href = href.replace(/([^\.\/\\]+)\.(md|markdown)/gi, '$1.html');
}
}

View File

@ -0,0 +1,64 @@
/**
* 配置 tooltips 注释
*
* ```markdown
* - [超链接有 tooltips 提示](#1xx-information) _Tooltips 展示内容_ <!--rehype:tooltips-->
* ```
*
* 上面示例:将 “Tooltips 展示内容” 放到 前一个 `<a>` dom 节点作为子节点
*
* - 注释配置的,前一个节点 AA 的前一个节点 B
* - 如果 A 和 B 其中一个不存在 `tooltips` 将失效
* - 设置 B 的类名称为 tooltips
*/
export function tooltips(node, index, parent) {
if (node.type === 'comment' && parent?.children.length > 2) {
const childs = parent?.children;
const result = [];
let recordPos = false; // 记录位置
let tooltipNode = null;
for(let i = childs.length; i > -1; i--) {
const node = childs[i];
// 记录 tooltip 的开始位置
if (node?.type === 'comment' && node?.value === 'rehype:tooltips') {
recordPos = true;
continue
}
// 记录 tooltip 的 node
if (recordPos && !tooltipNode) {
if (node.type === 'comment' || (node.type === 'text' && !node?.value?.replace(/\s\n/g, ''))) {
recordPos = false;
}
if (recordPos && node.type === 'element') {
tooltipNode = node;
tooltipNode.properties['class'] = 'tooltiptext';
delete tooltipNode.position;
continue
}
}
// 将 tooltip 节点,插入到下一个 element 节点的子节点中
if (tooltipNode) {
if (node.type === 'comment' || (node.type === 'text' && !node?.value?.replace(/\s\n/g, ''))) {
recordPos = false;
tooltipNode = null
}
if (tooltipNode && node?.type === 'element') {
recordPos = false;
node.properties['class'] = 'tooltip';
node.children.push(tooltipNode);
tooltipNode = null
}
}
if (!recordPos && node) {
result.push(node)
}
}
if (parent) {
parent.children = [...result.reverse()];
}
}
}
export function getPreviewNode() {
}