@lexibase/rdx-core 1.1.1 → 1.2.0
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/dist/mdx/index.js +98 -2
- package/package.json +1 -1
package/dist/mdx/index.js
CHANGED
|
@@ -7,8 +7,68 @@ import { default as default2 } from "rehype-slug";
|
|
|
7
7
|
|
|
8
8
|
// src/mdx/code-highlighting.ts
|
|
9
9
|
import rehypeShiki from "@shikijs/rehype";
|
|
10
|
+
|
|
11
|
+
// src/mdx/folder-tree.ts
|
|
12
|
+
function formatFolderTree(source) {
|
|
13
|
+
const roots = [];
|
|
14
|
+
const parents = [];
|
|
15
|
+
for (const [index, line] of source.split(/\r?\n/).entries()) {
|
|
16
|
+
if (!line.trim()) continue;
|
|
17
|
+
const indentation = line.match(/^[ \t]*/)?.[0].replaceAll(" ", " ") ?? "";
|
|
18
|
+
const depth = indentation.length / 2;
|
|
19
|
+
if (!Number.isInteger(depth) || depth > parents.length)
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Invalid folder fence at line ${index + 1}: use two spaces per level without skipping levels.`
|
|
22
|
+
);
|
|
23
|
+
const parent = parents[depth - 1];
|
|
24
|
+
if (parent && !parent.name.endsWith("/"))
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Invalid folder fence at line ${index + 1}: parent folders must end with /.`
|
|
27
|
+
);
|
|
28
|
+
const entry = { name: line.trim(), children: [] };
|
|
29
|
+
const siblings = parent ? parent.children : roots;
|
|
30
|
+
siblings.push(entry);
|
|
31
|
+
parents.length = depth;
|
|
32
|
+
parents.push(entry);
|
|
33
|
+
}
|
|
34
|
+
const lines = [];
|
|
35
|
+
function draw(entries, prefix, root = false) {
|
|
36
|
+
entries.forEach((entry, index) => {
|
|
37
|
+
const last = index === entries.length - 1;
|
|
38
|
+
lines.push(
|
|
39
|
+
root ? entry.name : `${prefix}${last ? "\u2514\u2500\u2500 " : "\u251C\u2500\u2500 "}${entry.name}`
|
|
40
|
+
);
|
|
41
|
+
draw(entry.children, root ? "" : prefix + (last ? " " : "\u2502 "));
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
draw(roots, "", true);
|
|
45
|
+
return lines.join("\n") + (source.endsWith("\n") ? "\n" : "");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/mdx/code-highlighting.ts
|
|
49
|
+
function prepareFolders(node) {
|
|
50
|
+
const code = node.tagName === "pre" ? node.children?.[0] : void 0;
|
|
51
|
+
const classes = code?.properties?.className;
|
|
52
|
+
if (code && Array.isArray(classes) && classes.some((name) => /^language-folder$/i.test(String(name)))) {
|
|
53
|
+
node.properties = { ...node.properties, "data-rdx-code-mode": "folder" };
|
|
54
|
+
code.properties.className = classes.filter(
|
|
55
|
+
(name) => !/^language-folder$/i.test(String(name))
|
|
56
|
+
);
|
|
57
|
+
code.children = [
|
|
58
|
+
{
|
|
59
|
+
type: "text",
|
|
60
|
+
value: formatFolderTree(
|
|
61
|
+
code.children?.map((child) => child.value ?? "").join("") ?? ""
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
];
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
node.children?.forEach(prepareFolders);
|
|
68
|
+
}
|
|
10
69
|
function rehypeDocumentationCode() {
|
|
11
|
-
|
|
70
|
+
const diffVariants = /* @__PURE__ */ new WeakMap();
|
|
71
|
+
const highlight = rehypeShiki.call(this, {
|
|
12
72
|
themes: { light: "github-light", dark: "github-dark" },
|
|
13
73
|
defaultColor: false,
|
|
14
74
|
addLanguageClass: true,
|
|
@@ -18,8 +78,44 @@ function rehypeDocumentationCode() {
|
|
|
18
78
|
fallbackLanguage: "text",
|
|
19
79
|
// The copy button must retain MDX's original trailing newline.
|
|
20
80
|
stripEndNewline: false,
|
|
21
|
-
inline: false
|
|
81
|
+
inline: false,
|
|
82
|
+
transformers: [
|
|
83
|
+
{
|
|
84
|
+
name: "rdx:diff-lines",
|
|
85
|
+
pre(node) {
|
|
86
|
+
if (this.options.lang === "diff")
|
|
87
|
+
node.properties["data-rdx-code-mode"] = "diff";
|
|
88
|
+
},
|
|
89
|
+
preprocess(code) {
|
|
90
|
+
if (this.options.lang === "diff") {
|
|
91
|
+
let inHunk = false;
|
|
92
|
+
diffVariants.set(
|
|
93
|
+
this.meta,
|
|
94
|
+
code.split("\n").map((text) => {
|
|
95
|
+
if (text.startsWith("diff --git ")) inHunk = false;
|
|
96
|
+
if (text.startsWith("@@")) {
|
|
97
|
+
inHunk = true;
|
|
98
|
+
return "hunk";
|
|
99
|
+
}
|
|
100
|
+
if (!inHunk && /^(\+\+\+|---)(?:\s|$)/.test(text))
|
|
101
|
+
return "default";
|
|
102
|
+
return text.startsWith("+") ? "added" : text.startsWith("-") ? "removed" : "default";
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
line(node, line) {
|
|
108
|
+
if (this.options.lang !== "diff") return;
|
|
109
|
+
node.properties["data-rdx-line-variant"] = diffVariants.get(this.meta)?.[line - 1] ?? "default";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
]
|
|
22
113
|
});
|
|
114
|
+
if (typeof highlight !== "function") return highlight;
|
|
115
|
+
return (tree, ...args) => {
|
|
116
|
+
prepareFolders(tree);
|
|
117
|
+
return highlight(tree, ...args);
|
|
118
|
+
};
|
|
23
119
|
}
|
|
24
120
|
export {
|
|
25
121
|
rehypeDocumentationCode,
|