MDX: Support customising included files in the map file

This commit is contained in:
Fuma Nama
2024-03-10 19:54:25 +08:00
parent d80b0974b5
commit e47c62f8a3
5 changed files with 55 additions and 31 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"fumadocs-mdx": patch
---
Support customising included files in the map file
+5
View File
@@ -0,0 +1,5 @@
---
"fumadocs-core": patch
---
Improve remark plugin typings
+29 -22
View File
@@ -60,6 +60,11 @@ export interface CreateMDXOptions {
* @defaultValue `'./content'`
*/
rootContentPath?: string;
/**
* {@link LoaderOptions.include}
*/
include?: string | string[];
}
function pluginOption(
@@ -89,29 +94,29 @@ function getMDXLoaderOptions({
...valueToExport,
];
const remarkPlugins = pluginOption(
(v) => [
remarkGfm,
remarkHeading,
...(remarkImageOptions === false
? []
: [[remarkImage, remarkImageOptions] satisfies Pluggable]),
...v,
remarkStructure,
[remarkMdxExport, { values: mdxExports }],
],
mdxOptions.remarkPlugins,
);
const remarkPlugins = pluginOption((v) => {
const plugins: Pluggable[] = [remarkGfm, remarkHeading];
const rehypePlugins: PluggableList = pluginOption(
(v) => [
...(rehypeCodeOptions === false
? []
: [[rehypeCode, rehypeCodeOptions] satisfies Pluggable]),
...v,
],
mdxOptions.rehypePlugins,
);
if (remarkImageOptions !== false)
plugins.push([remarkImage, remarkImageOptions]);
plugins.push(...v, remarkStructure, [
remarkMdxExport,
{ values: mdxExports },
]);
return plugins;
}, mdxOptions.remarkPlugins);
const rehypePlugins: PluggableList = pluginOption((v) => {
const plugins: Pluggable[] = [...v];
if (rehypeCodeOptions !== false) {
plugins.unshift([rehypeCode, rehypeCodeOptions]);
}
return plugins;
}, mdxOptions.rehypePlugins);
return {
providerImportSource: 'next-mdx-import-source-file',
@@ -128,6 +133,7 @@ const createMDX =
rootMapPath = './.map.ts',
rootContentPath = './content',
buildSearchIndex = false,
...loadOptions
}: CreateMDXOptions = {}) =>
(nextConfig: NextConfig = {}) => {
const rootMapFile = path.resolve(cwd, rootMapPath);
@@ -169,6 +175,7 @@ const createMDX =
options: {
rootContentDir,
rootMapFile,
...loadOptions,
} satisfies LoaderOptions,
},
},
+13 -2
View File
@@ -5,6 +5,13 @@ import type { LoaderContext } from 'webpack';
export interface LoaderOptions {
rootContentDir: string;
rootMapFile: string;
/**
* Included files in the map file
*
* @defaultValue './**/*.{md,mdx,json}'
*/
include?: string | string[];
}
/**
@@ -23,10 +30,14 @@ export default function loader(
callback(null, buildMap(options));
}
function buildMap({ rootContentDir, rootMapFile }: LoaderOptions): string {
function buildMap({
rootContentDir,
rootMapFile,
include = ['./**/*.{md,mdx,json}'],
}: LoaderOptions): string {
const mapDir = path.dirname(rootMapFile);
const files = fg.sync('./**/*.{md,mdx,json}', {
const files = fg.sync(include, {
cwd: rootContentDir,
});
@@ -21,6 +21,9 @@ export interface SearchIndex {
}
export interface Options {
rootContentDir: string;
rootMapFile: string;
/**
* Only build search indexes in production builds
*
@@ -28,13 +31,6 @@ export interface Options {
*/
productionOnly?: boolean;
/**
* Absolute path of root content directory
*/
rootContentDir: string;
rootMapFile: string;
/**
* @param path - MDX file path relative to root content dir
* @returns page URL