@@ -12,6 +12,15 @@ import { fileURLToPath } from 'node:url';
1212const VALID_BLUR_EXT = [ '.jpeg' , '.png' , '.webp' , '.avif' , '.jpg' ] ;
1313const EXTERNAL_URL_REGEX = / ^ h t t p s ? : \/ \/ / ;
1414
15+ type ExternalImageOptions =
16+ | {
17+ /**
18+ * timeout for fetching remote images (in milliseconds)
19+ */
20+ timeout ?: number ;
21+ }
22+ | boolean ;
23+
1524export 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
6271type 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