-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.config.ts
More file actions
90 lines (82 loc) · 2.93 KB
/
source.config.ts
File metadata and controls
90 lines (82 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { defineConfig, defineDocs, frontmatterSchema, metaSchema } from 'fumadocs-mdx/config';
import { z } from 'zod';
// You can customise Zod schemas for frontmatter and `meta.json` here
// see https://fumadocs.dev/docs/mdx/collections
export const docs = defineDocs({
dir: 'content/docs',
docs: {
schema: frontmatterSchema.extend({
lastUpdated: z.union([z.string(), z.date()]).optional(),
latest: z.boolean().optional(),
}),
postprocess: {
includeProcessedMarkdown: true,
},
},
meta: {
schema: metaSchema,
},
});
// Rehype plugin for local <img src="/..."> tags in MDX files.
//
// Problem: In MDX, explicit <img> JSX tags are compiled as React.createElement('img', ...)
// and do NOT reliably go through the components map (unlike Markdown ![]() images).
// This means the `img` override in mdx-components.tsx is never called for them.
//
// Solution: Rename those nodes to <DocImage> (uppercase). Uppercase components ARE always
// resolved from the components context, so DocImage in mdx-components.tsx will be called.
// We also prepend basePath here since Next.js <Image> does NOT do it automatically —
// the src must already include basePath before being passed to next/image.
function rehypeDocImage() {
const basePath =
process.env.NEXT_PUBLIC_BASE_PATH ||
(process.env.NODE_ENV === 'production' ? '/plugins-doc-site' : '');
function walk(node: any) {
// MDX JSX nodes: mdxJsxFlowElement / mdxJsxTextElement
if (
(node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') &&
node.name === 'img' &&
Array.isArray(node.attributes)
) {
// Rename to DocImage so the components map entry is used (enabling ImageZoom)
node.name = 'DocImage';
// Prepend basePath to local src paths
for (const attr of node.attributes) {
if (
attr.type === 'mdxJsxAttribute' &&
attr.name === 'src' &&
typeof attr.value === 'string'
) {
const src: string = attr.value;
if (basePath && src.startsWith('/') && !src.startsWith('//') && !src.startsWith(basePath)) {
attr.value = basePath + src;
}
}
}
}
// Standard rehype element nodes (fallback for any genuine HTML <img> nodes)
if (
node.type === 'element' &&
node.tagName === 'img' &&
typeof node.properties?.src === 'string'
) {
const src: string = node.properties.src;
if (basePath && src.startsWith('/') && !src.startsWith('//') && !src.startsWith(basePath)) {
node.properties.src = basePath + src;
}
}
if (Array.isArray(node.children)) {
for (const child of node.children) walk(child);
}
}
return (tree: any) => walk(tree);
}
export default defineConfig({
mdxOptions: {
remarkImageOptions: {
external: false, // Disable fetching external image sizes
},
remarkPlugins: [],
rehypePlugins: [rehypeDocImage],
},
});