@hanzo/docs-mdx 14.1.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.
Files changed (83) hide show
  1. package/LICENSE +21 -0
  2. package/dist/bin.d.ts +1 -0
  3. package/dist/bin.js +16 -0
  4. package/dist/build-mdx-LN7FEKIP.js +8 -0
  5. package/dist/build-mdx-RXJZQXGA.js +8 -0
  6. package/dist/bun/index.d.ts +25 -0
  7. package/dist/bun/index.js +52 -0
  8. package/dist/chunk-4JSFLXXT.js +8 -0
  9. package/dist/chunk-5OBUOALK.js +141 -0
  10. package/dist/chunk-72JS4QVZ.js +17 -0
  11. package/dist/chunk-7I4W7XGI.js +179 -0
  12. package/dist/chunk-7W73RILB.js +173 -0
  13. package/dist/chunk-A3YIGE7S.js +334 -0
  14. package/dist/chunk-AOTZP2TV.js +152 -0
  15. package/dist/chunk-AXJB5MGS.js +173 -0
  16. package/dist/chunk-DMIIE3XZ.js +179 -0
  17. package/dist/chunk-DTFUANSF.js +40 -0
  18. package/dist/chunk-JWZR25CW.js +116 -0
  19. package/dist/chunk-K7N6GD4M.js +17 -0
  20. package/dist/chunk-LXB7WYAF.js +334 -0
  21. package/dist/chunk-MYAVS2LD.js +116 -0
  22. package/dist/chunk-OLD35ARB.js +116 -0
  23. package/dist/chunk-PW2AZGGD.js +125 -0
  24. package/dist/chunk-RGBNABKS.js +334 -0
  25. package/dist/chunk-RPUM7REY.js +40 -0
  26. package/dist/chunk-RR2X6AE6.js +274 -0
  27. package/dist/chunk-S7KOJHHO.js +89 -0
  28. package/dist/chunk-STUCUTJQ.js +334 -0
  29. package/dist/chunk-T6G5VOED.js +116 -0
  30. package/dist/chunk-U5YPLCO5.js +89 -0
  31. package/dist/chunk-UB55AMXC.js +274 -0
  32. package/dist/chunk-VITVHHR6.js +334 -0
  33. package/dist/chunk-VKSHE52K.js +274 -0
  34. package/dist/chunk-VWJKRQZR.js +19 -0
  35. package/dist/chunk-WAAWFNDX.js +179 -0
  36. package/dist/chunk-WGXYJGSZ.js +141 -0
  37. package/dist/chunk-WLJ6EKOZ.js +17 -0
  38. package/dist/chunk-WMYYALAS.js +334 -0
  39. package/dist/chunk-Y7ISNZ7X.js +216 -0
  40. package/dist/chunk-YKRCQ42E.js +216 -0
  41. package/dist/chunk-ZAYZWFWP.js +89 -0
  42. package/dist/config/index.d.ts +18 -0
  43. package/dist/config/index.js +63 -0
  44. package/dist/core-BQcKaqmC.d.ts +350 -0
  45. package/dist/core-BhWOtait.d.ts +350 -0
  46. package/dist/core-CV8uFMUa.d.ts +350 -0
  47. package/dist/core-DAQ64Hkq.d.ts +350 -0
  48. package/dist/core-DZtRjhds.d.ts +350 -0
  49. package/dist/core-De5K4ixv.d.ts +350 -0
  50. package/dist/core-X5ggQtBM.d.ts +350 -0
  51. package/dist/index-BqkSNsGO.d.ts +8 -0
  52. package/dist/index.d.ts +61 -0
  53. package/dist/index.js +0 -0
  54. package/dist/load-from-file-FHW724YY.js +8 -0
  55. package/dist/load-from-file-S5CQ4H6T.js +8 -0
  56. package/dist/next/index.cjs +1165 -0
  57. package/dist/next/index.d.ts +33 -0
  58. package/dist/next/index.js +181 -0
  59. package/dist/node/loader.d.ts +5 -0
  60. package/dist/node/loader.js +41 -0
  61. package/dist/plugins/index-file.d.ts +14 -0
  62. package/dist/plugins/index-file.js +8 -0
  63. package/dist/plugins/json-schema.d.ts +31 -0
  64. package/dist/plugins/json-schema.js +80 -0
  65. package/dist/plugins/last-modified.d.ts +40 -0
  66. package/dist/plugins/last-modified.js +89 -0
  67. package/dist/runtime/browser.d.ts +53 -0
  68. package/dist/runtime/browser.js +67 -0
  69. package/dist/runtime/dynamic.d.ts +27 -0
  70. package/dist/runtime/dynamic.js +79 -0
  71. package/dist/runtime/server.d.ts +14 -0
  72. package/dist/runtime/server.js +8 -0
  73. package/dist/runtime/types.d.ts +61 -0
  74. package/dist/runtime/types.js +0 -0
  75. package/dist/vite/index.d.ts +44 -0
  76. package/dist/vite/index.js +122 -0
  77. package/dist/webpack/mdx.d.ts +6 -0
  78. package/dist/webpack/mdx.js +37 -0
  79. package/dist/webpack/meta.d.ts +6 -0
  80. package/dist/webpack/meta.js +40 -0
  81. package/loader-mdx.cjs +7 -0
  82. package/loader-meta.cjs +7 -0
  83. package/package.json +137 -0
