Skip to content

MDAST to DOCX plugin for resolving and embedding images. Supports base64, URLs, and custom resolvers for seamless DOCX image integration.

License

Notifications You must be signed in to change notification settings

md2docx/image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

@m2d/image

test Maintainability codecov Version Downloads npm bundle size

Converts Markdown (![alt](url)) and HTML <img> nodes into DOCX-compatible ImageRun elements.

Using <img> tag requires @m2d/html plugin.


๐Ÿ“ฆ Installation

npm install @m2d/image
pnpm add @m2d/image
yarn 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/mermaid ensure 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

  • jpeg
  • jpg
  • png
  • bmp
  • gif

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

  1. Checks if the image is a base64 or remote URL.
  2. Parses image format, dimensions, and scale.
  3. Wraps the image as a docx.ImageRun with metadata (like alt text).
  4. Provides fallbacks if image type is unsupported or fails.

๐Ÿ’ก Features

  • Inline Markdown images: ![alt](url)
  • 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:


๐Ÿงพ License

MIT ยฉ Mayank Chaudhari


Made with ๐Ÿ’– by Mayank Kumar Chaudhari

About

MDAST to DOCX plugin for resolving and embedding images. Supports base64, URLs, and custom resolvers for seamless DOCX image integration.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published