Appearance
@qingpai/word
Word(.docx)解析为 HTML。mammoth 负责结构映射(标题/段落/表格),图片以 data URL 内联——位图直接 base64,TIFF 转 PNG,WMF/EMF 转 SVG;mammoth 丢弃的图片显示尺寸、run 级格式、段落对齐、表格宽度由回填规则按配置接力。
用法
ts
import { Word } from '@qingpai/word' // '@qingpai/core' 亦已 re-export
const word = new Word()
const { html, skippedImages, warnings } = await word.render(file)无效的 .docx 直接抛错,由调用方处理。
配置项
Word 构造函数接收 WordOptions:
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
options? | WordOptions | — | 可识别格式的保留/移除覆盖项;缺省字段回落默认配置 |
options.format 逐项控制可识别格式的回填(true = 提取并回填,false = 移除):
ts
const word = new Word({
format: {
// 字号 / 字体 / 图片高度默认关闭,可按需打开
fontSize: true,
},
})| 键 | 默认值 | 说明 |
|---|---|---|
color | true | run 级:文字颜色 |
fontSize | false | run 级:字号 |
fontFamily | false | run 级:字体 |
backgroundColor | true | run 级:字符底纹与高亮 |
underline | true | run 级:下划线(含线型) |
strikethrough | true | run 级:删除线 |
textAlign | true | 段落对齐(注入块级元素 style="text-align:x",含样式继承链) |
imageWidth | true | 图片显示宽度(回填 <img> 的 width) |
imageHeight | false | 图片显示高度(回填 <img> 的 height) |
tableColumnWidth | true | 表格列宽(注入 colgroup 列宽百分比) |
tableWidthPercent | true | 表格整表宽度(注入 <table style="width:N%">) |
API
Word
方法
| 方法 | 说明 |
|---|---|
render(source, onProgress?) | 解析 .docx 为 HTML。source 为 File、ArrayBuffer 或 base64 编码字符串(容忍空白字符,如后端接口返回);onProgress 在各阶段开始时回调一次(convert → read → backfill,固定权重百分比,非精确值);返回 Promise<WordRenderResult>,解析失败抛错 |
返回值
Promise<WordRenderResult>:
| 名称 | 类型 | 说明 |
|---|---|---|
html | string | mammoth 解析出的 HTML(图片以 data URL 内联,可识别格式已按配置回填) |
skippedImages | number | 因格式不支持或转换失败被跳过的图片数 |
warnings | string[] | mammoth 解析告警(未识别样式/不支持的元素等),已按文案去重并轻量中文化 |
WordRenderStage 取值
| 值 | 说明 |
|---|---|
convert | mammoth 结构转换与图片内联(解析主体) |
read | 读取 docx 包内 document.xml / styles.xml(回填规则数据源) |
backfill | 回填规则管线(图片尺寸 → 表格宽度 → 文本格式 → 段落对齐) |
导出的类型
ts
/** .docx 来源:文件(File)、其二进制内容(ArrayBuffer)或 base64 编码字符串 */
type WordSource = File | ArrayBuffer | string
/** Word 构造函数选项:可识别格式的保留/移除覆盖项;缺省字段回落默认配置 */
interface WordOptions {
/** 格式保留/移除配置;每项 true = 提取并回填,false = 移除 */
format?: Partial<WordFormatConfig>
}
/** Word 解析结果 */
interface WordRenderResult {
/** mammoth 解析出的 HTML(图片以 data URL 内联) */
html: string
/** 因格式不支持或转换失败被跳过的图片数 */
skippedImages: number
/** mammoth 解析告警(未识别样式/不支持的元素等),已按文案去重 */
warnings: string[]
}
/** 解析阶段(进度回调用),按 render 实际执行顺序 */
type WordRenderStage = 'convert' | 'read' | 'backfill'
/** 解析进度(mammoth 无字节级进度钩子,各阶段映射固定权重百分比,非精确值) */
interface WordRenderProgress {
stage: WordRenderStage
/** 估算进度:convert 10 / read 60 / backfill 90 */
percent: number
}
/** Word 可识别格式的保留/移除开关(true = 提取并回填,false = 移除) */
interface WordFormatConfig {
/** run 级:文字颜色 */
color: boolean
/** run 级:字号 */
fontSize: boolean
/** run 级:字体 */
fontFamily: boolean
/** run 级:字符底纹与高亮 */
backgroundColor: boolean
/** run 级:下划线(含线型) */
underline: boolean
/** run 级:删除线 */
strikethrough: boolean
/** 段落对齐(注入块级元素 style="text-align:x",含样式继承链) */
textAlign: boolean
/** 图片显示宽度(回填 <img> 的 width) */
imageWidth: boolean
/** 图片显示高度(回填 <img> 的 height) */
imageHeight: boolean
/** 表格列宽(注入 colgroup 列宽百分比) */
tableColumnWidth: boolean
/** 表格整表宽度(注入 <table style="width:N%">) */
tableWidthPercent: boolean
}
/**
* run 级格式属性开关(WordFormatConfig 中 6 个 run 级属性的子集:color / fontSize /
* fontFamily / backgroundColor / underline / strikethrough,供 run 格式管线消费)
*/
type RunFormatConfig = Record<keyof RunFormat, boolean>@qingpai/core 已 re-export Word 与 WordSource / WordOptions / WordRenderResult / WordRenderProgress / WordRenderStage;WordFormatConfig / RunFormatConfig 需从 @qingpai/word 导入。
配套 UI
| 名称 | 说明 |
|---|---|
WordImportButton | 工具栏「导入 Word」按钮:执行 pickWordFile() 并在成功时 insertWordHtml() 插入;有图片被跳过时警告提示,解析失败错误提示。详见 WordImportButton |
编辑器侧扩展(pickWordFile / insertWordHtml 命令)见 Word 导入。
Source code
packages/word/src/