Core: Support timeout in remark image options

This commit is contained in:
Fuma Nama
2025-10-31 23:43:31 +08:00
parent 7cc712f379
commit f7e15e2e0d
2 changed files with 28 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
Support `timeout` in remark image options
+23 -6
View File
@@ -12,6 +12,15 @@ import { fileURLToPath } from 'node:url';
const VALID_BLUR_EXT = ['.jpeg', '.png', '.webp', '.avif', '.jpg'];
const EXTERNAL_URL_REGEX = /^https?:\/\//;
type ExternalImageOptions =
| {
/**
* timeout for fetching remote images (in milliseconds)
*/
timeout?: number;
}
| boolean;
export interface RemarkImageOptions {
/**
* Directory or base URL to resolve absolute image paths
@@ -56,7 +65,7 @@ export interface RemarkImageOptions {
*
* @defaultValue true
*/
external?: boolean;
external?: ExternalImageOptions;
}
type Source =
@@ -149,9 +158,7 @@ export function remarkImage({
return out;
}
if (src.type === 'url' && !external) return;
const size = await getImageSize(src).catch((e) => {
const size = await getImageSize(src, external).catch((e) => {
throw new Error(
`[Remark Image] Failed obtain image size for ${node.url} (public directory configured as ${publicDir})`,
{
@@ -160,6 +167,8 @@ export function remarkImage({
);
});
if (!size) return;
return {
type: 'mdxJsxFlowElement',
name: 'img',
@@ -306,10 +315,18 @@ function parseSrc(
};
}
async function getImageSize(src: Source): Promise<ISizeCalculationResult> {
async function getImageSize(
src: Source,
onExternal: ExternalImageOptions,
): Promise<ISizeCalculationResult | undefined> {
if (src.type === 'file') return imageSizeFromFile(src.file);
if (onExternal === false) return;
const res = await fetch(src.url);
const { timeout } = typeof onExternal === 'object' ? onExternal : {};
const res = await fetch(src.url, {
signal:
typeof timeout === 'number' ? AbortSignal.timeout(timeout) : undefined,
});
if (!res.ok) {
throw new Error(
`[Remark Image] Failed to fetch ${src.url} (${res.status}): ${await res.text()}`,