@@ -0,0 +1,334 @@
1
+ import {
2
+ createCodegen,
3
+ ident
4
+ } from "./chunk-PW2AZGGD.js";
5
+ import {
6
+ fumaMatter
7
+ } from "./chunk-VWJKRQZR.js";
8
+
9
+ // src/plugins/index-file.ts
10
+ import path2 from "path";
11
+ import { glob } from "tinyglobby";
12
+
13
+ // src/utils/fs-cache.ts
14
+ import fs from "fs/promises";
15
+ import path from "path";
16
+ var map = /* @__PURE__ */ new Map();
17
+ function createFSCache() {
18
+ return {
19
+ read(file) {
20
+ const fullPath = toFullPath(file);
21
+ const cached = map.get(fullPath);
22
+ if (cached) return cached;
23
+ const read = fs.readFile(fullPath).then((s) => s.toString());
24
+ map.set(fullPath, read);
25
+ return read;
26
+ },
27
+ delete(file) {
28
+ map.delete(toFullPath(file));
29
+ }
30
+ };
31
+ }
32
+ function toFullPath(file) {
33
+ if (path.isAbsolute(file)) {
34
+ return path.relative(process.cwd(), file);
35
+ }
36
+ return file;
37
+ }
38
+
39
+ // src/plugins/index-file.ts
40
+ import { createHash } from "crypto";
41
+ var indexFileCache = createFSCache();
42
+ function indexFile(options = {}) {
43
+ const {
44
+ target = "default",
45
+ addJsExtension,
46
+ browser = true,
47
+ dynamic = true
48
+ } = options;
49
+ let dynamicCollections;
50
+ function isDynamic(collection) {
51
+ return collection.type === "docs" && collection.docs.dynamic || collection.type === "doc" && collection.dynamic;
52
+ }
53
+ function generateConfigs(core) {
54
+ const serverOptions = {};
55
+ const typeConfigs = [
56
+ 'import("@hanzo/docs-mdx/runtime/types").InternalTypeConfig'
57
+ ];
58
+ const ctx = core.getPluginContext();
59
+ for (const plugin of core.getPlugins()) {
60
+ const indexFilePlugin = plugin["index-file"];
61
+ if (!indexFilePlugin) continue;
62
+ indexFilePlugin.serverOptions?.call(ctx, serverOptions);
63
+ const config = indexFilePlugin.generateTypeConfig?.call(ctx);
64
+ if (config) typeConfigs.push(config);
65
+ }
66
+ return {
67
+ serverOptions,
68
+ tc: typeConfigs.join(" & ")
69
+ };
70
+ }
71
+ return {
72
+ name: "index-file",
73
+ config() {
74
+ dynamicCollections = this.core.getCollections().filter(isDynamic);
75
+ },
76
+ configureServer(server) {
77
+ if (!server.watcher) return;
78
+ server.watcher.on("all", async (event, file) => {
79
+ indexFileCache.delete(file);
80
+ if (dynamicCollections.length === 0) {
81
+ if (target === "vite") return;
82
+ if (target === "default" && event === "change") return;
83
+ }
84
+ const updatedCollection = this.core.getCollections().find((collection) => collection.hasFile(file));
85
+ if (!updatedCollection) return;
86
+ if (!isDynamic(updatedCollection)) {
87
+ if (target === "vite") return;
88
+ if (target === "default" && event === "change") return;
89
+ }
90
+ await this.core.emit({
91
+ filterPlugin: (plugin) => plugin.name === "index-file",
92
+ filterWorkspace: () => false,
93
+ write: true
94
+ });
95
+ });
96
+ },
97
+ async emit() {
98
+ const globCache = /* @__PURE__ */ new Map();
99
+ const { workspace, outDir } = this.core.getOptions();
100
+ const { serverOptions, tc } = generateConfigs(this.core);
101
+ const toEmitEntry = async (path3, content) => {
102
+ const codegen = createCodegen({
103
+ target,
104
+ outDir,
105
+ jsExtension: addJsExtension,
106
+ globCache
107
+ });
108
+ await content({
109
+ core: this.core,
110
+ codegen,
111
+ serverOptions,
112
+ tc,
113
+ workspace: workspace?.name
114
+ });
115
+ return {
116
+ path: path3,
117
+ content: codegen.toString()
118
+ };
119
+ };
120
+ const out = [
121
+ toEmitEntry("server.ts", generateServerIndexFile)
122
+ ];
123
+ if (dynamic)
124
+ out.push(toEmitEntry("dynamic.ts", generateDynamicIndexFile));
125
+ if (browser)
126
+ out.push(toEmitEntry("browser.ts", generateBrowserIndexFile));
127
+ return await Promise.all(out);
128
+ }
129
+ };
130
+ }
131
+ async function generateServerIndexFile(ctx) {
132
+ const { core, codegen, serverOptions, tc } = ctx;
133
+ codegen.lines.push(
134
+ `import { server } from '@hanzo/docs/mdx/runtime/server';`,
135
+ `import type * as Config from '${codegen.formatImportPath(core.getOptions().configPath)}';`,
136
+ "",
137
+ `const create = server<typeof Config, ${tc}>(${JSON.stringify(serverOptions)});`
138
+ );
139
+ async function generateCollectionObject(collection) {
140
+ const base = getBase(collection);
141
+ switch (collection.type) {
142
+ case "docs": {
143
+ if (collection.docs.dynamic) return;
144
+ if (collection.docs.async) {
145
+ const [metaGlob2, headGlob, bodyGlob] = await Promise.all([
146
+ generateMetaCollectionGlob(ctx, collection.meta, true),
147
+ generateDocCollectionFrontmatterGlob(ctx, collection.docs, true),
148
+ generateDocCollectionGlob(ctx, collection.docs)
149
+ ]);
150
+ return `await create.docsLazy("${collection.name}", "${base}", ${metaGlob2}, ${headGlob}, ${bodyGlob})`;
151
+ }
152
+ const [metaGlob, docGlob] = await Promise.all([
153
+ generateMetaCollectionGlob(ctx, collection.meta, true),
154
+ generateDocCollectionGlob(ctx, collection.docs, true)
155
+ ]);
156
+ return `await create.docs("${collection.name}", "${base}", ${metaGlob}, ${docGlob})`;
157
+ }
158
+ case "doc":
159
+ if (collection.dynamic) return;
160
+ if (collection.async) {
161
+ const [headGlob, bodyGlob] = await Promise.all([
162
+ generateDocCollectionFrontmatterGlob(ctx, collection, true),
163
+ generateDocCollectionGlob(ctx, collection)
164
+ ]);
165
+ return `await create.docLazy("${collection.name}", "${base}", ${headGlob}, ${bodyGlob})`;
166
+ }
167
+ return `await create.doc("${collection.name}", "${base}", ${await generateDocCollectionGlob(
168
+ ctx,
169
+ collection,
170
+ true
171
+ )})`;
172
+ case "meta":
173
+ return `await create.meta("${collection.name}", "${base}", ${await generateMetaCollectionGlob(
174
+ ctx,
175
+ collection,
176
+ true
177
+ )})`;
178
+ }
179
+ }
180
+ await codegen.pushAsync(
181
+ core.getCollections().map(async (collection) => {
182
+ const obj = await generateCollectionObject(collection);
183
+ if (!obj) return;
184
+ return `
185
+ export const ${collection.name} = ${obj};`;
186
+ })
187
+ );
188
+ }
189
+ async function generateDynamicIndexFile(ctx) {
190
+ const { core, codegen, serverOptions, tc } = ctx;
191
+ const { configPath, environment, outDir } = core.getOptions();
192
+ const partialOptions = {
193
+ configPath,
194
+ environment,
195
+ outDir
196
+ };
197
+ codegen.lines.push(
198
+ `import { dynamic } from '@hanzo/docs/mdx/runtime/dynamic';`,
199
+ `import * as Config from '${codegen.formatImportPath(configPath)}';`,
200
+ "",
201
+ `const create = await dynamic<typeof Config, ${tc}>(Config, ${JSON.stringify(partialOptions)}, ${JSON.stringify(serverOptions)});`
202
+ );
203
+ async function generateCollectionObjectEntry(collection, absolutePath) {
204
+ const fullPath = path2.relative(process.cwd(), absolutePath);
205
+ const content = await indexFileCache.read(fullPath).catch(() => "");
206
+ const parsed = fumaMatter(content);
207
+ const data = await core.transformFrontmatter(
208
+ {
209
+ collection,
210
+ filePath: fullPath,
211
+ source: content
212
+ },
213
+ parsed.data
214
+ );
215
+ const hash = createHash("md5").update(content).digest("hex");
216
+ const infoStr = [
217
+ // make sure it's included in vercel/nft
218
+ `absolutePath: path.resolve(${JSON.stringify(fullPath)})`
219
+ ];
220
+ for (const [k, v] of Object.entries({
221
+ info: {
222
+ fullPath,
223
+ path: path2.relative(collection.dir, absolutePath)
224
+ },
225
+ data,
226
+ hash
227
+ })) {
228
+ infoStr.push(`${k}: ${JSON.stringify(v)}`);
229
+ }
230
+ return `{ ${infoStr.join(", ")} }`;
231
+ }
232
+ async function generateCollectionObject(parent) {
233
+ let collection;
234
+ if (parent.type === "doc") collection = parent;
235
+ else if (parent.type === "docs") collection = parent.docs;
236
+ if (!collection || !collection.dynamic) return;
237
+ const files = await glob(collection.patterns, {
238
+ cwd: collection.dir,
239
+ absolute: true
240
+ });
241
+ const entries = await Promise.all(
242
+ files.map((file) => generateCollectionObjectEntry(collection, file))
243
+ );
244
+ switch (parent.type) {
245
+ case "docs": {
246
+ const metaGlob = await generateMetaCollectionGlob(
247
+ ctx,
248
+ parent.meta,
249
+ true
250
+ );
251
+ return `await create.docs("${parent.name}", "${getBase(parent)}", ${metaGlob}, ${entries.join(", ")})`;
252
+ }
253
+ case "doc":
254
+ return `await create.doc("${collection.name}", "${getBase(collection)}", ${entries.join(", ")})`;
255
+ }
256
+ }
257
+ await codegen.pushAsync(
258
+ core.getCollections().map(async (collection) => {
259
+ const obj = await generateCollectionObject(collection);
260
+ if (!obj) return;
261
+ return `
262
+ export const ${collection.name} = ${obj};`;
263
+ })
264
+ );
265
+ }
266
+ async function generateBrowserIndexFile(ctx) {
267
+ const { core, codegen, tc } = ctx;
268
+ codegen.lines.push(
269
+ `import { browser } from '@hanzo/docs/mdx/runtime/browser';`,
270
+ `import type * as Config from '${codegen.formatImportPath(core.getOptions().configPath)}';`,
271
+ "",
272
+ `const create = browser<typeof Config, ${tc}>();`
273
+ );
274
+ async function generateCollectionObject(collection) {
275
+ switch (collection.type) {
276
+ case "docs": {
277
+ if (collection.docs.dynamic) return;
278
+ return generateCollectionObject(collection.docs);
279
+ }
280
+ case "doc":
281
+ if (collection.dynamic) return;
282
+ return `create.doc("${collection.name}", ${await generateDocCollectionGlob(ctx, collection)})`;
283
+ }
284
+ }
285
+ codegen.lines.push("const browserCollections = {");
286
+ await codegen.pushAsync(
287
+ core.getCollections().map(async (collection) => {
288
+ const obj = await generateCollectionObject(collection);
289
+ if (!obj) return;
290
+ return ident(`${collection.name}: ${obj},`);
291
+ })
292
+ );
293
+ codegen.lines.push("};", "export default browserCollections;");
294
+ }
295
+ function getBase(collection) {
296
+ return path2.relative(process.cwd(), collection.dir);
297
+ }
298
+ function generateDocCollectionFrontmatterGlob({ codegen, workspace }, collection, eager = false) {
299
+ return codegen.generateGlobImport(collection.patterns, {
300
+ query: {
301
+ collection: collection.name,
302
+ only: "frontmatter",
303
+ workspace
304
+ },
305
+ import: "frontmatter",
306
+ base: collection.dir,
307
+ eager
308
+ });
309
+ }
310
+ function generateDocCollectionGlob({ codegen, workspace }, collection, eager = false) {
311
+ return codegen.generateGlobImport(collection.patterns, {
312
+ query: {
313
+ collection: collection.name,
314
+ workspace
315
+ },
316
+ base: collection.dir,
317
+ eager
318
+ });
319
+ }
320
+ function generateMetaCollectionGlob({ codegen, workspace }, collection, eager = false) {
321
+ return codegen.generateGlobImport(collection.patterns, {
322
+ query: {
323
+ collection: collection.name,
324
+ workspace
325
+ },
326
+ import: "default",
327
+ base: collection.dir,
328
+ eager
329
+ });
330
+ }
331
+
332
+ export {
333
+ indexFile
334
+ };
@@ -0,0 +1,152 @@
1
+ // src/runtime/server.ts
2
+ import * as path from "path";
3
+ function server(options = {}) {
4
+ const { doc: { passthroughs: docPassthroughs = [] } = {} } = options;
5
+ function fileInfo(file, base) {
6
+ if (file.startsWith("./")) {
7
+ file = file.slice(2);
8
+ }
9
+ return {
10
+ path: file,
11
+ fullPath: path.join(base, file)
12
+ };
13
+ }
14
+ function mapDocData(entry) {
15
+ const cleanExports = /* @__PURE__ */ Object.create(null);
16
+ for (const key of Object.keys(entry)) {
17
+ cleanExports[key] = entry[key];
18
+ }
19
+ const data = {
20
+ body: entry.default,
21
+ toc: entry.toc,
22
+ structuredData: entry.structuredData,
23
+ _exports: cleanExports
24
+ };
25
+ for (const key of docPassthroughs) {
26
+ data[key] = entry[key];
27
+ }
28
+ return data;
29
+ }
30
+ return {
31
+ async doc(_name, base, glob) {
32
+ const out = await Promise.all(
33
+ Object.entries(glob).map(async ([k, v]) => {
34
+ const data = typeof v === "function" ? await v() : v;
35
+ return {
36
+ ...mapDocData(data),
37
+ ...data.frontmatter,
38
+ ...createDocMethods(fileInfo(k, base), () => data)
39
+ };
40
+ })
41
+ );
42
+ return out;
43
+ },
44
+ async docLazy(_name, base, head, body) {
45
+ const out = await Promise.all(
46
+ Object.entries(head).map(async ([k, v]) => {
47
+ const data = typeof v === "function" ? await v() : v;
48
+ const content = body[k];
49
+ const result = /* @__PURE__ */ Object.create(null);
50
+ if (data && typeof data === "object") {
51
+ for (const key of Object.keys(data)) {
52
+ result[key] = data[key];
53
+ }
54
+ }
55
+ const methods = createDocMethods(fileInfo(k, base), content);
56
+ result.info = methods.info;
57
+ result.getText = methods.getText;
58
+ result.getMDAST = methods.getMDAST;
59
+ result.load = async function() {
60
+ return mapDocData(await content());
61
+ };
62
+ return result;
63
+ })
64
+ );
65
+ return out;
66
+ },
67
+ async meta(_name, base, glob) {
68
+ const out = await Promise.all(
69
+ Object.entries(glob).map(async ([k, v]) => {
70
+ const data = typeof v === "function" ? await v() : v;
71
+ return {
72
+ info: fileInfo(k, base),
73
+ ...data
74
+ };
75
+ })
76
+ );
77
+ return out;
78
+ },
79
+ async docs(name, base, metaGlob, docGlob) {
80
+ const entry = {
81
+ docs: await this.doc(name, base, docGlob),
82
+ meta: await this.meta(name, base, metaGlob),
83
+ toDocsSource() {
84
+ return toDocsSource(this.docs, this.meta);
85
+ }
86
+ };
87
+ return entry;
88
+ },
89
+ async docsLazy(name, base, metaGlob, docHeadGlob, docBodyGlob) {
90
+ const entry = {
91
+ docs: await this.docLazy(name, base, docHeadGlob, docBodyGlob),
92
+ meta: await this.meta(name, base, metaGlob),
93
+ toDocsSource() {
94
+ return toDocsSource(this.docs, this.meta);
95
+ }
96
+ };
97
+ return entry;
98
+ }
99
+ };
100
+ }
101
+ function toDocsSource(pages, metas) {
102
+ const files = [];
103
+ for (const entry of pages) {
104
+ files.push({
105
+ type: "page",
106
+ path: entry.info.path,
107
+ absolutePath: entry.info.fullPath,
108
+ data: entry
109
+ });
110
+ }
111
+ for (const entry of metas) {
112
+ files.push({
113
+ type: "meta",
114
+ path: entry.info.path,
115
+ absolutePath: entry.info.fullPath,
116
+ data: entry
117
+ });
118
+ }
119
+ return {
120
+ files
121
+ };
122
+ }
123
+ function createDocMethods(info, load) {
124
+ return {
125
+ info,
126
+ async getText(type) {
127
+ if (type === "raw") {
128
+ const fs = await import("fs/promises");
129
+ return (await fs.readFile(info.fullPath)).toString();
130
+ }
131
+ const data = await load();
132
+ if (typeof data._markdown !== "string")
133
+ throw new Error(
134
+ "getText('processed') requires `includeProcessedMarkdown` to be enabled in your collection config."
135
+ );
136
+ return data._markdown;
137
+ },
138
+ async getMDAST() {
139
+ const data = await load();
140
+ if (!data._mdast)
141
+ throw new Error(
142
+ "getMDAST() requires `includeMDAST` to be enabled in your collection config."
143
+ );
144
+ return JSON.parse(data._mdast);
145
+ }
146
+ };
147
+ }
148
+
149
+ export {
150
+ server,
151
+ toDocsSource
152
+ };
@@ -0,0 +1,173 @@
1
+ import {
2
+ flattenNode,
3
+ remarkInclude
4
+ } from "./chunk-YKRCQ42E.js";
5
+
6
+ // src/loaders/mdx/build-mdx.ts
7
+ import { createProcessor } from "@mdx-js/mdx";
8
+ import { VFile } from "vfile";
9
+
10
+ // src/loaders/mdx/remark-postprocess.ts
11
+ import { visit } from "unist-util-visit";
12
+ import { toMarkdown } from "mdast-util-to-markdown";
13
+ import { valueToEstree } from "estree-util-value-to-estree";
14
+ import { removePosition } from "unist-util-remove-position";
15
+ import remarkMdx from "remark-mdx";
16
+ function remarkPostprocess({
17
+ _format,
18
+ includeProcessedMarkdown = false,
19
+ includeMDAST = false,
20
+ extractLinkReferences = false,
21
+ valueToExport = []
22
+ }) {
23
+ let _stringifyProcessor;
24
+ const getStringifyProcessor = () => {
25
+ return _stringifyProcessor ??= _format === "mdx" ? this : (
26
+ // force Markdown processor to stringify MDX nodes
27
+ this().use(remarkMdx).freeze()
28
+ );
29
+ };
30
+ return (tree, file) => {
31
+ const frontmatter = file.data.frontmatter ??= {};
32
+ if (!frontmatter.title) {
33
+ visit(tree, "heading", (node) => {
34
+ if (node.depth === 1) {
35
+ frontmatter.title = flattenNode(node);
36
+ return false;
37
+ }
38
+ });
39
+ }
40
+ file.data["mdx-export"] ??= [];
41
+ file.data["mdx-export"].push({
42
+ name: "frontmatter",
43
+ value: frontmatter
44
+ });
45
+ if (extractLinkReferences) {
46
+ const urls = [];
47
+ visit(tree, "link", (node) => {
48
+ urls.push({
49
+ href: node.url
50
+ });
51
+ return "skip";
52
+ });
53
+ file.data["mdx-export"].push({
54
+ name: "extractedReferences",
55
+ value: urls
56
+ });
57
+ }
58
+ if (includeProcessedMarkdown) {
59
+ const processor = getStringifyProcessor();
60
+ const markdown = toMarkdown(tree, {
61
+ ...processor.data("settings"),
62
+ // from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
63
+ extensions: processor.data("toMarkdownExtensions") || []
64
+ });
65
+ file.data["mdx-export"].push({
66
+ name: "_markdown",
67
+ value: markdown
68
+ });
69
+ }
70
+ if (includeMDAST) {
71
+ const options = includeMDAST === true ? {} : includeMDAST;
72
+ const mdast = JSON.stringify(
73
+ options.removePosition ? removePosition(structuredClone(tree)) : tree
74
+ );
75
+ file.data["mdx-export"].push({
76
+ name: "_mdast",
77
+ value: mdast
78
+ });
79
+ }
80
+ for (const { name, value } of file.data["mdx-export"]) {
81
+ tree.children.unshift(getMdastExport(name, value));
82
+ }
83
+ file.data["mdx-export"] = [];
84
+ for (const name of valueToExport) {
85
+ if (!(name in file.data)) continue;
86
+ tree.children.unshift(getMdastExport(name, file.data[name]));
87
+ }
88
+ };
89
+ }
90
+ function getMdastExport(name, value) {
91
+ return {
92
+ type: "mdxjsEsm",
93
+ value: "",
94
+ data: {
95
+ estree: {
96
+ type: "Program",
97
+ sourceType: "module",
98
+ body: [
99
+ {
100
+ type: "ExportNamedDeclaration",
101
+ attributes: [],
102
+ specifiers: [],
103
+ source: null,
104
+ declaration: {
105
+ type: "VariableDeclaration",
106
+ kind: "let",
107
+ declarations: [
108
+ {
109
+ type: "VariableDeclarator",
110
+ id: {
111
+ type: "Identifier",
112
+ name
113
+ },
114
+ init: valueToEstree(value)
115
+ }
116
+ ]
117
+ }
118
+ }
119
+ ]
120
+ }
121
+ }
122
+ };
123
+ }
124
+
125
+ // src/loaders/mdx/build-mdx.ts
126
+ async function buildMDX(core, collection, {
127
+ filePath,
128
+ frontmatter,
129
+ source,
130
+ _compiler,
131
+ environment,
132
+ isDevelopment
133
+ }) {
134
+ const mdxOptions = await core.getConfig().getMDXOptions(collection, environment);
135
+ function getProcessor(format) {
136
+ const cache = core.cache;
137
+ const key = `build-mdx:${collection?.name ?? "global"}:${format}`;
138
+ let processor = cache.get(key);
139
+ if (!processor) {
140
+ const postprocessOptions = {
141
+ _format: format,
142
+ ...collection?.postprocess
143
+ };
144
+ processor = createProcessor({
145
+ outputFormat: "program",
146
+ development: isDevelopment,
147
+ ...mdxOptions,
148
+ remarkPlugins: [
149
+ remarkInclude,
150
+ ...mdxOptions.remarkPlugins ?? [],
151
+ [remarkPostprocess, postprocessOptions]
152
+ ],
153
+ format
154
+ });
155
+ cache.set(key, processor);
156
+ }
157
+ return processor;
158
+ }
159
+ let vfile = new VFile({
160
+ value: source,
161
+ path: filePath,
162
+ cwd: collection?.cwd,
163
+ data: { frontmatter, _compiler, _getProcessor: getProcessor }
164
+ });
165
+ if (collection) {
166
+ vfile = await core.transformVFile({ collection, filePath, source }, vfile);
167
+ }
168
+ return getProcessor(filePath.endsWith(".mdx") ? "mdx" : "md").process(vfile);
169
+ }
170
+
171
+ export {
172
+ buildMDX
173
+ };