@xyd-js/content 0.1.0-xyd.4 → 0.1.0-xyd.6

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/vite.js ADDED
@@ -0,0 +1,223 @@
1
+ // packages/vite/index.ts
2
+ import mdx from "@mdx-js/rollup";
3
+
4
+ // packages/md/plugins/index.ts
5
+ import remarkFrontmatter from "remark-frontmatter";
6
+ import remarkMdxFrontmatter from "remark-mdx-frontmatter";
7
+ import remarkGfm from "remark-gfm";
8
+ import remarkDirective from "remark-directive";
9
+
10
+ // packages/md/plugins/md-toc.ts
11
+ var remarkMdxToc = (options) => () => async (ast) => {
12
+ const { visit: visit5 } = await import("unist-util-visit");
13
+ const { toString } = await import("mdast-util-to-string");
14
+ const { valueToEstree } = await import("estree-util-value-to-estree");
15
+ const { name: isIdentifierName } = await import("estree-util-is-identifier-name");
16
+ const mdast = ast;
17
+ const name = options.name ?? "toc";
18
+ if (!isIdentifierName(name)) {
19
+ throw new Error(`Invalid name for an identifier: ${name}`);
20
+ }
21
+ const toc = [];
22
+ const flatToc = [];
23
+ const createEntry = (node, depth) => {
24
+ let attributes = node.data || {};
25
+ if (node.type === "mdxJsxFlowElement") {
26
+ attributes = Object.fromEntries(
27
+ node.attributes.filter((attribute) => attribute.type === "mdxJsxAttribute" && typeof attribute.value === "string").map((attribute) => [attribute.name, attribute.value])
28
+ );
29
+ }
30
+ return {
31
+ depth,
32
+ value: toString(node, { includeImageAlt: false }),
33
+ attributes,
34
+ children: []
35
+ };
36
+ };
37
+ visit5(mdast, ["heading", "mdxJsxFlowElement"], (node) => {
38
+ let depth = 0;
39
+ if (node.type === "mdxJsxFlowElement") {
40
+ let valid = false;
41
+ if (/^h[1-6]$/.test(node.name || "")) {
42
+ valid = true;
43
+ depth = parseInt(node.name.substring(1));
44
+ } else if (options.customTags) {
45
+ for (const tag of options.customTags) {
46
+ if (tag.name.test(node.name || "")) {
47
+ valid = true;
48
+ depth = tag.depth(node.name || "");
49
+ break;
50
+ }
51
+ }
52
+ }
53
+ if (!valid) {
54
+ return;
55
+ }
56
+ } else if (node.type === "heading") {
57
+ depth = node.depth;
58
+ } else {
59
+ return;
60
+ }
61
+ if (depth && ((options == null ? void 0 : options.minDepth) && options.minDepth > depth)) {
62
+ return;
63
+ }
64
+ const entry = createEntry(node, depth);
65
+ flatToc.push(entry);
66
+ let parent = toc;
67
+ for (let i = flatToc.length - 1; i >= 0; --i) {
68
+ const current = flatToc[i];
69
+ if (current.depth < entry.depth) {
70
+ parent = current.children;
71
+ break;
72
+ }
73
+ }
74
+ parent.push(entry);
75
+ });
76
+ const tocExport = {
77
+ type: "mdxjsEsm",
78
+ value: "",
79
+ data: {
80
+ estree: {
81
+ type: "Program",
82
+ sourceType: "module",
83
+ body: [
84
+ {
85
+ type: "ExportNamedDeclaration",
86
+ specifiers: [],
87
+ source: null,
88
+ declaration: {
89
+ type: "VariableDeclaration",
90
+ kind: "const",
91
+ declarations: [
92
+ {
93
+ type: "VariableDeclarator",
94
+ id: {
95
+ type: "Identifier",
96
+ name
97
+ },
98
+ init: valueToEstree(toc)
99
+ }
100
+ ]
101
+ }
102
+ }
103
+ ]
104
+ }
105
+ }
106
+ };
107
+ mdast.children.unshift(tocExport);
108
+ };
109
+
110
+ // packages/md/plugins/md-code.ts
111
+ import { visit } from "unist-util-visit";
112
+ function remarkInjectCodeMeta() {
113
+ return (tree) => {
114
+ visit(tree, "code", (node) => {
115
+ if (node.meta) {
116
+ node.data = node.data || {};
117
+ node.data.hProperties = {
118
+ ...node.data.hProperties || {},
119
+ meta: node.meta
120
+ };
121
+ }
122
+ });
123
+ };
124
+ }
125
+
126
+ // packages/md/plugins/md-themeSettings.ts
127
+ import { visit as visit2 } from "unist-util-visit";
128
+ var extractThemeSettings = () => {
129
+ return (tree) => {
130
+ visit2(tree, "exportNamedDeclaration", (node) => {
131
+ const declaration = node.declaration;
132
+ if (declaration && declaration.declarations) {
133
+ declaration.declarations.forEach((decl) => {
134
+ if (decl.id.name === "themeSettings") {
135
+ global.themeSettings = decl.init;
136
+ }
137
+ });
138
+ }
139
+ });
140
+ };
141
+ };
142
+
143
+ // packages/md/plugins/md-page.ts
144
+ import { visit as visit3 } from "unist-util-visit";
145
+ var extractPage = () => {
146
+ return (tree) => {
147
+ visit3(tree, "exportNamedDeclaration", (node) => {
148
+ const declaration = node.declaration;
149
+ if (declaration && declaration.declarations) {
150
+ declaration.declarations.forEach((decl) => {
151
+ if (decl.id.name === "page") {
152
+ global.page = decl.init;
153
+ }
154
+ });
155
+ }
156
+ });
157
+ };
158
+ };
159
+
160
+ // packages/md/plugins/md-codegroup.ts
161
+ import { visit as visit4 } from "unist-util-visit";
162
+ function mdCodeGroup() {
163
+ return (tree) => {
164
+ visit4(tree, "containerDirective", (node) => {
165
+ var _a;
166
+ if (node.name !== "code-group") return;
167
+ const description = ((_a = node.attributes) == null ? void 0 : _a.title) || "";
168
+ const codeblocks = [];
169
+ for (const child of node.children) {
170
+ if (child.type === "code") {
171
+ const meta = child.meta || "";
172
+ const value = child.value || "";
173
+ const lang = child.lang || "";
174
+ codeblocks.push({ value, lang, meta });
175
+ }
176
+ }
177
+ node.data = {
178
+ hName: "DirectiveCodeSample",
179
+ hProperties: {
180
+ description,
181
+ codeblocks: JSON.stringify(codeblocks)
182
+ }
183
+ };
184
+ node.children = [];
185
+ });
186
+ };
187
+ }
188
+
189
+ // packages/md/plugins/index.ts
190
+ function defaultPlugins(toc) {
191
+ return [
192
+ remarkFrontmatter,
193
+ remarkMdxFrontmatter,
194
+ remarkGfm,
195
+ remarkDirective,
196
+ remarkMdxToc(toc),
197
+ remarkInjectCodeMeta,
198
+ extractThemeSettings,
199
+ extractPage,
200
+ mdCodeGroup
201
+ ];
202
+ }
203
+
204
+ // packages/md/index.ts
205
+ function mdOptions(toc) {
206
+ return {
207
+ remarkPlugins: [
208
+ ...defaultPlugins(toc)
209
+ ],
210
+ rehypePlugins: []
211
+ };
212
+ }
213
+
214
+ // packages/vite/index.ts
215
+ function vitePlugins(options) {
216
+ return [
217
+ mdx(mdOptions(options.toc))
218
+ ];
219
+ }
220
+ export {
221
+ vitePlugins
222
+ };
223
+ //# sourceMappingURL=vite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../packages/vite/index.ts","../packages/md/plugins/index.ts","../packages/md/plugins/md-toc.ts","../packages/md/plugins/md-code.ts","../packages/md/plugins/md-themeSettings.ts","../packages/md/plugins/md-page.ts","../packages/md/plugins/md-codegroup.ts","../packages/md/index.ts"],"sourcesContent":["import type {Plugin} from 'rollup';\nimport mdx from '@mdx-js/rollup';\n\nimport {RemarkMdxTocOptions, mdOptions} from \"../md\";\n\nexport interface VitePluginInterface {\n toc: RemarkMdxTocOptions\n}\n\nexport function vitePlugins(options: VitePluginInterface): Plugin[] {\n return [\n mdx(mdOptions(options.toc)),\n ];\n}","import remarkFrontmatter from \"remark-frontmatter\";\nimport remarkMdxFrontmatter from \"remark-mdx-frontmatter\";\nimport remarkGfm from \"remark-gfm\";\nimport remarkDirective from 'remark-directive'\n\nimport {remarkMdxToc, RemarkMdxTocOptions} from \"./md-toc\";\nimport {remarkInjectCodeMeta} from \"./md-code\";\nimport {extractThemeSettings} from \"./md-themeSettings\";\nimport {extractPage} from \"./md-page\";\nimport {mdCodeGroup} from \"./md-codegroup\";\n\nexport function defaultPlugins(toc: RemarkMdxTocOptions) {\n return [\n remarkFrontmatter,\n remarkMdxFrontmatter,\n remarkGfm,\n remarkDirective,\n remarkMdxToc(toc),\n remarkInjectCodeMeta,\n extractThemeSettings,\n extractPage,\n mdCodeGroup\n ]\n}","import {Root, Heading} from \"mdast\";\nimport {MdxjsEsm} from \"mdast-util-mdx\";\nimport {Plugin} from \"unified\"; // TODO: use Plugin type\nimport {MdxJsxFlowElement, MdxJsxAttribute} from \"mdast-util-mdx-jsx\";\n\nexport type TocEntry = {\n depth: number,\n value: string,\n attributes: { [key: string]: string },\n children: TocEntry[]\n};\n\nexport type CustomTag = {\n name: RegExp,\n depth: (name: string) => number\n};\n\nexport interface RemarkMdxTocOptions {\n name?: string,\n customTags?: CustomTag[],\n minDepth?: number\n}\n\n// TODO: fix any\nexport const remarkMdxToc = (options: RemarkMdxTocOptions): Plugin => () => async (ast: any) => {\n const {visit} = await import(\"unist-util-visit\");\n const {toString} = await import(\"mdast-util-to-string\");\n const {valueToEstree} = await import('estree-util-value-to-estree')\n const {name: isIdentifierName} = await import('estree-util-is-identifier-name');\n\n const mdast = ast as Root;\n const name = options.name ?? \"toc\";\n if (!isIdentifierName(name)) {\n throw new Error(`Invalid name for an identifier: ${name}`);\n }\n\n const toc: TocEntry[] = [];\n const flatToc: TocEntry[] = [];\n const createEntry = (node: Heading | MdxJsxFlowElement, depth: number): TocEntry => {\n let attributes = (node.data || {}) as TocEntry['attributes'];\n if (node.type === \"mdxJsxFlowElement\") {\n attributes = Object.fromEntries(\n node.attributes\n .filter(attribute => attribute.type === 'mdxJsxAttribute' && typeof attribute.value === 'string')\n .map(attribute => [(attribute as MdxJsxAttribute).name, attribute.value])\n ) as TocEntry['attributes'];\n }\n return {\n depth,\n value: toString(node, {includeImageAlt: false}),\n attributes,\n children: []\n }\n };\n\n visit(mdast, [\"heading\", \"mdxJsxFlowElement\"], node => {\n let depth = 0;\n if (node.type === \"mdxJsxFlowElement\") {\n let valid = false;\n if (/^h[1-6]$/.test(node.name || \"\")) {\n valid = true;\n depth = parseInt(node.name!.substring(1));\n } else if (options.customTags) {\n for (const tag of options.customTags) {\n if (tag.name.test(node.name || \"\")) {\n valid = true;\n depth = tag.depth(node.name || \"\");\n break;\n }\n }\n }\n\n if (!valid) {\n return;\n }\n } else if (node.type === \"heading\") {\n depth = node.depth;\n } else {\n return;\n }\n\n if (depth && (options?.minDepth && options.minDepth > depth)) {\n return\n }\n\n const entry = createEntry(node, depth);\n flatToc.push(entry);\n\n let parent: TocEntry[] = toc;\n for (let i = flatToc.length - 1; i >= 0; --i) {\n const current = flatToc[i];\n if (current.depth < entry.depth) {\n parent = current.children;\n break;\n }\n }\n parent.push(entry);\n });\n\n const tocExport: MdxjsEsm = {\n type: \"mdxjsEsm\",\n value: \"\",\n data: {\n estree: {\n type: \"Program\",\n sourceType: \"module\",\n body: [\n {\n type: \"ExportNamedDeclaration\",\n specifiers: [],\n source: null,\n declaration: {\n type: \"VariableDeclaration\",\n kind: \"const\",\n declarations: [\n {\n type: \"VariableDeclarator\",\n id: {\n type: \"Identifier\",\n name\n },\n init: valueToEstree(toc)\n }\n ]\n }\n }\n ]\n }\n }\n };\n mdast.children.unshift(tocExport);\n};","import {visit} from \"unist-util-visit\";\n\nexport function remarkInjectCodeMeta() {\n return (tree: any) => {\n visit(tree, 'code', (node) => {\n if (node.meta) {\n node.data = node.data || {};\n node.data.hProperties = {\n ...(node.data.hProperties || {}),\n meta: node.meta,\n };\n }\n });\n };\n}\n","import {Plugin} from 'unified';\nimport {Node as UnistNode} from 'unist';\nimport {visit} from 'unist-util-visit';\n\ninterface ThemeSettings {\n bigArticle: boolean;\n}\n\ndeclare global {\n var themeSettings: ThemeSettings | undefined;\n}\n\nexport const extractThemeSettings: Plugin = () => {\n return (tree: UnistNode) => {\n visit(tree, 'exportNamedDeclaration', (node: any) => {\n const declaration = node.declaration;\n if (declaration && declaration.declarations) {\n declaration.declarations.forEach((decl: any) => {\n if (decl.id.name === 'themeSettings') {\n global.themeSettings = decl.init as ThemeSettings;\n }\n });\n }\n });\n };\n};","import {Plugin} from 'unified';\nimport {Node as UnistNode} from 'unist';\nimport {visit} from 'unist-util-visit';\n\ndeclare global {\n var page: boolean | null | undefined\n}\n\nexport const extractPage: Plugin = () => {\n return (tree: UnistNode) => {\n visit(tree, 'exportNamedDeclaration', (node: any) => {\n const declaration = node.declaration;\n if (declaration && declaration.declarations) {\n declaration.declarations.forEach((decl: any) => {\n if (decl.id.name === 'page') {\n global.page = decl.init as boolean;\n }\n });\n }\n });\n };\n};","import {visit} from 'unist-util-visit';\n\n// This plugin transforms a custom container directive into a JSX node\n// https://github.com/remarkjs/remark-directive is needed to parse the container directive\n\nexport function mdCodeGroup() {\n return (tree: any) => {\n visit(tree, 'containerDirective', (node) => {\n if (node.name !== 'code-group') return;\n\n const description = node.attributes?.title || '';\n const codeblocks = [];\n\n for (const child of node.children) {\n if (child.type === 'code') {\n const meta = child.meta || '';\n const value = child.value || '';\n const lang = child.lang || '';\n\n codeblocks.push({value, lang, meta});\n }\n }\n\n // Add metadata to the node\n node.data = {\n hName: 'DirectiveCodeSample',\n hProperties: {\n description,\n codeblocks: JSON.stringify(codeblocks),\n },\n };\n\n node.children = [];\n });\n };\n}\n","import {RemarkMdxTocOptions} from \"./plugins/md-toc\";\nimport {defaultPlugins} from \"./plugins\"\n\nexport {RemarkMdxTocOptions} from \"./plugins/md-toc\";\n\nexport function mdOptions(toc: RemarkMdxTocOptions) {\n return {\n remarkPlugins: [\n ...defaultPlugins(toc)\n ],\n rehypePlugins: []\n }\n}"],"mappings":";AACA,OAAO,SAAS;;;ACDhB,OAAO,uBAAuB;AAC9B,OAAO,0BAA0B;AACjC,OAAO,eAAe;AACtB,OAAO,qBAAqB;;;ACqBrB,IAAM,eAAe,CAAC,YAAyC,MAAM,OAAO,QAAa;AAC5F,QAAM,EAAC,OAAAA,OAAK,IAAI,MAAM,OAAO,kBAAkB;AAC/C,QAAM,EAAC,SAAQ,IAAI,MAAM,OAAO,sBAAsB;AACtD,QAAM,EAAC,cAAa,IAAI,MAAM,OAAO,6BAA6B;AAClE,QAAM,EAAC,MAAM,iBAAgB,IAAI,MAAM,OAAO,gCAAgC;AAE9E,QAAM,QAAQ;AACd,QAAM,OAAO,QAAQ,QAAQ;AAC7B,MAAI,CAAC,iBAAiB,IAAI,GAAG;AACzB,UAAM,IAAI,MAAM,mCAAmC,IAAI,EAAE;AAAA,EAC7D;AAEA,QAAM,MAAkB,CAAC;AACzB,QAAM,UAAsB,CAAC;AAC7B,QAAM,cAAc,CAAC,MAAmC,UAA4B;AAChF,QAAI,aAAc,KAAK,QAAQ,CAAC;A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AA,EACJ;AACJ;;;AMlBO,SAAS,UAAU,KAA0B;AAChD,SAAO;AAAA,IACH,eAAe;AAAA,MACX,GAAG,eAAe,GAAG;AAAA,IACzB;AAAA,IACA,eAAe,CAAC;AAAA,EACpB;AACJ;;;APHO,SAAS,YAAY,SAAwC;AAChE,SAAO;AAAA,IACH,IAAI,UAAU,QAAQ,GAAG,CAAC;AAAA,EAC9B;AACJ;","names":["visit","visit","visit","visit"]}
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@xyd-js/content",
3
- "version": "0.1.0-xyd.4",
3
+ "version": "0.1.0-xyd.6",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  "./package.json": "./package.json",
9
9
  ".": "./dist/index.js",
