@office-open/docx 0.9.6 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -10
- package/dist/context-Bl2wL_dD.mjs +337 -0
- package/dist/context-Bl2wL_dD.mjs.map +1 -0
- package/dist/{core-properties-DMRyJWDp.d.mts → core-properties-CZzFLDat.d.mts} +357 -187
- package/dist/core-properties-CZzFLDat.d.mts.map +1 -0
- package/dist/{generate-B2i9hUSF.mjs → generate-CqKwXEWC.mjs} +86 -58
- package/dist/generate-CqKwXEWC.mjs.map +1 -0
- package/dist/generate.d.mts +1 -1
- package/dist/generate.mjs +1 -1
- package/dist/index.d.mts +5 -17
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/parse-BwZ29Ait.mjs +1939 -0
- package/dist/parse-BwZ29Ait.mjs.map +1 -0
- package/dist/parse.d.mts +1 -1
- package/dist/parse.mjs +1 -1
- package/dist/parts-Cp_NxQFi.mjs +12165 -0
- package/dist/parts-Cp_NxQFi.mjs.map +1 -0
- package/dist/patch/index.d.mts +1 -1
- package/dist/patch/index.mjs +1 -1
- package/package.json +3 -3
- package/dist/context-DfxK5XnG.mjs +0 -5116
- package/dist/context-DfxK5XnG.mjs.map +0 -1
- package/dist/core-properties-DMRyJWDp.d.mts.map +0 -1
- package/dist/document-D2OTVcbX.mjs +0 -6363
- package/dist/document-D2OTVcbX.mjs.map +0 -1
- package/dist/generate-B2i9hUSF.mjs.map +0 -1
- package/dist/parse-DqXV5xqQ.mjs +0 -514
- package/dist/parse-DqXV5xqQ.mjs.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generate-B2i9hUSF.mjs","names":[],"sources":["../src/parts/fonts/obfuscate-ttf-to-odttf.ts","../src/parts/header-footer.ts","../src/compiler.ts","../src/generate.ts"],"sourcesContent":["/**\n * Font obfuscation module for embedding fonts in WordprocessingML documents.\n *\n * This module implements the OOXML font obfuscation algorithm used to embed\n * fonts in DOCX documents. Obfuscation is required by the OOXML specification\n * to prevent simple extraction of embedded font files.\n *\n * Reference: ECMA-376 Part 2, Section 11.1 (Font Embedding)\n *\n * @module\n */\n\n/** Start offset for obfuscation in the font file */\nconst obfuscatedStartOffset = 0;\n/** End offset for obfuscation (first 32 bytes are obfuscated) */\nconst obfuscatedEndOffset = 32;\n/** Expected GUID size (32 hex characters without dashes) */\nconst guidSize = 32;\n\n/**\n * Obfuscates a TrueType font file for embedding in OOXML documents.\n *\n * The obfuscation algorithm XORs the first 32 bytes of the font file\n * with a reversed byte sequence derived from the font's GUID key.\n * This prevents simple extraction while maintaining font functionality.\n *\n * @param buf - The original font file as a byte array\n * @param fontKey - The GUID key for the font (with or without dashes)\n * @returns The obfuscated font data\n * @throws Error if the fontKey is not a valid 32-character GUID\n *\n * @example\n * ```typescript\n * const fontData = readFileSync(\"font.ttf\");\n * const fontKey = \"00000000-0000-0000-0000-000000000000\";\n * const obfuscatedData = obfuscate(fontData, fontKey);\n * ```\n *\n * @internal\n */\nexport const obfuscate = (buf: Uint8Array, fontKey: string): Uint8Array => {\n const guid = fontKey.replace(/-/g, \"\");\n if (guid.length !== guidSize) {\n throw new Error(`Error: Cannot extract GUID from font filename: ${fontKey}`);\n }\n\n const hexStrings = guid.replace(/(..)/g, \"$1 \").trim().split(\" \");\n const hexNumbers = hexStrings.map((hexString) => parseInt(hexString, 16));\n hexNumbers.reverse();\n\n const bytesToObfuscate = buf.slice(obfuscatedStartOffset, obfuscatedEndOffset);\n const obfuscatedBytes = bytesToObfuscate.map(\n (byte, i) => byte ^ hexNumbers[i % hexNumbers.length],\n );\n\n const out = new Uint8Array(\n obfuscatedStartOffset + obfuscatedBytes.length + Math.max(0, buf.length - obfuscatedEndOffset),\n );\n out.set(buf.slice(0, obfuscatedStartOffset));\n out.set(obfuscatedBytes, obfuscatedStartOffset);\n out.set(buf.slice(obfuscatedEndOffset), obfuscatedStartOffset + obfuscatedBytes.length);\n return out;\n};\n","/**\n * Header/Footer entry module for WordprocessingML documents.\n *\n * Replaces the former HeaderWrapper/FooterWrapper/Header/Footer/HeaderFooterBase\n * class hierarchy with a simple data structure + pure serialization function.\n *\n * Reference: ISO/IEC 29500-4, wml.xsd, CT_HdrFtr\n *\n * @module\n */\n\nimport type { Relationships } from \"@office-open/core\";\nimport { escapeXml } from \"@office-open/xml\";\nimport type { SectionChild } from \"@shared/section\";\n\nimport { stringifyBodyChild } from \"../body\";\nimport type { BodyContext } from \"../context\";\nimport { DocumentAttributeNamespaces } from \"./document/document-attributes\";\nimport type { DocumentAttributeNamespace } from \"./document/document-attributes\";\n\n/**\n * Simple data structure for a header or footer entry.\n *\n * Replaces HeaderWrapper/FooterWrapper — holds children, relationships,\n * and the reference ID needed for section property references.\n *\n * Children are raw SectionChild objects (plain JSON or class instances).\n */\nexport interface HeaderFooterEntry {\n children: SectionChild[];\n relationships: Relationships;\n referenceId: number;\n}\n\n/**\n * Namespace keys used by header elements.\n * @internal\n */\nexport const HEADER_NAMESPACES: DocumentAttributeNamespace[] = [\n \"cx\",\n \"cx1\",\n \"cx2\",\n \"cx3\",\n \"cx4\",\n \"cx5\",\n \"cx6\",\n \"cx7\",\n \"cx8\",\n \"m\",\n \"mc\",\n \"o\",\n \"r\",\n \"v\",\n \"w\",\n \"w10\",\n \"w14\",\n \"w15\",\n \"w16cid\",\n \"w16se\",\n \"wne\",\n \"wp\",\n \"wp14\",\n \"wpc\",\n \"wpg\",\n \"wpi\",\n \"wps\",\n];\n\n/**\n * Namespace keys used by footer elements.\n * @internal\n */\nexport const FOOTER_NAMESPACES: DocumentAttributeNamespace[] = [\n \"m\",\n \"mc\",\n \"o\",\n \"r\",\n \"v\",\n \"w\",\n \"w10\",\n \"w14\",\n \"w15\",\n \"wne\",\n \"wp\",\n \"wp14\",\n \"wpc\",\n \"wpg\",\n \"wpi\",\n \"wps\",\n];\n\n/**\n * Serialize a header or footer to XML.\n *\n * Builds the `<w:hdr>` or `<w:ftr>` element with namespace declarations,\n * then serializes each child element via `stringifyBodyChild()`.\n *\n * @param tag - Element tag name (\"w:hdr\" or \"w:ftr\")\n * @param namespaces - Namespace keys to declare on the root element\n * @param children - Block-level child elements (raw SectionChild objects)\n * @param ctx - Body context for stringification\n */\nexport function stringifyHeaderFooter(\n tag: string,\n namespaces: DocumentAttributeNamespace[],\n children: SectionChild[],\n ctx: BodyContext,\n): string {\n const attrParts: string[] = [];\n for (const ns of namespaces) {\n attrParts.push(`xmlns:${ns}=\"${escapeXml(DocumentAttributeNamespaces[ns])}\"`);\n }\n const attrStr = attrParts.join(\" \");\n\n const childParts: string[] = [];\n for (const child of children) {\n childParts.push(stringifyBodyChild(child, ctx));\n }\n\n const body = childParts.join(\"\");\n return body.length === 0 ? `<${tag} ${attrStr}/>` : `<${tag} ${attrStr}>${body}</${tag}>`;\n}\n","/**\n * DOCX document compiler — pure function entry point.\n *\n * compileDocument() accepts DocumentOptions directly,\n * creates a DocxWriteContext internally, and produces a Zippable result.\n * All XML parts are produced via descriptors or serialize() —\n * no Formatter dependency.\n *\n * @module\n */\n\nimport {\n addSmartArtRelationships,\n findAndReplaceImagePlaceholders,\n formatId,\n hasPlaceholders,\n replaceAllPlaceholders,\n replaceNumberingPlaceholders,\n} from \"@office-open/core\";\nimport type { XmlifyedFile, ZipOptions, Zippable } from \"@office-open/core\";\nimport { APP_PROPS_XML } from \"@office-open/core\";\nimport {\n DEFAULT_DRAWING_XML,\n getColorXml,\n getLayoutXml,\n getStyleXml,\n} from \"@office-open/core/smartart\";\nimport type { DocumentOptions } from \"@parts/core-properties\";\nimport { obfuscate } from \"@parts/fonts/obfuscate-ttf-to-odttf\";\nimport { HEADER_NAMESPACES, FOOTER_NAMESPACES, stringifyHeaderFooter } from \"@parts/header-footer\";\n\nimport { stringifyDocumentXml, stringifyBodyChild, type BodyContext } from \"./body\";\nimport { DocxWriteContext } from \"./context\";\nimport {\n corePropertiesDesc,\n customPropertiesDesc,\n appPropertiesDesc,\n contentTypesDesc,\n buildContentTypes,\n fontTableDesc,\n webSettingsDesc,\n commentsDesc,\n bibliographyDesc,\n settingsDesc,\n footnotesDesc,\n endnotesDesc,\n glossaryDesc,\n} from \"./parts\";\n\n/** Reusable TextEncoder (stateless, safe to share). */\nconst encoder = new TextEncoder();\n\n/** XML declaration prepended to every OOXML part. */\nconst XML_DECL = '<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>';\n\n/** Extended context for header/footer formatted view caching. */\ntype DocxContext = BodyContext & {\n headerFormattedViews?: Map<number, string>;\n footerFormattedViews?: Map<number, string>;\n};\n\n// ── Public API ──\n\n/**\n * Compile document options into a flat file map suitable for fflate zipSync.\n *\n * This is the primary entry point for DOCX generation — accepts DocumentOptions\n * directly.\n */\nexport function compileDocument(\n options: DocumentOptions,\n overrides: XmlifyedFile[] = [],\n mediaLevel: number = 0,\n): Zippable {\n const ctx = new DocxWriteContext(options);\n const files: Zippable = {};\n\n const headerFormattedViews = new Map<number, string>();\n const footerFormattedViews = new Map<number, string>();\n\n const xmlifiedFileMapping = xmlifyContext(ctx, headerFormattedViews, footerFormattedViews);\n const map = new Map<string, XmlifyedFile | XmlifyedFile[]>(Object.entries(xmlifiedFileMapping));\n\n for (const [, obj] of map) {\n if (Array.isArray(obj)) {\n for (const subFile of obj as XmlifyedFile[]) {\n files[subFile.path] =\n typeof subFile.data === \"string\" ? encoder.encode(subFile.data) : subFile.data;\n }\n } else {\n const fileObj = obj as XmlifyedFile;\n files[fileObj.path] =\n typeof fileObj.data === \"string\" ? encoder.encode(fileObj.data) : fileObj.data;\n }\n }\n\n for (const subFile of overrides) {\n files[subFile.path] =\n typeof subFile.data === \"string\" ? encoder.encode(subFile.data) : subFile.data;\n }\n\n // Media files\n const mediaArray = ctx.media.array;\n for (const mediaData of mediaArray) {\n files[`word/media/${mediaData.fileName}`] = [\n mediaData.data as Uint8Array,\n { level: mediaLevel as ZipOptions[\"level\"] },\n ];\n if (mediaData.type === \"svg\") {\n files[`word/media/${mediaData.fallback.fileName}`] = [\n mediaData.fallback.data as Uint8Array,\n { level: mediaLevel as ZipOptions[\"level\"] },\n ];\n }\n }\n\n // Font files\n for (const { data: buffer, name, fontKey } of ctx.fontTable.fontOptionsWithKey) {\n const [nameWithoutExtension] = name.split(\".\");\n files[`word/fonts/${nameWithoutExtension}.odttf`] = obfuscate(buffer, fontKey);\n }\n\n return files;\n}\n\n// ── Internal ──\n\n/**\n * Complete mapping of all XML files in an OOXML document package.\n */\ninterface XmlifyedFileMapping {\n Document: XmlifyedFile;\n Styles: XmlifyedFile;\n Properties: XmlifyedFile;\n Numbering: XmlifyedFile;\n Relationships: XmlifyedFile;\n FileRelationships: XmlifyedFile;\n Headers: XmlifyedFile[];\n Footers: XmlifyedFile[];\n HeaderRelationships: XmlifyedFile[];\n FooterRelationships: XmlifyedFile[];\n ContentTypes: XmlifyedFile;\n CustomProperties: XmlifyedFile;\n AppProperties: XmlifyedFile;\n FootNotes: XmlifyedFile;\n FootNotesRelationships: XmlifyedFile;\n Endnotes: XmlifyedFile;\n EndnotesRelationships: XmlifyedFile;\n Settings: XmlifyedFile;\n Comments?: XmlifyedFile;\n CommentsRelationships?: XmlifyedFile;\n FontTable?: XmlifyedFile;\n FontTableRelationships?: XmlifyedFile;\n Bibliography?: XmlifyedFile;\n Charts?: XmlifyedFile[];\n DiagramData?: XmlifyedFile[];\n DiagramLayout?: XmlifyedFile[];\n DiagramStyle?: XmlifyedFile[];\n DiagramColors?: XmlifyedFile[];\n DiagramDrawing?: XmlifyedFile[];\n AltChunks?: XmlifyedFile[];\n SubDocs?: XmlifyedFile[];\n Glossary?: XmlifyedFile;\n WebSettings?: XmlifyedFile;\n}\n\nfunction xmlifyContext(\n ctx: DocxWriteContext,\n headerFormattedViews: Map<number, string>,\n footerFormattedViews: Map<number, string>,\n): XmlifyedFileMapping {\n const mkCtx = (viewWrapper: DocxContext[\"viewWrapper\"] = ctx.document): DocxContext => ({\n fileData: ctx,\n file: ctx,\n viewWrapper,\n stringifyChild: stringifyBodyChild,\n addRelationship: (type: string, target: string, mode?: string) =>\n ctx.addRelationship(type, target, mode),\n addMedia: (data: Uint8Array, type: string) => ctx.addMedia(data, type),\n });\n\n const documentRelationshipCount = ctx.document.relationships.relationshipCount + 1;\n const docCtx = mkCtx(ctx.document);\n const documentXmlData = XML_DECL + stringifyDocumentXml(ctx, docCtx);\n\n const commentRelationshipCount = ctx.comments.relationships.relationshipCount + 1;\n const commentCtx = mkCtx({ relationships: ctx.comments.relationships });\n const commentXmlData =\n XML_DECL + commentsDesc.stringify(ctx._options.comments ?? { children: [] }, commentCtx);\n\n const footnoteRelationshipCount = ctx.footNotes.relationships.relationshipCount + 1;\n const footnoteCtx = mkCtx({\n relationships: ctx.footNotes.relationships,\n });\n const footnoteXmlData =\n XML_DECL + (footnotesDesc.stringify({ notes: ctx.footNotes.notes }, footnoteCtx) ?? \"\");\n\n const documentMedia = findAndReplaceImagePlaceholders(\n documentXmlData,\n ctx.media.array,\n documentRelationshipCount,\n );\n const commentMedia = findAndReplaceImagePlaceholders(\n commentXmlData,\n ctx.media.array,\n commentRelationshipCount,\n );\n const footnoteMedia = findAndReplaceImagePlaceholders(\n footnoteXmlData,\n ctx.media.array,\n footnoteRelationshipCount,\n );\n\n return {\n AppProperties: {\n data:\n XML_DECL +\n (ctx._options.appProperties\n ? (appPropertiesDesc.stringify(ctx._options.appProperties, ctx) ?? APP_PROPS_XML)\n : APP_PROPS_XML),\n path: \"docProps/app.xml\",\n },\n Comments: {\n data: (() => {\n const xmlData = commentMedia.referenced.length > 0 ? commentMedia.xml : commentXmlData;\n return replaceNumberingPlaceholders(xmlData, ctx.numbering.concreteNumbering);\n })(),\n path: \"word/comments.xml\",\n },\n CommentsRelationships: {\n data: (() => {\n for (let i = 0; i < commentMedia.referenced.length; i++) {\n ctx.comments.relationships.addRelationship(\n commentRelationshipCount + i,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\",\n `media/${commentMedia.referenced[i].fileName}`,\n );\n }\n return XML_DECL + ctx.comments.relationships.serialize();\n })(),\n path: \"word/_rels/comments.xml.rels\",\n },\n ContentTypes: {\n data:\n XML_DECL +\n (contentTypesDesc.stringify(\n buildContentTypes({\n headerCount: ctx.headers.length,\n footerCount: ctx.footers.length,\n chartCount: ctx.charts.array.length,\n smartArtCount: ctx.smartArts.array.length,\n hasBibliography: !!ctx._options.bibliography,\n hasGlossary: !!ctx.glossaryOptions,\n hasWebSettings: !!ctx.webSettings,\n altChunks: ctx.altChunks.array.map((ac) => ({\n path: `/word/${ac.path}`,\n contentType: ac.contentType ?? \"application/xhtml+xml\",\n })),\n subDocs: ctx.subDocs.array.map((sd) => ({ path: `/word/${sd.path}` })),\n }),\n ctx,\n ) ?? \"\"),\n path: \"[Content_Types].xml\",\n },\n CustomProperties: {\n data:\n XML_DECL +\n (customPropertiesDesc.stringify({ properties: ctx._options.customProperties ?? [] }, ctx) ??\n \"\"),\n path: \"docProps/custom.xml\",\n },\n Document: {\n data: (() => {\n let xmlData = documentMedia.referenced.length > 0 ? documentMedia.xml : documentXmlData;\n if (hasPlaceholders(xmlData)) {\n const mediaCount = documentMedia.referenced.length;\n const chartKeys = ctx.charts.array.map((c) => c.key);\n const smartArtKeys = ctx.smartArts.array.map((s) => s.key);\n const chartOffset = documentRelationshipCount + mediaCount;\n const smartArtOffset = chartOffset + chartKeys.length;\n\n // Build combined replacement entries for charts, smartart, and numbering\n const entries: Array<{ prefix?: string; key: string; value: string }> = [];\n for (let i = 0; i < chartKeys.length; i++) {\n entries.push({\n prefix: \"chart:\",\n key: chartKeys[i],\n value: formatId(chartOffset, i, \"rId\"),\n });\n }\n const saPrefixes = [\"smartart:\", \"smartart-lo:\", \"smartart-qs:\", \"smartart-cs:\"];\n for (let i = 0; i < smartArtKeys.length; i++) {\n for (let p = 0; p < saPrefixes.length; p++) {\n entries.push({\n prefix: saPrefixes[p],\n key: smartArtKeys[i],\n value: formatId(smartArtOffset + p * smartArtKeys.length, i, \"rId\"),\n });\n }\n }\n for (const { reference, instance, numId } of ctx.numbering.concreteNumbering) {\n entries.push({ key: `${reference}-${instance}`, value: numId.toString() });\n }\n xmlData = replaceAllPlaceholders(xmlData, entries);\n } else {\n xmlData = replaceNumberingPlaceholders(xmlData, ctx.numbering.concreteNumbering);\n }\n return xmlData;\n })(),\n path: \"word/document.xml\",\n },\n Endnotes: {\n data: (() => {\n const endnoteCtx = mkCtx({\n relationships: ctx.endnotes.relationships,\n });\n const xmlData =\n XML_DECL + (endnotesDesc.stringify({ notes: ctx.endnotes.notes }, endnoteCtx) ?? \"\");\n const endnoteRelCount = ctx.endnotes.relationships.relationshipCount + 1;\n const endnoteMedia = findAndReplaceImagePlaceholders(\n xmlData,\n ctx.media.array,\n endnoteRelCount,\n );\n if (endnoteMedia.referenced.length > 0) {\n for (let i = 0; i < endnoteMedia.referenced.length; i++) {\n ctx.endnotes.relationships.addRelationship(\n endnoteRelCount + i,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\",\n `media/${endnoteMedia.referenced[i].fileName}`,\n );\n }\n return replaceNumberingPlaceholders(endnoteMedia.xml, ctx.numbering.concreteNumbering);\n }\n return replaceNumberingPlaceholders(xmlData, ctx.numbering.concreteNumbering);\n })(),\n path: \"word/endnotes.xml\",\n },\n EndnotesRelationships: {\n data: XML_DECL + ctx.endnotes.relationships.serialize(),\n path: \"word/_rels/endnotes.xml.rels\",\n },\n FileRelationships: {\n data: XML_DECL + ctx.fileRelationships.serialize(),\n path: \"_rels/.rels\",\n },\n FontTable: {\n data:\n XML_DECL +\n (fontTableDesc.stringify({ fonts: ctx.fontTable.fontOptionsWithKey }, ctx) ?? \"\"),\n path: \"word/fontTable.xml\",\n },\n FontTableRelationships: {\n data: XML_DECL + ctx.fontTable.relationships.serialize(),\n path: \"word/_rels/fontTable.xml.rels\",\n },\n FootNotes: {\n data: (() => {\n const xmlData = footnoteMedia.referenced.length > 0 ? footnoteMedia.xml : footnoteXmlData;\n return replaceNumberingPlaceholders(xmlData, ctx.numbering.concreteNumbering);\n })(),\n path: \"word/footnotes.xml\",\n },\n FootNotesRelationships: {\n data: (() => {\n for (let i = 0; i < footnoteMedia.referenced.length; i++) {\n ctx.footNotes.relationships.addRelationship(\n footnoteRelationshipCount + i,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\",\n `media/${footnoteMedia.referenced[i].fileName}`,\n );\n }\n return XML_DECL + ctx.footNotes.relationships.serialize();\n })(),\n path: \"word/_rels/footnotes.xml.rels\",\n },\n FooterRelationships: ctx.footers.map((entry, index) => {\n const footerCtx = mkCtx({ relationships: entry.relationships });\n const xmlData =\n XML_DECL + stringifyHeaderFooter(\"w:ftr\", FOOTER_NAMESPACES, entry.children, footerCtx);\n footerFormattedViews.set(index, xmlData);\n const footerMedia = findAndReplaceImagePlaceholders(xmlData, ctx.media.array, 0);\n\n for (let i = 0; i < footerMedia.referenced.length; i++) {\n entry.relationships.addRelationship(\n i,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\",\n `media/${footerMedia.referenced[i].fileName}`,\n );\n }\n\n return {\n data: XML_DECL + entry.relationships.serialize(),\n path: `word/_rels/footer${index + 1}.xml.rels`,\n };\n }),\n Footers: ctx.footers.map((_entry, index) => {\n const tempXmlData = footerFormattedViews.get(index)!;\n const footerMedia = findAndReplaceImagePlaceholders(tempXmlData, ctx.media.array, 0);\n const xmlData = footerMedia.referenced.length > 0 ? footerMedia.xml : tempXmlData;\n\n return {\n data: replaceNumberingPlaceholders(xmlData, ctx.numbering.concreteNumbering),\n path: `word/footer${index + 1}.xml`,\n };\n }),\n HeaderRelationships: ctx.headers.map((entry, index) => {\n const headerCtx = mkCtx({ relationships: entry.relationships });\n const xmlData =\n XML_DECL + stringifyHeaderFooter(\"w:hdr\", HEADER_NAMESPACES, entry.children, headerCtx);\n headerFormattedViews.set(index, xmlData);\n const headerMedia = findAndReplaceImagePlaceholders(xmlData, ctx.media.array, 0);\n\n for (let i = 0; i < headerMedia.referenced.length; i++) {\n entry.relationships.addRelationship(\n i,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\",\n `media/${headerMedia.referenced[i].fileName}`,\n );\n }\n\n return {\n data: XML_DECL + entry.relationships.serialize(),\n path: `word/_rels/header${index + 1}.xml.rels`,\n };\n }),\n Headers: ctx.headers.map((_entry, index) => {\n const tempXmlData = headerFormattedViews.get(index)!;\n const headerMedia = findAndReplaceImagePlaceholders(tempXmlData, ctx.media.array, 0);\n const xmlData = headerMedia.referenced.length > 0 ? headerMedia.xml : tempXmlData;\n\n return {\n data: replaceNumberingPlaceholders(xmlData, ctx.numbering.concreteNumbering),\n path: `word/header${index + 1}.xml`,\n };\n }),\n Numbering: {\n data: ctx.numbering.serialize(),\n path: \"word/numbering.xml\",\n },\n Properties: {\n data: XML_DECL + (corePropertiesDesc.stringify(ctx._options, ctx) ?? \"\"),\n path: \"docProps/core.xml\",\n },\n Relationships: {\n data: (() => {\n for (let i = 0; i < documentMedia.referenced.length; i++) {\n ctx.document.relationships.addRelationship(\n documentRelationshipCount + i,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\",\n `media/${documentMedia.referenced[i].fileName}`,\n );\n }\n\n const chartOffset = documentRelationshipCount + documentMedia.referenced.length;\n for (let i = 0; i < ctx.charts.array.length; i++) {\n ctx.document.relationships.addRelationship(\n chartOffset + i,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart\",\n `charts/chart${i + 1}.xml`,\n );\n }\n\n addSmartArtRelationships(\n ctx.smartArts.array.map((s) => s.key),\n (id, type, target) => {\n ctx.document.relationships.addRelationship(id, type, target);\n },\n documentRelationshipCount + documentMedia.referenced.length + ctx.charts.array.length,\n 0,\n {\n pathPrefix: \"\",\n styleRelType: \"http://schemas.microsoft.com/office/2007/relationships/diagramStyle\",\n },\n );\n\n ctx.document.relationships.addRelationship(\n ctx.document.relationships.relationshipCount + 1,\n \"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\",\n \"fontTable.xml\",\n );\n\n return XML_DECL + ctx.document.relationships.serialize();\n })(),\n path: \"word/_rels/document.xml.rels\",\n },\n Settings: {\n data: XML_DECL + (settingsDesc.stringify(ctx._settingsOptions, ctx) ?? \"\"),\n path: \"word/settings.xml\",\n },\n Styles: {\n data: (() => {\n const xmlStyles = ctx.styles.serialize();\n return replaceNumberingPlaceholders(xmlStyles, ctx.numbering.concreteNumbering);\n })(),\n path: \"word/styles.xml\",\n },\n ...(ctx._options.bibliography\n ? {\n Bibliography: {\n data: XML_DECL + (bibliographyDesc.stringify(ctx._options.bibliography, ctx) ?? \"\"),\n path: \"word/bibliography.xml\",\n },\n }\n : {}),\n ...(ctx.charts.array.length > 0\n ? {\n Charts: ctx.charts.array.flatMap((chartData, i) => [\n {\n data: XML_DECL + chartData.chartSpaceXml,\n path: `word/charts/chart${i + 1}.xml`,\n },\n {\n data: '<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"/>',\n path: `word/charts/_rels/chart${i + 1}.xml.rels`,\n },\n ]),\n }\n : {}),\n ...(ctx.smartArts.array.length > 0\n ? {\n DiagramData: ctx.smartArts.array.map((smartArtData, i) => ({\n data: XML_DECL + smartArtData.dataModelXml,\n path: `word/diagrams/data${i + 1}.xml`,\n })),\n DiagramLayout: ctx.smartArts.array.map((smartArtData, i) => ({\n data: getLayoutXml(smartArtData.layout),\n path: `word/diagrams/layout${i + 1}.xml`,\n })),\n DiagramStyle: ctx.smartArts.array.map((smartArtData, i) => ({\n data: getStyleXml(smartArtData.style),\n path: `word/diagrams/quickStyle${i + 1}.xml`,\n })),\n DiagramColors: ctx.smartArts.array.map((smartArtData, i) => ({\n data: getColorXml(smartArtData.color),\n path: `word/diagrams/colors${i + 1}.xml`,\n })),\n DiagramDrawing: ctx.smartArts.array.map((_, i) => ({\n data: DEFAULT_DRAWING_XML,\n path: `word/diagrams/drawing${i + 1}.xml`,\n })),\n }\n : {}),\n ...(ctx.altChunks.array.length > 0\n ? {\n AltChunks: ctx.altChunks.array.map((altChunkData) => ({\n data: altChunkData.data,\n path: `word/${altChunkData.path}`,\n })),\n }\n : {}),\n ...(ctx.subDocs.array.length > 0\n ? {\n SubDocs: ctx.subDocs.array.map((subDocData) => ({\n data: subDocData.data,\n path: `word/${subDocData.path}`,\n })),\n }\n : {}),\n ...(ctx.glossaryOptions\n ? {\n Glossary: {\n data: (() => {\n const glossaryCtx = mkCtx(undefined);\n return XML_DECL + (glossaryDesc.stringify(ctx.glossaryOptions!, glossaryCtx) ?? \"\");\n })(),\n path: \"word/glossary/document.xml\",\n },\n }\n : {}),\n ...(ctx.webSettings\n ? {\n WebSettings: {\n data: XML_DECL + (webSettingsDesc.stringify(ctx._options.webSettings ?? {}, ctx) ?? \"\"),\n path: \"word/webSettings.xml\",\n },\n }\n : {}),\n };\n}\n","/**\n * Pure function API for generating DOCX files.\n *\n * @module\n */\n\nimport { createPacker, OoxmlMimeType } from \"@office-open/core\";\nimport type { OutputByType, OutputType, PackerOptions } from \"@office-open/core\";\nimport type { DocumentOptions } from \"@parts/core-properties\";\n\nimport { compileDocument } from \"./compiler\";\n\n/** @internal Packer instance for DOCX generation. */\nconst Packer = createPacker<DocumentOptions>({\n compile: (options, overrides, mediaLevel) => compileDocument(options, overrides, mediaLevel),\n mimeType: OoxmlMimeType.DOCX,\n});\n\n/**\n * Generate a DOCX file from pure JSON options.\n *\n * The output format is controlled by `packerOptions.type` (default: `\"nodebuffer\"` → Buffer).\n * For synchronous generation, use {@link generateDocumentSync}. For streaming, use {@link generateDocumentStream}.\n *\n * @param options - Document options (sections, styles, numbering, etc.)\n * @param packerOptions - Optional packer configuration (type, compression, overrides, etc.)\n *\n * @example\n * ```typescript\n * import { generateDocument } from \"@office-open/docx\";\n *\n * const buffer = await generateDocument({ sections: [...] });\n * const bytes = await generateDocument({ sections: [...] }, { type: \"uint8array\" });\n * const blob = await generateDocument({ sections: [...] }, { type: \"blob\" });\n * ```\n */\nexport function generateDocument<T extends OutputType = \"nodebuffer\">(\n options: DocumentOptions,\n packerOptions?: PackerOptions<T>,\n): Promise<OutputByType[T]> {\n return Packer.pack(options, packerOptions) as Promise<OutputByType[T]>;\n}\n\n/**\n * Synchronously generate a DOCX file from pure JSON options.\n */\nexport function generateDocumentSync<T extends OutputType = \"nodebuffer\">(\n options: DocumentOptions,\n packerOptions?: PackerOptions<T>,\n): OutputByType[T] {\n return Packer.packSync(options, packerOptions) as OutputByType[T];\n}\n\n/**\n * Generate a DOCX file as a `ReadableStream<Uint8Array>`.\n */\nexport function generateDocumentStream(\n options: DocumentOptions,\n packerOptions?: PackerOptions,\n): ReadableStream<Uint8Array> {\n return Packer.toStream(options, packerOptions);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAaA,MAAM,wBAAwB;;AAE9B,MAAM,sBAAsB;;AAE5B,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;AAuBjB,MAAa,aAAa,KAAiB,YAAgC;CACzE,MAAM,OAAO,QAAQ,QAAQ,MAAM,EAAE;CACrC,IAAI,KAAK,WAAW,UAClB,MAAM,IAAI,MAAM,kDAAkD,SAAS;CAI7E,MAAM,aADa,KAAK,QAAQ,SAAS,KAAK,EAAE,KAAK,EAAE,MAAM,GACjC,EAAE,KAAK,cAAc,SAAS,WAAW,EAAE,CAAC;CACxE,WAAW,QAAQ;CAGnB,MAAM,kBADmB,IAAI,MAAM,uBAAuB,mBACnB,EAAE,KACtC,MAAM,MAAM,OAAO,WAAW,IAAI,WAAW,OAChD;CAEA,MAAM,MAAM,IAAI,WACd,wBAAwB,gBAAgB,SAAS,KAAK,IAAI,GAAG,IAAI,SAAS,mBAAmB,CAC/F;CACA,IAAI,IAAI,IAAI,MAAM,GAAG,qBAAqB,CAAC;CAC3C,IAAI,IAAI,iBAAiB,qBAAqB;CAC9C,IAAI,IAAI,IAAI,MAAM,mBAAmB,GAAG,wBAAwB,gBAAgB,MAAM;CACtF,OAAO;AACT;;;;;;;ACxBA,MAAa,oBAAkD;CAC7D;CACA;CACA;CACA;CACA;CACA;CAC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MAAM,QAAQ,aAAa;EAC7B,EAAE,EACJ,IACA,CAAC;EACL,GAAI,IAAI,QAAQ,MAAM,SAAS,IAC3B,EACE,SAAS,IAAI,QAAQ,MAAM,KAAK,gBAAgB;GAC9C,MAAM,WAAW;GACjB,MAAM,QAAQ,WAAW;EAC3B,EAAE,EACJ,IACA,CAAC;EACL,GAAI,IAAI,kBACJ,EACE,UAAU;GACR,aAAa;IACX,MAAM,cAAc,MAAM,KAAA,CAAS;IACnC,OAAO,YAAY,aAAa,UAAU,IAAI,iBAAkB,WAAW,KAAK;GAClF,GAAG;GACH,MAAM;EACR,EACF,IACA,CAAC;EACL,GAAI,IAAI,cACJ,EACE,aAAa;GACX,MAAM,YAAY,gBAAgB,UAAU,IAAI,SAAS,eAAe,CAAC,GAAG,GAAG,KAAK;GACpF,MAAM;EACR,EACF,IACA,CAAC;CACP;AACF;;;;;;;;;ACtjBA,MAAM,SAAS,aAA8B;CAC3C,UAAU,SAAS,WAAW,eAAe,gBAAgB,SAAS,WAAW,UAAU;CAC3F,UAAU,cAAc;AAC1B,CAAC;;;;;;;;;;;;;;;;;;;AAoBD,SAAgB,iBACd,SACA,eAC0B;CAC1B,OAAO,OAAO,KAAK,SAAS,aAAa;AAC3C;;;;AAKA,SAAgB,qBACd,SACA,eACiB;CACjB,OAAO,OAAO,SAAS,SAAS,aAAa;AAC/C;;;;AAKA,SAAgB,uBACd,SACA,eAC4B;CAC5B,OAAO,OAAO,SAAS,SAAS,aAAa;AAC/C"}
|
package/dist/parse-DqXV5xqQ.mjs
DELETED
|
@@ -1,514 +0,0 @@
|
|
|
1
|
-
import { D as parseParagraphProperties, E as parseParagraph, F as settingsDesc, L as buildNumberingCache, R as buildStyleCache, T as footnotesDesc, V as parseNumberingDefinitions, _ as glossaryDesc, c as customPropertiesDesc, f as contentTypesDesc, j as parseToc, l as corePropertiesDesc, o as webSettingsDesc, p as commentsDesc, s as appPropertiesDesc, t as DocxReadContext, v as bibliographyDesc, w as endnotesDesc, y as fontTableDesc, z as parseStyleDefinitions } from "./context-DfxK5XnG.mjs";
|
|
2
|
-
import { $ as setBodyParseChild, D as parseSdtBlock, E as tableDesc, T as setTableParseChild, Z as parseCustomXmlPr, t as parseSectionPropertiesEl } from "./document-D2OTVcbX.mjs";
|
|
3
|
-
import { parseArchive, toUint8Array } from "@office-open/core";
|
|
4
|
-
import { attr, findChild, stringify } from "@office-open/xml";
|
|
5
|
-
//#region src/parts/alt-chunk/alt-chunk-parse.ts
|
|
6
|
-
/**
|
|
7
|
-
* AltChunk parser for DOCX documents.
|
|
8
|
-
*
|
|
9
|
-
* Parses w:altChunk elements and extracts embedded content from the ZIP.
|
|
10
|
-
*
|
|
11
|
-
* @module
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* Parse a w:altChunk element into AltChunkOptions.
|
|
15
|
-
* Reads the referenced data from the ZIP package.
|
|
16
|
-
*/
|
|
17
|
-
function parseAltChunk(el, ctx) {
|
|
18
|
-
const rId = attr(el, "r:id");
|
|
19
|
-
if (!rId) throw new Error("w:altChunk missing r:id attribute");
|
|
20
|
-
const path = ctx.docx.partRefs.afChunks.get(rId);
|
|
21
|
-
if (!path) throw new Error(`AltChunk relationship ${rId} not found`);
|
|
22
|
-
const data = ctx.docx.doc.getRaw(path);
|
|
23
|
-
if (!data) throw new Error(`AltChunk data not found at ${path}`);
|
|
24
|
-
const ext = path.split(".").pop() ?? "txt";
|
|
25
|
-
let contentType;
|
|
26
|
-
let extension;
|
|
27
|
-
switch (ext) {
|
|
28
|
-
case "html":
|
|
29
|
-
contentType = "text/html";
|
|
30
|
-
extension = "html";
|
|
31
|
-
break;
|
|
32
|
-
case "rtf":
|
|
33
|
-
contentType = "application/rtf";
|
|
34
|
-
extension = "rtf";
|
|
35
|
-
break;
|
|
36
|
-
default:
|
|
37
|
-
contentType = "text/plain";
|
|
38
|
-
extension = "txt";
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
return {
|
|
42
|
-
data,
|
|
43
|
-
contentType,
|
|
44
|
-
extension
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region src/parts/custom-xml/custom-xml-parse.ts
|
|
49
|
-
/**
|
|
50
|
-
* Parser for custom XML block elements (w:customXml).
|
|
51
|
-
*
|
|
52
|
-
* @module
|
|
53
|
-
*/
|
|
54
|
-
/**
|
|
55
|
-
* Parse w:customXml element into CustomXmlBlockOptions.
|
|
56
|
-
*
|
|
57
|
-
* Uses a callback for child parsing to avoid circular dependencies
|
|
58
|
-
* (same pattern as parseTable).
|
|
59
|
-
*/
|
|
60
|
-
function parseCustomXmlBlock(el, ctx, parseChild) {
|
|
61
|
-
const opts = {};
|
|
62
|
-
const element = attr(el, "w:element");
|
|
63
|
-
if (element) opts.element = element;
|
|
64
|
-
const uri = attr(el, "w:uri");
|
|
65
|
-
if (uri) opts.uri = uri;
|
|
66
|
-
const xmlPr = findChild(el, "w:customXmlPr");
|
|
67
|
-
if (xmlPr) opts.customXmlPr = parseCustomXmlPr(xmlPr);
|
|
68
|
-
const children = [];
|
|
69
|
-
for (const child of el.elements ?? []) {
|
|
70
|
-
if (child.name === "w:customXmlPr") continue;
|
|
71
|
-
const parsed = parseChild(child, ctx);
|
|
72
|
-
children.push(parsed);
|
|
73
|
-
}
|
|
74
|
-
if (children.length > 0) opts.children = children;
|
|
75
|
-
return opts;
|
|
76
|
-
}
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/parts/sub-doc/sub-doc-parse.ts
|
|
79
|
-
/**
|
|
80
|
-
* SubDoc parser for DOCX documents.
|
|
81
|
-
*
|
|
82
|
-
* Parses w:subDoc elements and extracts embedded document data.
|
|
83
|
-
*
|
|
84
|
-
* @module
|
|
85
|
-
*/
|
|
86
|
-
/**
|
|
87
|
-
* Parse a w:subDoc element into SubDocOptions.
|
|
88
|
-
* Reads the referenced document data from the ZIP package.
|
|
89
|
-
*/
|
|
90
|
-
function parseSubDoc(el, ctx) {
|
|
91
|
-
const rId = attr(el, "r:id");
|
|
92
|
-
if (!rId) throw new Error("w:subDoc missing r:id attribute");
|
|
93
|
-
const path = ctx.docx.partRefs.subDocs.get(rId);
|
|
94
|
-
if (!path) throw new Error(`SubDoc relationship ${rId} not found`);
|
|
95
|
-
const data = ctx.docx.doc.getRaw(path);
|
|
96
|
-
if (!data) throw new Error(`SubDoc data not found at ${path}`);
|
|
97
|
-
return { data };
|
|
98
|
-
}
|
|
99
|
-
//#endregion
|
|
100
|
-
//#region src/parts/textbox/textbox-parse.ts
|
|
101
|
-
/**
|
|
102
|
-
* Textbox parser for DOCX documents.
|
|
103
|
-
*
|
|
104
|
-
* Parses w:pict → v:shape → v:textbox → w:txbxContent elements.
|
|
105
|
-
*
|
|
106
|
-
* @module
|
|
107
|
-
*/
|
|
108
|
-
/**
|
|
109
|
-
* Parse VML shape style string into VmlShapeStyle-like object.
|
|
110
|
-
*/
|
|
111
|
-
function parseVmlStyle(styleStr) {
|
|
112
|
-
const style = {};
|
|
113
|
-
for (const part of styleStr.split(";")) {
|
|
114
|
-
const [key, val] = part.split(":").map((s) => s.trim());
|
|
115
|
-
if (key && val) style[key] = val;
|
|
116
|
-
}
|
|
117
|
-
return style;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Parse a w:pict element that contains a textbox.
|
|
121
|
-
* Returns an object suitable for the { textbox: ... } SectionChild variant.
|
|
122
|
-
*/
|
|
123
|
-
function parseTextbox(el, ctx, parseChildren) {
|
|
124
|
-
const shape = findDeep$1(el, "v:shape")[0];
|
|
125
|
-
if (!shape) return {};
|
|
126
|
-
const opts = {};
|
|
127
|
-
const styleAttr = attr(shape, "style");
|
|
128
|
-
if (styleAttr) opts.style = parseVmlStyle(styleAttr);
|
|
129
|
-
const textbox = findDeep$1(shape, "v:textbox")[0];
|
|
130
|
-
if (textbox) {
|
|
131
|
-
const txbxContent = findChild(textbox, "w:txbxContent");
|
|
132
|
-
if (txbxContent) {
|
|
133
|
-
const childList = parseChildren(txbxContent.elements ?? [], ctx);
|
|
134
|
-
if (childList.length > 0) opts.children = childList;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return opts;
|
|
138
|
-
}
|
|
139
|
-
function findDeep$1(parent, name) {
|
|
140
|
-
const result = [];
|
|
141
|
-
for (const child of parent.elements ?? []) {
|
|
142
|
-
if (child.name === name) result.push(child);
|
|
143
|
-
result.push(...findDeep$1(child, name));
|
|
144
|
-
}
|
|
145
|
-
return result;
|
|
146
|
-
}
|
|
147
|
-
//#endregion
|
|
148
|
-
//#region src/parse/body.ts
|
|
149
|
-
/**
|
|
150
|
-
* Body parser for DOCX documents.
|
|
151
|
-
*
|
|
152
|
-
* Parses w:body → SectionOptions[] by splitting at w:sectPr boundaries.
|
|
153
|
-
*
|
|
154
|
-
* @module
|
|
155
|
-
*/
|
|
156
|
-
/**
|
|
157
|
-
* Parse w:sectPr element into SectionPropertiesOptions.
|
|
158
|
-
* Delegates to the section properties descriptor's parse method.
|
|
159
|
-
*/
|
|
160
|
-
function parseSectionProperties(el, ctx) {
|
|
161
|
-
const opts = parseSectionPropertiesEl(el);
|
|
162
|
-
const headerRefs = {};
|
|
163
|
-
const footerRefs = {};
|
|
164
|
-
for (const child of el.elements ?? []) {
|
|
165
|
-
if (child.name === "w:headerReference") {
|
|
166
|
-
const rId = attr(child, "r:id");
|
|
167
|
-
const type = attr(child, "w:type");
|
|
168
|
-
if (rId && type) {
|
|
169
|
-
const headerChildren = parseHeaderFooterRef(rId, ctx);
|
|
170
|
-
if (headerChildren) headerRefs[type] = headerChildren;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (child.name === "w:footerReference") {
|
|
174
|
-
const rId = attr(child, "r:id");
|
|
175
|
-
const type = attr(child, "w:type");
|
|
176
|
-
if (rId && type) {
|
|
177
|
-
const footerChildren = parseHeaderFooterRef(rId, ctx);
|
|
178
|
-
if (footerChildren) footerRefs[type] = footerChildren;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
if (Object.keys(headerRefs).length > 0) opts.parsedHeaders = headerRefs;
|
|
183
|
-
if (Object.keys(footerRefs).length > 0) opts.parsedFooters = footerRefs;
|
|
184
|
-
return opts;
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Parse a header/footer reference by following the relationship to its XML part.
|
|
188
|
-
*/
|
|
189
|
-
function parseHeaderFooterRef(rId, ctx) {
|
|
190
|
-
const path = ctx.docx.partRefs.headers.get(rId) ?? ctx.docx.partRefs.footers.get(rId);
|
|
191
|
-
if (!path) return void 0;
|
|
192
|
-
const partEl = ctx.docx.doc.get(path);
|
|
193
|
-
if (!partEl) return void 0;
|
|
194
|
-
const children = [];
|
|
195
|
-
for (const child of partEl.elements ?? []) {
|
|
196
|
-
const sectionChild = parseSectionChild(child, ctx);
|
|
197
|
-
if (sectionChild !== void 0) children.push(sectionChild);
|
|
198
|
-
}
|
|
199
|
-
return children.length > 0 ? children : void 0;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Parse a single body child element into a SectionChild.
|
|
203
|
-
*/
|
|
204
|
-
function parseSectionChild(el, ctx) {
|
|
205
|
-
switch (el.name) {
|
|
206
|
-
case "w:p": {
|
|
207
|
-
const pict = findChild(el, "w:pict");
|
|
208
|
-
if (pict) {
|
|
209
|
-
if (findDeepElement(pict, "v:textbox")) return { textbox: parseTextbox(pict, ctx, parseSectionChildrenElements) };
|
|
210
|
-
}
|
|
211
|
-
return { paragraph: parseParagraph(el, ctx) };
|
|
212
|
-
}
|
|
213
|
-
case "w:tbl": return { table: tableDesc.parse(el, ctx) };
|
|
214
|
-
case "w:sdt": {
|
|
215
|
-
const tocResult = parseToc(el, ctx);
|
|
216
|
-
if (tocResult) return { toc: tocResult };
|
|
217
|
-
const sdtResult = parseSdtBlock(el, ctx, parseSectionChildrenElements);
|
|
218
|
-
return { sdt: {
|
|
219
|
-
properties: sdtResult.properties,
|
|
220
|
-
children: sdtResult.children
|
|
221
|
-
} };
|
|
222
|
-
}
|
|
223
|
-
case "w:altChunk": return { altChunk: parseAltChunk(el, ctx) };
|
|
224
|
-
case "w:subDoc": return { subDoc: parseSubDoc(el, ctx) };
|
|
225
|
-
case "w:customXml": return { customXml: parseCustomXmlBlock(el, ctx, parseSectionChild) };
|
|
226
|
-
default: return { rawXml: stringify(el) };
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Find a deep descendant element by name.
|
|
231
|
-
*/
|
|
232
|
-
function findDeepElement(parent, name) {
|
|
233
|
-
for (const child of parent.elements ?? []) {
|
|
234
|
-
if (child.name === name) return child;
|
|
235
|
-
const found = findDeepElement(child, name);
|
|
236
|
-
if (found) return found;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Parse w:body element into SectionOptions[].
|
|
241
|
-
*
|
|
242
|
-
* Splits body content at w:sectPr boundaries to create sections.
|
|
243
|
-
* The last w:sectPr (child of w:body directly) defines the last section.
|
|
244
|
-
* Previous w:sectPr elements appear inside w:pPr elements.
|
|
245
|
-
*/
|
|
246
|
-
function parseBody(body, ctx) {
|
|
247
|
-
setBodyParseChild(parseSectionChild);
|
|
248
|
-
const bodyChildren = [];
|
|
249
|
-
const boundaries = [];
|
|
250
|
-
for (const child of body.elements ?? []) if (child.name === "w:sectPr") boundaries.push({
|
|
251
|
-
index: bodyChildren.length,
|
|
252
|
-
sectPr: child
|
|
253
|
-
});
|
|
254
|
-
else {
|
|
255
|
-
bodyChildren.push(child);
|
|
256
|
-
if (child.name === "w:p") {
|
|
257
|
-
const pPr = findChild(child, "w:pPr");
|
|
258
|
-
if (pPr) {
|
|
259
|
-
const sectPr = findChild(pPr, "w:sectPr");
|
|
260
|
-
if (sectPr) boundaries.push({
|
|
261
|
-
index: bodyChildren.length,
|
|
262
|
-
sectPr
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
if (boundaries.length === 0) return [{ children: bodyChildren.map((el) => parseSectionChild(el, ctx)) }];
|
|
268
|
-
const sections = [];
|
|
269
|
-
let start = 0;
|
|
270
|
-
for (let i = 0; i < boundaries.length; i++) {
|
|
271
|
-
const boundary = boundaries[i];
|
|
272
|
-
const endIdx = i < boundaries.length - 1 ? Math.max(start, boundary.index - 1) : boundary.index;
|
|
273
|
-
const sectionElements = bodyChildren.slice(start, endIdx);
|
|
274
|
-
const parsedProps = parseSectionProperties(boundary.sectPr, ctx);
|
|
275
|
-
const rawProps = parsedProps;
|
|
276
|
-
const parsedHeaders = rawProps.parsedHeaders;
|
|
277
|
-
const parsedFooters = rawProps.parsedFooters;
|
|
278
|
-
const cleanProps = { ...parsedProps };
|
|
279
|
-
delete cleanProps.parsedHeaders;
|
|
280
|
-
delete cleanProps.parsedFooters;
|
|
281
|
-
const section = {
|
|
282
|
-
children: sectionElements.map((el) => parseSectionChild(el, ctx)),
|
|
283
|
-
properties: cleanProps,
|
|
284
|
-
...parsedHeaders ? { headers: parsedHeaders } : {},
|
|
285
|
-
...parsedFooters ? { footers: parsedFooters } : {}
|
|
286
|
-
};
|
|
287
|
-
sections.push(section);
|
|
288
|
-
start = boundary.index;
|
|
289
|
-
}
|
|
290
|
-
return sections;
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Parse a list of elements into SectionChild[].
|
|
294
|
-
* Used by SDT and textbox parsers for their content.
|
|
295
|
-
*/
|
|
296
|
-
function parseSectionChildrenElements(elements, ctx) {
|
|
297
|
-
return elements.map((el) => parseSectionChild(el, ctx));
|
|
298
|
-
}
|
|
299
|
-
//#endregion
|
|
300
|
-
//#region src/parse.ts
|
|
301
|
-
function resolveRelsPath(target) {
|
|
302
|
-
if (target.startsWith("/")) return target.slice(1);
|
|
303
|
-
if (target.startsWith("../")) return target.replace("../", "");
|
|
304
|
-
return `word/${target}`;
|
|
305
|
-
}
|
|
306
|
-
function parseDocPartRefs(doc) {
|
|
307
|
-
const refs = {
|
|
308
|
-
headers: /* @__PURE__ */ new Map(),
|
|
309
|
-
footers: /* @__PURE__ */ new Map(),
|
|
310
|
-
hyperlinks: /* @__PURE__ */ new Map(),
|
|
311
|
-
charts: /* @__PURE__ */ new Map(),
|
|
312
|
-
diagramData: /* @__PURE__ */ new Map(),
|
|
313
|
-
media: /* @__PURE__ */ new Map(),
|
|
314
|
-
afChunks: /* @__PURE__ */ new Map(),
|
|
315
|
-
subDocs: /* @__PURE__ */ new Map()
|
|
316
|
-
};
|
|
317
|
-
const relsEl = doc.get("word/_rels/document.xml.rels");
|
|
318
|
-
if (!relsEl) return refs;
|
|
319
|
-
for (const child of relsEl.elements ?? []) {
|
|
320
|
-
if (child.name !== "Relationship") continue;
|
|
321
|
-
const type = attr(child, "Type") ?? "";
|
|
322
|
-
const target = attr(child, "Target") ?? "";
|
|
323
|
-
const id = attr(child, "Id") ?? "";
|
|
324
|
-
if (!target) continue;
|
|
325
|
-
const path = resolveRelsPath(target);
|
|
326
|
-
if (type.includes("/header")) refs.headers.set(id, path);
|
|
327
|
-
else if (type.includes("/footer")) refs.footers.set(id, path);
|
|
328
|
-
else if (type.includes("/footnotes")) refs.footnotes = path;
|
|
329
|
-
else if (type.includes("/endnotes")) refs.endnotes = path;
|
|
330
|
-
else if (type.includes("/comments")) refs.comments = path;
|
|
331
|
-
else if (type.includes("/chart")) refs.charts.set(id, path);
|
|
332
|
-
else if (type.includes("/diagramData")) refs.diagramData.set(id, path);
|
|
333
|
-
else if (type.includes("/image") || type.includes("/media")) refs.media.set(id, path);
|
|
334
|
-
else if (type.includes("/aFChunk")) refs.afChunks.set(id, path);
|
|
335
|
-
else if (type.includes("/subDocument")) refs.subDocs.set(id, path);
|
|
336
|
-
else if (type.includes("/bibliography")) refs.bibliography = path;
|
|
337
|
-
else if (type.includes("/glossaryDocument")) refs.glossary = path;
|
|
338
|
-
else if (type.includes("/hyperlink")) refs.hyperlinks.set(id, target);
|
|
339
|
-
}
|
|
340
|
-
return refs;
|
|
341
|
-
}
|
|
342
|
-
function parseRootRels(doc) {
|
|
343
|
-
const relsEl = doc.get("_rels/.rels");
|
|
344
|
-
if (!relsEl) return {};
|
|
345
|
-
let coreProps;
|
|
346
|
-
let appProps;
|
|
347
|
-
let customProps;
|
|
348
|
-
for (const child of relsEl.elements ?? []) {
|
|
349
|
-
if (child.name !== "Relationship") continue;
|
|
350
|
-
const type = attr(child, "Type") ?? "";
|
|
351
|
-
const target = attr(child, "Target") ?? "";
|
|
352
|
-
if (!target) continue;
|
|
353
|
-
const path = target.startsWith("/") ? target.slice(1) : target;
|
|
354
|
-
if (type.includes("/core-properties")) coreProps = path;
|
|
355
|
-
else if (type.includes("/extended-properties")) appProps = path;
|
|
356
|
-
else if (type.includes("/custom-properties")) customProps = path;
|
|
357
|
-
}
|
|
358
|
-
return {
|
|
359
|
-
coreProps,
|
|
360
|
-
appProps,
|
|
361
|
-
customProps
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* Parse a .docx file and convert it into DocumentOptions.
|
|
366
|
-
*
|
|
367
|
-
* This is the main public API for parsing DOCX files.
|
|
368
|
-
* The returned options can be passed directly to `new Document(parsed)`
|
|
369
|
-
* to recreate the document.
|
|
370
|
-
*
|
|
371
|
-
* @param data - Raw bytes of a .docx file
|
|
372
|
-
* @returns Document options including sections and metadata
|
|
373
|
-
*/
|
|
374
|
-
function parseDocument(data) {
|
|
375
|
-
const docx = parseDocx(data);
|
|
376
|
-
const ctx = new DocxReadContext(docx, buildStyleCache(docx.styles), buildNumberingCache(docx.numbering));
|
|
377
|
-
setTableParseChild(parseSectionChild);
|
|
378
|
-
setBodyParseChild(parseSectionChild);
|
|
379
|
-
const opts = { sections: parseBody(docx.body, ctx) };
|
|
380
|
-
if (docx.background) {
|
|
381
|
-
const bg = {};
|
|
382
|
-
const color = attr(docx.background, "w:color");
|
|
383
|
-
if (color) bg.color = color;
|
|
384
|
-
const themeColor = attr(docx.background, "w:themeColor");
|
|
385
|
-
if (themeColor) bg.themeColor = themeColor;
|
|
386
|
-
const themeShade = attr(docx.background, "w:themeShade");
|
|
387
|
-
if (themeShade) bg.themeShade = themeShade;
|
|
388
|
-
const themeTint = attr(docx.background, "w:themeTint");
|
|
389
|
-
if (themeTint) bg.themeTint = themeTint;
|
|
390
|
-
if (Object.keys(bg).length > 0) opts.background = bg;
|
|
391
|
-
}
|
|
392
|
-
if (docx.coreProps) {
|
|
393
|
-
const corePropsEl = docx.doc.get(docx.coreProps);
|
|
394
|
-
if (corePropsEl) {
|
|
395
|
-
const cp = corePropertiesDesc.parse(corePropsEl, ctx);
|
|
396
|
-
if (cp.title) opts.title = cp.title;
|
|
397
|
-
if (cp.subject) opts.subject = cp.subject;
|
|
398
|
-
if (cp.creator) opts.creator = cp.creator;
|
|
399
|
-
if (cp.keywords) opts.keywords = cp.keywords;
|
|
400
|
-
if (cp.description) opts.description = cp.description;
|
|
401
|
-
if (cp.lastModifiedBy) opts.lastModifiedBy = cp.lastModifiedBy;
|
|
402
|
-
if (cp.revision) opts.revision = cp.revision;
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
if (docx.appProps) {
|
|
406
|
-
const appPropsEl = docx.doc.get(docx.appProps);
|
|
407
|
-
if (appPropsEl) {
|
|
408
|
-
const ap = appPropertiesDesc.parse(appPropsEl, ctx);
|
|
409
|
-
if (Object.keys(ap).length > 0) opts.appProperties = ap;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
if (docx.settings) Object.assign(opts, settingsDesc.parse(docx.settings, ctx));
|
|
413
|
-
if (docx.webSettings) {
|
|
414
|
-
const wsOpts = webSettingsDesc.parse(docx.webSettings, ctx);
|
|
415
|
-
if (Object.keys(wsOpts).length > 0) opts.webSettings = wsOpts;
|
|
416
|
-
}
|
|
417
|
-
if (docx.customProps) {
|
|
418
|
-
const customPropsEl = docx.doc.get(docx.customProps);
|
|
419
|
-
if (customPropsEl) {
|
|
420
|
-
const cpResult = customPropertiesDesc.parse(customPropsEl, ctx);
|
|
421
|
-
if (cpResult.properties && cpResult.properties.length > 0) opts.customProperties = cpResult.properties;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
if (docx.partRefs.comments) {
|
|
425
|
-
const commentsEl = docx.doc.get(docx.partRefs.comments);
|
|
426
|
-
if (commentsEl) {
|
|
427
|
-
const children = commentsDesc.parse(commentsEl, ctx).children;
|
|
428
|
-
if (children && children.length > 0) opts.comments = { children };
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
if (docx.partRefs.footnotes) {
|
|
432
|
-
const footnotesEl = docx.doc.get(docx.partRefs.footnotes);
|
|
433
|
-
if (footnotesEl) {
|
|
434
|
-
const fnResult = footnotesDesc.parse(footnotesEl, ctx);
|
|
435
|
-
const footnotesMap = {};
|
|
436
|
-
for (const [id, paragraphs] of fnResult.notes) footnotesMap[String(id)] = { children: paragraphs };
|
|
437
|
-
if (Object.keys(footnotesMap).length > 0) opts.footnotes = footnotesMap;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
if (docx.partRefs.endnotes) {
|
|
441
|
-
const endnotesEl = docx.doc.get(docx.partRefs.endnotes);
|
|
442
|
-
if (endnotesEl) {
|
|
443
|
-
const enResult = endnotesDesc.parse(endnotesEl, ctx);
|
|
444
|
-
const endnotesMap = {};
|
|
445
|
-
for (const [id, paragraphs] of enResult.notes) endnotesMap[String(id)] = { children: paragraphs };
|
|
446
|
-
if (Object.keys(endnotesMap).length > 0) opts.endnotes = endnotesMap;
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
if (docx.styles) {
|
|
450
|
-
const styleOpts = parseStyleDefinitions(docx.styles, parseParagraphProperties, ctx);
|
|
451
|
-
if (styleOpts) opts.styles = styleOpts;
|
|
452
|
-
}
|
|
453
|
-
if (docx.numbering) {
|
|
454
|
-
const numOpts = parseNumberingDefinitions(docx.numbering, parseParagraphProperties, ctx);
|
|
455
|
-
if (numOpts) opts.numbering = numOpts;
|
|
456
|
-
}
|
|
457
|
-
if (docx.fontTable) {
|
|
458
|
-
const ftResult = fontTableDesc.parse(docx.fontTable, ctx);
|
|
459
|
-
if (ftResult.fonts && ftResult.fonts.length > 0) opts.fonts = ftResult.fonts;
|
|
460
|
-
}
|
|
461
|
-
if (docx.partRefs.bibliography) {
|
|
462
|
-
const bibEl = docx.doc.get(docx.partRefs.bibliography);
|
|
463
|
-
if (bibEl) {
|
|
464
|
-
const bibResult = bibliographyDesc.parse(bibEl, ctx);
|
|
465
|
-
if (bibResult.sources && bibResult.sources.length > 0) opts.bibliography = bibResult;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
if (docx.partRefs.glossary) {
|
|
469
|
-
const glossaryEl = docx.doc.get(docx.partRefs.glossary);
|
|
470
|
-
if (glossaryEl) {
|
|
471
|
-
const glossaryResult = glossaryDesc.parse(glossaryEl, ctx);
|
|
472
|
-
if (glossaryResult.parts && glossaryResult.parts.length > 0) opts.glossary = glossaryResult;
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
if (docx.contentTypes) {
|
|
476
|
-
const ctResult = contentTypesDesc.parse(docx.contentTypes, ctx);
|
|
477
|
-
if (ctResult) opts.contentTypes = ctResult;
|
|
478
|
-
}
|
|
479
|
-
return opts;
|
|
480
|
-
}
|
|
481
|
-
function parseDocx(data) {
|
|
482
|
-
const doc = parseArchive(toUint8Array(data));
|
|
483
|
-
const documentEl = doc.get("word/document.xml");
|
|
484
|
-
if (!documentEl) throw new Error("word/document.xml not found");
|
|
485
|
-
const body = documentEl.elements?.find((e) => e.name === "w:body");
|
|
486
|
-
if (!body) throw new Error("w:body not found in word/document.xml");
|
|
487
|
-
const background = documentEl.elements?.find((e) => e.name === "w:background");
|
|
488
|
-
const styles = doc.get("word/styles.xml");
|
|
489
|
-
const numbering = doc.get("word/numbering.xml");
|
|
490
|
-
const settings = doc.get("word/settings.xml");
|
|
491
|
-
const fontTable = doc.get("word/fontTable.xml");
|
|
492
|
-
const webSettings = doc.get("word/webSettings.xml");
|
|
493
|
-
const partRefs = parseDocPartRefs(doc);
|
|
494
|
-
const { coreProps, appProps, customProps } = parseRootRels(doc);
|
|
495
|
-
return {
|
|
496
|
-
doc,
|
|
497
|
-
body,
|
|
498
|
-
background,
|
|
499
|
-
styles,
|
|
500
|
-
numbering,
|
|
501
|
-
settings,
|
|
502
|
-
fontTable,
|
|
503
|
-
webSettings,
|
|
504
|
-
partRefs,
|
|
505
|
-
coreProps,
|
|
506
|
-
appProps,
|
|
507
|
-
customProps,
|
|
508
|
-
contentTypes: doc.get("[Content_Types].xml")
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
//#endregion
|
|
512
|
-
export { parseDocument as n, parseDocx as r, parseArchive as t };
|
|
513
|
-
|
|
514
|
-
//# sourceMappingURL=parse-DqXV5xqQ.mjs.map
|