@ docen/ docx (英语)
DOCX 编辑器和转换器由 @office-open/docx 用 Tiptap 编辑层供电,支持 DOCX 和 Markdown 之间的双向转换(HTML 被接受为粘贴输入).
需要一个现成的视觉编辑?
@docen/editor用统键将引擎包裹在一个流畅的 UI 主机中<docen-document>网页组件.
特征
- 📝 无视图 Tiptap 引擎 – DOCX 编辑模式有 DOCX- 意识扩展;渲染属于主机(例如 @docen/editor的画布舞台)
- 🔄 DOCX 往返旅行 — 近损 DOCX 编辑器通过 @ office- open/ docx 转换
- 📄 标记下方支持 - Tittap JSON 马克下调
- 🎨 DOCX 属性 自定义 Tiptap 扩展带阴影、 边框、 缩进、 间隔、 浮动、 裁剪
- 🔗 模板补丁 - 替换
{{placeholders}}在带有 Tiptap- JSON 内容的 DOCX 模板中
安装
# Install with pnpm
$ pnpm add @docen/docx
# Install with npm
$ npm install @docen/docx
快速启动
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 密码
独立函数( 核心)
这些作品没有编辑器实例——用于无头/服务器/批量使用.
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
编辑器
import { createDocxEditor, docxExtensions } from "@docen/docx";
const editor = createDocxEditor({
element: document.querySelector("#editor"),
extensions: docxExtensions,
spellcheck: true,
editable: true,
});
需要一个现成的编辑器UI? @docen/editor是因为 <docen-document> 将这个引擎与打开/保存,帆布渲染,和流畅的主机捆绑.
模板补丁
替换 {{placeholders}} 在带有Tiptap-JSON内容的DOCX模板中。 每个
补丁 content 编写(默认:获取 http 图像),然后编译为 DOCX.
import { patchDOCX, parseMarkdown } from "@docen/docx";
const result = await patchDOCX({
template: templateBuffer,
patches: {
title: { content: parseMarkdown("# Report") },
},
outputType: "nodebuffer",
});
高级:模型桥
generateDOCX 运行 prepareDocument → compileDocument → generateDocument
内部。 你很少直接需要这些 工作时才联系他们
中间 DocumentOptions (OOXML持久性模型):
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
建筑
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)
- 运行时间模型: Tiptap JSON 通过自定义扩展具有丰富的 DOCX 属性
- 持久性模式:文档选项(完整的OOXML表达式)
- 独立职能是核心 ——编辑器装订为薄薄的包装