MDX: Fix last-modified plugin date checking

This commit is contained in:
Fuma Nama
2025-11-27 20:49:20 +08:00
parent 7ab1192832
commit f59339b022
2 changed files with 12 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Fix `last-modified` plugin date checking
+7 -7
View File
@@ -3,8 +3,8 @@ import { x } from 'tinyexec';
import type { Plugin } from '@/core';
import { ident } from '@/utils/codegen';
const cache = new Map<string, Promise<Date>>();
type VersionControlFn = (filePath: string) => Promise<Date | undefined>;
const cache = new Map<string, Promise<Date | null>>();
type VersionControlFn = (filePath: string) => Promise<Date | null | undefined>;
export interface LastModifiedPluginOptions {
/**
@@ -78,8 +78,7 @@ export default function lastModified(
if (!filter(this.collection.name)) return;
const timestamp = await fn(this.filePath);
if (timestamp !== undefined) {
if (timestamp) {
file.data['mdx-export'] ??= [];
file.data['mdx-export'].push({
name: 'lastModified',
@@ -91,7 +90,7 @@ export default function lastModified(
};
}
async function getGitTimestamp(file: string): Promise<Date | undefined> {
async function getGitTimestamp(file: string): Promise<Date | null> {
const cached = cache.get(file);
if (cached) return cached;
@@ -104,9 +103,10 @@ async function getGitTimestamp(file: string): Promise<Date | undefined> {
},
);
return new Date(out.stdout);
const date = new Date(out.stdout);
return isNaN(date.getTime()) ? null : date;
})();
cache.set(file, timePromise);
return timePromise.catch(() => undefined);
return timePromise.catch(() => null);
}