fuma-content 0.0.1 → 1.0.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/bin.d.ts +1 -0
- package/dist/bin.js +18 -0
- package/dist/build-mdx-Q4RMDWYK.js +8 -0
- package/dist/bun/index.d.ts +16 -0
- package/dist/bun/index.js +36 -0
- package/dist/bun-DMNX4PBC.js +40 -0
- package/dist/chunk-3VQS3KSP.js +39 -0
- package/dist/chunk-4RMSJCK2.js +50 -0
- package/dist/chunk-6XDQG2GV.js +17 -0
- package/dist/chunk-AMBGM4OK.js +412 -0
- package/dist/chunk-BTRE6MOX.js +16 -0
- package/dist/chunk-E4HRKSP4.js +24 -0
- package/dist/chunk-ERBMAQYP.js +33 -0
- package/dist/chunk-IGLM4N4P.js +34 -0
- package/dist/chunk-LDBQ66H3.js +38 -0
- package/dist/chunk-M72VQL5M.js +40 -0
- package/dist/chunk-OUJENWQ4.js +45 -0
- package/dist/chunk-PNA5UGSL.js +104 -0
- package/dist/chunk-RMSV4HP6.js +85 -0
- package/dist/chunk-RXR7OL76.js +37 -0
- package/dist/chunk-VWJKRQZR.js +19 -0
- package/dist/chunk-Z7KHD7MS.js +368 -0
- package/dist/collections/handlers/fs.d.ts +10 -0
- package/dist/collections/handlers/fs.js +6 -0
- package/dist/collections/index.d.ts +10 -0
- package/dist/collections/index.js +6 -0
- package/dist/collections/mdx/loader-webpack.d.ts +17 -0
- package/dist/collections/mdx/loader-webpack.js +38 -0
- package/dist/collections/mdx/runtime-browser.d.ts +44 -0
- package/dist/collections/mdx/runtime-browser.js +74 -0
- package/dist/collections/mdx/runtime-dynamic.d.ts +42 -0
- package/dist/collections/mdx/runtime-dynamic.js +94 -0
- package/dist/collections/mdx/runtime.d.ts +69 -0
- package/dist/collections/mdx/runtime.js +45 -0
- package/dist/collections/mdx.d.ts +10 -0
- package/dist/collections/mdx.js +287 -0
- package/dist/collections/meta/loader-webpack.d.ts +17 -0
- package/dist/collections/meta/loader-webpack.js +41 -0
- package/dist/collections/meta/runtime.d.ts +19 -0
- package/dist/collections/meta/runtime.js +18 -0
- package/dist/collections/meta.d.ts +10 -0
- package/dist/collections/meta.js +166 -0
- package/dist/collections/runtime/file-store.d.ts +19 -0
- package/dist/collections/runtime/file-store.js +7 -0
- package/dist/collections/runtime/store.d.ts +28 -0
- package/dist/collections/runtime/store.js +6 -0
- package/dist/config/index.d.ts +10 -0
- package/dist/config/index.js +7 -0
- package/dist/core-DxnSmTRe.d.ts +411 -0
- package/dist/index.d.ts +13 -31
- package/dist/index.js +0 -53
- package/dist/load-from-file-MLL4WQ5J.js +7 -0
- package/dist/loader-T756NSCS.js +7 -0
- package/dist/loader-VGDLYG4T.js +7 -0
- package/dist/next/index.cjs +571 -0
- package/dist/next/index.d.ts +27 -0
- package/dist/next/index.js +99 -0
- package/dist/node/loader.d.ts +5 -0
- package/dist/node/loader.js +46 -0
- package/dist/node-DCMYL4DL.js +34 -0
- package/dist/plugins/git.d.ts +10 -0
- package/dist/plugins/git.js +70 -0
- package/dist/plugins/json-schema.d.ts +10 -0
- package/dist/plugins/json-schema.js +59 -0
- package/dist/plugins/with-loader/index.d.ts +61 -0
- package/dist/plugins/with-loader/index.js +6 -0
- package/dist/plugins/with-loader/webpack.d.ts +27 -0
- package/dist/plugins/with-loader/webpack.js +11 -0
- package/dist/vite/index.d.ts +33 -0
- package/dist/vite/index.js +60 -0
- package/dist/vite-QCUPZHHB.js +32 -0
- package/package.json +103 -36
- package/dist/internal.d.ts +0 -108
- package/dist/internal.js +0 -384
package/dist/bin.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/bin.ts
|
|
4
|
+
import { existsSync } from "fs";
|
|
5
|
+
async function start() {
|
|
6
|
+
const [configPath, outDir] = process.argv.slice(2);
|
|
7
|
+
const isNext = existsSync("next.config.js") || existsSync("next.config.mjs") || existsSync("next.config.mts") || existsSync("next.config.ts");
|
|
8
|
+
if (isNext) {
|
|
9
|
+
const { createStandaloneCore } = await import("./next/index.js");
|
|
10
|
+
const core = await createStandaloneCore({ configPath, outDir });
|
|
11
|
+
await core.emit({ write: true });
|
|
12
|
+
} else {
|
|
13
|
+
const { createStandaloneCore } = await import("./vite/index.js");
|
|
14
|
+
const core = await createStandaloneCore({ configPath, outDir });
|
|
15
|
+
await core.emit({ write: true });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
void start();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BunPlugin } from 'bun';
|
|
2
|
+
import { C as CoreOptions } from '../core-DxnSmTRe.js';
|
|
3
|
+
import 'chokidar';
|
|
4
|
+
import '@mdx-js/mdx';
|
|
5
|
+
import 'vfile';
|
|
6
|
+
import '@standard-schema/spec';
|
|
7
|
+
import 'unified';
|
|
8
|
+
import 'mdast';
|
|
9
|
+
import 'vite';
|
|
10
|
+
import 'next';
|
|
11
|
+
import 'node:module';
|
|
12
|
+
|
|
13
|
+
type ContentPluginOptions = Partial<CoreOptions>;
|
|
14
|
+
declare function createContentPlugin(options?: ContentPluginOptions): BunPlugin;
|
|
15
|
+
|
|
16
|
+
export { type ContentPluginOptions, createContentPlugin };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildConfig
|
|
3
|
+
} from "../chunk-IGLM4N4P.js";
|
|
4
|
+
import {
|
|
5
|
+
Core
|
|
6
|
+
} from "../chunk-Z7KHD7MS.js";
|
|
7
|
+
import "../chunk-6XDQG2GV.js";
|
|
8
|
+
|
|
9
|
+
// src/bun/index.ts
|
|
10
|
+
import { pathToFileURL } from "url";
|
|
11
|
+
function createContentPlugin(options = {}) {
|
|
12
|
+
const {
|
|
13
|
+
outDir = Core.defaultOptions.outDir,
|
|
14
|
+
configPath = Core.defaultOptions.configPath
|
|
15
|
+
} = options;
|
|
16
|
+
return {
|
|
17
|
+
name: "fuma-content",
|
|
18
|
+
async setup(build) {
|
|
19
|
+
const importPath = pathToFileURL(configPath).href;
|
|
20
|
+
const core = new Core({
|
|
21
|
+
outDir,
|
|
22
|
+
configPath
|
|
23
|
+
});
|
|
24
|
+
await core.init({
|
|
25
|
+
config: buildConfig(await import(importPath))
|
|
26
|
+
});
|
|
27
|
+
const ctx = core.getPluginContext();
|
|
28
|
+
for (const plugin of core.getPlugins(true)) {
|
|
29
|
+
await plugin.bun?.build?.call(ctx, build);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
createContentPlugin
|
|
36
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/plugins/with-loader/bun.ts
|
|
2
|
+
import { parse } from "querystring";
|
|
3
|
+
import { readFileSync } from "fs";
|
|
4
|
+
function toBun(test = /.+/, loader) {
|
|
5
|
+
function toResult(output) {
|
|
6
|
+
if (!output) return;
|
|
7
|
+
return {
|
|
8
|
+
contents: output.code,
|
|
9
|
+
loader: "js"
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
return (build) => {
|
|
13
|
+
build.onLoad({ filter: test }, (args) => {
|
|
14
|
+
const [filePath, query = ""] = args.path.split("?", 2);
|
|
15
|
+
const input = {
|
|
16
|
+
async getSource() {
|
|
17
|
+
return Bun.file(filePath).text();
|
|
18
|
+
},
|
|
19
|
+
query: parse(query),
|
|
20
|
+
filePath,
|
|
21
|
+
development: false,
|
|
22
|
+
compiler: {
|
|
23
|
+
addDependency() {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
if (loader.bun?.load) {
|
|
28
|
+
return loader.bun.load(readFileSync(filePath).toString(), input);
|
|
29
|
+
}
|
|
30
|
+
const result = loader.load(input);
|
|
31
|
+
if (result instanceof Promise) {
|
|
32
|
+
return result.then(toResult);
|
|
33
|
+
}
|
|
34
|
+
return toResult(result);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
toBun
|
|
40
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/collections/runtime/store.ts
|
|
2
|
+
var SimpleCollectionStore = class {
|
|
3
|
+
dataMap;
|
|
4
|
+
dataList;
|
|
5
|
+
constructor(input) {
|
|
6
|
+
this.dataMap = input;
|
|
7
|
+
this.dataList = Array.from(input.values());
|
|
8
|
+
}
|
|
9
|
+
get(id) {
|
|
10
|
+
return this.dataMap.get(id);
|
|
11
|
+
}
|
|
12
|
+
list() {
|
|
13
|
+
return this.dataList;
|
|
14
|
+
}
|
|
15
|
+
$data(_cast) {
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* in-place transformation on all data
|
|
20
|
+
*/
|
|
21
|
+
transform(fn) {
|
|
22
|
+
this.dataList.length = 0;
|
|
23
|
+
const dataMap = this.dataMap;
|
|
24
|
+
const dataList = this.dataList;
|
|
25
|
+
for (const [k, v] of this.dataMap) {
|
|
26
|
+
const updated = fn(v);
|
|
27
|
+
dataMap.set(k, updated);
|
|
28
|
+
dataList.push(updated);
|
|
29
|
+
}
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
get $inferData() {
|
|
33
|
+
return void 0;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
SimpleCollectionStore
|
|
39
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ValidationError
|
|
3
|
+
} from "./chunk-OUJENWQ4.js";
|
|
4
|
+
import {
|
|
5
|
+
Core
|
|
6
|
+
} from "./chunk-Z7KHD7MS.js";
|
|
7
|
+
|
|
8
|
+
// src/plugins/with-loader/webpack.ts
|
|
9
|
+
import { parse } from "querystring";
|
|
10
|
+
import path from "path";
|
|
11
|
+
var core;
|
|
12
|
+
function getCore(options) {
|
|
13
|
+
return core ??= new Core({
|
|
14
|
+
outDir: options.outDir,
|
|
15
|
+
configPath: options.configPath
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function toWebpack(loader) {
|
|
19
|
+
return async function(source, callback) {
|
|
20
|
+
try {
|
|
21
|
+
const result = await loader.load({
|
|
22
|
+
filePath: this.resourcePath,
|
|
23
|
+
query: parse(this.resourceQuery.slice(1)),
|
|
24
|
+
getSource() {
|
|
25
|
+
return source;
|
|
26
|
+
},
|
|
27
|
+
development: this.mode === "development",
|
|
28
|
+
compiler: this
|
|
29
|
+
});
|
|
30
|
+
if (result === null) {
|
|
31
|
+
callback(void 0, source);
|
|
32
|
+
} else {
|
|
33
|
+
callback(void 0, result.code, result.map);
|
|
34
|
+
}
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error instanceof ValidationError) {
|
|
37
|
+
return callback(new Error(await error.toStringFormatted()));
|
|
38
|
+
}
|
|
39
|
+
if (!(error instanceof Error)) throw error;
|
|
40
|
+
const fpath = path.relative(this.context, this.resourcePath);
|
|
41
|
+
error.message = `${fpath}:${error.name}: ${error.message}`;
|
|
42
|
+
callback(error);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
getCore,
|
|
49
|
+
toWebpack
|
|
50
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/utils/async-cache.ts
|
|
2
|
+
function createCache(store = /* @__PURE__ */ new Map()) {
|
|
3
|
+
return {
|
|
4
|
+
store,
|
|
5
|
+
cached(key, fn) {
|
|
6
|
+
let cached = store.get(key);
|
|
7
|
+
if (cached) return cached;
|
|
8
|
+
cached = fn();
|
|
9
|
+
store.set(key, cached);
|
|
10
|
+
return cached;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
createCache
|
|
17
|
+
};
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fumaMatter
|
|
3
|
+
} from "./chunk-VWJKRQZR.js";
|
|
4
|
+
import {
|
|
5
|
+
createCache
|
|
6
|
+
} from "./chunk-6XDQG2GV.js";
|
|
7
|
+
|
|
8
|
+
// src/collections/mdx/build-mdx.ts
|
|
9
|
+
import { createProcessor } from "@mdx-js/mdx";
|
|
10
|
+
import { VFile as VFile2 } from "vfile";
|
|
11
|
+
|
|
12
|
+
// src/collections/mdx/remark-include.ts
|
|
13
|
+
import {
|
|
14
|
+
unified
|
|
15
|
+
} from "unified";
|
|
16
|
+
import { visit as visit2 } from "unist-util-visit";
|
|
17
|
+
import * as path from "path";
|
|
18
|
+
import * as fs from "fs/promises";
|
|
19
|
+
import { VFile } from "vfile";
|
|
20
|
+
|
|
21
|
+
// src/collections/mdx/remark-unravel.ts
|
|
22
|
+
import { visit } from "unist-util-visit";
|
|
23
|
+
function remarkMarkAndUnravel() {
|
|
24
|
+
return (tree) => {
|
|
25
|
+
visit(tree, (node, index, parent) => {
|
|
26
|
+
let offset = -1;
|
|
27
|
+
let all = true;
|
|
28
|
+
let oneOrMore = false;
|
|
29
|
+
if (parent && typeof index === "number" && node.type === "paragraph") {
|
|
30
|
+
const children = node.children;
|
|
31
|
+
while (++offset < children.length) {
|
|
32
|
+
const child = children[offset];
|
|
33
|
+
if (child.type === "mdxJsxTextElement" || child.type === "mdxTextExpression") {
|
|
34
|
+
oneOrMore = true;
|
|
35
|
+
} else if (child.type === "text" && child.value.trim().length === 0) {
|
|
36
|
+
} else {
|
|
37
|
+
all = false;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (all && oneOrMore) {
|
|
42
|
+
offset = -1;
|
|
43
|
+
const newChildren = [];
|
|
44
|
+
while (++offset < children.length) {
|
|
45
|
+
const child = children[offset];
|
|
46
|
+
if (child.type === "mdxJsxTextElement") {
|
|
47
|
+
child.type = "mdxJsxFlowElement";
|
|
48
|
+
}
|
|
49
|
+
if (child.type === "mdxTextExpression") {
|
|
50
|
+
child.type = "mdxFlowExpression";
|
|
51
|
+
}
|
|
52
|
+
if (child.type === "text" && /^[\t\r\n ]+$/.test(String(child.value))) {
|
|
53
|
+
} else {
|
|
54
|
+
newChildren.push(child);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
parent.children.splice(index, 1, ...newChildren);
|
|
58
|
+
return index;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/collections/mdx/mdast-utils.ts
|
|
66
|
+
function flattenNode(node) {
|
|
67
|
+
if ("children" in node)
|
|
68
|
+
return node.children.map((child) => flattenNode(child)).join("");
|
|
69
|
+
if ("value" in node) return node.value;
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/collections/mdx/remark-include.ts
|
|
74
|
+
var ElementLikeTypes = [
|
|
75
|
+
"mdxJsxFlowElement",
|
|
76
|
+
"mdxJsxTextElement",
|
|
77
|
+
"containerDirective",
|
|
78
|
+
"textDirective",
|
|
79
|
+
"leafDirective"
|
|
80
|
+
];
|
|
81
|
+
function isElementLike(node) {
|
|
82
|
+
return ElementLikeTypes.includes(node.type);
|
|
83
|
+
}
|
|
84
|
+
function parseElementAttributes(element) {
|
|
85
|
+
if (Array.isArray(element.attributes)) {
|
|
86
|
+
const attributes = {};
|
|
87
|
+
for (const attr of element.attributes) {
|
|
88
|
+
if (attr.type === "mdxJsxAttribute" && (typeof attr.value === "string" || attr.value === null)) {
|
|
89
|
+
attributes[attr.name] = attr.value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return attributes;
|
|
93
|
+
}
|
|
94
|
+
return element.attributes ?? {};
|
|
95
|
+
}
|
|
96
|
+
function parseSpecifier(specifier) {
|
|
97
|
+
const idx = specifier.lastIndexOf("#");
|
|
98
|
+
if (idx === -1) return { file: specifier };
|
|
99
|
+
return {
|
|
100
|
+
file: specifier.slice(0, idx),
|
|
101
|
+
section: specifier.slice(idx + 1)
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function extractSection(root, section) {
|
|
105
|
+
let nodes;
|
|
106
|
+
let capturingHeadingContent = false;
|
|
107
|
+
visit2(root, (node) => {
|
|
108
|
+
if (node.type === "heading") {
|
|
109
|
+
if (capturingHeadingContent) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
if (node.data?.hProperties?.id === section) {
|
|
113
|
+
capturingHeadingContent = true;
|
|
114
|
+
nodes = [node];
|
|
115
|
+
return "skip";
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (capturingHeadingContent) {
|
|
120
|
+
nodes?.push(node);
|
|
121
|
+
return "skip";
|
|
122
|
+
}
|
|
123
|
+
if (isElementLike(node) && node.name === "section") {
|
|
124
|
+
const attributes = parseElementAttributes(node);
|
|
125
|
+
if (attributes.id === section) {
|
|
126
|
+
nodes = node.children;
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
if (nodes)
|
|
132
|
+
return {
|
|
133
|
+
type: "root",
|
|
134
|
+
children: nodes
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function remarkInclude({ preprocess = [] } = {}) {
|
|
138
|
+
const TagName = "include";
|
|
139
|
+
const preprocessor = unified().use(remarkMarkAndUnravel).use(preprocess);
|
|
140
|
+
const embedContent = async (targetPath, heading, params, parent) => {
|
|
141
|
+
const { _getProcessor = () => this, _compiler } = parent.data;
|
|
142
|
+
let content;
|
|
143
|
+
try {
|
|
144
|
+
content = (await fs.readFile(targetPath)).toString();
|
|
145
|
+
} catch (e) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`failed to read file ${targetPath}
|
|
148
|
+
${e instanceof Error ? e.message : String(e)}`,
|
|
149
|
+
{ cause: e }
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
const ext = path.extname(targetPath);
|
|
153
|
+
_compiler?.addDependency(targetPath);
|
|
154
|
+
if (params.lang || ext !== ".md" && ext !== ".mdx") {
|
|
155
|
+
const lang = params.lang ?? ext.slice(1);
|
|
156
|
+
return {
|
|
157
|
+
type: "code",
|
|
158
|
+
lang,
|
|
159
|
+
meta: params.meta,
|
|
160
|
+
value: content,
|
|
161
|
+
data: {}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
const parser = await _getProcessor(ext === ".mdx" ? "mdx" : "md");
|
|
165
|
+
const parsed = fumaMatter(content);
|
|
166
|
+
const targetFile = new VFile({
|
|
167
|
+
path: targetPath,
|
|
168
|
+
value: parsed.content,
|
|
169
|
+
data: {
|
|
170
|
+
...parent.data,
|
|
171
|
+
frontmatter: parsed.data
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
let mdast = await preprocessor.run(
|
|
175
|
+
parser.parse(targetFile),
|
|
176
|
+
targetFile
|
|
177
|
+
);
|
|
178
|
+
if (heading) {
|
|
179
|
+
const extracted = extractSection(mdast, heading);
|
|
180
|
+
if (!extracted)
|
|
181
|
+
throw new Error(
|
|
182
|
+
`Cannot find section ${heading} in ${targetPath}, make sure you have encapsulated the section in a <section id="${heading}"> tag, or a :::section directive with remark-directive configured.`
|
|
183
|
+
);
|
|
184
|
+
mdast = extracted;
|
|
185
|
+
}
|
|
186
|
+
await update(mdast, targetFile);
|
|
187
|
+
return mdast;
|
|
188
|
+
};
|
|
189
|
+
async function update(tree, file) {
|
|
190
|
+
const queue = [];
|
|
191
|
+
visit2(tree, ElementLikeTypes, (_node, _, parent) => {
|
|
192
|
+
const node = _node;
|
|
193
|
+
if (node.name !== TagName) return;
|
|
194
|
+
const specifier = flattenNode(node);
|
|
195
|
+
if (specifier.length === 0) return "skip";
|
|
196
|
+
const attributes = parseElementAttributes(node);
|
|
197
|
+
const { file: relativePath, section } = parseSpecifier(specifier);
|
|
198
|
+
const targetPath = path.resolve(
|
|
199
|
+
"cwd" in attributes ? file.cwd : file.dirname ?? "",
|
|
200
|
+
relativePath
|
|
201
|
+
);
|
|
202
|
+
queue.push(
|
|
203
|
+
embedContent(targetPath, section, attributes, file).then((replace) => {
|
|
204
|
+
Object.assign(
|
|
205
|
+
parent && parent.type === "paragraph" ? parent : node,
|
|
206
|
+
replace
|
|
207
|
+
);
|
|
208
|
+
})
|
|
209
|
+
);
|
|
210
|
+
return "skip";
|
|
211
|
+
});
|
|
212
|
+
await Promise.all(queue);
|
|
213
|
+
}
|
|
214
|
+
return async (tree, file) => {
|
|
215
|
+
await update(tree, file);
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/collections/mdx/remark-postprocess.ts
|
|
220
|
+
import { visit as visit3 } from "unist-util-visit";
|
|
221
|
+
import { toMarkdown } from "mdast-util-to-markdown";
|
|
222
|
+
import { valueToEstree } from "estree-util-value-to-estree";
|
|
223
|
+
import { removePosition } from "unist-util-remove-position";
|
|
224
|
+
import remarkMdx from "remark-mdx";
|
|
225
|
+
function remarkPostprocess({
|
|
226
|
+
_format,
|
|
227
|
+
includeProcessedMarkdown = false,
|
|
228
|
+
includeMDAST = false,
|
|
229
|
+
extractLinkReferences = false,
|
|
230
|
+
valueToExport = []
|
|
231
|
+
}) {
|
|
232
|
+
let _stringifyProcessor;
|
|
233
|
+
const getStringifyProcessor = () => {
|
|
234
|
+
return _stringifyProcessor ??= _format === "mdx" ? this : (
|
|
235
|
+
// force Markdown processor to stringify MDX nodes
|
|
236
|
+
this().use(remarkMdx).freeze()
|
|
237
|
+
);
|
|
238
|
+
};
|
|
239
|
+
return (tree, file) => {
|
|
240
|
+
const frontmatter = file.data.frontmatter ??= {};
|
|
241
|
+
if (!frontmatter.title) {
|
|
242
|
+
visit3(tree, "heading", (node) => {
|
|
243
|
+
if (node.depth === 1) {
|
|
244
|
+
frontmatter.title = flattenNode(node);
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
file.data["mdx-export"] ??= [];
|
|
250
|
+
file.data["mdx-export"].push({
|
|
251
|
+
name: "frontmatter",
|
|
252
|
+
value: frontmatter
|
|
253
|
+
});
|
|
254
|
+
if (extractLinkReferences) {
|
|
255
|
+
const urls = [];
|
|
256
|
+
visit3(tree, "link", (node) => {
|
|
257
|
+
urls.push({
|
|
258
|
+
href: node.url
|
|
259
|
+
});
|
|
260
|
+
return "skip";
|
|
261
|
+
});
|
|
262
|
+
file.data["mdx-export"].push({
|
|
263
|
+
name: "extractedReferences",
|
|
264
|
+
value: urls
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
if (includeProcessedMarkdown) {
|
|
268
|
+
const processor = getStringifyProcessor();
|
|
269
|
+
const markdown = toMarkdown(tree, {
|
|
270
|
+
...processor.data("settings"),
|
|
271
|
+
// from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
|
|
272
|
+
extensions: processor.data("toMarkdownExtensions") || []
|
|
273
|
+
});
|
|
274
|
+
file.data["mdx-export"].push({
|
|
275
|
+
name: "_markdown",
|
|
276
|
+
value: markdown
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (includeMDAST) {
|
|
280
|
+
const options = includeMDAST === true ? {} : includeMDAST;
|
|
281
|
+
const mdast = JSON.stringify(
|
|
282
|
+
options.removePosition ? removePosition(structuredClone(tree)) : tree
|
|
283
|
+
);
|
|
284
|
+
file.data["mdx-export"].push({
|
|
285
|
+
name: "_mdast",
|
|
286
|
+
value: mdast
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
for (const { name, value } of file.data["mdx-export"]) {
|
|
290
|
+
tree.children.unshift(getMdastExport(name, value));
|
|
291
|
+
}
|
|
292
|
+
file.data["mdx-export"] = [];
|
|
293
|
+
for (const name of valueToExport) {
|
|
294
|
+
if (!(name in file.data)) continue;
|
|
295
|
+
tree.children.unshift(getMdastExport(name, file.data[name]));
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function getMdastExport(name, value) {
|
|
300
|
+
return {
|
|
301
|
+
type: "mdxjsEsm",
|
|
302
|
+
value: "",
|
|
303
|
+
data: {
|
|
304
|
+
estree: {
|
|
305
|
+
type: "Program",
|
|
306
|
+
sourceType: "module",
|
|
307
|
+
body: [
|
|
308
|
+
{
|
|
309
|
+
type: "ExportNamedDeclaration",
|
|
310
|
+
specifiers: [],
|
|
311
|
+
attributes: [],
|
|
312
|
+
source: null,
|
|
313
|
+
declaration: {
|
|
314
|
+
type: "VariableDeclaration",
|
|
315
|
+
kind: "let",
|
|
316
|
+
declarations: [
|
|
317
|
+
{
|
|
318
|
+
type: "VariableDeclarator",
|
|
319
|
+
id: {
|
|
320
|
+
type: "Identifier",
|
|
321
|
+
name
|
|
322
|
+
},
|
|
323
|
+
init: valueToEstree(value)
|
|
324
|
+
}
|
|
325
|
+
]
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
]
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// src/collections/mdx/remark-preprocess.ts
|
|
335
|
+
function remarkPreprocess(options) {
|
|
336
|
+
return async (tree, file) => {
|
|
337
|
+
if (file.data._preprocessed) return;
|
|
338
|
+
file.data._preprocessed = true;
|
|
339
|
+
if (options?.preprocessor) {
|
|
340
|
+
return await options.preprocessor.run(tree);
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/collections/mdx/build-mdx.ts
|
|
346
|
+
async function buildMDX(core, collection, {
|
|
347
|
+
filePath,
|
|
348
|
+
frontmatter,
|
|
349
|
+
source,
|
|
350
|
+
_compiler,
|
|
351
|
+
environment,
|
|
352
|
+
isDevelopment
|
|
353
|
+
}) {
|
|
354
|
+
const handler = collection?.handlers.mdx;
|
|
355
|
+
const processorCache = createCache(core.cache);
|
|
356
|
+
function getProcessor(format) {
|
|
357
|
+
const key = `build-mdx:${collection?.name ?? "global"}:${format}`;
|
|
358
|
+
return processorCache.cached(key, async () => {
|
|
359
|
+
const mdxOptions = await handler?.getMDXOptions?.(environment);
|
|
360
|
+
const preprocessPlugin = [
|
|
361
|
+
remarkPreprocess,
|
|
362
|
+
handler?.preprocess
|
|
363
|
+
];
|
|
364
|
+
const postprocessOptions = {
|
|
365
|
+
_format: format,
|
|
366
|
+
...handler?.postprocess
|
|
367
|
+
};
|
|
368
|
+
const remarkIncludeOptions = {
|
|
369
|
+
preprocess: [preprocessPlugin]
|
|
370
|
+
};
|
|
371
|
+
return createProcessor({
|
|
372
|
+
outputFormat: "program",
|
|
373
|
+
development: isDevelopment,
|
|
374
|
+
...mdxOptions,
|
|
375
|
+
remarkPlugins: [
|
|
376
|
+
preprocessPlugin,
|
|
377
|
+
[remarkInclude, remarkIncludeOptions],
|
|
378
|
+
...mdxOptions?.remarkPlugins ?? [],
|
|
379
|
+
[remarkPostprocess, postprocessOptions]
|
|
380
|
+
],
|
|
381
|
+
format
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
let vfile = new VFile2({
|
|
386
|
+
value: source,
|
|
387
|
+
path: filePath,
|
|
388
|
+
cwd: handler?.cwd,
|
|
389
|
+
data: {
|
|
390
|
+
frontmatter,
|
|
391
|
+
_compiler,
|
|
392
|
+
_getProcessor: getProcessor
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
if (collection && handler?.vfile) {
|
|
396
|
+
vfile = await handler.vfile.call(
|
|
397
|
+
{
|
|
398
|
+
collection,
|
|
399
|
+
filePath,
|
|
400
|
+
source
|
|
401
|
+
},
|
|
402
|
+
vfile
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
return (await getProcessor(filePath.endsWith(".mdx") ? "mdx" : "md")).process(
|
|
406
|
+
vfile
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export {
|
|
411
|
+
buildMDX
|
|
412
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/collections/index.ts
|
|
2
|
+
function createCollection(info, init) {
|
|
3
|
+
return {
|
|
4
|
+
name: "",
|
|
5
|
+
handlers: {},
|
|
6
|
+
init(options) {
|
|
7
|
+
this.name = options.name;
|
|
8
|
+
init(this, options);
|
|
9
|
+
},
|
|
10
|
+
typeInfo: info
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
createCollection
|
|
16
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/collections/handlers/fs.ts
|
|
2
|
+
import picomatch from "picomatch";
|
|
3
|
+
import path from "path";
|
|
4
|
+
function buildFileHandler({ workspace }, config, supportedFormats) {
|
|
5
|
+
const patterns = config.files ?? [`**/*.{${supportedFormats.join(",")}}`];
|
|
6
|
+
let matcher;
|
|
7
|
+
return {
|
|
8
|
+
patterns,
|
|
9
|
+
dir: workspace ? path.resolve(workspace.dir, config.dir) : config.dir,
|
|
10
|
+
isFileSupported(filePath) {
|
|
11
|
+
return supportedFormats.some((format) => filePath.endsWith(`.${format}`));
|
|
12
|
+
},
|
|
13
|
+
hasFile(filePath) {
|
|
14
|
+
if (!this.isFileSupported(filePath)) return false;
|
|
15
|
+
const relativePath = path.relative(this.dir, filePath);
|
|
16
|
+
if (relativePath.startsWith(`..${path.sep}`)) return false;
|
|
17
|
+
return (matcher ??= picomatch(patterns))(relativePath);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
buildFileHandler
|
|
24
|
+
};
|