| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import {
- Paragraph,
- TextRun,
- HeadingLevel,
- ParagraphChild
- } from "docx"
- import { handleNodes } from "./handleNodes"
- const sizeMap: Record<string, number> = {
- h1: 36,
- h2: 32,
- h3: 28,
- h4: 24,
- h5: 22,
- h6: 20,
- p: 22
- }
- const handleText = (el: Element): Paragraph | undefined => {
- const tag = el.tagName.toLowerCase()
- // 标题段落处理
- if (/^h[1-6]$/.test(tag)) {
- const level = parseInt(tag[1])
- const paragraph = new Paragraph({
- children: [new TextRun({ text: el.textContent?.trim() ?? "", bold: true, size: sizeMap[tag] ?? 22 })],
- heading: (("Heading" + (level)) as (typeof HeadingLevel)[keyof typeof HeadingLevel]),
- spacing: { after: 200 },
- })
- return paragraph
- }
- // 段落处理
- if (tag === "p") {
- if (el.children.length === 0) {
- // 普通文本结构
- const paragraph = new Paragraph({
- children: [new TextRun({ text: el.textContent?.trim() ?? "" })],
- spacing: { after: 200 },
- })
- return paragraph
- } else {
- // 复杂文本,交给handleNodes统一处理
- return handleNodes(el.childNodes)
- }
- }
- }
- /**
- *
- * @param el
- * @returns undefined 匹配失败
- * @returns ParagraphChild 段落子部件
- * ParagraphChild介绍
- * ParagraphChild = TextRun | ImageRun | SymbolRun | Bookmark | PageBreak | ColumnBreak | SequentialIdentifier | FootnoteReferenceRun | InternalHyperlink | ExternalHyperlink | InsertedTextRun | DeletedTextRun | Math | SimpleField | SimpleMailMergeField | Comments | Comment | CommentRangeStart | CommentRangeEnd | CommentReference | CheckBox
- * TextRun
- * 表示一段文本,支持字体、加粗、颜色、高亮、上标/下标、大小、斜体等样式。是最常见的段落子元素。
- * ImageRun
- * 插入图像,可以是 Buffer、base64 或路径。支持控制大小、浮动、对齐方式等。
- * SymbolRun
- * 插入符号,例如特殊字符(☑、★等)。
- * Bookmark
- * 插入书签,供跳转或引用使用。
- * PageBreak
- * 插入分页符。
- * ColumnBreak
- * 插入分栏符,用于多栏布局的换栏操作。
- * SequentialIdentifier
- * 插入一个自动编号序列,如编号字段。
- * FootnoteReferenceRun
- * 插入脚注引用,链接到底部脚注。
- * InternalHyperlink
- * 插入指向文档内部书签的链接(例如跳转到某一章节)。
- * ExternalHyperlink
- * 插入外部链接,例如网页 URL。
- * InsertedTextRun/DeletedTextRun
- * 表示插入或删除的文字(用于显示修改痕迹/修订内容)。
- * Math
- * 表示数学公式,内部基于 Office MathML (OMML) 或 MathType 结构。用来插入数学表达式。
- * SimpleField/SimpleMailMergeField
- * 插入 Word 字段(如页码、日期等)或邮件合并字段。
- * CommentRangeStart/CommentRangeEnd
- * 注释范围的起始和结束标记,用于将某一段文字标记为注释内容。
- * CommentReference/Comments/Comment
- * 插入注释引用或注释本体内容,供审阅使用。
- * CheckBox
- * 插入复选框,可设置是否勾选。
- */
- const handleEmbeddedText = (el: Element): ParagraphChild | undefined => {
- const tag = el.tagName.toLowerCase()
- if (tag === 'br') {
- return new TextRun({ break: 1 }) // 添加换行
- }
- if (tag === 'strong') {
- let textRun: TextRun
- if (el.children.length === 0) {
- // 普通文本结构
- new TextRun({ text: el.textContent?.trim() ?? "", bold: true})
- return paragraph
- } else {
- // 复杂文本,交给handleNodes统一处理
- return handleNodes(el.childNodes)
- }
- }
- }
- export default {
- handleText,
- handleEmbeddedText,
- config: {} // 无配置
- }
|