@xyd-js/content 0.1.0-xyd.2
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/README.md +3 -0
- package/dist/index.d.ts +960 -0
- package/dist/index.js +216 -0
- package/dist/navigation.d.ts +6 -0
- package/dist/navigation.js +2345 -0
- package/index.ts +10 -0
- package/navigation.ts +4 -0
- package/package.json +40 -0
- package/src/mdx/code.ts +15 -0
- package/src/mdx/options.ts +23 -0
- package/src/mdx/page.ts +22 -0
- package/src/mdx/themeSettings.ts +26 -0
- package/src/mdx/toc.ts +132 -0
- package/src/navigation/index.ts +188 -0
- package/src/utils/index.ts +43 -0
- package/src/vite-plugins/index.ts +15 -0
- package/tsconfig.json +26 -0
- package/tsup.config.ts +27 -0
- package/vite.config.js +53 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// src/utils/index.ts
|
|
2
|
+
import { promises as fs } from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { VFile } from "vfile";
|
|
5
|
+
import { compile as mdxCompile } from "@mdx-js/mdx";
|
|
6
|
+
|
|
7
|
+
// src/mdx/options.ts
|
|
8
|
+
import remarkFrontmatter from "remark-frontmatter";
|
|
9
|
+
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
|
|
10
|
+
import remarkGfm from "remark-gfm";
|
|
11
|
+
|
|
12
|
+
// src/mdx/toc.ts
|
|
13
|
+
var remarkMdxToc = (options) => () => async (ast) => {
|
|
14
|
+
const { visit: visit4 } = await import("unist-util-visit");
|
|
15
|
+
const { toString } = await import("mdast-util-to-string");
|
|
16
|
+
const { valueToEstree } = await import("estree-util-value-to-estree");
|
|
17
|
+
const { name: isIdentifierName } = await import("estree-util-is-identifier-name");
|
|
18
|
+
const mdast = ast;
|
|
19
|
+
const name = options.name ?? "toc";
|
|
20
|
+
if (!isIdentifierName(name)) {
|
|
21
|
+
throw new Error(`Invalid name for an identifier: ${name}`);
|
|
22
|
+
}
|
|
23
|
+
const toc = [];
|
|
24
|
+
const flatToc = [];
|
|
25
|
+
const createEntry = (node, depth) => {
|
|
26
|
+
let attributes = node.data || {};
|
|
27
|
+
if (node.type === "mdxJsxFlowElement") {
|
|
28
|
+
attributes = Object.fromEntries(
|
|
29
|
+
node.attributes.filter((attribute) => attribute.type === "mdxJsxAttribute" && typeof attribute.value === "string").map((attribute) => [attribute.name, attribute.value])
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
depth,
|
|
34
|
+
value: toString(node, { includeImageAlt: false }),
|
|
35
|
+
attributes,
|
|
36
|
+
children: []
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
visit4(mdast, ["heading", "mdxJsxFlowElement"], (node) => {
|
|
40
|
+
let depth = 0;
|
|
41
|
+
if (node.type === "mdxJsxFlowElement") {
|
|
42
|
+
let valid = false;
|
|
43
|
+
if (/^h[1-6]$/.test(node.name || "")) {
|
|
44
|
+
valid = true;
|
|
45
|
+
depth = parseInt(node.name.substring(1));
|
|
46
|
+
} else if (options.customTags) {
|
|
47
|
+
for (const tag of options.customTags) {
|
|
48
|
+
if (tag.name.test(node.name || "")) {
|
|
49
|
+
valid = true;
|
|
50
|
+
depth = tag.depth(node.name || "");
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!valid) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
} else if (node.type === "heading") {
|
|
59
|
+
depth = node.depth;
|
|
60
|
+
} else {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (depth && ((options == null ? void 0 : options.minDepth) && options.minDepth > depth)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const entry = createEntry(node, depth);
|
|
67
|
+
flatToc.push(entry);
|
|
68
|
+
let parent = toc;
|
|
69
|
+
for (let i = flatToc.length - 1; i >= 0; --i) {
|
|
70
|
+
const current = flatToc[i];
|
|
71
|
+
if (current.depth < entry.depth) {
|
|
72
|
+
parent = current.children;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
parent.push(entry);
|
|
77
|
+
});
|
|
78
|
+
const tocExport = {
|
|
79
|
+
type: "mdxjsEsm",
|
|
80
|
+
value: "",
|
|
81
|
+
data: {
|
|
82
|
+
estree: {
|
|
83
|
+
type: "Program",
|
|
84
|
+
sourceType: "module",
|
|
85
|
+
body: [
|
|
86
|
+
{
|
|
87
|
+
type: "ExportNamedDeclaration",
|
|
88
|
+
specifiers: [],
|
|
89
|
+
source: null,
|
|
90
|
+
declaration: {
|
|
91
|
+
type: "VariableDeclaration",
|
|
92
|
+
kind: "const",
|
|
93
|
+
declarations: [
|
|
94
|
+
{
|
|
95
|
+
type: "VariableDeclarator",
|
|
96
|
+
id: {
|
|
97
|
+
type: "Identifier",
|
|
98
|
+
name
|
|
99
|
+
},
|
|
100
|
+
init: valueToEstree(toc)
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
mdast.children.unshift(tocExport);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// src/mdx/code.ts
|
|
113
|
+
import { visit } from "unist-util-visit";
|
|
114
|
+
function remarkInjectCodeMeta() {
|
|
115
|
+
return (tree) => {
|
|
116
|
+
visit(tree, "code", (node) => {
|
|
117
|
+
if (node.meta) {
|
|
118
|
+
node.data = node.data || {};
|
|
119
|
+
node.data.hProperties = {
|
|
120
|
+
...node.data.hProperties || {},
|
|
121
|
+
meta: node.meta
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/mdx/themeSettings.ts
|
|
129
|
+
import { visit as visit2 } from "unist-util-visit";
|
|
130
|
+
var extractThemeSettings = () => {
|
|
131
|
+
return (tree) => {
|
|
132
|
+
visit2(tree, "exportNamedDeclaration", (node) => {
|
|
133
|
+
const declaration = node.declaration;
|
|
134
|
+
if (declaration && declaration.declarations) {
|
|
135
|
+
declaration.declarations.forEach((decl) => {
|
|
136
|
+
if (decl.id.name === "themeSettings") {
|
|
137
|
+
global.themeSettings = decl.init;
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/mdx/page.ts
|
|
146
|
+
import { visit as visit3 } from "unist-util-visit";
|
|
147
|
+
var extractPage = () => {
|
|
148
|
+
return (tree) => {
|
|
149
|
+
visit3(tree, "exportNamedDeclaration", (node) => {
|
|
150
|
+
const declaration = node.declaration;
|
|
151
|
+
if (declaration && declaration.declarations) {
|
|
152
|
+
declaration.declarations.forEach((decl) => {
|
|
153
|
+
if (decl.id.name === "page") {
|
|
154
|
+
global.page = decl.init;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// src/mdx/options.ts
|
|
163
|
+
function mdxOptions(toc) {
|
|
164
|
+
return {
|
|
165
|
+
remarkPlugins: [
|
|
166
|
+
remarkFrontmatter,
|
|
167
|
+
remarkMdxFrontmatter,
|
|
168
|
+
remarkGfm,
|
|
169
|
+
remarkInjectCodeMeta,
|
|
170
|
+
remarkMdxToc(toc),
|
|
171
|
+
extractThemeSettings,
|
|
172
|
+
extractPage
|
|
173
|
+
],
|
|
174
|
+
rehypePlugins: []
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// src/utils/index.ts
|
|
179
|
+
async function compileBySlug(slug, mdx2) {
|
|
180
|
+
const filePath = path.join(process.cwd(), `${slug}.${mdx2 ? "mdx" : "md"}`);
|
|
181
|
+
await fs.access(filePath);
|
|
182
|
+
const content = await fs.readFile(filePath, "utf-8");
|
|
183
|
+
return await compile(content, filePath);
|
|
184
|
+
}
|
|
185
|
+
async function compile(content, filePath) {
|
|
186
|
+
const vfile = new VFile({
|
|
187
|
+
path: filePath,
|
|
188
|
+
value: content,
|
|
189
|
+
contents: content
|
|
190
|
+
});
|
|
191
|
+
const mdOptions = mdxOptions({
|
|
192
|
+
minDepth: 2
|
|
193
|
+
// TODO: configurable?
|
|
194
|
+
});
|
|
195
|
+
const compiled = await mdxCompile(vfile, {
|
|
196
|
+
remarkPlugins: mdOptions.remarkPlugins,
|
|
197
|
+
rehypePlugins: mdOptions.rehypePlugins,
|
|
198
|
+
recmaPlugins: [],
|
|
199
|
+
outputFormat: "function-body",
|
|
200
|
+
development: false
|
|
201
|
+
});
|
|
202
|
+
return String(compiled);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/vite-plugins/index.ts
|
|
206
|
+
import mdx from "@mdx-js/rollup";
|
|
207
|
+
function vitePlugins(options) {
|
|
208
|
+
return [
|
|
209
|
+
mdx(mdxOptions(options.toc))
|
|
210
|
+
];
|
|
211
|
+
}
|
|
212
|
+
export {
|
|
213
|
+
compileBySlug,
|
|
214
|
+
mdxOptions,
|
|
215
|
+
vitePlugins
|
|
216
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Sidebar, PageFrontMatter, Header } from '@xyd-js/core';
|
|
2
|
+
|
|
3
|
+
declare function pageFrontMatters(navigation: Sidebar[]): Promise<PageFrontMatter>;
|
|
4
|
+
declare function filterNavigationByLevels(headers: Header[], slug: string): (nav: Sidebar) => boolean;
|
|
5
|
+
|
|
6
|
+
export { filterNavigationByLevels, pageFrontMatters };
|