Package Exports
- @m2d/image
Readme
@m2d/image 
Converts Markdown (
) and HTML<img>nodes into DOCX-compatibleImageRunelements.
Using
<img>tag requires@m2d/htmlplugin.
๐ฆ Installation
npm install @m2d/imagepnpm add @m2d/imageyarn add @m2d/image๐ Overview
The @m2d/image plugin for mdast2docx enables inline image rendering in DOCX files.
It supports:
- Base64 and external URLs
- Common image formats like
jpg,png,gif,bmp - Fallbacks and transformations
๐ ๏ธ Usage
import { imagePlugin } from "@m2d/image";
const plugins = [
htmlPlugin(),
mermaidPlugin(),
imagePlugin(), // โ
Place after htmlPlugin and mermaidPlugin
tablePlugin(),
];๐ง If you're using
@m2d/html, or@m2d/mermaidensure it comes before this plugin so HTML-based<img>or<svg>tags are parsed correctly.
๐งช Production Ready
This plugin is production-ready and powers inline image rendering for mdast2docx.
It has built-in fallbacks and intelligent resolution for base64 and external images.
๐ฌ Contributions and ideas are welcome!
Feel free to open an issue or PR.
๐ผ๏ธ Supported Image Types
jpegjpgpngbmpgif
SVG is supported with fallback rendering into PNG.
โ๏ธ Plugin Options
interface IImagePluginOptions {
scale?: number; // default: 3 โ scales image resolution when using base64
fallbackImageType?: "png" | "jpg" | "bmp" | "gif";
imageResolver?: (src: string, options?: IImagePluginOptions) => Promise<IImageOptions>;
}Custom Image Resolver
Use this to override how images are loaded and transformed:
const customResolver: ImageResolver = async (src, options) => {
const response = await fetch(src);
const arrayBuffer = await response.arrayBuffer();
return {
type: "png",
data: arrayBuffer,
transformation: {
width: 400,
height: 300,
},
};
};
imagePlugin({ imageResolver: customResolver });๐ง How It Works
- Checks if the image is a base64 or remote URL.
- Parses image format, dimensions, and scale.
- Wraps the image as a
docx.ImageRunwith metadata (like alt text). - Provides fallbacks if image type is unsupported or fails.
๐ก Features
- Inline Markdown images:
 - HTML
<img>tags: when combined with@m2d/html - Auto-scaled rendering using canvas
- SVG support with fallback to PNG via
canvas
โ ๏ธ Limitations
- Requires client-side (DOM) environment (uses
<canvas>,<img>, etc.) - Not compatible with server-side rendering (SSR) Node.js image plugin coming soon!
- External images must be accessible (CORS-safe URLs)
๐ Related Plugins/Packages
| Plugin | Purpose |
|---|---|
@m2d/core |
Converts extended MDAST to DOCX |
@m2d/html |
Parses raw HTML to extended MDAST |
@m2d/table |
Renders table nodes to DOCX |
@m2d/list |
Enhanced list support (tasks, bullets) |
โญ Support Us
If you find this useful:
- โญ Star mdast2docx on GitHub
- โค๏ธ Consider sponsoring
๐งพ License
MIT ยฉ Mayank Chaudhari
Made with ๐ by Mayank Kumar Chaudhari