@acdvorak/myst-to-docx

中文文档
1.0.14发布于 2024/11/14MITTypeScriptES Module近 30 天下载 7424

Export from a MyST Markdown document to Microsoft Word (*.docx)

原始使用文档
文档目录7 节
  1. Overview
  2. Basic usage in browser
  3. Basic usage in node
  4. Extended usage
  5. Supported Block Nodes
  6. Supported Style Nodes
  7. Resources

myst-to-docx

Export a MyST document to a Microsoft Word file, using docx.

Overview

myst-to-docx has a DocxSerializer object that you write to as you walk the MyST document. It is a light wrapper around https://docx.js.org/, which actually does the export. myst-to-docx is write only (i.e. can export to, but can’t read from *.docx), and has all standard MyST nodes covered (see below).

myst-to-docx can be used in the browser or in node. This library currently only has dependence on docx, myst-frontmatter and buffer-image-size - and the serialization handlers can be edited externally (see Extended Usage below).

The AST should be transformed through myst-transforms to ensure that all nodes are enumerated.

Basic usage in browser

The utility fetchImagesAsBuffers walks the AST and downloads images into buffers to be used by docx, it also figures out the size and returns an object with getImageBuffer and getImageDimensions, which need to be passed to the options. If your images are more complex (e.g. they are mermaid diagrams, or Jupyter Outputs), you will need to do more complex preprocessing to create these two functions.

A Blob is returned, which can be downloaded client side.

import { unified } from 'unified';
import { mystToDocx, fetchImagesAsBuffers, DocxResult } from 'myst-to-docx';

const opts = await fetchImagesAsBuffers(tree);
const file = unified().use(mystToDocx, opts).stringify(tree);
const blob = await (file.result as DocxResult);

Basic usage in node

In node, the image dimensions are discovered using a node-only package, and don't need to be provided. The result of the conversion in node is a Buffer as the file.result, which can be saved to disk.

import { unified } from 'unified';
import { mystToDocx, DocxResult } from 'myst-to-docx';

const file = unified()
  .use(mystToDocx, {
    getImageBuffer(url) {
      return fs.readFileSync(url).buffer;
    };
  })
  .stringify(tree);
const buffer = await (file.result as DocxResult);

Extended usage

Instead of using the defaultDocxSerializer you can override or provide cusome serializers.

import { DocxSerializer, defaultNodes, defaultMarks } from 'myst-to-docx';

const nodeSerializer = {
  ...defaultNodes,
  my_paragraph(state, node) {
    state.renderInline(node);
    state.closeBlock(node);
  },
};

export const myDocxSerializer = new DocxSerializer(nodeSerializer, defaultMarks);

The state is the DocxSerializerState and has helper methods to interact with docx.

Supported Block Nodes

  • text
  • link
  • paragraph
  • heading (levels)
    • Including numbering of headings
  • blockquote
  • code
    • TODO: No styles supported
  • thematicBreak
  • break
  • list
  • listItem
  • image
  • math
    • Including numbering
  • inlineMath
  • crossReference
  • abbreviation
  • block
  • definitionList
  • definitionTerm
  • definitionDescription
  • container
  • caption
  • captionNumber
  • tables

Supported Style Nodes

  • emphasis
  • strong
  • inlineCode
  • subscript
  • superscript
  • delete (strikethrough)
  • underline
  • smallcaps

Resources

版本与发布记录

@acdvorak/myst-to-docx 共发布 3 个版本,首次发布于 2024/11/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.14latest2024/11/142 年前
1.0.132024/11/132 年前
1.0.122024/11/132 年前

关于 @acdvorak/myst-to-docx 的常见问题

@acdvorak/myst-to-docx 是什么?

Export from a MyST Markdown document to Microsoft Word (*.docx)

如何安装 @acdvorak/myst-to-docx?

在项目目录运行 npm install @acdvorak/myst-to-docx,或使用 pnpm add / yarn add / bun add。

@acdvorak/myst-to-docx 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@acdvorak/myst-to-docx 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@acdvorak/myst-to-docx 可以商用吗?

@acdvorak/myst-to-docx 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@acdvorak/myst-to-docx 的下载量有多少?

根据 npm 官方下载统计,@acdvorak/myst-to-docx 在 2026/10/05 – 2026/11/03 这 30 天内被下载 7,424 次,日均约 247 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@acdvorak/myst-to-docx 还在维护吗?

最新版本 1.0.14 发布于 2024/11/14(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/11/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。