@m2d/image


Converts Markdown (
), HTML <img>
, and custom image
/svg
nodes into DOCX-compatible ImageRun
elements.
Parsing HTML or Mermaid to SVG or image tags requires @m2d/html
or @m2d/mermaid
, respectively.
However, @m2d/image
handles all image rendering to DOCX regardless of origin.
📦 Installation
npm install @m2d/image
pnpm add @m2d/image
yarn add @m2d/image
🚀 Overview
The @m2d/image
plugin for mdast2docx
renders image nodes in DOCX output.
It supports:
- Markdown image syntax:

- Images parsed from HTML via
@m2d/html
- Images or SVGs emitted by plugins like
@m2d/mermaid
- Base64 and external URLs
- Fallback handling
- Automatic caching
🛠️ Usage
import { imagePlugin } from "@m2d/image";
const plugins = [
htmlPlugin(),
mermaidPlugin(),
imagePlugin(),
tablePlugin(),
];
🧠 If using @m2d/html
or @m2d/mermaid
, place them before this plugin.
They generate image
or svg
nodes, but only @m2d/image
renders them to DOCX.
🧪 Production Ready
This plugin is production-grade, supporting all major image scenarios:
- Remote images (CORS-safe)
- Data URIs and base64
- SVG-to-PNG fallback
- Image resolution caching (memory + IndexedDB)
- Partial style propagation via
data-*
attributes from HTML
💬 We welcome feedback, bugs, and contributions — open an issue or PR anytime.
⚙️ Plugin Options
IImagePluginOptions {
scale?: number;
fallbackImageType?: "png" | "jpg" | "bmp" | "gif";
imageResolver?: ImageResolver;
maxW?: number;
maxH?: number;
placeholder?: string;
idb?: boolean;
salt?: string;
maxAgeMinutes?: number;
fixGeneratedSvg?: (svg: string, metadata: any) => string;
}
imageResolver
A custom function to fetch or transform the image. Receives the full image
or svg
node.
imagePlugin({
imageResolver: async (src, options, node) => {
const response = await fetch(src);
const arrayBuffer = await response.arrayBuffer();
return {
type: "png",
data: arrayBuffer,
transformation: {
width: 400,
height: 300,
},
};
},
});
You don’t need to implement caching — it’s handled internally with persistent storage. And it is configurable!
placeholder
Used if the image fails to load or decode. Can be:
- A base64/data URL string
- An url to placeholder image
idb
Whether to enable IndexedDB caching (in addition to in-memory).
true
(default): uses IndexedDB
false
: disables persistent cache
🧠 How It Works
- Walks the MDAST tree for
image
and svg
nodes
- Resolves the image via default or custom
imageResolver
- Converts to a
docx.ImageRun
, including alt text and limited styles
- Caches results keyed on the full node and options
- Falls back if resolution fails
💡 Features
- Markdown images:

- HTML
<img>
, <svg>
support via @m2d/html
- Mermaid support via
@m2d/mermaid
or custom plugins
- Cross-environment caching (memory + IndexedDB)
- Alt text + basic style support from HTML via
data-*
⚠️ 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)
- Only a subset of styles are supported
object-fit
, fitWidth
, and complex layout constraints are not supported
- Broken images are replaced with placeholder if defined; otherwise skipped
🔌 Related Plugins/Packages
⭐ Support Us
If you find this useful:
🧾 License
MIT © Mayank Chaudhari
Made with 💖 by Mayank Kumar Chaudhari