@mdast2docx/image
Version:
MDAST to DOCX plugin for resolving and embedding images. Supports base64, URLs, and custom resolvers for seamless DOCX image integration.
69 lines (68 loc) • 2.29 kB
TypeScript
import type { IImageOptions } from "docx";
import type { IPlugin, Optional } from "@m2d/core";
/**
* A function that resolves an image source into a `docx`-compatible image options object.
*
* @param src - Image source, either a base64-encoded string or URL.
* @param options - Plugin options used during image resolution.
* @returns Promise resolving to image options used in DOCX generation.
*/
export type ImageResolver = (src: string, options: IDefaultImagePluginOptions, isPlaceholder?: boolean) => Promise<IImageOptions>;
/**
* Configuration options for the image plugin.
*/
export interface IDefaultImagePluginOptions {
/**
* Scaling factor applied to base64 images to simulate resolution.
*
* @default 3
*/
scale: number;
/**
* Fallback image format for unsupported or unrecognized types.
*
* @default "png"
*/
fallbackImageType: "png" | "jpg" | "bmp" | "gif";
/**
* Custom function to resolve image sources into DOCX image options.
*/
imageResolver: ImageResolver;
/**
* Maximum allowed image width in inches.
*/
maxW: number;
/**
* Maximum allowed image height in inches.
*/
maxH: number;
/**
* Placeholder Image Src
*/
placeholder?: string;
/**
* Target DPI (dots per inch) to calculate dimensions from pixels.
*/
dpi: number;
}
/**
* Optional configuration input for the plugin constructor.
* The `dpi` field is managed internally and excluded.
*/
type IImagePluginOptions = Optional<Omit<IDefaultImagePluginOptions, "dpi">>;
/**
* Determines the MIME type of an image buffer using file signature detection.
*
* @param buffer - Binary image data as a Buffer or ArrayBuffer.
* @returns Detected MIME type, or `undefined` if unknown.
*/
export declare const getImageMimeType: (buffer: Buffer | ArrayBuffer) => "bmp" | "png" | "jpg" | "gif" | undefined;
/**
* Image plugin for processing inline image nodes in the Markdown AST.
* Resolves both base64 and URL-based images for inclusion in DOCX.
*
* @param options - Optional image plugin configuration.
* @returns Plugin implementation for use in the `@m2d/core` pipeline.
*/
export declare const imagePlugin: (options?: IImagePluginOptions) => IPlugin;
export {};