@docen/docx

中文文档
0.7.0发布于 2026/09/07MITTypeScriptES Module近 30 天下载 1.8万

DOCX editor and converter powered by @office-open/docx with a Tiptap editing layer — bidirectional DOCX and Markdown conversion, styled HTML paste input

原始使用文档
文档目录10 节
  1. Features
  2. Installation
  3. Quick Start
  4. API
  5. Standalone Functions (Core)
  6. Editor
  7. Template Patching
  8. Advanced: Model Bridge
  9. Architecture
  10. License

@docen/docx

npm version npm downloads npm license

DOCX editor and converter powered by @office-open/docx with Tiptap editing layer, supporting bidirectional conversion between DOCX and Markdown (styled HTML is accepted as paste input).

Need a ready-made visual editor? @docen/editor wraps this engine in a Fluent UI host with the turnkey <docen-document> web component.

Features

  • 📝 Viewless Tiptap engine — the DOCX editing model with DOCX-aware extensions; rendering belongs to the host (e.g. @docen/editor's canvas stage)
  • 🔄 DOCX Round-trip — Near-lossless DOCX ↔ Editor conversion via @office-open/docx
  • 📄 Markdown Support — Tiptap JSON ↔ Markdown conversion
  • 🎨 DOCX Properties — Custom Tiptap extensions carry shading, borders, indent, spacing, floating, crop
  • 🔗 Template Patching — Replace {{placeholders}} in DOCX templates with Tiptap-JSON content

Installation

# Install with pnpm
$ pnpm add @docen/docx

# Install with npm
$ npm install @docen/docx

Quick Start

import { docxExtensions, parseDOCX, generateDOCX } from "@docen/docx";
import { Editor } from "@docen/docx/core";

// Viewless: the editor is the editing model — rendering belongs to the host.
const editor = new Editor({
  element: null,
  extensions: docxExtensions,
  content: await parseDOCX(buffer),
});

// Save back to DOCX
const output = await generateDOCX(editor.getJSON());

API

Standalone Functions (Core)

These work without an editor instance — for headless/server/batch use.

import {
  parseDOCX,
  generateDOCX,
  generateDOCXSync,
  generateDOCXStream,
  parseMarkdown,
  generateMarkdown,
} from "@docen/docx";

// DOCX pipeline: DOCX binary ↔ Tiptap JSON
const json = parseDOCX(buffer); // → JSONContent
const buffer = await generateDOCX(json); // → Buffer (pre-fetches http images by default)
const blob = await generateDOCX(json, { packer: { type: "blob" } }); // → Blob
const sync = generateDOCXSync(json); // → Buffer (skips prepare)
const stream = await generateDOCXStream(json); // → ReadableStream<Uint8Array>

// Markdown pipeline: Markdown string ↔ Tiptap JSON
const json = parseMarkdown("# Hello"); // → JSONContent
const md = generateMarkdown(json); // → string

Editor

import { createDocxEditor, docxExtensions } from "@docen/docx";

const editor = createDocxEditor({
  element: document.querySelector("#editor"),
  extensions: docxExtensions,
  spellcheck: true,
  editable: true,
});

Need a ready-made editor UI? @docen/editor's <docen-document> bundles this engine with open/save, canvas rendering, and the Fluent host.

Template Patching

Replace {{placeholders}} in a DOCX template with Tiptap-JSON content. Each patch's content is prepared (default: fetch http images) then compiled to DOCX.

import { patchDOCX, parseMarkdown } from "@docen/docx";

const result = await patchDOCX({
  template: templateBuffer,
  patches: {
    title: { content: parseMarkdown("# Report") },
  },
  outputType: "nodebuffer",
});

Advanced: Model Bridge

generateDOCX runs prepareDocument → compileDocument → generateDocument internally. You rarely need these directly — reach for them only when working with the intermediate DocumentOptions (the OOXML persistence model):

import { resolveDocument, compileDocument, prepareDocument } from "@docen/docx";

const json = resolveDocument(docOpts); // DocumentOptions → JSONContent
const docOpts = compileDocument(json); // JSONContent → DocumentOptions
await prepareDocument(json); // in place: http image URLs → data URLs

Architecture

Standalone Functions (core)
  parseDOCX / generateDOCX / generateDOCXSync / generateDOCXStream / patchDOCX
  parseMarkdown / generateMarkdown
  resolveDocument / compileDocument / prepareDocument  (model bridge, advanced)
        ↕ used by
createDocxEditor / docxExtensions — the viewless Tiptap editing model
        ↕ rendered by
@docen/editor's canvas stage (LeaferJS pages, caret/selection mapping)
  • Runtime model: Tiptap JSON with DOCX-rich attributes via custom extensions
  • Persistence model: DocumentOptions (complete OOXML expressiveness)
  • Standalone functions are core — the editor bindings are thin wrappers

License

版本与发布记录

@docen/docx 共发布 25 个版本,首次发布于 2026/06/23;过去 12 个月发布了 18 个版本。

版本发布日期距今
0.7.0latest2026/09/072 个月前
0.6.22026/09/022 个月前
0.6.12026/08/312 个月前
0.6.02026/08/312 个月前
0.5.52026/08/063 个月前
0.5.42026/08/063 个月前

关于 @docen/docx 的常见问题

@docen/docx 是什么?

DOCX editor and converter powered by @office-open/docx with a Tiptap editing layer — bidirectional DOCX and Markdown conversion, styled HTML paste input

如何安装 @docen/docx?

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

@docen/docx 支持 TypeScript 吗?

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

@docen/docx 支持 ESM 还是 CommonJS?

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

@docen/docx 可以商用吗?

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

@docen/docx 的下载量有多少?

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

@docen/docx 还在维护吗?

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