MDX: Change cache path of manifest files

This commit is contained in:
fuma-nama
2024-09-07 21:10:27 +08:00
parent 558173387a
commit f21c871db2
3 changed files with 15 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Change cache path of manifest files
+2 -2
View File
@@ -8,7 +8,7 @@ import { findCollectionId } from '@/utils/find-collection';
import { getConfigHash, loadConfigCached } from '@/config/cached';
import { buildMDX } from '@/utils/build-mdx';
import { getDefaultMDXOptions, type TransformContext } from '@/config';
import { getKey } from '@/map/manifest';
import { getManifestEntryPath } from '@/map/manifest';
import { formatError } from '@/utils/format-error';
import { getGitTimestamp } from './utils/git-timestamp';
@@ -154,7 +154,7 @@ export default async function loader(
if (config.global?.generateManifest) {
await fs.mkdir('.next/cache/fumadocs', { recursive: true });
await fs.writeFile(
path.resolve('.next/cache/fumadocs', `${getKey(filePath)}.json`),
getManifestEntryPath(filePath),
JSON.stringify({
path: filePath,
data: file.data,
+8 -6
View File
@@ -1,5 +1,4 @@
import fs from 'node:fs';
import { createHash } from 'node:crypto';
import path from 'node:path';
import { type LoadedConfig } from '@/config/load';
import type { MetaFile } from '@/loader-mdx';
@@ -11,8 +10,13 @@ export interface Manifest {
})[];
}
export function getKey(key: string): string {
return createHash('sha256').update(key).digest('hex');
export function getManifestEntryPath(originalPath: string): string {
const toName = path
.relative(process.cwd(), originalPath)
.replaceAll(`..${path.sep}`, '-')
.replaceAll(path.sep, '_');
return path.resolve('.next/cache/fumadocs', `${toName}.json`);
}
export function writeManifest(to: string, config: LoadedConfig): void {
@@ -24,9 +28,7 @@ export function writeManifest(to: string, config: LoadedConfig): void {
if (type === 'meta') continue;
try {
const content = fs.readFileSync(
path.resolve('.next/cache/fumadocs', `${getKey(file)}.json`),
);
const content = fs.readFileSync(getManifestEntryPath(file));
const meta = JSON.parse(content.toString()) as MetaFile;
output.files.push({