mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 23:49:51 +00:00
MDX: Support mirror pages for symlinks of MDX file
This commit is contained in:
@@ -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
|
||||
---
|
||||
@@ -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(
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user