Skip to content

Commit f7e15e2

Browse files
committed
Core: Support timeout in remark image options
1 parent 7cc712f commit f7e15e2

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

.changeset/stupid-women-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'fumadocs-core': patch
3+
---
4+
5+
Support `timeout` in remark image options

packages/core/src/mdx-plugins/remark-image.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import { fileURLToPath } from 'node:url';
1212
const VALID_BLUR_EXT = ['.jpeg', '.png', '.webp', '.avif', '.jpg'];
1313
const EXTERNAL_URL_REGEX = /^https?:\/\//;
1414

15+
type ExternalImageOptions =
16+
| {
17+
/**
18+
* timeout for fetching remote images (in milliseconds)
19+
*/
20+
timeout?: number;
21+
}
22+
| boolean;
23+
1524
export interface RemarkImageOptions {
1625
/**
1726
* Directory or base URL to resolve absolute image paths
@@ -56,7 +65,7 @@ export interface RemarkImageOptions {
5665
*
5766
* @defaultValue true
5867
*/
59-
external?: boolean;
68+
external?: ExternalImageOptions;
6069
}
6170

6271
type Source =
@@ -149,9 +158,7 @@ export function remarkImage({
149158
return out;
150159
}
151160

152-
if (src.type === 'url' && !external) return;
153-
154-
const size = await getImageSize(src).catch((e) => {
161+
const size = await getImageSize(src, external).catch((e) => {
155162
throw new Error(
156163
`[Remark Image] Failed obtain image size for ${node.url} (public directory configured as ${publicDir})`,
157164
{
@@ -160,6 +167,8 @@ export function remarkImage({
160167
);
161168
});
162169

170+
if (!size) return;
171+
163172
return {
164173
type: 'mdxJsxFlowElement',
165174
name: 'img',
@@ -306,10 +315,18 @@ function parseSrc(
306315
};
307316
}
308317

309-
async function getImageSize(src: Source): Promise<ISizeCalculationResult> {
318+
async function getImageSize(
319+
src: Source,
320+
onExternal: ExternalImageOptions,
321+
): Promise<ISizeCalculationResult | undefined> {
310322
if (src.type === 'file') return imageSizeFromFile(src.file);
323+
if (onExternal === false) return;
311324

312-
const res = await fetch(src.url);
325+
const { timeout } = typeof onExternal === 'object' ? onExternal : {};
326+
const res = await fetch(src.url, {
327+
signal:
328+
typeof timeout === 'number' ? AbortSignal.timeout(timeout) : undefined,
329+
});
313330
if (!res.ok) {
314331
throw new Error(
315332
`[Remark Image] Failed to fetch ${src.url} (${res.status}): ${await res.text()}`,

0 commit comments

Comments
 (0)