10
- "./navigation": "./dist/navigation.js"
10
+ "./vite": "./dist/vite.js"
11
11
  },
12
12
  "dependencies": {
13
13
  "@mdx-js/mdx": "^3.1.0",
@@ -19,13 +19,14 @@
19
19
  "mdast-util-mdx": "^3.0.0",
20
20
  "mdast-util-mdx-jsx": "^3.1.3",
21
21
  "mdast-util-to-string": "^4.0.0",
22
+ "remark-directive": "^3.0.0",
22
23
  "remark-frontmatter": "^5.0.0",
23
24
  "remark-gfm": "^4.0.0",
24
25
  "remark-mdx-frontmatter": "^5.0.0",
25
26
  "unified": "^11.0.5",
26
27
  "unist-util-visit": "^5.0.0",
27
28
  "vfile": "^6.0.3",
28
- "@xyd-js/core": "0.1.0-xyd.2"
29
+ "@xyd-js/core": "0.1.0-xyd.5"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/node": "^22.7.8",
@@ -0,0 +1,13 @@
1
+ import {RemarkMdxTocOptions} from "./plugins/md-toc";
2
+ import {defaultPlugins} from "./plugins"
3
+
4
+ export {RemarkMdxTocOptions} from "./plugins/md-toc";
5
+
6
+ export function mdOptions(toc: RemarkMdxTocOptions) {
7
+ return {
8
+ remarkPlugins: [
9
+ ...defaultPlugins(toc)
10
+ ],
11
+ rehypePlugins: []
12
+ }
13
+ }
@@ -0,0 +1,24 @@
1
+ import remarkFrontmatter from "remark-frontmatter";
2
+ import remarkMdxFrontmatter from "remark-mdx-frontmatter";
3
+ import remarkGfm from "remark-gfm";
4
+ import remarkDirective from 'remark-directive'
5
+
6
+ import {remarkMdxToc, RemarkMdxTocOptions} from "./md-toc";
7
+ import {remarkInjectCodeMeta} from "./md-code";
8
+ import {extractThemeSettings} from "./md-themeSettings";
9
+ import {extractPage} from "./md-page";
10
+ import {mdCodeGroup} from "./md-codegroup";
11
+
12
+ export function defaultPlugins(toc: RemarkMdxTocOptions) {
13
+ return [
14
+ remarkFrontmatter,
15
+ remarkMdxFrontmatter,
16
+ remarkGfm,
17
+ remarkDirective,
18
+ remarkMdxToc(toc),
19
+ remarkInjectCodeMeta,
20
+ extractThemeSettings,
21
+ extractPage,
22
+ mdCodeGroup
23
+ ]
24
+ }
@@ -0,0 +1,36 @@
1
+ import {visit} from 'unist-util-visit';
2
+
3
+ // This plugin transforms a custom container directive into a JSX node
4
+ // https://github.com/remarkjs/remark-directive is needed to parse the container directive
5
+
6
+ export function mdCodeGroup() {
7
+ return (tree: any) => {
8
+ visit(tree, 'containerDirective', (node) => {
9
+ if (node.name !== 'code-group') return;
10
+
11
+ const description = node.attributes?.title || '';
12
+ const codeblocks = [];
13
+
14
+ for (const child of node.children) {
15
+ if (child.type === 'code') {
16
+ const meta = child.meta || '';
17
+ const value = child.value || '';
18
+ const lang = child.lang || '';
19
+
20
+ codeblocks.push({value, lang, meta});
21
+ }
22
+ }
23
+
24
+ // Add metadata to the node
25
+ node.data = {
26
+ hName: 'DirectiveCodeSample',
27
+ hProperties: {
28
+ description,
29
+ codeblocks: JSON.stringify(codeblocks),
30
+ },
31
+ };
32
+
33
+ node.children = [];
34
+ });
35
+ };
36
+ }
@@ -1,8 +1,7 @@
1
1
  import type {Plugin} from 'rollup';
2
2
  import mdx from '@mdx-js/rollup';
3
3
 
4
- import {RemarkMdxTocOptions} from "@/mdx/toc";
5
- import {mdxOptions} from "@/mdx/options";
4
+ import {RemarkMdxTocOptions, mdOptions} from "../md";
6
5
 
7
6
  export interface VitePluginInterface {
8
7
  toc: RemarkMdxTocOptions
@@ -10,6 +9,6 @@ export interface VitePluginInterface {
10
9
 
11
10
  export function vitePlugins(options: VitePluginInterface): Plugin[] {
12
11
  return [
13
- mdx(mdxOptions(options.toc)),
12
+ mdx(mdOptions(options.toc)),
14
13
  ];
15
14
  }
@@ -4,7 +4,7 @@ import path from "path";
4
4
  import {VFile} from "vfile";
5
5
  import {compile as mdxCompile} from "@mdx-js/mdx";
6
6
 
7
- import {mdxOptions} from "@/mdx/options";
7
+ import {mdOptions} from "../packages/md";
8
8
 
9
9
  export async function compileBySlug(
10
10
  slug: string,
@@ -27,13 +27,13 @@ async function compile(content: string, filePath: string): Promise<string> {
27
27
  contents: content
28
28
  });
29
29
 
30
- const mdOptions = mdxOptions({
30
+ const opt = mdOptions({
31
31
  minDepth: 2 // TODO: configurable?
32
32
  })
33
33
 
34
34
  const compiled = await mdxCompile(vfile, {
35
- remarkPlugins: mdOptions.remarkPlugins,
36
- rehypePlugins: mdOptions.rehypePlugins,
35
+ remarkPlugins: opt.remarkPlugins,
36
+ rehypePlugins: opt.rehypePlugins,
37
37
  recmaPlugins: [],
38
38
  outputFormat: 'function-body',
39
39
  development: false,
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export {
2
+ compileBySlug
3
+ } from "./fs"
4
+
5
+ export {
6
+ pageFrontMatters,
7
+ filterNavigationByLevels
8
+ } from "./navigation"
@@ -96,27 +96,6 @@ export function filterNavigationByLevels(
96
96
  }
97
97
  }
98
98
 
99
- async function getFrontmatter(filePath: string, chunkSize = 1024 * 5) { // 5 KB chunk
100
- return new Promise((resolve, reject) => {
101
- open(filePath, 'r', (err, fd) => {
102
- if (err) return reject(err);
103
-
104
- const buffer = Buffer.alloc(chunkSize);
105
- fs2.read(fd, buffer, 0, chunkSize, 0, (err, bytesRead) => {
106
- if (err) return reject(err);
107
-
108
- const uint8Array = new Uint8Array(buffer.buffer, buffer.byteOffset, bytesRead);
109
- const content = new TextDecoder('utf-8').decode(uint8Array);
110
- const {data: frontmatter} = matter(content); // extract frontmatter
111
- resolve(frontmatter);
112
-
113
- fs2.close(fd, () => {
114
- });
115
- });
116
- });
117
- });
118
- }
119
-
120
99
  function mdxExport(code: string) {
121
100
  const scope = {
122
101
  Fragment: React.Fragment,
@@ -128,7 +107,7 @@ function mdxExport(code: string) {
128
107
  return fn(scope)
129
108
  }
130
109
 
131
- async function getFrontmatterV2(filePath: string): Promise<FrontMatter> {
110
+ async function getFrontmatter(filePath: string): Promise<FrontMatter> {
132
111
  const body = await fs.readFile(filePath, "utf-8");
133
112
 
134
113
  const vfile = new VFile({
@@ -157,9 +136,14 @@ async function getFrontmatterV2(filePath: string): Promise<FrontMatter> {
157
136
 
158
137
  const matter: FrontMatter = frontmatter
159
138
 
139
+ let title = ""
140
+ if (typeof matter.title === "string" ) {
141
+ title = matter.title
142
+ }
160
143
  if (reactFrontmatter) {
161
144
  if (typeof reactFrontmatter?.title === "function") {
162
145
  matter.title = {
146
+ title,
163
147
  code: reactFrontmatter.title.toString()
164
148
  }
165
149
  }
@@ -182,7 +166,7 @@ async function job(page: string, frontmatters: PageFrontMatter) {
182
166
  }
183
167
  }
184
168
 
185
- const matter = await getFrontmatterV2(filePath)
169
+ const matter = await getFrontmatter(filePath)
186
170
 
187
171
  frontmatters[page] = matter
188
172
  }
package/tsup.config.ts CHANGED
@@ -1,21 +1,21 @@
1
- import { defineConfig, Options } from 'tsup';
1
+ import {defineConfig, Options} from 'tsup';
2
2
 
3
3
  const config: Options = {
4
4
  entry: {
5
- index: 'index.ts',
6
- navigation: 'navigation.ts',
5
+ index: 'src/index.ts',
6
+ vite: 'packages/vite/index.ts',
7
7
  },
8
8
  dts: {
9
9
  entry: {
10
- index: 'index.ts',
11
- navigation: 'navigation.ts',
10
+ index: 'src/index.ts',
11
+ vite: 'packages/vite/index.ts',
12
12
  },
13
13
  resolve: true, // Resolve external types
14
14
  },
15
15
  format: ['esm'], // Output both ESM and CJS formats
16
16
  target: 'node16', // Ensure compatibility with Node.js 16
17
17
  splitting: false, // Disable code splitting
18
- sourcemap: false, // Generate source maps
18
+ sourcemap: true, // Generate source maps
19
19
  clean: true, // Clean the output directory before each build
20
20
  // esbuildOptions: (options) => {
21
21
  // options.platform = 'node'; // Ensure the platform is set to Node.js
@@ -1,6 +0,0 @@
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 };