reallysimpledocs 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import rehypeParse from "rehype-parse";
|
|
2
2
|
import rehypeStringify from "rehype-stringify";
|
|
3
3
|
import { unified } from "unified";
|
|
4
|
+
import GithubSlugger from "github-slugger";
|
|
4
5
|
|
|
5
6
|
const containerClass = ["table-container", "scrollbar", "my-6"];
|
|
6
7
|
const tableClass = ["table"];
|
|
7
8
|
|
|
8
9
|
export function rehypeBasecoatTables() {
|
|
9
10
|
return (tree) => {
|
|
11
|
+
transformHeadings(tree);
|
|
10
12
|
transformTables(tree);
|
|
11
13
|
transformCodeBlocks(tree);
|
|
12
14
|
};
|
|
@@ -41,6 +43,42 @@ function transformTables(parent) {
|
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
46
|
+
function transformHeadings(parent, slugger = new GithubSlugger()) {
|
|
47
|
+
if (!Array.isArray(parent?.children)) return;
|
|
48
|
+
|
|
49
|
+
for (const child of parent.children) {
|
|
50
|
+
if (isHeading(child)) {
|
|
51
|
+
addHeadingAnchor(child, slugger);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
transformHeadings(child, slugger);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function addHeadingAnchor(node, slugger) {
|
|
59
|
+
if (node.children?.some((child) => isElement(child, "a") && classList(child).includes("header-anchor"))) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
node.properties ||= {};
|
|
64
|
+
const id = String(node.properties.id || "") || slugger.slug(nodeText(node));
|
|
65
|
+
if (!id) return;
|
|
66
|
+
node.properties.id = id;
|
|
67
|
+
node.properties.tabIndex ??= -1;
|
|
68
|
+
node.children = [
|
|
69
|
+
{
|
|
70
|
+
type: "element",
|
|
71
|
+
tagName: "a",
|
|
72
|
+
properties: { className: ["header-anchor"], href: `#${id}` },
|
|
73
|
+
children: node.children || [],
|
|
74
|
+
},
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isHeading(node) {
|
|
79
|
+
return /^h[1-6]$/.test(node?.tagName || "");
|
|
80
|
+
}
|
|
81
|
+
|
|
44
82
|
function transformCodeBlocks(parent) {
|
|
45
83
|
if (!Array.isArray(parent?.children)) return;
|
|
46
84
|
|
|
@@ -53,6 +91,12 @@ function transformCodeBlocks(parent) {
|
|
|
53
91
|
}
|
|
54
92
|
}
|
|
55
93
|
|
|
94
|
+
function nodeText(node) {
|
|
95
|
+
if (node?.type === "text") return node.value || "";
|
|
96
|
+
if (!Array.isArray(node?.children)) return "";
|
|
97
|
+
return node.children.map(nodeText).join("");
|
|
98
|
+
}
|
|
99
|
+
|
|
56
100
|
function isElement(node, tagName) {
|
|
57
101
|
return node?.type === "element" && node.tagName === tagName;
|
|
58
102
|
}
|
package/src/css/custom.css
CHANGED
|
@@ -3,7 +3,7 @@ import { codeToHtml } from "shiki";
|
|
|
3
3
|
export async function highlightCode(code, lang = "text") {
|
|
4
4
|
const value = String(code || "");
|
|
5
5
|
const language = String(lang || "text").toLowerCase() === "mdx" ? "tsx" : String(lang || "text");
|
|
6
|
-
|
|
6
|
+
const html = await codeToHtml(value, {
|
|
7
7
|
lang: language,
|
|
8
8
|
themes: {
|
|
9
9
|
light: "github-light",
|
|
@@ -11,4 +11,15 @@ export async function highlightCode(code, lang = "text") {
|
|
|
11
11
|
},
|
|
12
12
|
defaultColor: false,
|
|
13
13
|
});
|
|
14
|
+
return addPreClass(html, "scrollbar");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function addPreClass(html, className) {
|
|
18
|
+
return String(html).replace(/<pre(\s[^>]*)?>/, (match, attrs = "") => {
|
|
19
|
+
const classMatch = attrs.match(/\sclass=(["'])(.*?)\1/s);
|
|
20
|
+
if (!classMatch) return `<pre class="${className}"${attrs}>`;
|
|
21
|
+
const classes = new Set(classMatch[2].split(/\s+/).filter(Boolean));
|
|
22
|
+
classes.add(className);
|
|
23
|
+
return match.replace(classMatch[0], ` class=${classMatch[1]}${[...classes].join(" ")}${classMatch[1]}`);
|
|
24
|
+
});
|
|
14
25
|
}
|