Skip to content

Commit

Permalink
MDX: Fix include hot-reload issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Dec 22, 2024
1 parent c49a485 commit 3445182
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-pillows-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---

Fix `include` hot-reload issues
4 changes: 2 additions & 2 deletions apps/docs/content/docs/ui/page-conventions.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Organizing Pages
description: Organize documents with file-system based routing
_mdx:
mirror: ../headless/page-conventions.mdx
---

<include>../headless/page-conventions.mdx</include>
19 changes: 0 additions & 19 deletions packages/mdx/src/loader-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ export interface Options {
};
}

interface InternalFrontmatter {
_mdx?: {
/**
* Mirror another MDX file
*/
mirror?: string;
};
}

export interface MetaFile {
path: string;
data: {
Expand Down Expand Up @@ -116,16 +107,6 @@ export default async function loader(
frontmatter = result.data as Record<string, unknown>;
}

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 (config.global?.lastModifiedTime === 'git')
timestamp = (await getGitTimestamp(filePath))?.getTime();
Expand Down
6 changes: 3 additions & 3 deletions packages/mdx/src/map/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ async function getCollectionFiles(
absolute: true,
});

result.forEach((item) => {
if (getTypeFromPath(item) !== collection.type) return;
for (const item of result) {
if (getTypeFromPath(item) !== collection.type) continue;

files.set(item, {
path: path.relative(dir, item),
absolutePath: item,
});
});
}
}),
);

Expand Down
38 changes: 28 additions & 10 deletions packages/mdx/src/utils/build-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Transformer } from 'unified';
import { visit } from 'unist-util-visit';
import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
import type { Literal } from 'mdast';
import { readFileSync } from 'node:fs';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import matter from 'gray-matter';

Expand Down Expand Up @@ -41,8 +41,19 @@ function cacheKey(group: string, format: string): string {
return `${group}:${format}`;
}

function remarkInclude(this: Processor, options: CompilerOptions): Transformer {
return (tree, file) => {
declare module 'vfile' {
interface DataMap {
/**
* The compiler object from loader
*/
_compiler?: CompilerOptions;
}
}

function remarkInclude(this: Processor): Transformer {
return async (tree, file) => {
const queue: Promise<void>[] = [];

visit(tree, 'mdxJsxFlowElement', (node: MdxJsxFlowElement) => {
if (node.name === 'include') {
const child = node.children.at(0) as Literal | undefined;
Expand All @@ -52,15 +63,22 @@ function remarkInclude(this: Processor, options: CompilerOptions): Transformer {

const targetPath = path.resolve(path.dirname(file.path), specifier);

const content = readFileSync(targetPath);
const parsed = this.parse(matter(content).content);
queue.push(
fs.readFile(targetPath).then((content) => {
const parsed = this.parse(matter(content).content);

options.addDependency(targetPath);
Object.assign(node, parsed);
if (file.data._compiler) {
file.data._compiler.addDependency(targetPath);
}
Object.assign(node, parsed);
}),
);
}

return 'skip';
});

await Promise.all(queue);
};
}

Expand Down Expand Up @@ -94,9 +112,8 @@ export function buildMDX(
development: process.env.NODE_ENV === 'development',
...rest,
remarkPlugins: [
options._compiler
? ([remarkInclude, options._compiler] as any)
: null,
// @ts-expect-error -- processor
remarkInclude,
...(rest.remarkPlugins ?? []),
],
format,
Expand All @@ -114,6 +131,7 @@ export function buildMDX(
data: {
...data,
frontmatter,
_compiler: options._compiler,
},
});
}

0 comments on commit 3445182

Please sign in to comment.