MDX: Support JSON Schema plugin (fumadocs-mdx/plugins/json-schema)

This commit is contained in:
Fuma Nama
2025-10-21 23:40:41 +08:00
parent 8d0c164786
commit 29ce826b29
4 changed files with 59 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': minor
---
Support JSON Schema plugin (`fumadocs-mdx/plugins/json-schema`)
+4
View File
@@ -60,6 +60,10 @@
"./bun": {
"import": "./dist/bun/index.js",
"types": "./dist/bun/index.d.ts"
},
"./plugins/*": {
"import": "./dist/plugins/*.js",
"types": "./dist/plugins/*.d.ts"
}
},
"main": "./dist/index.js",
+49
View File
@@ -0,0 +1,49 @@
import type { EmitEntry, Plugin } from '@/plugins';
import type { LoadedConfig } from '@/loaders/config';
import { z } from 'zod';
/**
* Generate JSON schemas locally for collection schemas
*
* note: **it only works with Zod**
*/
export default function jsonSchema(): Plugin {
let config: LoadedConfig;
return {
config(v) {
config = v;
},
emit() {
const files: EmitEntry[] = [];
function onSchema(name: string, schema: z.ZodSchema) {
files.push({
path: `json-schema/${name}.json`,
content: JSON.stringify(
z.toJSONSchema(schema, {
io: 'input',
unrepresentable: 'any',
}),
),
});
}
for (const [name, collection] of config.collections) {
if (collection.type === 'docs') {
if (collection.meta.schema instanceof z.ZodType) {
onSchema(`${name}.meta`, collection.meta.schema);
}
if (collection.docs.schema instanceof z.ZodType) {
onSchema(`${name}.docs`, collection.docs.schema);
}
} else if (collection.schema instanceof z.ZodType) {
onSchema(name, collection.schema);
}
}
return files;
},
};
}
+1
View File
@@ -7,6 +7,7 @@ export default defineConfig({
'./src/node/loader.ts',
'./src/runtime/next/{index,async}.ts',
'./src/runtime/vite/{browser,server}.ts',
'./src/plugins/{index,json-schema}.ts',
],
format: ['esm', 'cjs'],
external: ['next', 'typescript', 'bun'],