mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-25 04:20:02 +00:00
Core: Support timeout in remark image options
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'fumadocs-core': patch
|
||||
---
|
||||
|
||||
Support `timeout` in remark image options
|
||||
@@ -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()}`,
|
||||
|
||||
Reference in New Issue
Block a user