markdown-docx

repository·main·Indexed 43 days ago

https://github.com/vace/markdown-docx

A library and CLI tool for converting Markdown files into high-fidelity DOCX documents, supporting Node.js and browser environments. Version 1.7.0 features GitHub Flavored Markdown support, LaTeX mathematical equation rendering via KaTeX, and customizable themes for colors, font sizes, and margins. Includes a CLI for quick conversions and a custom ImageAdapter for image processing.

Tokens
13.4K
Snippets
47
Records
63
Agent score
56%

What's inside markdown-docx

  1. Supported Markdown Syntax for DOCX Conversion

    main

    The markdown-docx converter supports a wide range of standard Markdown syntax to ensure rich formatting in the resulting DOCX file. Supported features include:

    • Text Styling: Bold (**text**), Italics (*text*), Strikethrough (~~text~~), and Inline Code (`code`).
    • Links: Standard inline links [text](url) and links with titles [text](url "title").
    • Lists & Tasks: Unordered lists and Task Lists (- [x] completed, - [ ] incomplete).
    • Block Elements: Blockquotes (> quote), Tables (with alignment support using :), and Code Blocks (```).
    • Media: Image embedding using ![alt text](url "title").
    • Escaping: Use backslashes (\) to escape Markdown special characters.
    • HTML: Inline HTML elements (e.g., <dl>, <dt>, <dd>, <em>) are supported.
    • Mathematics: Both inline formulas (e.g., $E=mc^2$) and block-level formulas (using $$) are supported.
  2. Math Equations (LaTeX) support

    main

    The converter supports LaTeX-style mathematical notation for both inline and block equations:

    • Inline Math: Wrap equations in single dollar signs, e.g., $E=mc^2$. This works for Greek letters, fractions, and complex formulas within text.
    • Block Math: Use double dollar signs $$ for display equations that appear on their own line, e.g.:
    $$a^2 + b^2 = c^2$$
  3. Supported Markdown features in markdown-docx

    main

    The converter supports a wide range of Markdown and extended syntax:

    • Standard Formatting: Bold (**), italics (*), strikethrough (~~), and inline code (`).
    • Links: Inline-style links [text](url) and links with titles [text](url "title").
    • Task Lists: Checkboxes using - [x] for completed and - [ ] for incomplete items.
    • Images: Supports standard syntax ![alt](url), dimension specification (e.g., "64x64"), and scaling percentages (e.g., "30%x30%"). WebP format is supported.
    • Tables: Supports standard Markdown tables with alignment control using colons (:) in the separator row (left, center, or right-aligned).
    • Blockquotes: Supported using the > syntax.
    • Escaping: You can escape Markdown characters using a backslash \ to prevent them from being interpreted as formatting.
  4. Convert Markdown to DOCX using the conversion script

    main

    The scripts/convert_md_to_docx.py script allows you to convert Markdown files (.md, .markdown) to Microsoft Word DOCX format. It uses the markdown-docx npm package via npx.

    Requirements

    • Python 3+
    • Node.js and npm (to run npx)

    Basic Usage

    Single file conversion: Converts a single file and creates a .docx version with the same name in the same directory.

    python scripts/convert_md_to_docx.py -i document.md

    Batch conversion: Converts multiple specified files.

    python scripts/convert_md_to_docx.py -i file1.md file2.md file3.md

    Using glob patterns: Convert all markdown files in the current directory.

    python scripts/convert_md_to_docx.py -i *.md
    python scripts/convert_md_to_docx.py -i document.md
  5. Configure custom output names and directories

    main

    You can control where the converted DOCX files are saved using the -o flag.

    • Single file with custom name: Specify a filename for the output.
    • Batch conversion to a directory: Specify a directory path. The script will create the directory if it does not exist and place all converted files inside it.

    Examples:

    Custom output filename:

    python scripts/convert_md_to_docx.py -i notes.md -o report.docx

    Batch conversion to a specific folder:

    python scripts/convert_md_to_docx.py -i docs/*.md -o exports/
    python scripts/convert_md_to_docx.py -i notes.md -o report.docx
  6. Customize image sizes in Markdown

    main

    You can control the dimensions of images in the generated DOCX by specifying a size in the image title attribute within your Markdown. Supported formats include absolute pixels (600x400) or percentages (30%x30%).

    ![Alt text](image.png "600x400")
    ![Alt text](image.png "30%x30%")
  7. Set custom image dimensions in Markdown

    main

    To control the size of images in the generated DOCX file and avoid Word's maximum width constraints, you can specify dimensions in the Markdown image title attribute using the widthxheight format (e.g., 600x400 or 30%x30%).

    This overrides the original image dimensions in the output document without changing the source image data.

    ![Alt text](image.png "600x400")
    ![Alt text](image.png "30%x30%")
  8. Install markdown-docx

    main

    You can install markdown-docx using npm, yarn, or pnpm to convert Markdown files to DOCX format in both Node.js and browser environments.

    # Using npm
    npm install markdown-docx
    
    # Using yarn
    yarn add markdown-docx
    
    # Using pnpm
    pnpm add markdown-docx
  9. Use mathematical equations in Markdown

    main

    The library supports LaTeX-style mathematical equations using several formats. By default, equations are rendered via KaTeX, which converts LaTeX to MathML and then to native Word math (OMML). This supports complex structures like fractions, square roots, sums, integrals, and matrices.

    Supported Formats

    1. Inline Math Use single dollar signs for equations within a sentence: $E=mc^2$

    2. Block Math Use double dollar signs for display equations on their own line:

    $$
    E=mc^2
    $$

    3. Fenced Math Blocks Use fenced code blocks with the labels math, latex, or katex:

    ```math
    \left( \sum_{k=1}^n a_k b_k \right)^2
    
    ```markdown
    Einstein's famous equation is $E=mc^2$.
    
    $$
    E=mc^2
    $$
    
    ```math
    \left( \sum_{k=1}^n a_k b_k \right)^2
  10. Configure math rendering engines

    main

    You can control how mathematical equations are rendered via the math option in the markdownDocx configuration object.

    Built-in Renderer (Lightweight)

    If you want to avoid the complexity of OMML and prefer a minimal output (LaTeX converted to simple Unicode text), use the builtin engine:

    LibreOffice Compatibility Mode

    LibreOffice has partial support for OMML. If equations appear incorrectly in LibreOffice, enable libreOfficeCompat: true. This mode uses simpler constructs (e.g., rendering sums/integrals as operators with sub/superscripts instead of native n-ary structures) to improve compatibility. Note that Word still renders these correctly.

    Note: The default engine is katex.

    import markdownDocx, { Packer } from 'markdown-docx'
    
    // Fallback to builtin (simple text) renderer
    const doc = await markdownDocx(markdown, {
      math: { engine: 'builtin' }
    })
    
    // Enable LibreOffice compatibility mode
    const docCompat = await markdownDocx(markdown, {
      math: {
        engine: 'katex',
        libreOfficeCompat: true
      }
    })
  11. Resize images using the title attribute

    main

    You can control the dimensions of an image in your Markdown by including size information in the image's title attribute. The markdown-docx parser supports two formats for the title:

    1. Absolute pixels: e.g., ![alt text](image.png "600x400") sets the width to 600 and height to 400.
    2. Percentage of original size: e.g., ![alt text](image.png "50%x50%") scales the image to 50% of its original width and height.

    If the title does not match the [width]x[height] pattern, the image will default to its original dimensions, and the title will be used as the image's title metadata.

    ![alt text](image.png "600x400")
    ![alt text](image.png "50%x50%")