fumadocs-mdx 13.0.4 → 13.0.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/bin.cjs +164 -133
- package/dist/bin.js +1 -1
- package/dist/bun/index.cjs +156 -132
- package/dist/bun/index.d.cts +6 -1
- package/dist/bun/index.d.ts +6 -1
- package/dist/bun/index.js +8 -11
- package/dist/{chunk-TZ5EQBFW.js → chunk-2AQRQXSO.js} +7 -1
- package/dist/chunk-2E2JCOSO.js +135 -0
- package/dist/chunk-4757L6ST.js +77 -0
- package/dist/{chunk-ZNVPB2IR.js → chunk-ETIN2W7C.js} +49 -41
- package/dist/next/index.js +2 -4
- package/dist/node/loader.cjs +146 -119
- package/dist/node/loader.js +6 -10
- package/dist/vite/index.cjs +167 -135
- package/dist/vite/index.js +9 -15
- package/dist/webpack/mdx.cjs +86 -75
- package/dist/webpack/mdx.js +3 -3
- package/dist/webpack/meta.cjs +64 -37
- package/dist/webpack/meta.js +3 -3
- package/package.json +7 -7
- package/dist/chunk-4JSFLXXT.js +0 -8
- package/dist/chunk-XYGORKQA.js +0 -130
- package/dist/chunk-YAIPHUCZ.js +0 -56
package/dist/bun/index.cjs
CHANGED
|
@@ -663,6 +663,12 @@ var import_zod = require("zod");
|
|
|
663
663
|
var import_promises = __toESM(require("fs/promises"), 1);
|
|
664
664
|
var import_node_path2 = __toESM(require("path"), 1);
|
|
665
665
|
var import_node_crypto = require("crypto");
|
|
666
|
+
|
|
667
|
+
// src/loaders/index.ts
|
|
668
|
+
var metaLoaderGlob = /\.(json|yaml)(\?.+?)?$/;
|
|
669
|
+
var mdxLoaderGlob = /\.mdx?(\?.+?)?$/;
|
|
670
|
+
|
|
671
|
+
// src/loaders/mdx/index.ts
|
|
666
672
|
var querySchema = import_zod.z.object({
|
|
667
673
|
only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
|
|
668
674
|
collection: import_zod.z.string().optional()
|
|
@@ -673,84 +679,88 @@ var cacheEntry = import_zod.z.object({
|
|
|
673
679
|
hash: import_zod.z.string().optional()
|
|
674
680
|
});
|
|
675
681
|
function createMdxLoader(configLoader) {
|
|
676
|
-
return
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
const
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
await import_promises.default.
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
682
|
+
return {
|
|
683
|
+
test: mdxLoaderGlob,
|
|
684
|
+
async load({
|
|
685
|
+
getSource,
|
|
686
|
+
development: isDevelopment,
|
|
687
|
+
query,
|
|
688
|
+
compiler,
|
|
689
|
+
filePath
|
|
690
|
+
}) {
|
|
691
|
+
const value = await getSource();
|
|
692
|
+
const matter = fumaMatter(value);
|
|
693
|
+
const parsed = querySchema.parse(query);
|
|
694
|
+
const config = await configLoader.getConfig();
|
|
695
|
+
let after;
|
|
696
|
+
if (!isDevelopment && config.global.experimentalBuildCache) {
|
|
697
|
+
const cacheDir = config.global.experimentalBuildCache;
|
|
698
|
+
const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
|
|
699
|
+
const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
|
|
700
|
+
if (cached && cached.hash === generateCacheHash(value)) return cached;
|
|
701
|
+
after = async () => {
|
|
702
|
+
await import_promises.default.mkdir(cacheDir, { recursive: true });
|
|
703
|
+
await import_promises.default.writeFile(
|
|
704
|
+
import_node_path2.default.join(cacheDir, cacheKey),
|
|
705
|
+
JSON.stringify({
|
|
706
|
+
...out,
|
|
707
|
+
hash: generateCacheHash(value)
|
|
708
|
+
})
|
|
709
|
+
);
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
const collection = parsed.collection ? config.getCollection(parsed.collection) : void 0;
|
|
713
|
+
let docCollection;
|
|
714
|
+
switch (collection?.type) {
|
|
715
|
+
case "doc":
|
|
716
|
+
docCollection = collection;
|
|
717
|
+
break;
|
|
718
|
+
case "docs":
|
|
719
|
+
docCollection = collection.docs;
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
if (docCollection?.schema) {
|
|
723
|
+
matter.data = await validate(
|
|
724
|
+
docCollection.schema,
|
|
725
|
+
matter.data,
|
|
726
|
+
{
|
|
727
|
+
source: value,
|
|
728
|
+
path: filePath
|
|
729
|
+
},
|
|
730
|
+
`invalid frontmatter in ${filePath}`
|
|
700
731
|
);
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
matter.data,
|
|
732
|
+
}
|
|
733
|
+
if (parsed.only === "frontmatter") {
|
|
734
|
+
return {
|
|
735
|
+
code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
|
|
736
|
+
map: null
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const data = {};
|
|
740
|
+
if (config.global.lastModifiedTime === "git") {
|
|
741
|
+
data.lastModified = (await getGitTimestamp(filePath))?.getTime();
|
|
742
|
+
}
|
|
743
|
+
const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
|
|
744
|
+
const compiled = await buildMDX(
|
|
745
|
+
`${getConfigHash(config)}:${parsed.collection ?? "global"}`,
|
|
746
|
+
"\n".repeat(lineOffset) + matter.content,
|
|
717
747
|
{
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
748
|
+
development: isDevelopment,
|
|
749
|
+
...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
|
|
750
|
+
postprocess: docCollection?.postprocess,
|
|
751
|
+
data,
|
|
752
|
+
filePath,
|
|
753
|
+
frontmatter: matter.data,
|
|
754
|
+
_compiler: compiler
|
|
755
|
+
}
|
|
722
756
|
);
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
|
|
727
|
-
map: null
|
|
757
|
+
const out = {
|
|
758
|
+
code: String(compiled.value),
|
|
759
|
+
map: compiled.map
|
|
728
760
|
};
|
|
761
|
+
await after?.();
|
|
762
|
+
return out;
|
|
729
763
|
}
|
|
730
|
-
const data = {};
|
|
731
|
-
if (config.global.lastModifiedTime === "git") {
|
|
732
|
-
data.lastModified = (await getGitTimestamp(filePath))?.getTime();
|
|
733
|
-
}
|
|
734
|
-
const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
|
|
735
|
-
const compiled = await buildMDX(
|
|
736
|
-
`${getConfigHash(config)}:${parsed.collection ?? "global"}`,
|
|
737
|
-
"\n".repeat(lineOffset) + matter.content,
|
|
738
|
-
{
|
|
739
|
-
development: isDevelopment,
|
|
740
|
-
...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
|
|
741
|
-
postprocess: docCollection?.postprocess,
|
|
742
|
-
data,
|
|
743
|
-
filePath,
|
|
744
|
-
frontmatter: matter.data,
|
|
745
|
-
_compiler: compiler
|
|
746
|
-
}
|
|
747
|
-
);
|
|
748
|
-
const out = {
|
|
749
|
-
code: String(compiled.value),
|
|
750
|
-
map: compiled.map
|
|
751
|
-
};
|
|
752
|
-
await after?.();
|
|
753
|
-
return out;
|
|
754
764
|
};
|
|
755
765
|
}
|
|
756
766
|
var hashes = /* @__PURE__ */ new WeakMap();
|
|
@@ -863,44 +873,64 @@ var querySchema2 = import_zod2.z.object({
|
|
|
863
873
|
}).loose();
|
|
864
874
|
function createMetaLoader(configLoader, resolve2 = {}) {
|
|
865
875
|
const { json: resolveJson = "js", yaml: resolveYaml = "js" } = resolve2;
|
|
866
|
-
|
|
867
|
-
const isJson = filePath.endsWith(".json");
|
|
868
|
-
const parsed = querySchema2.parse(query);
|
|
869
|
-
const collection = parsed.collection ? (await configLoader.getConfig()).getCollection(parsed.collection) : void 0;
|
|
870
|
-
if (!collection) return null;
|
|
871
|
-
let data;
|
|
872
|
-
try {
|
|
873
|
-
data = isJson ? JSON.parse(source) : (0, import_js_yaml2.load)(source);
|
|
874
|
-
} catch (e) {
|
|
875
|
-
throw new Error(`invalid data in ${filePath}`, { cause: e });
|
|
876
|
-
}
|
|
877
|
-
let schema;
|
|
878
|
-
switch (collection?.type) {
|
|
879
|
-
case "meta":
|
|
880
|
-
schema = collection.schema;
|
|
881
|
-
break;
|
|
882
|
-
case "docs":
|
|
883
|
-
schema = collection.meta.schema;
|
|
884
|
-
break;
|
|
885
|
-
}
|
|
886
|
-
if (schema) {
|
|
887
|
-
data = await validate(
|
|
888
|
-
schema,
|
|
889
|
-
data,
|
|
890
|
-
{ path: filePath, source },
|
|
891
|
-
`invalid data in ${filePath}`
|
|
892
|
-
);
|
|
893
|
-
}
|
|
894
|
-
let code;
|
|
876
|
+
function stringifyOutput(isJson, data) {
|
|
895
877
|
if (isJson) {
|
|
896
|
-
|
|
878
|
+
return resolveJson === "json" ? JSON.stringify(data) : `export default ${JSON.stringify(data)}`;
|
|
897
879
|
} else {
|
|
898
|
-
|
|
880
|
+
return resolveYaml === "yaml" ? (0, import_js_yaml2.dump)(data) : `export default ${JSON.stringify(data)}`;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return {
|
|
884
|
+
test: metaLoaderGlob,
|
|
885
|
+
async load({ filePath, query, getSource }) {
|
|
886
|
+
const parsed = querySchema2.parse(query);
|
|
887
|
+
const collection = parsed.collection ? (await configLoader.getConfig()).getCollection(parsed.collection) : void 0;
|
|
888
|
+
if (!collection) return null;
|
|
889
|
+
const isJson = filePath.endsWith(".json");
|
|
890
|
+
const source = await getSource();
|
|
891
|
+
let data;
|
|
892
|
+
try {
|
|
893
|
+
data = isJson ? JSON.parse(source) : (0, import_js_yaml2.load)(source);
|
|
894
|
+
} catch (e) {
|
|
895
|
+
throw new Error(`invalid data in ${filePath}`, { cause: e });
|
|
896
|
+
}
|
|
897
|
+
let schema;
|
|
898
|
+
switch (collection?.type) {
|
|
899
|
+
case "meta":
|
|
900
|
+
schema = collection.schema;
|
|
901
|
+
break;
|
|
902
|
+
case "docs":
|
|
903
|
+
schema = collection.meta.schema;
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
if (schema) {
|
|
907
|
+
data = await validate(
|
|
908
|
+
schema,
|
|
909
|
+
data,
|
|
910
|
+
{ path: filePath, source },
|
|
911
|
+
`invalid data in ${filePath}`
|
|
912
|
+
);
|
|
913
|
+
}
|
|
914
|
+
return {
|
|
915
|
+
code: stringifyOutput(isJson, data)
|
|
916
|
+
};
|
|
917
|
+
},
|
|
918
|
+
bun: {
|
|
919
|
+
async fallback({ getSource, filePath }) {
|
|
920
|
+
const source = await getSource();
|
|
921
|
+
const isJson = filePath.endsWith(".json");
|
|
922
|
+
let data;
|
|
923
|
+
try {
|
|
924
|
+
data = isJson ? JSON.parse(source) : (0, import_js_yaml2.load)(source);
|
|
925
|
+
} catch (e) {
|
|
926
|
+
throw new Error(`invalid data in ${filePath}`, { cause: e });
|
|
927
|
+
}
|
|
928
|
+
return {
|
|
929
|
+
loader: "object",
|
|
930
|
+
exports: data
|
|
931
|
+
};
|
|
932
|
+
}
|
|
899
933
|
}
|
|
900
|
-
return {
|
|
901
|
-
code,
|
|
902
|
-
map: null
|
|
903
|
-
};
|
|
904
934
|
};
|
|
905
935
|
}
|
|
906
936
|
|
|
@@ -909,31 +939,28 @@ var import_node_url = require("url");
|
|
|
909
939
|
var import_promises4 = __toESM(require("fs/promises"), 1);
|
|
910
940
|
var import_node_querystring = require("querystring");
|
|
911
941
|
var import_node_path4 = __toESM(require("path"), 1);
|
|
912
|
-
function toBun(loader
|
|
942
|
+
function toBun(loader) {
|
|
913
943
|
return (build) => {
|
|
914
|
-
|
|
915
|
-
build.onResolve({ filter: test }, (args) => {
|
|
944
|
+
build.onLoad({ filter: loader.test ?? /.+/ }, async (args) => {
|
|
916
945
|
const [filePath, query = ""] = args.path.split("?", 2);
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
source: content,
|
|
924
|
-
query: queryData.get(args.path) ?? {},
|
|
925
|
-
filePath: args.path,
|
|
946
|
+
const input = {
|
|
947
|
+
async getSource() {
|
|
948
|
+
return Bun.file(filePath).text();
|
|
949
|
+
},
|
|
950
|
+
query: (0, import_node_querystring.parse)(query),
|
|
951
|
+
filePath,
|
|
926
952
|
development: false,
|
|
927
953
|
compiler: {
|
|
928
954
|
addDependency() {
|
|
929
955
|
}
|
|
930
956
|
}
|
|
931
|
-
}
|
|
957
|
+
};
|
|
958
|
+
const result = await loader.load(input);
|
|
932
959
|
if (result === null) {
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
960
|
+
if (!loader.bun?.fallback) {
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
return loader.bun.fallback(input);
|
|
937
964
|
}
|
|
938
965
|
return {
|
|
939
966
|
contents: result.code,
|
|
@@ -943,16 +970,13 @@ function toBun(loader, test) {
|
|
|
943
970
|
};
|
|
944
971
|
}
|
|
945
972
|
|
|
946
|
-
// src/loaders/index.ts
|
|
947
|
-
var metaLoaderGlob = /\.(json|yaml)(\?.+?)?$/;
|
|
948
|
-
var mdxLoaderGlob = /\.mdx?(\?.+?)?$/;
|
|
949
|
-
|
|
950
973
|
// src/bun/index.ts
|
|
951
974
|
function createMdxPlugin(options = {}) {
|
|
952
975
|
const {
|
|
953
976
|
environment = "bun",
|
|
954
977
|
outDir = ".source",
|
|
955
|
-
configPath = findConfigFile()
|
|
978
|
+
configPath = findConfigFile(),
|
|
979
|
+
disableMetaFile = false
|
|
956
980
|
} = options;
|
|
957
981
|
return {
|
|
958
982
|
name: "bun-plugin-fumadocs-mdx",
|
|
@@ -966,8 +990,8 @@ function createMdxPlugin(options = {}) {
|
|
|
966
990
|
config: buildConfig(await import(importPath))
|
|
967
991
|
});
|
|
968
992
|
const configLoader = createIntegratedConfigLoader(core);
|
|
969
|
-
toBun(createMdxLoader(configLoader)
|
|
970
|
-
toBun(createMetaLoader(configLoader)
|
|
993
|
+
toBun(createMdxLoader(configLoader))(build);
|
|
994
|
+
if (!disableMetaFile) toBun(createMetaLoader(configLoader))(build);
|
|
971
995
|
}
|
|
972
996
|
};
|
|
973
997
|
}
|
package/dist/bun/index.d.cts
CHANGED
|
@@ -7,7 +7,12 @@ import 'unified';
|
|
|
7
7
|
import 'zod';
|
|
8
8
|
import 'chokidar';
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
interface MdxPluginOptions extends Partial<CoreOptions> {
|
|
11
|
+
/**
|
|
12
|
+
* Skip meta file transformation step
|
|
13
|
+
*/
|
|
14
|
+
disableMetaFile?: boolean;
|
|
15
|
+
}
|
|
11
16
|
declare function createMdxPlugin(options?: MdxPluginOptions): BunPlugin;
|
|
12
17
|
|
|
13
18
|
export { type MdxPluginOptions, createMdxPlugin };
|
package/dist/bun/index.d.ts
CHANGED
|
@@ -7,7 +7,12 @@ import 'unified';
|
|
|
7
7
|
import 'zod';
|
|
8
8
|
import 'chokidar';
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
interface MdxPluginOptions extends Partial<CoreOptions> {
|
|
11
|
+
/**
|
|
12
|
+
* Skip meta file transformation step
|
|
13
|
+
*/
|
|
14
|
+
disableMetaFile?: boolean;
|
|
15
|
+
}
|
|
11
16
|
declare function createMdxPlugin(options?: MdxPluginOptions): BunPlugin;
|
|
12
17
|
|
|
13
18
|
export { type MdxPluginOptions, createMdxPlugin };
|
package/dist/bun/index.js
CHANGED
|
@@ -3,25 +3,21 @@ import {
|
|
|
3
3
|
} from "../chunk-2HXTGJBI.js";
|
|
4
4
|
import {
|
|
5
5
|
createMdxLoader
|
|
6
|
-
} from "../chunk-
|
|
6
|
+
} from "../chunk-2E2JCOSO.js";
|
|
7
7
|
import {
|
|
8
8
|
createMetaLoader
|
|
9
|
-
} from "../chunk-
|
|
9
|
+
} from "../chunk-4757L6ST.js";
|
|
10
10
|
import {
|
|
11
11
|
createIntegratedConfigLoader,
|
|
12
12
|
toBun
|
|
13
|
-
} from "../chunk-
|
|
13
|
+
} from "../chunk-ETIN2W7C.js";
|
|
14
14
|
import "../chunk-3J3WL7WN.js";
|
|
15
15
|
import "../chunk-K5ZLPEIQ.js";
|
|
16
|
-
import {
|
|
17
|
-
mdxLoaderGlob,
|
|
18
|
-
metaLoaderGlob
|
|
19
|
-
} from "../chunk-4JSFLXXT.js";
|
|
20
16
|
import "../chunk-VUEZTR2H.js";
|
|
21
17
|
import {
|
|
22
18
|
createCore,
|
|
23
19
|
findConfigFile
|
|
24
|
-
} from "../chunk-
|
|
20
|
+
} from "../chunk-2AQRQXSO.js";
|
|
25
21
|
import "../chunk-VWJKRQZR.js";
|
|
26
22
|
|
|
27
23
|
// src/bun/index.ts
|
|
@@ -30,7 +26,8 @@ function createMdxPlugin(options = {}) {
|
|
|
30
26
|
const {
|
|
31
27
|
environment = "bun",
|
|
32
28
|
outDir = ".source",
|
|
33
|
-
configPath = findConfigFile()
|
|
29
|
+
configPath = findConfigFile(),
|
|
30
|
+
disableMetaFile = false
|
|
34
31
|
} = options;
|
|
35
32
|
return {
|
|
36
33
|
name: "bun-plugin-fumadocs-mdx",
|
|
@@ -44,8 +41,8 @@ function createMdxPlugin(options = {}) {
|
|
|
44
41
|
config: buildConfig(await import(importPath))
|
|
45
42
|
});
|
|
46
43
|
const configLoader = createIntegratedConfigLoader(core);
|
|
47
|
-
toBun(createMdxLoader(configLoader)
|
|
48
|
-
toBun(createMetaLoader(configLoader)
|
|
44
|
+
toBun(createMdxLoader(configLoader))(build);
|
|
45
|
+
if (!disableMetaFile) toBun(createMetaLoader(configLoader))(build);
|
|
49
46
|
}
|
|
50
47
|
};
|
|
51
48
|
}
|
|
@@ -105,9 +105,15 @@ function createCore(options, defaultPlugins = []) {
|
|
|
105
105
|
};
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
// src/loaders/index.ts
|
|
109
|
+
var metaLoaderGlob = /\.(json|yaml)(\?.+?)?$/;
|
|
110
|
+
var mdxLoaderGlob = /\.mdx?(\?.+?)?$/;
|
|
111
|
+
|
|
108
112
|
export {
|
|
109
113
|
ValidationError,
|
|
110
114
|
validate,
|
|
111
115
|
findConfigFile,
|
|
112
|
-
createCore
|
|
116
|
+
createCore,
|
|
117
|
+
metaLoaderGlob,
|
|
118
|
+
mdxLoaderGlob
|
|
113
119
|
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildMDX
|
|
3
|
+
} from "./chunk-3J3WL7WN.js";
|
|
4
|
+
import {
|
|
5
|
+
getGitTimestamp
|
|
6
|
+
} from "./chunk-VUEZTR2H.js";
|
|
7
|
+
import {
|
|
8
|
+
mdxLoaderGlob,
|
|
9
|
+
validate
|
|
10
|
+
} from "./chunk-2AQRQXSO.js";
|
|
11
|
+
import {
|
|
12
|
+
fumaMatter
|
|
13
|
+
} from "./chunk-VWJKRQZR.js";
|
|
14
|
+
|
|
15
|
+
// src/loaders/mdx/index.ts
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import fs from "fs/promises";
|
|
18
|
+
import path from "path";
|
|
19
|
+
import { createHash } from "crypto";
|
|
20
|
+
var querySchema = z.object({
|
|
21
|
+
only: z.literal(["frontmatter", "all"]).default("all"),
|
|
22
|
+
collection: z.string().optional()
|
|
23
|
+
}).loose();
|
|
24
|
+
var cacheEntry = z.object({
|
|
25
|
+
code: z.string(),
|
|
26
|
+
map: z.any().optional(),
|
|
27
|
+
hash: z.string().optional()
|
|
28
|
+
});
|
|
29
|
+
function createMdxLoader(configLoader) {
|
|
30
|
+
return {
|
|
31
|
+
test: mdxLoaderGlob,
|
|
32
|
+
async load({
|
|
33
|
+
getSource,
|
|
34
|
+
development: isDevelopment,
|
|
35
|
+
query,
|
|
36
|
+
compiler,
|
|
37
|
+
filePath
|
|
38
|
+
}) {
|
|
39
|
+
const value = await getSource();
|
|
40
|
+
const matter = fumaMatter(value);
|
|
41
|
+
const parsed = querySchema.parse(query);
|
|
42
|
+
const config = await configLoader.getConfig();
|
|
43
|
+
let after;
|
|
44
|
+
if (!isDevelopment && config.global.experimentalBuildCache) {
|
|
45
|
+
const cacheDir = config.global.experimentalBuildCache;
|
|
46
|
+
const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
|
|
47
|
+
const cached = await fs.readFile(path.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
|
|
48
|
+
if (cached && cached.hash === generateCacheHash(value)) return cached;
|
|
49
|
+
after = async () => {
|
|
50
|
+
await fs.mkdir(cacheDir, { recursive: true });
|
|
51
|
+
await fs.writeFile(
|
|
52
|
+
path.join(cacheDir, cacheKey),
|
|
53
|
+
JSON.stringify({
|
|
54
|
+
...out,
|
|
55
|
+
hash: generateCacheHash(value)
|
|
56
|
+
})
|
|
57
|
+
);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const collection = parsed.collection ? config.getCollection(parsed.collection) : void 0;
|
|
61
|
+
let docCollection;
|
|
62
|
+
switch (collection?.type) {
|
|
63
|
+
case "doc":
|
|
64
|
+
docCollection = collection;
|
|
65
|
+
break;
|
|
66
|
+
case "docs":
|
|
67
|
+
docCollection = collection.docs;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
if (docCollection?.schema) {
|
|
71
|
+
matter.data = await validate(
|
|
72
|
+
docCollection.schema,
|
|
73
|
+
matter.data,
|
|
74
|
+
{
|
|
75
|
+
source: value,
|
|
76
|
+
path: filePath
|
|
77
|
+
},
|
|
78
|
+
`invalid frontmatter in ${filePath}`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (parsed.only === "frontmatter") {
|
|
82
|
+
return {
|
|
83
|
+
code: `export const frontmatter = ${JSON.stringify(matter.data)}`,
|
|
84
|
+
map: null
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const data = {};
|
|
88
|
+
if (config.global.lastModifiedTime === "git") {
|
|
89
|
+
data.lastModified = (await getGitTimestamp(filePath))?.getTime();
|
|
90
|
+
}
|
|
91
|
+
const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
|
|
92
|
+
const compiled = await buildMDX(
|
|
93
|
+
`${getConfigHash(config)}:${parsed.collection ?? "global"}`,
|
|
94
|
+
"\n".repeat(lineOffset) + matter.content,
|
|
95
|
+
{
|
|
96
|
+
development: isDevelopment,
|
|
97
|
+
...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
|
|
98
|
+
postprocess: docCollection?.postprocess,
|
|
99
|
+
data,
|
|
100
|
+
filePath,
|
|
101
|
+
frontmatter: matter.data,
|
|
102
|
+
_compiler: compiler
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
const out = {
|
|
106
|
+
code: String(compiled.value),
|
|
107
|
+
map: compiled.map
|
|
108
|
+
};
|
|
109
|
+
await after?.();
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
var hashes = /* @__PURE__ */ new WeakMap();
|
|
115
|
+
function getConfigHash(config) {
|
|
116
|
+
let hash = hashes.get(config);
|
|
117
|
+
if (hash) return hash;
|
|
118
|
+
hash = Date.now().toString();
|
|
119
|
+
hashes.set(config, hash);
|
|
120
|
+
return hash;
|
|
121
|
+
}
|
|
122
|
+
function generateCacheHash(input) {
|
|
123
|
+
return createHash("md5").update(input).digest("hex");
|
|
124
|
+
}
|
|
125
|
+
function countLines(s) {
|
|
126
|
+
let num = 0;
|
|
127
|
+
for (const c of s) {
|
|
128
|
+
if (c === "\n") num++;
|
|
129
|
+
}
|
|
130
|
+
return num;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export {
|
|
134
|
+
createMdxLoader
|
|
135
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
metaLoaderGlob,
|
|
3
|
+
validate
|
|
4
|
+
} from "./chunk-2AQRQXSO.js";
|
|
5
|
+
|
|
6
|
+
// src/loaders/meta.ts
|
|
7
|
+
import { dump, load } from "js-yaml";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
var querySchema = z.object({
|
|
10
|
+
collection: z.string().optional()
|
|
11
|
+
}).loose();
|
|
12
|
+
function createMetaLoader(configLoader, resolve = {}) {
|
|
13
|
+
const { json: resolveJson = "js", yaml: resolveYaml = "js" } = resolve;
|
|
14
|
+
function stringifyOutput(isJson, data) {
|
|
15
|
+
if (isJson) {
|
|
16
|
+
return resolveJson === "json" ? JSON.stringify(data) : `export default ${JSON.stringify(data)}`;
|
|
17
|
+
} else {
|
|
18
|
+
return resolveYaml === "yaml" ? dump(data) : `export default ${JSON.stringify(data)}`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
test: metaLoaderGlob,
|
|
23
|
+
async load({ filePath, query, getSource }) {
|
|
24
|
+
const parsed = querySchema.parse(query);
|
|
25
|
+
const collection = parsed.collection ? (await configLoader.getConfig()).getCollection(parsed.collection) : void 0;
|
|
26
|
+
if (!collection) return null;
|
|
27
|
+
const isJson = filePath.endsWith(".json");
|
|
28
|
+
const source = await getSource();
|
|
29
|
+
let data;
|
|
30
|
+
try {
|
|
31
|
+
data = isJson ? JSON.parse(source) : load(source);
|
|
32
|
+
} catch (e) {
|
|
33
|
+
throw new Error(`invalid data in ${filePath}`, { cause: e });
|
|
34
|
+
}
|
|
35
|
+
let schema;
|
|
36
|
+
switch (collection?.type) {
|
|
37
|
+
case "meta":
|
|
38
|
+
schema = collection.schema;
|
|
39
|
+
break;
|
|
40
|
+
case "docs":
|
|
41
|
+
schema = collection.meta.schema;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
if (schema) {
|
|
45
|
+
data = await validate(
|
|
46
|
+
schema,
|
|
47
|
+
data,
|
|
48
|
+
{ path: filePath, source },
|
|
49
|
+
`invalid data in ${filePath}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
code: stringifyOutput(isJson, data)
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
bun: {
|
|
57
|
+
async fallback({ getSource, filePath }) {
|
|
58
|
+
const source = await getSource();
|
|
59
|
+
const isJson = filePath.endsWith(".json");
|
|
60
|
+
let data;
|
|
61
|
+
try {
|
|
62
|
+
data = isJson ? JSON.parse(source) : load(source);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
throw new Error(`invalid data in ${filePath}`, { cause: e });
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
loader: "object",
|
|
68
|
+
exports: data
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export {
|
|
76
|
+
createMetaLoader
|
|
77
|
+
};
|