MDX: Support mirror pages for symlinks of MDX file

This commit is contained in:
fuma-nama
2024-07-16 21:52:12 +08:00
parent 3a3ab3696a
commit c2d956b3b6
4 changed files with 38 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Support mirror pages for symlinks of MDX file
@@ -1 +0,0 @@
../headless/internationalization.mdx
@@ -0,0 +1,7 @@
---
title: Internationalization
description: Support multiple languages in your documentation
icon: Globe
_mdx:
mirror: ../headless/internationalization.mdx
---
+17 -4
View File
@@ -1,8 +1,10 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { createProcessor, type ProcessorOptions } from '@mdx-js/mdx';
import { type Processor } from '@mdx-js/mdx/internal-create-format-aware-processors';
import grayMatter from 'gray-matter';
import { type LoaderContext } from 'webpack';
import type { InternalFrontmatter } from '@/types';
import { getGitTimestamp } from './utils/git-timestamp';
export interface Options extends ProcessorOptions {
@@ -39,10 +41,8 @@ export default async function loader(
const context = this.context;
const filePath = this.resourcePath;
const { lastModifiedTime, ...options } = this.getOptions();
const { content, data: frontmatter } = grayMatter(source);
const detectedFormat = filePath.endsWith('.mdx') ? 'mdx' : 'md';
const format = options.format ?? detectedFormat;
let timestamp: number | undefined;
let processor = cache.get(format);
if (processor === undefined) {
@@ -55,16 +55,29 @@ export default async function loader(
cache.set(format, processor);
}
const matter = grayMatter(source);
const props = (matter.data as InternalFrontmatter)._mdx ?? {};
if (props.mirror) {
const mirrorPath = path.resolve(path.dirname(filePath), props.mirror);
this.addDependency(mirrorPath);
matter.content = await fs
.readFile(mirrorPath)
.then((res) => grayMatter(res.toString()).content);
}
let timestamp: number | undefined;
if (lastModifiedTime === 'git')
timestamp = (await getGitTimestamp(filePath))?.getTime();
processor
.process({
value: content,
value: matter.content,
path: filePath,
data: {
lastModified: timestamp,
frontmatter,
frontmatter: matter.data,
},
})
.then(
+9
View File
@@ -33,4 +33,13 @@ export type MDXPageData<Frontmatter extends PageData = PageData> = Omit<
exports: Omit<MDXExport<Frontmatter>, 'frontmatter'>;
};
export interface InternalFrontmatter {
_mdx?: {
/**
* Mirror another MDX file
*/
mirror?: string;
};
}
export type { SearchIndex } from './webpack-plugins/search-index-plugin';