@beyondwork/docx-react-component 1.0.27 → 1.0.29
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/dist/canonical-document-BLEbzL2J.d.cts +844 -0
- package/dist/canonical-document-BLEbzL2J.d.ts +844 -0
- package/dist/chunk-2FJS5GZM.js +763 -0
- package/dist/chunk-2FJS5GZM.js.map +1 -0
- package/{src/core/commands/section-layout-commands.ts → dist/chunk-2OQBZS3F.js} +106 -340
- package/dist/chunk-2OQBZS3F.js.map +1 -0
- package/dist/chunk-2S7W4KFO.js +127 -0
- package/dist/chunk-2S7W4KFO.js.map +1 -0
- package/dist/chunk-2TG72QSW.js +3874 -0
- package/dist/chunk-2TG72QSW.js.map +1 -0
- package/{src/core/commands/table-structure-commands.ts → dist/chunk-36QNIZBO.js} +126 -315
- package/dist/chunk-36QNIZBO.js.map +1 -0
- package/dist/chunk-4AQOYAW4.js +3069 -0
- package/dist/chunk-4AQOYAW4.js.map +1 -0
- package/dist/chunk-4D5EWJ3P.js +77 -0
- package/dist/chunk-4D5EWJ3P.js.map +1 -0
- package/dist/chunk-5FN54NDH.js +2257 -0
- package/dist/chunk-5FN54NDH.js.map +1 -0
- package/dist/chunk-BOYGQYRQ.js +7306 -0
- package/dist/chunk-BOYGQYRQ.js.map +1 -0
- package/dist/chunk-CN3XMECL.js +212 -0
- package/dist/chunk-CN3XMECL.js.map +1 -0
- package/dist/chunk-EBI3BX6U.js +164 -0
- package/dist/chunk-EBI3BX6U.js.map +1 -0
- package/dist/chunk-EILUG3VB.js +1275 -0
- package/dist/chunk-EILUG3VB.js.map +1 -0
- package/dist/chunk-FUDY333O.js +70 -0
- package/dist/chunk-FUDY333O.js.map +1 -0
- package/dist/chunk-GBVOWFIK.js +1237 -0
- package/dist/chunk-GBVOWFIK.js.map +1 -0
- package/dist/chunk-H4TQ3H3Y.js +262 -0
- package/dist/chunk-H4TQ3H3Y.js.map +1 -0
- package/{src/core/commands/style-commands.ts → dist/chunk-JGB3IXZO.js} +40 -113
- package/dist/chunk-JGB3IXZO.js.map +1 -0
- package/dist/chunk-KD2QRQPY.js +4342 -0
- package/dist/chunk-KD2QRQPY.js.map +1 -0
- package/dist/chunk-KLMXQVYK.js +369 -0
- package/dist/chunk-KLMXQVYK.js.map +1 -0
- package/dist/chunk-KZUG5KFQ.js +214 -0
- package/dist/chunk-KZUG5KFQ.js.map +1 -0
- package/{src/core/state/text-transaction.ts → dist/chunk-QDAQ4CJU.js} +79 -236
- package/dist/chunk-QDAQ4CJU.js.map +1 -0
- package/{src/legal/bookmarks.ts → dist/chunk-RMH72RZI.js} +44 -130
- package/dist/chunk-RMH72RZI.js.map +1 -0
- package/dist/chunk-SWKWQZXM.js +117 -0
- package/dist/chunk-SWKWQZXM.js.map +1 -0
- package/{src/core/commands/formatting-commands.ts → dist/chunk-TJBP2K4T.js} +196 -536
- package/dist/chunk-TJBP2K4T.js.map +1 -0
- package/dist/chunk-TLCEAQDQ.js +542 -0
- package/dist/chunk-TLCEAQDQ.js.map +1 -0
- package/{src/core/commands/text-commands.ts → dist/chunk-UZXBISGO.js} +86 -142
- package/dist/chunk-UZXBISGO.js.map +1 -0
- package/dist/chunk-WGBAKP3Q.js +3220 -0
- package/dist/chunk-WGBAKP3Q.js.map +1 -0
- package/dist/compare/index.cjs +5475 -0
- package/dist/compare/index.cjs.map +1 -0
- package/dist/compare/index.d.cts +114 -0
- package/dist/compare/index.d.ts +114 -0
- package/dist/compare/index.js +731 -0
- package/dist/compare/index.js.map +1 -0
- package/dist/core/commands/formatting-commands.cjs +828 -0
- package/dist/core/commands/formatting-commands.cjs.map +1 -0
- package/dist/core/commands/formatting-commands.d.cts +63 -0
- package/dist/core/commands/formatting-commands.d.ts +63 -0
- package/dist/core/commands/formatting-commands.js +37 -0
- package/dist/core/commands/formatting-commands.js.map +1 -0
- package/dist/core/commands/image-commands.cjs +2023 -0
- package/dist/core/commands/image-commands.cjs.map +1 -0
- package/dist/core/commands/image-commands.d.cts +58 -0
- package/dist/core/commands/image-commands.d.ts +58 -0
- package/dist/core/commands/image-commands.js +18 -0
- package/dist/core/commands/image-commands.js.map +1 -0
- package/dist/core/commands/section-layout-commands.cjs +477 -0
- package/dist/core/commands/section-layout-commands.cjs.map +1 -0
- package/dist/core/commands/section-layout-commands.d.cts +62 -0
- package/dist/core/commands/section-layout-commands.d.ts +62 -0
- package/dist/core/commands/section-layout-commands.js +21 -0
- package/dist/core/commands/section-layout-commands.js.map +1 -0
- package/dist/core/commands/style-commands.cjs +214 -0
- package/dist/core/commands/style-commands.cjs.map +1 -0
- package/dist/core/commands/style-commands.d.cts +13 -0
- package/dist/core/commands/style-commands.d.ts +13 -0
- package/dist/core/commands/style-commands.js +9 -0
- package/dist/core/commands/style-commands.js.map +1 -0
- package/dist/core/commands/table-structure-commands.cjs +1883 -0
- package/dist/core/commands/table-structure-commands.cjs.map +1 -0
- package/dist/core/commands/table-structure-commands.d.cts +59 -0
- package/dist/core/commands/table-structure-commands.d.ts +59 -0
- package/dist/core/commands/table-structure-commands.js +12 -0
- package/dist/core/commands/table-structure-commands.js.map +1 -0
- package/dist/core/commands/text-commands.cjs +2391 -0
- package/dist/core/commands/text-commands.cjs.map +1 -0
- package/dist/core/commands/text-commands.d.cts +24 -0
- package/dist/core/commands/text-commands.d.ts +24 -0
- package/dist/core/commands/text-commands.js +28 -0
- package/dist/core/commands/text-commands.js.map +1 -0
- package/dist/core/selection/mapping.cjs +200 -0
- package/dist/core/selection/mapping.cjs.map +1 -0
- package/dist/core/selection/mapping.d.cts +2 -0
- package/dist/core/selection/mapping.d.ts +2 -0
- package/dist/core/selection/mapping.js +31 -0
- package/dist/core/selection/mapping.js.map +1 -0
- package/dist/core/state/editor-state.cjs +2278 -0
- package/dist/core/state/editor-state.cjs.map +1 -0
- package/dist/core/state/editor-state.d.cts +2 -0
- package/dist/core/state/editor-state.d.ts +2 -0
- package/dist/core/state/editor-state.js +26 -0
- package/dist/core/state/editor-state.js.map +1 -0
- package/dist/index.cjs +38553 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +7856 -0
- package/dist/index.js.map +1 -0
- package/dist/io/docx-session.cjs +16236 -0
- package/dist/io/docx-session.cjs.map +1 -0
- package/dist/io/docx-session.d.cts +21 -0
- package/dist/io/docx-session.d.ts +21 -0
- package/dist/io/docx-session.js +18 -0
- package/dist/io/docx-session.js.map +1 -0
- package/dist/legal/index.cjs +3900 -0
- package/dist/legal/index.cjs.map +1 -0
- package/dist/legal/index.d.cts +86 -0
- package/dist/legal/index.d.ts +86 -0
- package/dist/legal/index.js +616 -0
- package/dist/legal/index.js.map +1 -0
- package/dist/public-types-7ZL_94cz.d.ts +1573 -0
- package/dist/public-types-CeMaDueh.d.cts +1573 -0
- package/dist/public-types.cjs +19 -0
- package/dist/public-types.cjs.map +1 -0
- package/dist/public-types.d.cts +2 -0
- package/dist/public-types.d.ts +2 -0
- package/dist/public-types.js +1 -0
- package/dist/public-types.js.map +1 -0
- package/dist/runtime/document-runtime.cjs +11140 -0
- package/dist/runtime/document-runtime.cjs.map +1 -0
- package/dist/runtime/document-runtime.d.cts +231 -0
- package/dist/runtime/document-runtime.d.ts +231 -0
- package/dist/runtime/document-runtime.js +21 -0
- package/dist/runtime/document-runtime.js.map +1 -0
- package/dist/structural-helpers-CilgOVhh.d.cts +10 -0
- package/dist/structural-helpers-q0Gd-eBN.d.ts +10 -0
- package/dist/ui-tailwind/editor-surface/search-plugin.cjs +313 -0
- package/dist/ui-tailwind/editor-surface/search-plugin.cjs.map +1 -0
- package/dist/ui-tailwind/editor-surface/search-plugin.d.cts +67 -0
- package/dist/ui-tailwind/editor-surface/search-plugin.d.ts +67 -0
- package/dist/ui-tailwind/editor-surface/search-plugin.js +23 -0
- package/dist/ui-tailwind/editor-surface/search-plugin.js.map +1 -0
- package/dist/ui-tailwind/index.cjs +4833 -0
- package/dist/ui-tailwind/index.cjs.map +1 -0
- package/dist/ui-tailwind/index.d.cts +617 -0
- package/dist/ui-tailwind/index.d.ts +617 -0
- package/dist/ui-tailwind/index.js +575 -0
- package/dist/ui-tailwind/index.js.map +1 -0
- package/package.json +64 -54
- package/src/README.md +0 -85
- package/src/api/README.md +0 -26
- package/src/api/public-types.ts +0 -1418
- package/src/api/session-state.ts +0 -60
- package/src/compare/diff-engine.ts +0 -623
- package/src/compare/export-redlines.ts +0 -280
- package/src/compare/index.ts +0 -25
- package/src/compare/snapshot.ts +0 -97
- package/src/component-inventory.md +0 -99
- package/src/core/README.md +0 -10
- package/src/core/commands/README.md +0 -3
- package/src/core/commands/image-commands.ts +0 -373
- package/src/core/commands/index.ts +0 -1757
- package/src/core/commands/list-commands.ts +0 -565
- package/src/core/commands/paragraph-layout-commands.ts +0 -339
- package/src/core/commands/review-commands.ts +0 -108
- package/src/core/commands/structural-helpers.ts +0 -309
- package/src/core/schema/README.md +0 -3
- package/src/core/schema/text-schema.ts +0 -516
- package/src/core/search/search-text.ts +0 -357
- package/src/core/selection/README.md +0 -3
- package/src/core/selection/mapping.ts +0 -289
- package/src/core/selection/review-anchors.ts +0 -183
- package/src/core/state/README.md +0 -3
- package/src/core/state/editor-state.ts +0 -892
- package/src/formats/xlsx/io/parse-shared-strings.ts +0 -41
- package/src/formats/xlsx/io/parse-sheet.ts +0 -459
- package/src/formats/xlsx/io/parse-styles.ts +0 -59
- package/src/formats/xlsx/io/parse-workbook.ts +0 -75
- package/src/formats/xlsx/io/serialize-shared-strings.ts +0 -72
- package/src/formats/xlsx/io/serialize-sheet.ts +0 -333
- package/src/formats/xlsx/io/serialize-styles.ts +0 -98
- package/src/formats/xlsx/io/serialize-workbook.ts +0 -429
- package/src/formats/xlsx/io/xlsx-session.ts +0 -314
- package/src/formats/xlsx/model/cell.ts +0 -189
- package/src/formats/xlsx/model/sheet.ts +0 -326
- package/src/formats/xlsx/model/styles.ts +0 -118
- package/src/formats/xlsx/model/workbook.ts +0 -453
- package/src/formats/xlsx/runtime/cell-commands.ts +0 -567
- package/src/formats/xlsx/runtime/sheet-commands.ts +0 -206
- package/src/formats/xlsx/runtime/workbook-runtime.ts +0 -177
- package/src/formats/xlsx/runtime/workbook-transaction.ts +0 -822
- package/src/index.ts +0 -101
- package/src/io/README.md +0 -10
- package/src/io/docx-session.ts +0 -2882
- package/src/io/export/README.md +0 -3
- package/src/io/export/export-session.ts +0 -220
- package/src/io/export/minimal-docx.ts +0 -115
- package/src/io/export/reattach-preserved-parts.ts +0 -54
- package/src/io/export/serialize-comments.ts +0 -947
- package/src/io/export/serialize-footnotes.ts +0 -399
- package/src/io/export/serialize-headers-footers.ts +0 -372
- package/src/io/export/serialize-main-document.ts +0 -1376
- package/src/io/export/serialize-numbering.ts +0 -118
- package/src/io/export/serialize-revisions.ts +0 -389
- package/src/io/export/serialize-runtime-revisions.ts +0 -269
- package/src/io/export/serialize-tables.ts +0 -174
- package/src/io/export/split-review-boundaries.ts +0 -356
- package/src/io/normalize/README.md +0 -3
- package/src/io/normalize/normalize-text.ts +0 -639
- package/src/io/ooxml/README.md +0 -3
- package/src/io/ooxml/highlight-colors.ts +0 -39
- package/src/io/ooxml/numbering-sentinels.ts +0 -44
- package/src/io/ooxml/parse-comments.ts +0 -846
- package/src/io/ooxml/parse-complex-content.ts +0 -287
- package/src/io/ooxml/parse-fields.ts +0 -834
- package/src/io/ooxml/parse-footnotes.ts +0 -896
- package/src/io/ooxml/parse-headers-footers.ts +0 -1169
- package/src/io/ooxml/parse-inline-media.ts +0 -461
- package/src/io/ooxml/parse-main-document.ts +0 -2877
- package/src/io/ooxml/parse-numbering.ts +0 -432
- package/src/io/ooxml/parse-revisions.ts +0 -931
- package/src/io/ooxml/parse-settings.ts +0 -184
- package/src/io/ooxml/parse-shapes.ts +0 -296
- package/src/io/ooxml/parse-styles.ts +0 -463
- package/src/io/ooxml/parse-tables.ts +0 -618
- package/src/io/ooxml/parse-theme.ts +0 -346
- package/src/io/ooxml/part-manifest.ts +0 -136
- package/src/io/ooxml/revision-boundaries.ts +0 -351
- package/src/io/opc/README.md +0 -3
- package/src/io/opc/corrupt-package.ts +0 -166
- package/src/io/opc/docx-package.ts +0 -74
- package/src/io/opc/package-reader.ts +0 -325
- package/src/io/opc/package-writer.ts +0 -273
- package/src/io/source-package-provenance.ts +0 -241
- package/src/legal/cross-references.ts +0 -414
- package/src/legal/defined-terms.ts +0 -203
- package/src/legal/index.ts +0 -32
- package/src/legal/signature-blocks.ts +0 -259
- package/src/model/README.md +0 -3
- package/src/model/canonical-document.ts +0 -2632
- package/src/model/cds-1.0.0.ts +0 -212
- package/src/model/snapshot.ts +0 -649
- package/src/preservation/README.md +0 -3
- package/src/preservation/markup-compatibility.ts +0 -48
- package/src/preservation/opaque-fragment-store.ts +0 -89
- package/src/preservation/opaque-region.ts +0 -233
- package/src/preservation/package-preservation.ts +0 -113
- package/src/preservation/preserved-part-manifest.ts +0 -56
- package/src/preservation/relationship-retention.ts +0 -57
- package/src/preservation/store.ts +0 -185
- package/src/review/README.md +0 -16
- package/src/review/store/README.md +0 -3
- package/src/review/store/comment-anchors.ts +0 -70
- package/src/review/store/comment-remapping.ts +0 -154
- package/src/review/store/comment-store.ts +0 -331
- package/src/review/store/comment-thread.ts +0 -109
- package/src/review/store/revision-actions.ts +0 -394
- package/src/review/store/revision-store.ts +0 -312
- package/src/review/store/revision-types.ts +0 -171
- package/src/review/store/runtime-comment-store.ts +0 -43
- package/src/runtime/README.md +0 -3
- package/src/runtime/ai-action-policy.ts +0 -764
- package/src/runtime/collab-review-sync.ts +0 -254
- package/src/runtime/document-layout.ts +0 -332
- package/src/runtime/document-navigation.ts +0 -603
- package/src/runtime/document-runtime.ts +0 -3159
- package/src/runtime/document-search.ts +0 -145
- package/src/runtime/numbering-prefix.ts +0 -216
- package/src/runtime/page-layout-estimation.ts +0 -212
- package/src/runtime/read-only-diagnostics-runtime.ts +0 -241
- package/src/runtime/review-runtime.ts +0 -44
- package/src/runtime/revision-runtime.ts +0 -107
- package/src/runtime/session-capabilities.ts +0 -192
- package/src/runtime/story-context.ts +0 -164
- package/src/runtime/story-targeting.ts +0 -162
- package/src/runtime/surface-projection.ts +0 -1357
- package/src/runtime/table-commands.ts +0 -173
- package/src/runtime/table-schema.ts +0 -309
- package/src/runtime/view-state.ts +0 -477
- package/src/runtime/virtualized-rendering.ts +0 -258
- package/src/runtime/workflow-markup.ts +0 -353
- package/src/ui/README.md +0 -30
- package/src/ui/WordReviewEditor.tsx +0 -4097
- package/src/ui/browser-export.ts +0 -52
- package/src/ui/comments/README.md +0 -3
- package/src/ui/compatibility/README.md +0 -3
- package/src/ui/editor-command-bag.ts +0 -120
- package/src/ui/editor-runtime-boundary.ts +0 -1457
- package/src/ui/editor-shell-view.tsx +0 -142
- package/src/ui/editor-surface/README.md +0 -3
- package/src/ui/editor-surface-controller.tsx +0 -63
- package/src/ui/headless/comment-decoration-model.ts +0 -124
- package/src/ui/headless/preserve-editor-selection.ts +0 -5
- package/src/ui/headless/revision-decoration-model.ts +0 -128
- package/src/ui/headless/selection-helpers.ts +0 -54
- package/src/ui/headless/selection-toolbar-model.ts +0 -34
- package/src/ui/headless/use-editor-keyboard.ts +0 -103
- package/src/ui/review/README.md +0 -3
- package/src/ui/runtime-snapshot-selectors.ts +0 -197
- package/src/ui/shared/revision-filters.ts +0 -31
- package/src/ui/status/README.md +0 -3
- package/src/ui/theme/README.md +0 -3
- package/src/ui/toolbar/README.md +0 -3
- package/src/ui/workflow-surface-blocked-rails.ts +0 -94
- package/src/ui-tailwind/chrome/tw-alert-banner.tsx +0 -64
- package/src/ui-tailwind/chrome/tw-image-context-toolbar.tsx +0 -129
- package/src/ui-tailwind/chrome/tw-layout-panel.tsx +0 -114
- package/src/ui-tailwind/chrome/tw-object-context-toolbar.tsx +0 -34
- package/src/ui-tailwind/chrome/tw-page-ruler.tsx +0 -386
- package/src/ui-tailwind/chrome/tw-selection-toolbar.tsx +0 -186
- package/src/ui-tailwind/chrome/tw-suggestion-card.tsx +0 -139
- package/src/ui-tailwind/chrome/tw-table-context-toolbar.tsx +0 -128
- package/src/ui-tailwind/chrome/tw-unsaved-modal.tsx +0 -58
- package/src/ui-tailwind/chrome/use-before-unload.ts +0 -20
- package/src/ui-tailwind/editor-surface/perf-probe.ts +0 -179
- package/src/ui-tailwind/editor-surface/pm-collab-plugins.ts +0 -40
- package/src/ui-tailwind/editor-surface/pm-command-bridge.ts +0 -178
- package/src/ui-tailwind/editor-surface/pm-contextual-ui.ts +0 -31
- package/src/ui-tailwind/editor-surface/pm-decorations.ts +0 -427
- package/src/ui-tailwind/editor-surface/pm-position-map.ts +0 -123
- package/src/ui-tailwind/editor-surface/pm-schema.ts +0 -876
- package/src/ui-tailwind/editor-surface/pm-state-from-snapshot.ts +0 -504
- package/src/ui-tailwind/editor-surface/search-plugin.ts +0 -168
- package/src/ui-tailwind/editor-surface/surface-build-keys.ts +0 -61
- package/src/ui-tailwind/editor-surface/tw-caret.tsx +0 -12
- package/src/ui-tailwind/editor-surface/tw-editor-surface.tsx +0 -150
- package/src/ui-tailwind/editor-surface/tw-inline-token.tsx +0 -129
- package/src/ui-tailwind/editor-surface/tw-opaque-block.tsx +0 -58
- package/src/ui-tailwind/editor-surface/tw-paragraph-block.tsx +0 -151
- package/src/ui-tailwind/editor-surface/tw-prosemirror-surface.tsx +0 -973
- package/src/ui-tailwind/editor-surface/tw-segment-view.tsx +0 -111
- package/src/ui-tailwind/editor-surface/tw-table-node-view.tsx +0 -436
- package/src/ui-tailwind/index.ts +0 -62
- package/src/ui-tailwind/page-chrome-model.ts +0 -27
- package/src/ui-tailwind/review/tw-comment-sidebar.tsx +0 -406
- package/src/ui-tailwind/review/tw-health-panel.tsx +0 -149
- package/src/ui-tailwind/review/tw-review-rail.tsx +0 -120
- package/src/ui-tailwind/review/tw-revision-sidebar.tsx +0 -164
- package/src/ui-tailwind/status/tw-status-bar.tsx +0 -61
- package/src/ui-tailwind/toolbar/tw-toolbar-icon-button.tsx +0 -52
- package/src/ui-tailwind/toolbar/tw-toolbar.tsx +0 -1064
- package/src/ui-tailwind/tw-review-workspace.tsx +0 -1417
- package/src/validation/README.md +0 -3
- package/src/validation/compatibility-engine.ts +0 -634
- package/src/validation/compatibility-report.ts +0 -161
- package/src/validation/diagnostics.ts +0 -204
- package/src/validation/docx-comment-proof.ts +0 -707
- package/src/validation/import-diagnostics.ts +0 -128
- package/src/validation/low-priority-word-surfaces.ts +0 -373
- /package/{src → dist}/ui-tailwind/theme/editor-theme.css +0 -0
|
@@ -0,0 +1,3220 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONTENT_TYPES_PATH,
|
|
3
|
+
PACKAGE_RELATIONSHIPS_PATH,
|
|
4
|
+
comparePartPaths,
|
|
5
|
+
getExtension,
|
|
6
|
+
getRelationshipsPartPath,
|
|
7
|
+
normalizePartPath,
|
|
8
|
+
resolveRelationshipTarget
|
|
9
|
+
} from "./chunk-FUDY333O.js";
|
|
10
|
+
import {
|
|
11
|
+
getOpaqueFragment
|
|
12
|
+
} from "./chunk-SWKWQZXM.js";
|
|
13
|
+
|
|
14
|
+
// src/io/opc/package-writer.ts
|
|
15
|
+
function writeOpcPackage(input) {
|
|
16
|
+
const entries = collectZipEntries(input.manifest, input.parts);
|
|
17
|
+
return buildZipArchive(entries);
|
|
18
|
+
}
|
|
19
|
+
function collectZipEntries(manifest, parts) {
|
|
20
|
+
const entries = [];
|
|
21
|
+
const contentParts = [...parts.values()].filter((part) => part.surfaceKind === "content").sort((left, right) => comparePartPaths(left.path, right.path));
|
|
22
|
+
entries.push(createZipPayload(CONTENT_TYPES_PATH, "deflate", encodeXml(buildContentTypesXml(manifest, contentParts))));
|
|
23
|
+
entries.push(
|
|
24
|
+
createZipPayload(
|
|
25
|
+
PACKAGE_RELATIONSHIPS_PATH,
|
|
26
|
+
"deflate",
|
|
27
|
+
encodeXml(buildRelationshipsXml(manifest.packageRelationships))
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
for (const part of contentParts) {
|
|
31
|
+
entries.push(createZipPayload(part.path, part.compression, part.bytes));
|
|
32
|
+
if (part.relationships.length > 0) {
|
|
33
|
+
entries.push(
|
|
34
|
+
createZipPayload(
|
|
35
|
+
getRelationshipsPartPath(part.path),
|
|
36
|
+
"deflate",
|
|
37
|
+
encodeXml(buildRelationshipsXml(part.relationships))
|
|
38
|
+
)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return entries.sort((left, right) => comparePartPaths(left.path, right.path));
|
|
43
|
+
}
|
|
44
|
+
function buildContentTypesXml(manifest, parts) {
|
|
45
|
+
const defaults = {
|
|
46
|
+
...manifest.contentTypes.defaults,
|
|
47
|
+
rels: manifest.contentTypes.defaults.rels ?? "application/vnd.openxmlformats-package.relationships+xml",
|
|
48
|
+
xml: manifest.contentTypes.defaults.xml ?? "application/xml"
|
|
49
|
+
};
|
|
50
|
+
const overrides = { ...manifest.contentTypes.overrides };
|
|
51
|
+
for (const part of parts) {
|
|
52
|
+
if (!part.contentType) {
|
|
53
|
+
throw new Error(`Cannot write OPC package: missing content type for ${part.path}.`);
|
|
54
|
+
}
|
|
55
|
+
const extension = getExtension(part.path);
|
|
56
|
+
if (!extension) {
|
|
57
|
+
overrides[part.path] = part.contentType;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const defaultContentType = defaults[extension];
|
|
61
|
+
if (!defaultContentType) {
|
|
62
|
+
defaults[extension] = part.contentType;
|
|
63
|
+
delete overrides[part.path];
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (defaultContentType === part.contentType) {
|
|
67
|
+
delete overrides[part.path];
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
overrides[part.path] = part.contentType;
|
|
71
|
+
}
|
|
72
|
+
const defaultEntries = Object.entries(defaults).sort(([left], [right]) => left.localeCompare(right));
|
|
73
|
+
const overrideEntries = Object.entries(overrides).sort(([left], [right]) => comparePartPaths(left, right));
|
|
74
|
+
const defaultXml = defaultEntries.map(([extension, contentType]) => ` <Default Extension="${escapeXml(extension)}" ContentType="${escapeXml(contentType)}"/>`).join("\n");
|
|
75
|
+
const overrideXml = overrideEntries.map(([partName, contentType]) => ` <Override PartName="${escapeXml(partName)}" ContentType="${escapeXml(contentType)}"/>`).join("\n");
|
|
76
|
+
const body = [defaultXml, overrideXml].filter((value) => value.length > 0).join("\n");
|
|
77
|
+
return [
|
|
78
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
79
|
+
`<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">`,
|
|
80
|
+
body,
|
|
81
|
+
`</Types>`
|
|
82
|
+
].join("\n");
|
|
83
|
+
}
|
|
84
|
+
function buildRelationshipsXml(relationships) {
|
|
85
|
+
const relationshipXml = [...relationships].sort((left, right) => left.id.localeCompare(right.id)).map((relationship) => {
|
|
86
|
+
const targetMode = relationship.targetMode === "external" ? ` TargetMode="External"` : "";
|
|
87
|
+
return ` <Relationship Id="${escapeXml(relationship.id)}" Type="${escapeXml(relationship.type)}" Target="${escapeXml(relationship.target)}"${targetMode}/>`;
|
|
88
|
+
}).join("\n");
|
|
89
|
+
return [
|
|
90
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
91
|
+
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">`,
|
|
92
|
+
relationshipXml,
|
|
93
|
+
`</Relationships>`
|
|
94
|
+
].join("\n");
|
|
95
|
+
}
|
|
96
|
+
function createZipPayload(path, compression, bytes) {
|
|
97
|
+
const normalizedPath = normalizePartPath(path).slice(1);
|
|
98
|
+
const uncompressedBytes = new Uint8Array(bytes);
|
|
99
|
+
const compressedBytes = uncompressedBytes;
|
|
100
|
+
const effectiveCompression = "store";
|
|
101
|
+
return {
|
|
102
|
+
path: normalizedPath,
|
|
103
|
+
compression: effectiveCompression,
|
|
104
|
+
uncompressedBytes,
|
|
105
|
+
compressedBytes,
|
|
106
|
+
crc32: calculateCrc32(uncompressedBytes)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function buildZipArchive(entries) {
|
|
110
|
+
const localChunks = [];
|
|
111
|
+
const centralChunks = [];
|
|
112
|
+
let localOffset = 0;
|
|
113
|
+
for (const entry of entries) {
|
|
114
|
+
const fileName = encodeUtf8(entry.path);
|
|
115
|
+
const localHeader = createByteBuffer(30 + fileName.length);
|
|
116
|
+
const localHeaderView = new DataView(localHeader.buffer);
|
|
117
|
+
localHeaderView.setUint32(0, 67324752, true);
|
|
118
|
+
localHeaderView.setUint16(4, 20, true);
|
|
119
|
+
localHeaderView.setUint16(6, 0, true);
|
|
120
|
+
localHeaderView.setUint16(8, entry.compression === "store" ? 0 : 8, true);
|
|
121
|
+
localHeaderView.setUint16(10, 0, true);
|
|
122
|
+
localHeaderView.setUint16(12, 0, true);
|
|
123
|
+
localHeaderView.setUint32(14, entry.crc32 >>> 0, true);
|
|
124
|
+
localHeaderView.setUint32(18, entry.compressedBytes.byteLength, true);
|
|
125
|
+
localHeaderView.setUint32(22, entry.uncompressedBytes.byteLength, true);
|
|
126
|
+
localHeaderView.setUint16(26, fileName.length, true);
|
|
127
|
+
localHeaderView.setUint16(28, 0, true);
|
|
128
|
+
localHeader.set(fileName, 30);
|
|
129
|
+
localChunks.push(localHeader, entry.compressedBytes);
|
|
130
|
+
const centralHeader = createByteBuffer(46 + fileName.length);
|
|
131
|
+
const centralHeaderView = new DataView(centralHeader.buffer);
|
|
132
|
+
centralHeaderView.setUint32(0, 33639248, true);
|
|
133
|
+
centralHeaderView.setUint16(4, 20, true);
|
|
134
|
+
centralHeaderView.setUint16(6, 20, true);
|
|
135
|
+
centralHeaderView.setUint16(8, 0, true);
|
|
136
|
+
centralHeaderView.setUint16(10, entry.compression === "store" ? 0 : 8, true);
|
|
137
|
+
centralHeaderView.setUint16(12, 0, true);
|
|
138
|
+
centralHeaderView.setUint16(14, 0, true);
|
|
139
|
+
centralHeaderView.setUint32(16, entry.crc32 >>> 0, true);
|
|
140
|
+
centralHeaderView.setUint32(20, entry.compressedBytes.byteLength, true);
|
|
141
|
+
centralHeaderView.setUint32(24, entry.uncompressedBytes.byteLength, true);
|
|
142
|
+
centralHeaderView.setUint16(28, fileName.length, true);
|
|
143
|
+
centralHeaderView.setUint16(30, 0, true);
|
|
144
|
+
centralHeaderView.setUint16(32, 0, true);
|
|
145
|
+
centralHeaderView.setUint16(34, 0, true);
|
|
146
|
+
centralHeaderView.setUint16(36, 0, true);
|
|
147
|
+
centralHeaderView.setUint32(38, 0, true);
|
|
148
|
+
centralHeaderView.setUint32(42, localOffset, true);
|
|
149
|
+
centralHeader.set(fileName, 46);
|
|
150
|
+
centralChunks.push(centralHeader);
|
|
151
|
+
localOffset += localHeader.byteLength + entry.compressedBytes.byteLength;
|
|
152
|
+
}
|
|
153
|
+
const centralDirectoryOffset = localOffset;
|
|
154
|
+
const centralDirectory = concatByteArrays(centralChunks);
|
|
155
|
+
const endOfCentralDirectory = createByteBuffer(22);
|
|
156
|
+
const endOfCentralDirectoryView = new DataView(endOfCentralDirectory.buffer);
|
|
157
|
+
endOfCentralDirectoryView.setUint32(0, 101010256, true);
|
|
158
|
+
endOfCentralDirectoryView.setUint16(4, 0, true);
|
|
159
|
+
endOfCentralDirectoryView.setUint16(6, 0, true);
|
|
160
|
+
endOfCentralDirectoryView.setUint16(8, entries.length, true);
|
|
161
|
+
endOfCentralDirectoryView.setUint16(10, entries.length, true);
|
|
162
|
+
endOfCentralDirectoryView.setUint32(12, centralDirectory.byteLength, true);
|
|
163
|
+
endOfCentralDirectoryView.setUint32(16, centralDirectoryOffset, true);
|
|
164
|
+
endOfCentralDirectoryView.setUint16(20, 0, true);
|
|
165
|
+
return concatByteArrays([...localChunks, centralDirectory, endOfCentralDirectory]);
|
|
166
|
+
}
|
|
167
|
+
function encodeXml(xml) {
|
|
168
|
+
return encodeUtf8(xml);
|
|
169
|
+
}
|
|
170
|
+
function escapeXml(value) {
|
|
171
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">").replace(/'/g, "'");
|
|
172
|
+
}
|
|
173
|
+
function calculateCrc32(bytes) {
|
|
174
|
+
let crc = 4294967295;
|
|
175
|
+
for (const byte of bytes) {
|
|
176
|
+
crc ^= byte;
|
|
177
|
+
for (let bit = 0; bit < 8; bit += 1) {
|
|
178
|
+
const mask = -(crc & 1);
|
|
179
|
+
crc = crc >>> 1 ^ 3988292384 & mask;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return (crc ^ 4294967295) >>> 0;
|
|
183
|
+
}
|
|
184
|
+
function createByteBuffer(size) {
|
|
185
|
+
return new Uint8Array(size);
|
|
186
|
+
}
|
|
187
|
+
function concatByteArrays(chunks) {
|
|
188
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
189
|
+
const combined = new Uint8Array(totalLength);
|
|
190
|
+
let offset = 0;
|
|
191
|
+
for (const chunk of chunks) {
|
|
192
|
+
combined.set(chunk, offset);
|
|
193
|
+
offset += chunk.byteLength;
|
|
194
|
+
}
|
|
195
|
+
return combined;
|
|
196
|
+
}
|
|
197
|
+
function encodeUtf8(value) {
|
|
198
|
+
return new TextEncoder().encode(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/io/opc/docx-package.ts
|
|
202
|
+
var DOCX_MIME_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
203
|
+
var DOCX_DOCUMENT_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
|
|
204
|
+
var DOCX_DOCUMENT_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
|
|
205
|
+
function writeDocxPackage(input) {
|
|
206
|
+
const documentPath = "/word/document.xml";
|
|
207
|
+
const contentType = input.documentContentType ?? DOCX_DOCUMENT_CONTENT_TYPE;
|
|
208
|
+
const relationshipType = input.packageRelationshipType ?? DOCX_DOCUMENT_RELATIONSHIP_TYPE;
|
|
209
|
+
const documentBytes = new TextEncoder().encode(input.documentXml);
|
|
210
|
+
const documentPart = {
|
|
211
|
+
path: documentPath,
|
|
212
|
+
surfaceKind: "content",
|
|
213
|
+
contentType,
|
|
214
|
+
relationshipsPartPath: getRelationshipsPartPath(documentPath),
|
|
215
|
+
relationships: [],
|
|
216
|
+
compression: "deflate",
|
|
217
|
+
bytes: documentBytes,
|
|
218
|
+
crc32: 0
|
|
219
|
+
};
|
|
220
|
+
const manifest = {
|
|
221
|
+
contentTypes: {
|
|
222
|
+
defaults: {
|
|
223
|
+
rels: "application/vnd.openxmlformats-package.relationships+xml",
|
|
224
|
+
xml: "application/xml"
|
|
225
|
+
},
|
|
226
|
+
overrides: {
|
|
227
|
+
[documentPath]: contentType
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
packageRelationships: [
|
|
231
|
+
{
|
|
232
|
+
id: "rId1",
|
|
233
|
+
type: relationshipType,
|
|
234
|
+
target: "word/document.xml",
|
|
235
|
+
targetMode: "internal"
|
|
236
|
+
}
|
|
237
|
+
],
|
|
238
|
+
parts: [
|
|
239
|
+
{
|
|
240
|
+
path: documentPart.path,
|
|
241
|
+
surfaceKind: documentPart.surfaceKind,
|
|
242
|
+
contentType: documentPart.contentType,
|
|
243
|
+
relationshipsPartPath: documentPart.relationshipsPartPath,
|
|
244
|
+
relationships: documentPart.relationships,
|
|
245
|
+
compression: documentPart.compression,
|
|
246
|
+
byteLength: documentPart.bytes.byteLength
|
|
247
|
+
}
|
|
248
|
+
]
|
|
249
|
+
};
|
|
250
|
+
return writeOpcPackage({
|
|
251
|
+
manifest,
|
|
252
|
+
parts: /* @__PURE__ */ new Map([[documentPart.path, documentPart]])
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/io/opc/package-reader.ts
|
|
257
|
+
import { inflateSync } from "fflate";
|
|
258
|
+
var EOCD_SIGNATURE = 101010256;
|
|
259
|
+
var CENTRAL_DIRECTORY_SIGNATURE = 33639248;
|
|
260
|
+
var LOCAL_FILE_HEADER_SIGNATURE = 67324752;
|
|
261
|
+
function readOpcPackage(source) {
|
|
262
|
+
const bytes = source instanceof Uint8Array ? source : new Uint8Array(source);
|
|
263
|
+
const centralDirectory = readCentralDirectory(bytes);
|
|
264
|
+
const parts = /* @__PURE__ */ new Map();
|
|
265
|
+
for (const entry of centralDirectory) {
|
|
266
|
+
if (entry.path.endsWith("/")) {
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const storedBytes = readZipEntry(bytes, entry);
|
|
270
|
+
const normalizedPath = normalizePartPath(entry.path);
|
|
271
|
+
const surfaceKind = getSurfaceKind(normalizedPath);
|
|
272
|
+
parts.set(normalizedPath, {
|
|
273
|
+
path: normalizedPath,
|
|
274
|
+
surfaceKind,
|
|
275
|
+
contentType: null,
|
|
276
|
+
relationshipsPartPath: surfaceKind === "content" ? getRelationshipsPartPath(normalizedPath) : null,
|
|
277
|
+
relationships: [],
|
|
278
|
+
compression: entry.compression,
|
|
279
|
+
bytes: storedBytes,
|
|
280
|
+
crc32: entry.crc32 >>> 0
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const contentTypesPart = parts.get(CONTENT_TYPES_PATH);
|
|
284
|
+
if (!contentTypesPart) {
|
|
285
|
+
throw new Error("Invalid OPC package: missing /[Content_Types].xml.");
|
|
286
|
+
}
|
|
287
|
+
const contentTypes = parseContentTypesXml(decodeXml(contentTypesPart.bytes));
|
|
288
|
+
const packageRelationshipsPart = parts.get(PACKAGE_RELATIONSHIPS_PATH);
|
|
289
|
+
const packageRelationships = packageRelationshipsPart ? parseRelationshipsXml(decodeXml(packageRelationshipsPart.bytes)) : [];
|
|
290
|
+
const manifestParts = [];
|
|
291
|
+
for (const [path, part] of [...parts.entries()].sort(([left], [right]) => left.localeCompare(right))) {
|
|
292
|
+
const contentType = resolveContentType(path, contentTypes);
|
|
293
|
+
const relationshipsPartPath = part.surfaceKind === "content" ? getRelationshipsPartPath(path) : null;
|
|
294
|
+
const relationshipsPart = relationshipsPartPath ? parts.get(relationshipsPartPath) : void 0;
|
|
295
|
+
const relationships = relationshipsPart ? parseRelationshipsXml(decodeXml(relationshipsPart.bytes)) : [];
|
|
296
|
+
const nextPart = {
|
|
297
|
+
...part,
|
|
298
|
+
contentType,
|
|
299
|
+
relationshipsPartPath,
|
|
300
|
+
relationships
|
|
301
|
+
};
|
|
302
|
+
parts.set(path, nextPart);
|
|
303
|
+
manifestParts.push({
|
|
304
|
+
path,
|
|
305
|
+
surfaceKind: nextPart.surfaceKind,
|
|
306
|
+
contentType,
|
|
307
|
+
relationshipsPartPath,
|
|
308
|
+
relationships,
|
|
309
|
+
compression: nextPart.compression,
|
|
310
|
+
byteLength: nextPart.bytes.byteLength
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
manifest: {
|
|
315
|
+
contentTypes,
|
|
316
|
+
packageRelationships,
|
|
317
|
+
parts: manifestParts
|
|
318
|
+
},
|
|
319
|
+
parts,
|
|
320
|
+
sourceByteLength: bytes.byteLength
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function readCentralDirectory(archive) {
|
|
324
|
+
const view = new DataView(archive.buffer, archive.byteOffset, archive.byteLength);
|
|
325
|
+
const eocdOffset = findEndOfCentralDirectory(archive, view);
|
|
326
|
+
const entryCount = view.getUint16(eocdOffset + 10, true);
|
|
327
|
+
const centralDirectoryOffset = view.getUint32(eocdOffset + 16, true);
|
|
328
|
+
const entries = [];
|
|
329
|
+
let offset = centralDirectoryOffset;
|
|
330
|
+
for (let index = 0; index < entryCount; index += 1) {
|
|
331
|
+
const signature = view.getUint32(offset, true);
|
|
332
|
+
if (signature !== CENTRAL_DIRECTORY_SIGNATURE) {
|
|
333
|
+
throw new Error(`Invalid ZIP central directory signature at offset ${offset}.`);
|
|
334
|
+
}
|
|
335
|
+
const generalPurposeBitFlag = view.getUint16(offset + 8, true);
|
|
336
|
+
const compressionMethod = view.getUint16(offset + 10, true);
|
|
337
|
+
const crc32 = view.getUint32(offset + 16, true);
|
|
338
|
+
const compressedSize = view.getUint32(offset + 20, true);
|
|
339
|
+
const uncompressedSize = view.getUint32(offset + 24, true);
|
|
340
|
+
const fileNameLength = view.getUint16(offset + 28, true);
|
|
341
|
+
const extraFieldLength = view.getUint16(offset + 30, true);
|
|
342
|
+
const fileCommentLength = view.getUint16(offset + 32, true);
|
|
343
|
+
const localHeaderOffset = view.getUint32(offset + 42, true);
|
|
344
|
+
const fileNameOffset = offset + 46;
|
|
345
|
+
const fileName = decodeUtf8(archive.subarray(fileNameOffset, fileNameOffset + fileNameLength));
|
|
346
|
+
entries.push({
|
|
347
|
+
path: fileName,
|
|
348
|
+
compression: mapCompressionMethod(compressionMethod),
|
|
349
|
+
crc32,
|
|
350
|
+
compressedSize,
|
|
351
|
+
uncompressedSize,
|
|
352
|
+
localHeaderOffset,
|
|
353
|
+
generalPurposeBitFlag
|
|
354
|
+
});
|
|
355
|
+
offset += 46 + fileNameLength + extraFieldLength + fileCommentLength;
|
|
356
|
+
}
|
|
357
|
+
return entries;
|
|
358
|
+
}
|
|
359
|
+
function findEndOfCentralDirectory(archive, view) {
|
|
360
|
+
const minimumEocdSize = 22;
|
|
361
|
+
const maximumCommentSize = 65535;
|
|
362
|
+
const searchStart = Math.max(0, archive.length - minimumEocdSize - maximumCommentSize);
|
|
363
|
+
for (let offset = archive.length - minimumEocdSize; offset >= searchStart; offset -= 1) {
|
|
364
|
+
if (view.getUint32(offset, true) === EOCD_SIGNATURE) {
|
|
365
|
+
return offset;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
throw new Error("Invalid ZIP archive: end of central directory not found.");
|
|
369
|
+
}
|
|
370
|
+
function readZipEntry(archive, entry) {
|
|
371
|
+
const view = new DataView(archive.buffer, archive.byteOffset, archive.byteLength);
|
|
372
|
+
const headerOffset = entry.localHeaderOffset;
|
|
373
|
+
const signature = view.getUint32(headerOffset, true);
|
|
374
|
+
if (signature !== LOCAL_FILE_HEADER_SIGNATURE) {
|
|
375
|
+
throw new Error(`Invalid ZIP local file header signature at offset ${headerOffset}.`);
|
|
376
|
+
}
|
|
377
|
+
const fileNameLength = view.getUint16(headerOffset + 26, true);
|
|
378
|
+
const extraFieldLength = view.getUint16(headerOffset + 28, true);
|
|
379
|
+
const dataOffset = headerOffset + 30 + fileNameLength + extraFieldLength;
|
|
380
|
+
const compressed = archive.subarray(dataOffset, dataOffset + entry.compressedSize);
|
|
381
|
+
switch (entry.compression) {
|
|
382
|
+
case "store":
|
|
383
|
+
return new Uint8Array(compressed);
|
|
384
|
+
case "deflate":
|
|
385
|
+
return inflateSync(compressed);
|
|
386
|
+
default:
|
|
387
|
+
throw new Error(`Unsupported ZIP compression for ${entry.path}.`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
function mapCompressionMethod(method) {
|
|
391
|
+
switch (method) {
|
|
392
|
+
case 0:
|
|
393
|
+
return "store";
|
|
394
|
+
case 8:
|
|
395
|
+
return "deflate";
|
|
396
|
+
default:
|
|
397
|
+
throw new Error(`Unsupported ZIP compression method ${method}.`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function getSurfaceKind(path) {
|
|
401
|
+
if (path === CONTENT_TYPES_PATH) {
|
|
402
|
+
return "content-types";
|
|
403
|
+
}
|
|
404
|
+
if (path === PACKAGE_RELATIONSHIPS_PATH || path.includes("/_rels/")) {
|
|
405
|
+
return "relationships";
|
|
406
|
+
}
|
|
407
|
+
return "content";
|
|
408
|
+
}
|
|
409
|
+
function decodeXml(bytes) {
|
|
410
|
+
return new TextDecoder("utf-8").decode(bytes);
|
|
411
|
+
}
|
|
412
|
+
function decodeUtf8(bytes) {
|
|
413
|
+
return new TextDecoder("utf-8").decode(bytes);
|
|
414
|
+
}
|
|
415
|
+
function parseContentTypesXml(xml) {
|
|
416
|
+
const defaults = {};
|
|
417
|
+
const overrides = {};
|
|
418
|
+
for (const attributes of findTagAttributes(xml, "Default")) {
|
|
419
|
+
const extension = attributes.Extension?.toLowerCase();
|
|
420
|
+
const contentType = attributes.ContentType;
|
|
421
|
+
if (!extension || !contentType) {
|
|
422
|
+
continue;
|
|
423
|
+
}
|
|
424
|
+
defaults[extension] = contentType;
|
|
425
|
+
}
|
|
426
|
+
for (const attributes of findTagAttributes(xml, "Override")) {
|
|
427
|
+
const partName = attributes.PartName;
|
|
428
|
+
const contentType = attributes.ContentType;
|
|
429
|
+
if (!partName || !contentType) {
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
overrides[normalizePartPath(partName)] = contentType;
|
|
433
|
+
}
|
|
434
|
+
return { defaults, overrides };
|
|
435
|
+
}
|
|
436
|
+
function parseRelationshipsXml(xml) {
|
|
437
|
+
const relationships = [];
|
|
438
|
+
for (const attributes of findTagAttributes(xml, "Relationship")) {
|
|
439
|
+
const id = attributes.Id;
|
|
440
|
+
const type = attributes.Type;
|
|
441
|
+
const target = attributes.Target;
|
|
442
|
+
if (!id || !type || !target) {
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
relationships.push({
|
|
446
|
+
id,
|
|
447
|
+
type,
|
|
448
|
+
target,
|
|
449
|
+
targetMode: attributes.TargetMode === "External" ? "external" : "internal"
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
return relationships;
|
|
453
|
+
}
|
|
454
|
+
function findTagAttributes(xml, tagName) {
|
|
455
|
+
const tagPattern = new RegExp(`<(?:[A-Za-z_][\\w.-]*:)?${tagName}\\b([^>]*)\\/?>`, "g");
|
|
456
|
+
const results = [];
|
|
457
|
+
let tagMatch = tagPattern.exec(xml);
|
|
458
|
+
while (tagMatch) {
|
|
459
|
+
results.push(parseAttributes(tagMatch[1] ?? ""));
|
|
460
|
+
tagMatch = tagPattern.exec(xml);
|
|
461
|
+
}
|
|
462
|
+
return results;
|
|
463
|
+
}
|
|
464
|
+
function parseAttributes(rawAttributes) {
|
|
465
|
+
const attributes = {};
|
|
466
|
+
const attributePattern = /([A-Za-z_][\w:.-]*)\s*=\s*(["'])([\s\S]*?)\2/g;
|
|
467
|
+
let match = attributePattern.exec(rawAttributes);
|
|
468
|
+
while (match) {
|
|
469
|
+
attributes[match[1]] = decodeXmlEntities(match[3]);
|
|
470
|
+
match = attributePattern.exec(rawAttributes);
|
|
471
|
+
}
|
|
472
|
+
return attributes;
|
|
473
|
+
}
|
|
474
|
+
function decodeXmlEntities(value) {
|
|
475
|
+
return value.replace(/"/g, '"').replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
476
|
+
}
|
|
477
|
+
function resolveContentType(path, contentTypes) {
|
|
478
|
+
if (path === CONTENT_TYPES_PATH) {
|
|
479
|
+
return "application/xml";
|
|
480
|
+
}
|
|
481
|
+
const override = contentTypes.overrides[path];
|
|
482
|
+
if (override) {
|
|
483
|
+
return override;
|
|
484
|
+
}
|
|
485
|
+
const extension = path.includes(".") ? path.slice(path.lastIndexOf(".") + 1).toLowerCase() : "";
|
|
486
|
+
return extension ? contentTypes.defaults[extension] ?? null : null;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// src/io/export/reattach-preserved-parts.ts
|
|
490
|
+
function reattachPreservedParts(sourcePackage, workingParts, packageRelationships, ownedPaths) {
|
|
491
|
+
for (const [path, sourcePart] of sourcePackage.parts.entries()) {
|
|
492
|
+
if (ownedPaths.has(path) || sourcePart.surfaceKind !== "content") {
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (!workingParts.has(path)) {
|
|
496
|
+
workingParts.set(path, clonePart(sourcePart));
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
const currentPart = workingParts.get(path);
|
|
500
|
+
if (!currentPart) {
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
currentPart.contentType = sourcePart.contentType;
|
|
504
|
+
currentPart.relationships = sourcePart.relationships.map(cloneRelationship);
|
|
505
|
+
currentPart.relationshipsPartPath = sourcePart.relationshipsPartPath;
|
|
506
|
+
currentPart.compression = sourcePart.compression;
|
|
507
|
+
currentPart.bytes = new Uint8Array(sourcePart.bytes);
|
|
508
|
+
currentPart.crc32 = sourcePart.crc32;
|
|
509
|
+
}
|
|
510
|
+
const knownPackageRelationshipIds = new Set(packageRelationships.map((relationship) => relationship.id));
|
|
511
|
+
for (const relationship of sourcePackage.manifest.packageRelationships) {
|
|
512
|
+
if (knownPackageRelationshipIds.has(relationship.id)) {
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
packageRelationships.push(cloneRelationship(relationship));
|
|
516
|
+
knownPackageRelationshipIds.add(relationship.id);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
function clonePart(part) {
|
|
520
|
+
return {
|
|
521
|
+
...part,
|
|
522
|
+
relationships: part.relationships.map(cloneRelationship),
|
|
523
|
+
bytes: new Uint8Array(part.bytes)
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
function cloneRelationship(relationship) {
|
|
527
|
+
return { ...relationship };
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// src/io/export/export-session.ts
|
|
531
|
+
var ExportSession = class {
|
|
532
|
+
constructor(sourcePackage, ownedOutputPaths) {
|
|
533
|
+
this.sourcePackage = sourcePackage;
|
|
534
|
+
this.ownedPaths = new Set(
|
|
535
|
+
[...ownedOutputPaths].map((path) => {
|
|
536
|
+
const normalized = normalizePartPath(path);
|
|
537
|
+
if (normalized === CONTENT_TYPES_PATH || normalized.endsWith(".rels") || normalized === PACKAGE_RELATIONSHIPS_PATH) {
|
|
538
|
+
throw new Error(`ExportSession cannot directly own reserved OPC manifest path ${normalized}.`);
|
|
539
|
+
}
|
|
540
|
+
return normalized;
|
|
541
|
+
})
|
|
542
|
+
);
|
|
543
|
+
this.workingParts = cloneParts(sourcePackage.parts);
|
|
544
|
+
this.packageRelationships = sourcePackage.manifest.packageRelationships.map(cloneRelationship2);
|
|
545
|
+
}
|
|
546
|
+
ownedPaths;
|
|
547
|
+
workingParts;
|
|
548
|
+
packageRelationships;
|
|
549
|
+
replaceOwnedPart(replacement) {
|
|
550
|
+
const normalizedPath = normalizePartPath(replacement.path);
|
|
551
|
+
if (!this.ownedPaths.has(normalizedPath)) {
|
|
552
|
+
throw new Error(`Attempted to rewrite non-owned OPC path ${normalizedPath}.`);
|
|
553
|
+
}
|
|
554
|
+
const existing = this.workingParts.get(normalizedPath);
|
|
555
|
+
const nextPart = {
|
|
556
|
+
path: normalizedPath,
|
|
557
|
+
surfaceKind: "content",
|
|
558
|
+
contentType: replacement.contentType,
|
|
559
|
+
relationshipsPartPath: getRelationshipsPartPath(normalizedPath),
|
|
560
|
+
relationships: (replacement.relationships ?? existing?.relationships ?? []).map(cloneRelationship2),
|
|
561
|
+
compression: replacement.compression ?? existing?.compression ?? "deflate",
|
|
562
|
+
bytes: new Uint8Array(replacement.bytes),
|
|
563
|
+
crc32: 0
|
|
564
|
+
};
|
|
565
|
+
this.workingParts.set(normalizedPath, nextPart);
|
|
566
|
+
}
|
|
567
|
+
withOwnedPart(replacement) {
|
|
568
|
+
this.replaceOwnedPart(replacement);
|
|
569
|
+
return this;
|
|
570
|
+
}
|
|
571
|
+
ensurePackageRelationship(input) {
|
|
572
|
+
const targetMode = input.targetMode ?? "internal";
|
|
573
|
+
const normalizedTarget = targetMode === "internal" ? normalizePartPath(input.target) : input.target;
|
|
574
|
+
const existing = this.packageRelationships.find((relationship) => {
|
|
575
|
+
if (relationship.type !== input.type || relationship.targetMode !== targetMode) {
|
|
576
|
+
return false;
|
|
577
|
+
}
|
|
578
|
+
return targetMode === "internal" ? resolveRelationshipTarget(null, relationship) === normalizedTarget : relationship.target === normalizedTarget;
|
|
579
|
+
});
|
|
580
|
+
if (existing) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const relationshipId = createUniqueRelationshipId(
|
|
584
|
+
new Set(this.packageRelationships.map((relationship) => relationship.id)),
|
|
585
|
+
input.preferredId ?? "rIdPackage1"
|
|
586
|
+
);
|
|
587
|
+
this.packageRelationships.push({
|
|
588
|
+
id: relationshipId,
|
|
589
|
+
type: input.type,
|
|
590
|
+
target: targetMode === "internal" ? toPackageRelationshipTarget(normalizedTarget) : normalizedTarget,
|
|
591
|
+
targetMode
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
toPackage() {
|
|
595
|
+
reattachPreservedParts(
|
|
596
|
+
this.sourcePackage,
|
|
597
|
+
this.workingParts,
|
|
598
|
+
this.packageRelationships,
|
|
599
|
+
this.ownedPaths
|
|
600
|
+
);
|
|
601
|
+
const parts = pruneReservedManifestParts(this.workingParts);
|
|
602
|
+
const manifest = buildManifest(parts, this.packageRelationships, this.sourcePackage.manifest.contentTypes);
|
|
603
|
+
return {
|
|
604
|
+
manifest,
|
|
605
|
+
parts,
|
|
606
|
+
sourceByteLength: this.sourcePackage.sourceByteLength
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
serialize() {
|
|
610
|
+
return writeOpcPackage(this.toPackage());
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
function createExportSession(sourcePackage, ownedOutputPaths) {
|
|
614
|
+
return new ExportSession(sourcePackage, ownedOutputPaths);
|
|
615
|
+
}
|
|
616
|
+
function cloneParts(parts) {
|
|
617
|
+
return new Map(
|
|
618
|
+
[...parts.entries()].map(([path, part]) => [
|
|
619
|
+
path,
|
|
620
|
+
{
|
|
621
|
+
...part,
|
|
622
|
+
relationships: part.relationships.map(cloneRelationship2),
|
|
623
|
+
bytes: new Uint8Array(part.bytes)
|
|
624
|
+
}
|
|
625
|
+
])
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
function pruneReservedManifestParts(parts) {
|
|
629
|
+
return new Map(
|
|
630
|
+
[...parts.entries()].filter(([path]) => path !== CONTENT_TYPES_PATH && path !== PACKAGE_RELATIONSHIPS_PATH && !path.includes("/_rels/"))
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
function buildManifest(parts, packageRelationships, contentTypesSeed) {
|
|
634
|
+
const partsList = [...parts.values()].sort((left, right) => comparePartPaths(left.path, right.path));
|
|
635
|
+
const overrides = { ...contentTypesSeed.overrides };
|
|
636
|
+
for (const key of Object.keys(overrides)) {
|
|
637
|
+
if (!parts.has(normalizePartPath(key))) {
|
|
638
|
+
delete overrides[key];
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
for (const part of partsList) {
|
|
642
|
+
if (!part.contentType) {
|
|
643
|
+
throw new Error(`Cannot export part without content type: ${part.path}`);
|
|
644
|
+
}
|
|
645
|
+
overrides[part.path] = part.contentType;
|
|
646
|
+
}
|
|
647
|
+
return {
|
|
648
|
+
contentTypes: {
|
|
649
|
+
defaults: { ...contentTypesSeed.defaults },
|
|
650
|
+
overrides
|
|
651
|
+
},
|
|
652
|
+
packageRelationships: packageRelationships.map(cloneRelationship2),
|
|
653
|
+
parts: partsList.map((part) => ({
|
|
654
|
+
path: part.path,
|
|
655
|
+
surfaceKind: part.surfaceKind,
|
|
656
|
+
contentType: part.contentType,
|
|
657
|
+
relationshipsPartPath: part.relationshipsPartPath,
|
|
658
|
+
relationships: part.relationships.map(cloneRelationship2),
|
|
659
|
+
compression: part.compression,
|
|
660
|
+
byteLength: part.bytes.byteLength
|
|
661
|
+
}))
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
function cloneRelationship2(relationship) {
|
|
665
|
+
return { ...relationship };
|
|
666
|
+
}
|
|
667
|
+
function createUniqueRelationshipId(existingIds, preferredId) {
|
|
668
|
+
if (!existingIds.has(preferredId)) {
|
|
669
|
+
return preferredId;
|
|
670
|
+
}
|
|
671
|
+
let nextIndex = 2;
|
|
672
|
+
while (existingIds.has(`${preferredId}-${nextIndex}`)) {
|
|
673
|
+
nextIndex += 1;
|
|
674
|
+
}
|
|
675
|
+
return `${preferredId}-${nextIndex}`;
|
|
676
|
+
}
|
|
677
|
+
function toPackageRelationshipTarget(partPath) {
|
|
678
|
+
return normalizePartPath(partPath).slice(1);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// src/io/ooxml/numbering-sentinels.ts
|
|
682
|
+
var DOCX_NULL_NUMBERING_INSTANCE_ID = "num:0";
|
|
683
|
+
var DOCX_NULL_ABSTRACT_NUMBERING_ID = "abstract-num:__docx-import-null__";
|
|
684
|
+
function createSyntheticDocxNullNumberingCatalog() {
|
|
685
|
+
return {
|
|
686
|
+
abstractDefinitions: {
|
|
687
|
+
[DOCX_NULL_ABSTRACT_NUMBERING_ID]: {
|
|
688
|
+
abstractNumberingId: DOCX_NULL_ABSTRACT_NUMBERING_ID,
|
|
689
|
+
levels: Array.from({ length: 9 }, (_unused, level) => ({
|
|
690
|
+
level,
|
|
691
|
+
format: "none",
|
|
692
|
+
text: ""
|
|
693
|
+
}))
|
|
694
|
+
}
|
|
695
|
+
},
|
|
696
|
+
instances: {
|
|
697
|
+
[DOCX_NULL_NUMBERING_INSTANCE_ID]: {
|
|
698
|
+
numberingInstanceId: DOCX_NULL_NUMBERING_INSTANCE_ID,
|
|
699
|
+
abstractNumberingId: DOCX_NULL_ABSTRACT_NUMBERING_ID,
|
|
700
|
+
overrides: []
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
function isSyntheticDocxNullAbstractDefinition(definition) {
|
|
706
|
+
return definition.abstractNumberingId === DOCX_NULL_ABSTRACT_NUMBERING_ID;
|
|
707
|
+
}
|
|
708
|
+
function isSyntheticDocxNullNumberingInstance(instance) {
|
|
709
|
+
return instance.numberingInstanceId === DOCX_NULL_NUMBERING_INSTANCE_ID && instance.abstractNumberingId === DOCX_NULL_ABSTRACT_NUMBERING_ID;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// src/io/export/serialize-numbering.ts
|
|
713
|
+
var WORD_NUMBERING_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml";
|
|
714
|
+
function serializeNumberingXml(catalog) {
|
|
715
|
+
const abstractDefinitions = Object.values(catalog.abstractDefinitions).filter((definition) => !isSyntheticDocxNullAbstractDefinition(definition)).sort(
|
|
716
|
+
(left, right) => compareSerializedIds(left.abstractNumberingId, right.abstractNumberingId)
|
|
717
|
+
);
|
|
718
|
+
const instances = Object.values(catalog.instances).filter((instance) => !isSyntheticDocxNullNumberingInstance(instance)).sort(
|
|
719
|
+
(left, right) => compareSerializedIds(left.numberingInstanceId, right.numberingInstanceId)
|
|
720
|
+
);
|
|
721
|
+
const body = [
|
|
722
|
+
...abstractDefinitions.map((definition) => serializeAbstractDefinition(definition)),
|
|
723
|
+
...instances.map((instance) => serializeInstance(instance))
|
|
724
|
+
].join("");
|
|
725
|
+
return [
|
|
726
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
727
|
+
`<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">${body}</w:numbering>`
|
|
728
|
+
].join("\n");
|
|
729
|
+
}
|
|
730
|
+
function hasSerializableNumberingEntries(catalog) {
|
|
731
|
+
return Object.values(catalog.abstractDefinitions).some(
|
|
732
|
+
(definition) => !isSyntheticDocxNullAbstractDefinition(definition)
|
|
733
|
+
) || Object.values(catalog.instances).some(
|
|
734
|
+
(instance) => !isSyntheticDocxNullNumberingInstance(instance)
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
function serializeParagraphNumberingProperties(numbering) {
|
|
738
|
+
if (!numbering) {
|
|
739
|
+
return "";
|
|
740
|
+
}
|
|
741
|
+
return `<w:numPr><w:ilvl w:val="${numbering.level}"/><w:numId w:val="${escapeAttribute(
|
|
742
|
+
stripCanonicalPrefix(numbering.numberingInstanceId, "num:")
|
|
743
|
+
)}"/></w:numPr>`;
|
|
744
|
+
}
|
|
745
|
+
function serializeAbstractDefinition(definition) {
|
|
746
|
+
const abstractNumId = escapeAttribute(
|
|
747
|
+
stripCanonicalPrefix(definition.abstractNumberingId, "abstract-num:")
|
|
748
|
+
);
|
|
749
|
+
const levels = [...definition.levels].sort((left, right) => left.level - right.level).map((level) => serializeLevel(level)).join("");
|
|
750
|
+
return `<w:abstractNum w:abstractNumId="${abstractNumId}">${levels}</w:abstractNum>`;
|
|
751
|
+
}
|
|
752
|
+
function serializeLevel(level) {
|
|
753
|
+
const start = level.startAt !== void 0 ? `<w:start w:val="${level.startAt}"/>` : "";
|
|
754
|
+
const paragraphStyle = level.paragraphStyleId ? `<w:pStyle w:val="${escapeAttribute(level.paragraphStyleId)}"/>` : "";
|
|
755
|
+
const isLegal = level.isLegalNumbering ? "<w:isLgl/>" : "";
|
|
756
|
+
const suffix = level.suffix ? `<w:suff w:val="${escapeAttribute(level.suffix)}"/>` : "";
|
|
757
|
+
return `<w:lvl w:ilvl="${level.level}">${start}<w:numFmt w:val="${escapeAttribute(
|
|
758
|
+
level.format
|
|
759
|
+
)}"/><w:lvlText w:val="${escapeAttribute(level.text)}"/>${paragraphStyle}${isLegal}${suffix}</w:lvl>`;
|
|
760
|
+
}
|
|
761
|
+
function serializeInstance(instance) {
|
|
762
|
+
const numId = escapeAttribute(stripCanonicalPrefix(instance.numberingInstanceId, "num:"));
|
|
763
|
+
const abstractNumId = escapeAttribute(
|
|
764
|
+
stripCanonicalPrefix(instance.abstractNumberingId, "abstract-num:")
|
|
765
|
+
);
|
|
766
|
+
const overrides = [...instance.overrides].sort((left, right) => left.level - right.level).map((override) => serializeOverride(override)).join("");
|
|
767
|
+
return `<w:num w:numId="${numId}"><w:abstractNumId w:val="${abstractNumId}"/>${overrides}</w:num>`;
|
|
768
|
+
}
|
|
769
|
+
function serializeOverride(override) {
|
|
770
|
+
const startOverride = override.startAt !== void 0 ? `<w:startOverride w:val="${override.startAt}"/>` : "";
|
|
771
|
+
return `<w:lvlOverride w:ilvl="${override.level}">${startOverride}</w:lvlOverride>`;
|
|
772
|
+
}
|
|
773
|
+
function compareSerializedIds(left, right) {
|
|
774
|
+
return stripKnownPrefix(left).localeCompare(stripKnownPrefix(right), "en", { numeric: true });
|
|
775
|
+
}
|
|
776
|
+
function stripKnownPrefix(value) {
|
|
777
|
+
return value.replace(/^abstract-num:|^num:/, "");
|
|
778
|
+
}
|
|
779
|
+
function stripCanonicalPrefix(value, prefix) {
|
|
780
|
+
return value.startsWith(prefix) ? value.slice(prefix.length) : value;
|
|
781
|
+
}
|
|
782
|
+
function escapeAttribute(value) {
|
|
783
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
// src/preservation/relationship-retention.ts
|
|
787
|
+
function retainRelationshipsForFragment(fragment, relationships, existingRelationshipMap, retainedRelationshipIds) {
|
|
788
|
+
if (!fragment || fragment.payloadKind !== "xml-subtree") {
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
for (const relationshipId of extractReferencedRelationshipIds(fragment.payloadReference)) {
|
|
792
|
+
if (retainedRelationshipIds.has(relationshipId)) {
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
const relationship = existingRelationshipMap.get(relationshipId);
|
|
796
|
+
if (!relationship) {
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
relationships.push({ ...relationship });
|
|
800
|
+
retainedRelationshipIds.add(relationshipId);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
function extractReferencedRelationshipIds(xml) {
|
|
804
|
+
const relationshipIds = /* @__PURE__ */ new Set();
|
|
805
|
+
const relationshipPattern = /\br:id=(["'])([^"']+)\1/gu;
|
|
806
|
+
for (const match of xml.matchAll(relationshipPattern)) {
|
|
807
|
+
const relationshipId = match[2];
|
|
808
|
+
if (relationshipId) {
|
|
809
|
+
relationshipIds.add(relationshipId);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
return relationshipIds;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// src/io/export/serialize-main-document.ts
|
|
816
|
+
var HYPERLINK_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
|
|
817
|
+
function serializeMainDocument(content, preservation = { opaqueFragments: {}, packageParts: {} }, existingRelationships = [], options = {}) {
|
|
818
|
+
const nextRelationshipIndex = existingRelationships.reduce((maxIndex, relationship) => {
|
|
819
|
+
const match = /^rIdHyperlink(\d+)$/.exec(relationship.id);
|
|
820
|
+
return match ? Math.max(maxIndex, Number.parseInt(match[1] ?? "0", 10)) : maxIndex;
|
|
821
|
+
}, 0) + 1;
|
|
822
|
+
const state = {
|
|
823
|
+
nextHyperlinkRelationshipIndex: nextRelationshipIndex,
|
|
824
|
+
relationships: existingRelationships.filter(
|
|
825
|
+
(relationship) => relationship.type !== HYPERLINK_RELATIONSHIP_TYPE
|
|
826
|
+
).map(cloneRelationship3),
|
|
827
|
+
existingRelationshipMap: new Map(
|
|
828
|
+
existingRelationships.map((relationship) => [relationship.id, cloneRelationship3(relationship)])
|
|
829
|
+
),
|
|
830
|
+
retainedRelationshipIds: new Set(
|
|
831
|
+
existingRelationships.filter((relationship) => relationship.type !== HYPERLINK_RELATIONSHIP_TYPE).map((relationship) => relationship.id)
|
|
832
|
+
),
|
|
833
|
+
media: options.media ?? { items: {} },
|
|
834
|
+
preservation
|
|
835
|
+
};
|
|
836
|
+
const documentOpen = `<w:document${serializeDocumentAttributes(options.documentAttributes, content)}>`;
|
|
837
|
+
const prefix = [
|
|
838
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
839
|
+
documentOpen,
|
|
840
|
+
` <w:body>`
|
|
841
|
+
].join("\n");
|
|
842
|
+
const suffix = `</w:body>
|
|
843
|
+
</w:document>`;
|
|
844
|
+
const bodyPieces = [];
|
|
845
|
+
const paragraphBoundaries = [];
|
|
846
|
+
let bodyLength = 0;
|
|
847
|
+
let sectionPropertiesXml = options.finalSectionProperties ? serializeSectionPropertiesXml(options.finalSectionProperties) : "<w:sectPr/>";
|
|
848
|
+
let cursor = 0;
|
|
849
|
+
let paragraphIndex = -1;
|
|
850
|
+
let previousWasParagraph = false;
|
|
851
|
+
for (const block of content.children) {
|
|
852
|
+
if (block.type === "paragraph") {
|
|
853
|
+
if (previousWasParagraph) {
|
|
854
|
+
cursor += 1;
|
|
855
|
+
}
|
|
856
|
+
paragraphIndex += 1;
|
|
857
|
+
const serializedParagraph = serializeParagraph(
|
|
858
|
+
block,
|
|
859
|
+
state,
|
|
860
|
+
cursor,
|
|
861
|
+
paragraphIndex
|
|
862
|
+
);
|
|
863
|
+
const paragraphOffset = prefix.length + bodyLength;
|
|
864
|
+
bodyPieces.push(serializedParagraph.xml);
|
|
865
|
+
bodyLength += serializedParagraph.xml.length;
|
|
866
|
+
paragraphBoundaries.push(
|
|
867
|
+
offsetParagraphBoundary(serializedParagraph.boundary, paragraphOffset)
|
|
868
|
+
);
|
|
869
|
+
cursor = serializedParagraph.nextCursor;
|
|
870
|
+
previousWasParagraph = true;
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
if (block.type === "table") {
|
|
874
|
+
const tableXml = serializeTableNode(block, state);
|
|
875
|
+
bodyPieces.push(tableXml);
|
|
876
|
+
bodyLength += tableXml.length;
|
|
877
|
+
cursor += 1;
|
|
878
|
+
previousWasParagraph = false;
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
if (block.type === "sdt") {
|
|
882
|
+
const sdtXml = serializeSdtNode(block, state);
|
|
883
|
+
bodyPieces.push(sdtXml);
|
|
884
|
+
bodyLength += sdtXml.length;
|
|
885
|
+
previousWasParagraph = false;
|
|
886
|
+
continue;
|
|
887
|
+
}
|
|
888
|
+
if (block.type === "custom_xml") {
|
|
889
|
+
const customXml = serializeCustomXmlNode(block, state);
|
|
890
|
+
bodyPieces.push(customXml);
|
|
891
|
+
bodyLength += customXml.length;
|
|
892
|
+
previousWasParagraph = false;
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (block.type === "alt_chunk") {
|
|
896
|
+
const altChunkXml = serializeAltChunkNode(block);
|
|
897
|
+
bodyPieces.push(altChunkXml);
|
|
898
|
+
bodyLength += altChunkXml.length;
|
|
899
|
+
cursor += 1;
|
|
900
|
+
previousWasParagraph = false;
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
if (block.type === "section_break") {
|
|
904
|
+
let inlineSectPr;
|
|
905
|
+
if (block.sectionPropertiesXml ?? block.propertiesXml) {
|
|
906
|
+
inlineSectPr = block.sectionPropertiesXml ?? block.propertiesXml;
|
|
907
|
+
} else if (block.sectionProperties) {
|
|
908
|
+
inlineSectPr = serializeSectionPropertiesXml(block.sectionProperties);
|
|
909
|
+
} else {
|
|
910
|
+
inlineSectPr = "<w:sectPr/>";
|
|
911
|
+
}
|
|
912
|
+
const sectionParagraphXml = `<w:p><w:pPr>${inlineSectPr}</w:pPr></w:p>`;
|
|
913
|
+
bodyPieces.push(sectionParagraphXml);
|
|
914
|
+
bodyLength += sectionParagraphXml.length;
|
|
915
|
+
cursor += 1;
|
|
916
|
+
previousWasParagraph = false;
|
|
917
|
+
continue;
|
|
918
|
+
}
|
|
919
|
+
const blockXml = serializeOpaqueBlock(block, state);
|
|
920
|
+
if (looksLikeSectionPropertiesXml(blockXml)) {
|
|
921
|
+
sectionPropertiesXml = blockXml;
|
|
922
|
+
} else {
|
|
923
|
+
bodyPieces.push(blockXml);
|
|
924
|
+
bodyLength += blockXml.length;
|
|
925
|
+
}
|
|
926
|
+
cursor += 1;
|
|
927
|
+
previousWasParagraph = false;
|
|
928
|
+
}
|
|
929
|
+
const bodyXml = bodyPieces.join("");
|
|
930
|
+
const documentXml = `${prefix}${bodyXml || "<w:p><w:r><w:t></w:t></w:r></w:p>"}${sectionPropertiesXml}${suffix}`;
|
|
931
|
+
return {
|
|
932
|
+
documentXml,
|
|
933
|
+
relationships: state.relationships,
|
|
934
|
+
paragraphBoundaries
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
function serializeOpaqueBlock(block, state) {
|
|
938
|
+
return lookupOpaqueXml(block.fragmentId, state);
|
|
939
|
+
}
|
|
940
|
+
function serializeTableNode(table, state) {
|
|
941
|
+
const propertiesXml = table.propertiesXml ?? buildTablePropertiesXml(table);
|
|
942
|
+
const gridXml = table.gridColumns.length > 0 ? `<w:tblGrid>${table.gridColumns.map((width) => `<w:gridCol w:w="${width}"/>`).join("")}</w:tblGrid>` : "";
|
|
943
|
+
const rowsXml = table.rows.map((row) => {
|
|
944
|
+
const rowPropertiesXml = row.propertiesXml ?? buildTableRowPropertiesXml(row);
|
|
945
|
+
const cellsXml = row.cells.map((cell) => serializeTableCellNode(cell, state)).join("");
|
|
946
|
+
return `<w:tr>${rowPropertiesXml}${cellsXml}</w:tr>`;
|
|
947
|
+
}).join("");
|
|
948
|
+
return `<w:tbl>${propertiesXml}${gridXml}${rowsXml}</w:tbl>`;
|
|
949
|
+
}
|
|
950
|
+
function serializeTableCellNode(cell, state) {
|
|
951
|
+
const propertiesXml = cell.propertiesXml ?? buildCellPropertiesXml(cell);
|
|
952
|
+
const blocksXml = cell.children.map((child) => serializeBlockNode(child, state)).join("");
|
|
953
|
+
return `<w:tc>${propertiesXml}${blocksXml || "<w:p/>"}</w:tc>`;
|
|
954
|
+
}
|
|
955
|
+
function serializeBlockNode(block, state) {
|
|
956
|
+
switch (block.type) {
|
|
957
|
+
case "paragraph":
|
|
958
|
+
return serializeTableCellParagraph(block, state);
|
|
959
|
+
case "table":
|
|
960
|
+
return serializeTableNode(block, state);
|
|
961
|
+
case "sdt":
|
|
962
|
+
return serializeSdtNode(block, state);
|
|
963
|
+
case "custom_xml":
|
|
964
|
+
return serializeCustomXmlNode(block, state);
|
|
965
|
+
case "alt_chunk":
|
|
966
|
+
return serializeAltChunkNode(block);
|
|
967
|
+
case "opaque_block":
|
|
968
|
+
return lookupOpaqueXml(block.fragmentId, state);
|
|
969
|
+
case "section_break":
|
|
970
|
+
if (block.sectionPropertiesXml ?? block.propertiesXml) {
|
|
971
|
+
return block.sectionPropertiesXml ?? block.propertiesXml;
|
|
972
|
+
}
|
|
973
|
+
if (block.sectionProperties) return serializeSectionPropertiesXml(block.sectionProperties);
|
|
974
|
+
return "<w:sectPr/>";
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
function buildCellPropertiesXml(cell) {
|
|
978
|
+
const children = [];
|
|
979
|
+
if (cell.gridSpan && cell.gridSpan > 1) {
|
|
980
|
+
children.push(`<w:gridSpan w:val="${cell.gridSpan}"/>`);
|
|
981
|
+
}
|
|
982
|
+
if (cell.verticalMerge) {
|
|
983
|
+
children.push(
|
|
984
|
+
cell.verticalMerge === "restart" ? `<w:vMerge w:val="restart"/>` : `<w:vMerge/>`
|
|
985
|
+
);
|
|
986
|
+
}
|
|
987
|
+
return children.length > 0 ? `<w:tcPr>${children.join("")}</w:tcPr>` : "";
|
|
988
|
+
}
|
|
989
|
+
function buildTableRowPropertiesXml(row) {
|
|
990
|
+
const children = [];
|
|
991
|
+
if (row.gridBefore !== void 0) {
|
|
992
|
+
children.push(`<w:gridBefore w:val="${row.gridBefore}"/>`);
|
|
993
|
+
}
|
|
994
|
+
if (row.widthBefore) {
|
|
995
|
+
children.push(`<w:wBefore w:w="${row.widthBefore.value}" w:type="${row.widthBefore.type}"/>`);
|
|
996
|
+
}
|
|
997
|
+
if (row.gridAfter !== void 0) {
|
|
998
|
+
children.push(`<w:gridAfter w:val="${row.gridAfter}"/>`);
|
|
999
|
+
}
|
|
1000
|
+
if (row.widthAfter) {
|
|
1001
|
+
children.push(`<w:wAfter w:w="${row.widthAfter.value}" w:type="${row.widthAfter.type}"/>`);
|
|
1002
|
+
}
|
|
1003
|
+
if (children.length === 0) {
|
|
1004
|
+
return "";
|
|
1005
|
+
}
|
|
1006
|
+
return `<w:trPr>${children.join("")}</w:trPr>`;
|
|
1007
|
+
}
|
|
1008
|
+
function serializeSdtNode(block, state) {
|
|
1009
|
+
const propertiesXml = block.properties.propertiesXml ?? buildSdtPropertiesXml(block);
|
|
1010
|
+
const childrenXml = block.children.map((child) => serializeBlockNode(child, state)).join("") || "<w:p/>";
|
|
1011
|
+
return `<w:sdt>${propertiesXml}<w:sdtContent>${childrenXml}</w:sdtContent></w:sdt>`;
|
|
1012
|
+
}
|
|
1013
|
+
function serializeCustomXmlNode(block, state) {
|
|
1014
|
+
const attrs = [];
|
|
1015
|
+
if (block.uri) {
|
|
1016
|
+
attrs.push(`w:uri="${escapeAttribute2(block.uri)}"`);
|
|
1017
|
+
}
|
|
1018
|
+
if (block.element) {
|
|
1019
|
+
attrs.push(`w:element="${escapeAttribute2(block.element)}"`);
|
|
1020
|
+
}
|
|
1021
|
+
const attrXml = attrs.length > 0 ? ` ${attrs.join(" ")}` : "";
|
|
1022
|
+
const childrenXml = block.children.map((child) => serializeBlockNode(child, state)).join("");
|
|
1023
|
+
return `<w:customXml${attrXml}>${childrenXml || "<w:p/>"}</w:customXml>`;
|
|
1024
|
+
}
|
|
1025
|
+
function serializeAltChunkNode(block) {
|
|
1026
|
+
return `<w:altChunk r:id="${escapeAttribute2(block.relationshipId)}"/>`;
|
|
1027
|
+
}
|
|
1028
|
+
function buildSdtPropertiesXml(block) {
|
|
1029
|
+
const children = [];
|
|
1030
|
+
if (block.properties.alias) {
|
|
1031
|
+
children.push(`<w:alias w:val="${escapeAttribute2(block.properties.alias)}"/>`);
|
|
1032
|
+
}
|
|
1033
|
+
if (block.properties.tag) {
|
|
1034
|
+
children.push(`<w:tag w:val="${escapeAttribute2(block.properties.tag)}"/>`);
|
|
1035
|
+
}
|
|
1036
|
+
if (block.properties.lock) {
|
|
1037
|
+
children.push(`<w:lock w:val="${escapeAttribute2(block.properties.lock)}"/>`);
|
|
1038
|
+
}
|
|
1039
|
+
if (block.properties.showingPlcHdr) {
|
|
1040
|
+
children.push(`<w:showingPlcHdr/>`);
|
|
1041
|
+
}
|
|
1042
|
+
if (block.properties.checkbox) {
|
|
1043
|
+
const cb = block.properties.checkbox;
|
|
1044
|
+
const cbParts = [];
|
|
1045
|
+
cbParts.push(`<w14:checked w14:val="${cb.checked ? "1" : "0"}"/>`);
|
|
1046
|
+
if (cb.checkedChar) cbParts.push(`<w14:checkedState w14:val="${escapeAttribute2(cb.checkedChar)}"/>`);
|
|
1047
|
+
if (cb.uncheckedChar) cbParts.push(`<w14:uncheckedState w14:val="${escapeAttribute2(cb.uncheckedChar)}"/>`);
|
|
1048
|
+
children.push(`<w14:checkbox>${cbParts.join("")}</w14:checkbox>`);
|
|
1049
|
+
} else if (block.properties.datePicker) {
|
|
1050
|
+
const dp = block.properties.datePicker;
|
|
1051
|
+
const dateAttrs = dp.fullDate ? ` w:fullDate="${escapeAttribute2(dp.fullDate)}"` : "";
|
|
1052
|
+
const dpParts = [];
|
|
1053
|
+
if (dp.dateFormat) dpParts.push(`<w:dateFormat w:val="${escapeAttribute2(dp.dateFormat)}"/>`);
|
|
1054
|
+
if (dp.lid) dpParts.push(`<w:lid w:val="${escapeAttribute2(dp.lid)}"/>`);
|
|
1055
|
+
children.push(dpParts.length > 0 ? `<w:date${dateAttrs}>${dpParts.join("")}</w:date>` : `<w:date${dateAttrs}/>`);
|
|
1056
|
+
} else if (block.properties.dropdownList) {
|
|
1057
|
+
const items = block.properties.dropdownList.map((item) => {
|
|
1058
|
+
const dt = item.displayText ? ` w:displayText="${escapeAttribute2(item.displayText)}"` : "";
|
|
1059
|
+
return `<w:listItem${dt} w:value="${escapeAttribute2(item.value)}"/>`;
|
|
1060
|
+
}).join("");
|
|
1061
|
+
children.push(`<w:dropDownList>${items}</w:dropDownList>`);
|
|
1062
|
+
} else if (block.properties.comboBox) {
|
|
1063
|
+
const items = block.properties.comboBox.map((item) => {
|
|
1064
|
+
const dt = item.displayText ? ` w:displayText="${escapeAttribute2(item.displayText)}"` : "";
|
|
1065
|
+
return `<w:listItem${dt} w:value="${escapeAttribute2(item.value)}"/>`;
|
|
1066
|
+
}).join("");
|
|
1067
|
+
children.push(`<w:comboBox>${items}</w:comboBox>`);
|
|
1068
|
+
} else if (block.properties.sdtType === "plainText") {
|
|
1069
|
+
children.push(`<w:text/>`);
|
|
1070
|
+
} else if (block.properties.sdtType === "richText") {
|
|
1071
|
+
children.push(`<w:richText/>`);
|
|
1072
|
+
} else if (block.properties.sdtType) {
|
|
1073
|
+
children.push(`<w:${block.properties.sdtType}/>`);
|
|
1074
|
+
}
|
|
1075
|
+
return children.length > 0 ? `<w:sdtPr>${children.join("")}</w:sdtPr>` : "<w:sdtPr/>";
|
|
1076
|
+
}
|
|
1077
|
+
function serializeTableCellParagraph(paragraph, state) {
|
|
1078
|
+
let xml = "<w:p>";
|
|
1079
|
+
const paragraphPropertiesXml = buildParagraphPropertiesXml(paragraph);
|
|
1080
|
+
if (paragraphPropertiesXml.length > 0) {
|
|
1081
|
+
xml += paragraphPropertiesXml;
|
|
1082
|
+
}
|
|
1083
|
+
const childrenXml = paragraph.children.map((child) => serializeTableInlineNode(child, state)).join("");
|
|
1084
|
+
xml += childrenXml || "<w:r><w:t></w:t></w:r>";
|
|
1085
|
+
xml += "</w:p>";
|
|
1086
|
+
return xml;
|
|
1087
|
+
}
|
|
1088
|
+
function serializeTableInlineNode(node, state) {
|
|
1089
|
+
switch (node.type) {
|
|
1090
|
+
case "text": {
|
|
1091
|
+
const marks = node.marks;
|
|
1092
|
+
const properties = serializeRunPropertiesFromMarks(marks);
|
|
1093
|
+
const preserve = requiresPreservedSpace(node.text) ? ` xml:space="preserve"` : "";
|
|
1094
|
+
return `<w:r>${properties}<w:t${preserve}>${escapeXml2(node.text)}</w:t></w:r>`;
|
|
1095
|
+
}
|
|
1096
|
+
case "tab":
|
|
1097
|
+
return "<w:r><w:tab/></w:r>";
|
|
1098
|
+
case "column_break":
|
|
1099
|
+
return '<w:r><w:br w:type="column"/></w:r>';
|
|
1100
|
+
case "hard_break":
|
|
1101
|
+
return "<w:r><w:br/></w:r>";
|
|
1102
|
+
case "symbol": {
|
|
1103
|
+
const properties = serializeRunPropertiesFromMarks(node.marks);
|
|
1104
|
+
const fontAttribute = node.font ? ` w:font="${escapeAttribute2(node.font)}"` : "";
|
|
1105
|
+
return `<w:r>${properties}<w:sym${fontAttribute} w:char="${escapeAttribute2(node.char)}"/></w:r>`;
|
|
1106
|
+
}
|
|
1107
|
+
case "image":
|
|
1108
|
+
return serializeImageNode(node, state);
|
|
1109
|
+
case "opaque_inline":
|
|
1110
|
+
return lookupOpaqueXml(node.fragmentId, state);
|
|
1111
|
+
case "chart_preview":
|
|
1112
|
+
case "smartart_preview":
|
|
1113
|
+
case "shape":
|
|
1114
|
+
case "wordart":
|
|
1115
|
+
case "vml_shape":
|
|
1116
|
+
return wrapInlineRawXml(node.rawXml);
|
|
1117
|
+
case "hyperlink": {
|
|
1118
|
+
const hyperlinkOpen = node.href.startsWith("#") ? `<w:hyperlink w:anchor="${escapeAttribute2(node.href.slice(1))}">` : (() => {
|
|
1119
|
+
const relationshipId = `rIdHyperlink${state.nextHyperlinkRelationshipIndex}`;
|
|
1120
|
+
state.nextHyperlinkRelationshipIndex += 1;
|
|
1121
|
+
state.relationships.push({
|
|
1122
|
+
id: relationshipId,
|
|
1123
|
+
type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
|
1124
|
+
target: node.href,
|
|
1125
|
+
targetMode: "external"
|
|
1126
|
+
});
|
|
1127
|
+
state.retainedRelationshipIds.add(relationshipId);
|
|
1128
|
+
return `<w:hyperlink r:id="${relationshipId}">`;
|
|
1129
|
+
})();
|
|
1130
|
+
const childrenXml = node.children.map((child) => serializeTableInlineNode(child, state)).join("");
|
|
1131
|
+
return `${hyperlinkOpen}${childrenXml}</w:hyperlink>`;
|
|
1132
|
+
}
|
|
1133
|
+
case "field": {
|
|
1134
|
+
if (node.children && node.children.length > 0) {
|
|
1135
|
+
const childrenXml = node.children.map((child) => serializeTableInlineNode(child, state)).join("");
|
|
1136
|
+
if (node.fieldType === "complex") {
|
|
1137
|
+
return `<w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText xml:space="preserve"> ${escapeXml2(node.instruction)} </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r>` + childrenXml + `<w:r><w:fldChar w:fldCharType="end"/></w:r>`;
|
|
1138
|
+
}
|
|
1139
|
+
return `<w:fldSimple w:instr="${escapeAttribute2(node.instruction)}">${childrenXml}</w:fldSimple>`;
|
|
1140
|
+
}
|
|
1141
|
+
return `<w:fldSimple w:instr="${escapeAttribute2(node.instruction)}"/>`;
|
|
1142
|
+
}
|
|
1143
|
+
case "bookmark_start":
|
|
1144
|
+
return `<w:bookmarkStart w:id="${escapeAttribute2(node.bookmarkId)}" w:name="${escapeAttribute2(node.name)}"/>`;
|
|
1145
|
+
case "bookmark_end":
|
|
1146
|
+
return `<w:bookmarkEnd w:id="${escapeAttribute2(node.bookmarkId)}"/>`;
|
|
1147
|
+
case "footnote_ref": {
|
|
1148
|
+
const refElement = node.noteKind === "footnote" ? `<w:footnoteReference w:id="${escapeAttribute2(node.noteId)}"/>` : `<w:endnoteReference w:id="${escapeAttribute2(node.noteId)}"/>`;
|
|
1149
|
+
const styleVal = node.noteKind === "footnote" ? "FootnoteReference" : "EndnoteReference";
|
|
1150
|
+
return `<w:r><w:rPr><w:rStyle w:val="${styleVal}"/></w:rPr>${refElement}</w:r>`;
|
|
1151
|
+
}
|
|
1152
|
+
default:
|
|
1153
|
+
return "";
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
function buildParagraphPropertiesXml(paragraph) {
|
|
1157
|
+
const children = [];
|
|
1158
|
+
if (paragraph.styleId) {
|
|
1159
|
+
children.push(`<w:pStyle w:val="${escapeAttribute2(paragraph.styleId)}"/>`);
|
|
1160
|
+
}
|
|
1161
|
+
if (paragraph.keepNext) {
|
|
1162
|
+
children.push("<w:keepNext/>");
|
|
1163
|
+
}
|
|
1164
|
+
if (paragraph.keepLines) {
|
|
1165
|
+
children.push("<w:keepLines/>");
|
|
1166
|
+
}
|
|
1167
|
+
if (paragraph.pageBreakBefore) {
|
|
1168
|
+
children.push("<w:pageBreakBefore/>");
|
|
1169
|
+
}
|
|
1170
|
+
if (paragraph.widowControl) {
|
|
1171
|
+
children.push("<w:widowControl/>");
|
|
1172
|
+
}
|
|
1173
|
+
if (paragraph.outlineLevel !== void 0) {
|
|
1174
|
+
children.push(`<w:outlineLvl w:val="${paragraph.outlineLevel}"/>`);
|
|
1175
|
+
}
|
|
1176
|
+
if (paragraph.numbering) {
|
|
1177
|
+
children.push(serializeParagraphNumberingProperties(paragraph.numbering));
|
|
1178
|
+
}
|
|
1179
|
+
if (paragraph.spacing) {
|
|
1180
|
+
const s = paragraph.spacing;
|
|
1181
|
+
const attrs = [];
|
|
1182
|
+
if (s.before !== void 0) attrs.push(`w:before="${s.before}"`);
|
|
1183
|
+
if (s.after !== void 0) attrs.push(`w:after="${s.after}"`);
|
|
1184
|
+
if (s.line !== void 0) attrs.push(`w:line="${s.line}"`);
|
|
1185
|
+
if (s.lineRule !== void 0) attrs.push(`w:lineRule="${s.lineRule}"`);
|
|
1186
|
+
if (attrs.length > 0) children.push(`<w:spacing ${attrs.join(" ")}/>`);
|
|
1187
|
+
}
|
|
1188
|
+
if (paragraph.contextualSpacing !== void 0) {
|
|
1189
|
+
children.push(
|
|
1190
|
+
paragraph.contextualSpacing ? "<w:contextualSpacing/>" : `<w:contextualSpacing w:val="0"/>`
|
|
1191
|
+
);
|
|
1192
|
+
}
|
|
1193
|
+
if (paragraph.indentation) {
|
|
1194
|
+
const ind = paragraph.indentation;
|
|
1195
|
+
const attrs = [];
|
|
1196
|
+
if (ind.left !== void 0) attrs.push(`w:left="${ind.left}"`);
|
|
1197
|
+
if (ind.right !== void 0) attrs.push(`w:right="${ind.right}"`);
|
|
1198
|
+
if (ind.firstLine !== void 0) attrs.push(`w:firstLine="${ind.firstLine}"`);
|
|
1199
|
+
if (ind.hanging !== void 0) attrs.push(`w:hanging="${ind.hanging}"`);
|
|
1200
|
+
if (attrs.length > 0) children.push(`<w:ind ${attrs.join(" ")}/>`);
|
|
1201
|
+
}
|
|
1202
|
+
if (paragraph.alignment) {
|
|
1203
|
+
children.push(`<w:jc w:val="${paragraph.alignment}"/>`);
|
|
1204
|
+
}
|
|
1205
|
+
if (paragraph.borders) {
|
|
1206
|
+
const bordersXml = serializeParagraphBorders(paragraph.borders);
|
|
1207
|
+
if (bordersXml) {
|
|
1208
|
+
children.push(bordersXml);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
if (paragraph.shading) {
|
|
1212
|
+
const shadingXml = serializeParagraphShading(paragraph.shading);
|
|
1213
|
+
if (shadingXml) {
|
|
1214
|
+
children.push(shadingXml);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
if (paragraph.bidi) {
|
|
1218
|
+
children.push("<w:bidi/>");
|
|
1219
|
+
}
|
|
1220
|
+
if (paragraph.suppressLineNumbers) {
|
|
1221
|
+
children.push("<w:suppressLineNumbers/>");
|
|
1222
|
+
}
|
|
1223
|
+
if (paragraph.cnfStyle) {
|
|
1224
|
+
children.push(`<w:cnfStyle w:val="${escapeAttribute2(paragraph.cnfStyle)}"/>`);
|
|
1225
|
+
}
|
|
1226
|
+
if (paragraph.tabStops && paragraph.tabStops.length > 0) {
|
|
1227
|
+
const tabsXml = paragraph.tabStops.map((tab) => {
|
|
1228
|
+
const leaderAttr = tab.leader ? ` w:leader="${tab.leader}"` : "";
|
|
1229
|
+
return `<w:tab w:val="${tab.align}" w:pos="${tab.position}"${leaderAttr}/>`;
|
|
1230
|
+
}).join("");
|
|
1231
|
+
children.push(`<w:tabs>${tabsXml}</w:tabs>`);
|
|
1232
|
+
}
|
|
1233
|
+
if (children.length === 0) return "";
|
|
1234
|
+
return `<w:pPr>${children.join("")}</w:pPr>`;
|
|
1235
|
+
}
|
|
1236
|
+
function buildTablePropertiesXml(table) {
|
|
1237
|
+
const children = [];
|
|
1238
|
+
if (table.styleId) {
|
|
1239
|
+
children.push(`<w:tblStyle w:val="${escapeAttribute2(table.styleId)}"/>`);
|
|
1240
|
+
}
|
|
1241
|
+
if (table.tblLook) {
|
|
1242
|
+
const attrs = [];
|
|
1243
|
+
if (table.tblLook.val) {
|
|
1244
|
+
attrs.push(`w:val="${escapeAttribute2(table.tblLook.val)}"`);
|
|
1245
|
+
}
|
|
1246
|
+
for (const [key, attr] of [
|
|
1247
|
+
["firstRow", "w:firstRow"],
|
|
1248
|
+
["lastRow", "w:lastRow"],
|
|
1249
|
+
["firstColumn", "w:firstColumn"],
|
|
1250
|
+
["lastColumn", "w:lastColumn"],
|
|
1251
|
+
["noHBand", "w:noHBand"],
|
|
1252
|
+
["noVBand", "w:noVBand"]
|
|
1253
|
+
]) {
|
|
1254
|
+
const value = table.tblLook[key];
|
|
1255
|
+
if (value !== void 0) {
|
|
1256
|
+
attrs.push(`${attr}="${value ? "1" : "0"}"`);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
if (attrs.length > 0) {
|
|
1260
|
+
children.push(`<w:tblLook ${attrs.join(" ")}/>`);
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
return children.length > 0 ? `<w:tblPr>${children.join("")}</w:tblPr>` : "";
|
|
1264
|
+
}
|
|
1265
|
+
function serializeParagraphBorders(borders) {
|
|
1266
|
+
if (!borders) {
|
|
1267
|
+
return "";
|
|
1268
|
+
}
|
|
1269
|
+
const parts = [];
|
|
1270
|
+
for (const [name, border] of [
|
|
1271
|
+
["top", borders.top],
|
|
1272
|
+
["left", borders.left],
|
|
1273
|
+
["bottom", borders.bottom],
|
|
1274
|
+
["right", borders.right],
|
|
1275
|
+
["bar", borders.bar],
|
|
1276
|
+
["between", borders.between]
|
|
1277
|
+
]) {
|
|
1278
|
+
const xml = serializeBorder(name, border);
|
|
1279
|
+
if (xml) {
|
|
1280
|
+
parts.push(xml);
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
return parts.length > 0 ? `<w:pBdr>${parts.join("")}</w:pBdr>` : "";
|
|
1284
|
+
}
|
|
1285
|
+
function serializeBorder(name, border) {
|
|
1286
|
+
if (!border) {
|
|
1287
|
+
return "";
|
|
1288
|
+
}
|
|
1289
|
+
const attrs = [];
|
|
1290
|
+
if (border.value) attrs.push(`w:val="${escapeAttribute2(border.value)}"`);
|
|
1291
|
+
if (border.size !== void 0) attrs.push(`w:sz="${border.size}"`);
|
|
1292
|
+
if (border.space !== void 0) attrs.push(`w:space="${border.space}"`);
|
|
1293
|
+
if (border.color) attrs.push(`w:color="${escapeAttribute2(border.color)}"`);
|
|
1294
|
+
return attrs.length > 0 ? `<w:${name} ${attrs.join(" ")}/>` : "";
|
|
1295
|
+
}
|
|
1296
|
+
function serializeParagraphShading(shading) {
|
|
1297
|
+
if (!shading) {
|
|
1298
|
+
return "";
|
|
1299
|
+
}
|
|
1300
|
+
const attrs = [];
|
|
1301
|
+
if (shading.val) attrs.push(`w:val="${escapeAttribute2(shading.val)}"`);
|
|
1302
|
+
if (shading.color) attrs.push(`w:color="${escapeAttribute2(shading.color)}"`);
|
|
1303
|
+
if (shading.fill) attrs.push(`w:fill="${escapeAttribute2(shading.fill)}"`);
|
|
1304
|
+
return attrs.length > 0 ? `<w:shd ${attrs.join(" ")}/>` : "";
|
|
1305
|
+
}
|
|
1306
|
+
function serializeRunPropertiesFromMarks(marks) {
|
|
1307
|
+
return serializeRunProperties(marks);
|
|
1308
|
+
}
|
|
1309
|
+
function serializeParagraph(paragraph, state, cursor, paragraphIndex) {
|
|
1310
|
+
let xml = "<w:p>";
|
|
1311
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1312
|
+
const paragraphStart = 0;
|
|
1313
|
+
const paragraphStartTagEnd = xml.length;
|
|
1314
|
+
boundaries.set(cursor, paragraphStartTagEnd);
|
|
1315
|
+
let paragraphPropertiesStart;
|
|
1316
|
+
let paragraphPropertiesEnd;
|
|
1317
|
+
const paragraphPropertiesXml = buildParagraphPropertiesXml(paragraph);
|
|
1318
|
+
if (paragraphPropertiesXml.length > 0) {
|
|
1319
|
+
paragraphPropertiesStart = xml.length;
|
|
1320
|
+
xml += paragraphPropertiesXml;
|
|
1321
|
+
paragraphPropertiesEnd = xml.length;
|
|
1322
|
+
}
|
|
1323
|
+
const children = serializeParagraphChildren(paragraph.children, state, cursor, xml.length);
|
|
1324
|
+
xml += children.xml;
|
|
1325
|
+
const contentEmpty = children.xml.length === 0;
|
|
1326
|
+
if (contentEmpty) {
|
|
1327
|
+
xml += "<w:r><w:t></w:t></w:r>";
|
|
1328
|
+
}
|
|
1329
|
+
const paragraphEndTagStart = xml.length;
|
|
1330
|
+
xml += "</w:p>";
|
|
1331
|
+
if (!children.boundaries.has(children.cursor)) {
|
|
1332
|
+
children.boundaries.set(children.cursor, paragraphEndTagStart);
|
|
1333
|
+
}
|
|
1334
|
+
return {
|
|
1335
|
+
xml,
|
|
1336
|
+
nextCursor: children.cursor,
|
|
1337
|
+
boundary: {
|
|
1338
|
+
paragraphIndex,
|
|
1339
|
+
start: cursor,
|
|
1340
|
+
end: children.cursor,
|
|
1341
|
+
boundaries: children.boundaries,
|
|
1342
|
+
paragraphStart,
|
|
1343
|
+
paragraphStartTagEnd,
|
|
1344
|
+
paragraphEndTagStart,
|
|
1345
|
+
paragraphEnd: xml.length,
|
|
1346
|
+
...paragraphPropertiesStart !== void 0 ? { paragraphPropertiesStart } : {},
|
|
1347
|
+
...paragraphPropertiesEnd !== void 0 ? { paragraphPropertiesEnd } : {}
|
|
1348
|
+
}
|
|
1349
|
+
};
|
|
1350
|
+
}
|
|
1351
|
+
function serializeParagraphChildren(children, state, cursor, xmlOffset) {
|
|
1352
|
+
const pieces = [];
|
|
1353
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1354
|
+
let nextCursor = cursor;
|
|
1355
|
+
let nextOffset = xmlOffset;
|
|
1356
|
+
boundaries.set(nextCursor, nextOffset);
|
|
1357
|
+
for (const child of children) {
|
|
1358
|
+
const result = serializeInlineNode(child, state, nextCursor, nextOffset);
|
|
1359
|
+
pieces.push(result.xml);
|
|
1360
|
+
for (const [position, index] of result.boundaries) {
|
|
1361
|
+
boundaries.set(position, index);
|
|
1362
|
+
}
|
|
1363
|
+
nextCursor = result.cursor;
|
|
1364
|
+
nextOffset += result.xml.length;
|
|
1365
|
+
}
|
|
1366
|
+
return {
|
|
1367
|
+
xml: pieces.join(""),
|
|
1368
|
+
cursor: nextCursor,
|
|
1369
|
+
boundaries
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
function serializeRunPiece(piece) {
|
|
1373
|
+
switch (piece.kind) {
|
|
1374
|
+
case "text":
|
|
1375
|
+
return serializeText(piece.text);
|
|
1376
|
+
case "tab":
|
|
1377
|
+
return "<w:tab/>";
|
|
1378
|
+
case "hard_break":
|
|
1379
|
+
return "<w:br/>";
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
function serializeText(text) {
|
|
1383
|
+
const preserve = requiresPreservedSpace(text) ? ` xml:space="preserve"` : "";
|
|
1384
|
+
return `<w:t${preserve}>${escapeXml2(text)}</w:t>`;
|
|
1385
|
+
}
|
|
1386
|
+
function serializeInlineNode(node, state, cursor, xmlOffset) {
|
|
1387
|
+
switch (node.type) {
|
|
1388
|
+
case "text": {
|
|
1389
|
+
const xml = serializeRun({
|
|
1390
|
+
kind: "text",
|
|
1391
|
+
text: node.text,
|
|
1392
|
+
...node.marks && node.marks.length > 0 ? { marks: node.marks } : {}
|
|
1393
|
+
});
|
|
1394
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1395
|
+
boundaries.set(cursor, xmlOffset);
|
|
1396
|
+
boundaries.set(cursor + Array.from(node.text).length, xmlOffset + xml.length);
|
|
1397
|
+
return {
|
|
1398
|
+
xml,
|
|
1399
|
+
cursor: cursor + Array.from(node.text).length,
|
|
1400
|
+
boundaries
|
|
1401
|
+
};
|
|
1402
|
+
}
|
|
1403
|
+
case "tab": {
|
|
1404
|
+
const xml = serializeRun({ kind: "tab" });
|
|
1405
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1406
|
+
boundaries.set(cursor, xmlOffset);
|
|
1407
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1408
|
+
return {
|
|
1409
|
+
xml,
|
|
1410
|
+
cursor: cursor + 1,
|
|
1411
|
+
boundaries
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
case "column_break": {
|
|
1415
|
+
const xml = `<w:r><w:br w:type="column"/></w:r>`;
|
|
1416
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1417
|
+
boundaries.set(cursor, xmlOffset);
|
|
1418
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1419
|
+
return {
|
|
1420
|
+
xml,
|
|
1421
|
+
cursor: cursor + 1,
|
|
1422
|
+
boundaries
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
case "hard_break": {
|
|
1426
|
+
const xml = serializeRun({ kind: "hard_break" });
|
|
1427
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1428
|
+
boundaries.set(cursor, xmlOffset);
|
|
1429
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1430
|
+
return {
|
|
1431
|
+
xml,
|
|
1432
|
+
cursor: cursor + 1,
|
|
1433
|
+
boundaries
|
|
1434
|
+
};
|
|
1435
|
+
}
|
|
1436
|
+
case "symbol": {
|
|
1437
|
+
const xml = serializeTableInlineNode(node, state);
|
|
1438
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1439
|
+
boundaries.set(cursor, xmlOffset);
|
|
1440
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1441
|
+
return {
|
|
1442
|
+
xml,
|
|
1443
|
+
cursor: cursor + 1,
|
|
1444
|
+
boundaries
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
case "image": {
|
|
1448
|
+
const xml = serializeImageNode(node, state);
|
|
1449
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1450
|
+
boundaries.set(cursor, xmlOffset);
|
|
1451
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1452
|
+
return {
|
|
1453
|
+
xml,
|
|
1454
|
+
cursor: cursor + 1,
|
|
1455
|
+
boundaries
|
|
1456
|
+
};
|
|
1457
|
+
}
|
|
1458
|
+
case "opaque_inline": {
|
|
1459
|
+
const xml = lookupOpaqueXml(node.fragmentId, state);
|
|
1460
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1461
|
+
boundaries.set(cursor, xmlOffset);
|
|
1462
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1463
|
+
return {
|
|
1464
|
+
xml,
|
|
1465
|
+
cursor: cursor + 1,
|
|
1466
|
+
boundaries
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
case "chart_preview":
|
|
1470
|
+
case "smartart_preview":
|
|
1471
|
+
case "shape":
|
|
1472
|
+
case "wordart":
|
|
1473
|
+
case "vml_shape": {
|
|
1474
|
+
const xml = wrapInlineRawXml(node.rawXml);
|
|
1475
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1476
|
+
boundaries.set(cursor, xmlOffset);
|
|
1477
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1478
|
+
return {
|
|
1479
|
+
xml,
|
|
1480
|
+
cursor: cursor + 1,
|
|
1481
|
+
boundaries
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
case "hyperlink": {
|
|
1485
|
+
const hyperlinkOpen = node.href.startsWith("#") ? `<w:hyperlink w:anchor="${escapeAttribute2(node.href.slice(1))}">` : (() => {
|
|
1486
|
+
const relationshipId = `rIdHyperlink${state.nextHyperlinkRelationshipIndex}`;
|
|
1487
|
+
state.nextHyperlinkRelationshipIndex += 1;
|
|
1488
|
+
state.relationships.push({
|
|
1489
|
+
id: relationshipId,
|
|
1490
|
+
type: HYPERLINK_RELATIONSHIP_TYPE,
|
|
1491
|
+
target: node.href,
|
|
1492
|
+
targetMode: "external"
|
|
1493
|
+
});
|
|
1494
|
+
state.retainedRelationshipIds.add(relationshipId);
|
|
1495
|
+
return `<w:hyperlink r:id="${relationshipId}">`;
|
|
1496
|
+
})();
|
|
1497
|
+
const hyperlinkClose = "</w:hyperlink>";
|
|
1498
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1499
|
+
let nextCursor = cursor;
|
|
1500
|
+
let nextOffset = xmlOffset + hyperlinkOpen.length;
|
|
1501
|
+
boundaries.set(cursor, nextOffset);
|
|
1502
|
+
const children = [];
|
|
1503
|
+
for (const child of node.children) {
|
|
1504
|
+
const result = serializeInlineNode(child, state, nextCursor, nextOffset);
|
|
1505
|
+
children.push(result.xml);
|
|
1506
|
+
for (const [position, index] of result.boundaries) {
|
|
1507
|
+
boundaries.set(position, index);
|
|
1508
|
+
}
|
|
1509
|
+
nextCursor = result.cursor;
|
|
1510
|
+
nextOffset += result.xml.length;
|
|
1511
|
+
}
|
|
1512
|
+
boundaries.set(nextCursor, nextOffset);
|
|
1513
|
+
return {
|
|
1514
|
+
xml: `${hyperlinkOpen}${children.join("")}${hyperlinkClose}`,
|
|
1515
|
+
cursor: nextCursor,
|
|
1516
|
+
boundaries
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1519
|
+
case "field": {
|
|
1520
|
+
if (node.children && node.children.length > 0) {
|
|
1521
|
+
const boundaries2 = /* @__PURE__ */ new Map();
|
|
1522
|
+
boundaries2.set(cursor, xmlOffset);
|
|
1523
|
+
let nextCursor = cursor;
|
|
1524
|
+
let nextOffset = xmlOffset;
|
|
1525
|
+
if (node.fieldType === "complex") {
|
|
1526
|
+
const beginXml = `<w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText xml:space="preserve"> ${escapeXml2(node.instruction)} </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r>`;
|
|
1527
|
+
nextOffset += beginXml.length;
|
|
1528
|
+
const children2 = [beginXml];
|
|
1529
|
+
for (const child of node.children) {
|
|
1530
|
+
const result = serializeInlineNode(child, state, nextCursor, nextOffset);
|
|
1531
|
+
children2.push(result.xml);
|
|
1532
|
+
for (const [position, index] of result.boundaries) {
|
|
1533
|
+
boundaries2.set(position, index);
|
|
1534
|
+
}
|
|
1535
|
+
nextCursor = result.cursor;
|
|
1536
|
+
nextOffset += result.xml.length;
|
|
1537
|
+
}
|
|
1538
|
+
const endXml = `<w:r><w:fldChar w:fldCharType="end"/></w:r>`;
|
|
1539
|
+
children2.push(endXml);
|
|
1540
|
+
nextOffset += endXml.length;
|
|
1541
|
+
boundaries2.set(nextCursor, nextOffset);
|
|
1542
|
+
return { xml: children2.join(""), cursor: nextCursor, boundaries: boundaries2 };
|
|
1543
|
+
}
|
|
1544
|
+
const openXml = `<w:fldSimple w:instr="${escapeAttribute2(node.instruction)}">`;
|
|
1545
|
+
nextOffset += openXml.length;
|
|
1546
|
+
const children = [openXml];
|
|
1547
|
+
for (const child of node.children) {
|
|
1548
|
+
const result = serializeInlineNode(child, state, nextCursor, nextOffset);
|
|
1549
|
+
children.push(result.xml);
|
|
1550
|
+
for (const [position, index] of result.boundaries) {
|
|
1551
|
+
boundaries2.set(position, index);
|
|
1552
|
+
}
|
|
1553
|
+
nextCursor = result.cursor;
|
|
1554
|
+
nextOffset += result.xml.length;
|
|
1555
|
+
}
|
|
1556
|
+
children.push("</w:fldSimple>");
|
|
1557
|
+
nextOffset += "</w:fldSimple>".length;
|
|
1558
|
+
boundaries2.set(nextCursor, nextOffset);
|
|
1559
|
+
return { xml: children.join(""), cursor: nextCursor, boundaries: boundaries2 };
|
|
1560
|
+
}
|
|
1561
|
+
const xml = `<w:fldSimple w:instr="${escapeAttribute2(node.instruction)}"/>`;
|
|
1562
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1563
|
+
boundaries.set(cursor, xmlOffset);
|
|
1564
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1565
|
+
return {
|
|
1566
|
+
xml,
|
|
1567
|
+
cursor: cursor + 1,
|
|
1568
|
+
boundaries
|
|
1569
|
+
};
|
|
1570
|
+
}
|
|
1571
|
+
case "bookmark_start": {
|
|
1572
|
+
const xml = `<w:bookmarkStart w:id="${escapeAttribute2(node.bookmarkId)}" w:name="${escapeAttribute2(node.name)}"/>`;
|
|
1573
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1574
|
+
boundaries.set(cursor, xmlOffset);
|
|
1575
|
+
boundaries.set(cursor, xmlOffset + xml.length);
|
|
1576
|
+
return { xml, cursor, boundaries };
|
|
1577
|
+
}
|
|
1578
|
+
case "bookmark_end": {
|
|
1579
|
+
const xml = `<w:bookmarkEnd w:id="${escapeAttribute2(node.bookmarkId)}"/>`;
|
|
1580
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1581
|
+
boundaries.set(cursor, xmlOffset);
|
|
1582
|
+
boundaries.set(cursor, xmlOffset + xml.length);
|
|
1583
|
+
return { xml, cursor, boundaries };
|
|
1584
|
+
}
|
|
1585
|
+
case "footnote_ref": {
|
|
1586
|
+
const refElement = node.noteKind === "footnote" ? `<w:footnoteReference w:id="${escapeAttribute2(node.noteId)}"/>` : `<w:endnoteReference w:id="${escapeAttribute2(node.noteId)}"/>`;
|
|
1587
|
+
const styleVal = node.noteKind === "footnote" ? "FootnoteReference" : "EndnoteReference";
|
|
1588
|
+
const xml = `<w:r><w:rPr><w:rStyle w:val="${styleVal}"/></w:rPr>${refElement}</w:r>`;
|
|
1589
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1590
|
+
boundaries.set(cursor, xmlOffset);
|
|
1591
|
+
boundaries.set(cursor + 1, xmlOffset + xml.length);
|
|
1592
|
+
return {
|
|
1593
|
+
xml,
|
|
1594
|
+
cursor: cursor + 1,
|
|
1595
|
+
boundaries
|
|
1596
|
+
};
|
|
1597
|
+
}
|
|
1598
|
+
default: {
|
|
1599
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
1600
|
+
boundaries.set(cursor, xmlOffset);
|
|
1601
|
+
return { xml: "", cursor, boundaries };
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
function serializeImageNode(node, state) {
|
|
1606
|
+
const placementXml = typeof node.placementXml === "string" ? node.placementXml.trim() : "";
|
|
1607
|
+
if (placementXml.length > 0) {
|
|
1608
|
+
return placementXml.startsWith("<w:r") ? placementXml : `<w:r>${placementXml}</w:r>`;
|
|
1609
|
+
}
|
|
1610
|
+
const mediaItem = state.media.items[node.mediaId];
|
|
1611
|
+
if (mediaItem?.relationshipId && state.existingRelationshipMap.has(mediaItem.relationshipId)) {
|
|
1612
|
+
const altText = node.altText ?? mediaItem.altText ?? mediaItem.filename;
|
|
1613
|
+
return `<w:r><w:drawing><wp:inline xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"><wp:extent cx="9525" cy="9525"/><wp:docPr id="1" name="${escapeAttribute2(mediaItem.filename)}" descr="${escapeAttribute2(altText ?? "")}"/><wp:cNvGraphicFramePr/><a:graphic><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic><pic:nvPicPr><pic:cNvPr id="0" name="${escapeAttribute2(mediaItem.filename)}"/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip r:embed="${escapeAttribute2(mediaItem.relationshipId)}"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="9525" cy="9525"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r>`;
|
|
1614
|
+
}
|
|
1615
|
+
return serializeRun({
|
|
1616
|
+
kind: "text",
|
|
1617
|
+
text: node.altText ?? "[Image]"
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
function serializeRun(piece) {
|
|
1621
|
+
const marks = piece.kind === "text" ? piece.marks : void 0;
|
|
1622
|
+
const properties = serializeRunProperties(marks);
|
|
1623
|
+
const content = serializeRunPiece(piece);
|
|
1624
|
+
return `<w:r>${properties}${content}</w:r>`;
|
|
1625
|
+
}
|
|
1626
|
+
function serializeRunProperties(marks) {
|
|
1627
|
+
if (!marks || marks.length === 0) {
|
|
1628
|
+
return "";
|
|
1629
|
+
}
|
|
1630
|
+
const markParts = [];
|
|
1631
|
+
for (const mark of marks) {
|
|
1632
|
+
switch (mark.type) {
|
|
1633
|
+
case "bold":
|
|
1634
|
+
markParts.push("<w:b/>");
|
|
1635
|
+
break;
|
|
1636
|
+
case "italic":
|
|
1637
|
+
markParts.push("<w:i/>");
|
|
1638
|
+
break;
|
|
1639
|
+
case "underline":
|
|
1640
|
+
markParts.push(`<w:u w:val="single"/>`);
|
|
1641
|
+
break;
|
|
1642
|
+
case "strikethrough":
|
|
1643
|
+
markParts.push("<w:strike/>");
|
|
1644
|
+
break;
|
|
1645
|
+
case "doubleStrikethrough":
|
|
1646
|
+
markParts.push("<w:dstrike/>");
|
|
1647
|
+
break;
|
|
1648
|
+
case "vanish":
|
|
1649
|
+
markParts.push("<w:vanish/>");
|
|
1650
|
+
break;
|
|
1651
|
+
case "lang":
|
|
1652
|
+
markParts.push(`<w:lang w:val="${escapeAttribute2(mark.val)}"/>`);
|
|
1653
|
+
break;
|
|
1654
|
+
case "highlight":
|
|
1655
|
+
markParts.push(`<w:highlight w:val="${escapeAttribute2(mark.val)}"/>`);
|
|
1656
|
+
break;
|
|
1657
|
+
case "backgroundColor":
|
|
1658
|
+
markParts.push(
|
|
1659
|
+
`<w:shd w:val="clear" w:color="auto" w:fill="${escapeAttribute2(mark.color)}"/>`
|
|
1660
|
+
);
|
|
1661
|
+
break;
|
|
1662
|
+
case "charSpacing":
|
|
1663
|
+
markParts.push(`<w:spacing w:val="${mark.val}"/>`);
|
|
1664
|
+
break;
|
|
1665
|
+
case "kerning":
|
|
1666
|
+
markParts.push(`<w:kern w:val="${mark.val}"/>`);
|
|
1667
|
+
break;
|
|
1668
|
+
case "emboss":
|
|
1669
|
+
markParts.push("<w:emboss/>");
|
|
1670
|
+
break;
|
|
1671
|
+
case "imprint":
|
|
1672
|
+
markParts.push("<w:imprint/>");
|
|
1673
|
+
break;
|
|
1674
|
+
case "shadow":
|
|
1675
|
+
markParts.push("<w:shadow/>");
|
|
1676
|
+
break;
|
|
1677
|
+
case "position":
|
|
1678
|
+
markParts.push(`<w:position w:val="${mark.val}"/>`);
|
|
1679
|
+
break;
|
|
1680
|
+
case "textFill":
|
|
1681
|
+
markParts.push(mark.xml);
|
|
1682
|
+
break;
|
|
1683
|
+
case "fontFamily":
|
|
1684
|
+
markParts.push(`<w:rFonts w:ascii="${escapeAttribute2(mark.val)}" w:hAnsi="${escapeAttribute2(mark.val)}"/>`);
|
|
1685
|
+
break;
|
|
1686
|
+
case "fontSize":
|
|
1687
|
+
markParts.push(`<w:sz w:val="${mark.val}"/>`);
|
|
1688
|
+
break;
|
|
1689
|
+
case "textColor":
|
|
1690
|
+
markParts.push(`<w:color w:val="${escapeAttribute2(mark.color)}"/>`);
|
|
1691
|
+
break;
|
|
1692
|
+
case "smallCaps":
|
|
1693
|
+
markParts.push("<w:smallCaps/>");
|
|
1694
|
+
break;
|
|
1695
|
+
case "allCaps":
|
|
1696
|
+
markParts.push("<w:caps/>");
|
|
1697
|
+
break;
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
const children = markParts.join("");
|
|
1701
|
+
return children.length > 0 ? `<w:rPr>${children}</w:rPr>` : "";
|
|
1702
|
+
}
|
|
1703
|
+
function requiresPreservedSpace(text) {
|
|
1704
|
+
return /^\s/.test(text) || /\s$/.test(text) || text.includes(" ");
|
|
1705
|
+
}
|
|
1706
|
+
function lookupOpaqueXml(fragmentId, state) {
|
|
1707
|
+
const fragment = getOpaqueFragment(state.preservation, fragmentId);
|
|
1708
|
+
if (!fragment || fragment.payloadKind !== "xml-subtree") {
|
|
1709
|
+
throw new Error(`Missing preserved OOXML fragment ${fragmentId} during serialization.`);
|
|
1710
|
+
}
|
|
1711
|
+
retainRelationshipsForFragment(
|
|
1712
|
+
fragment,
|
|
1713
|
+
state.relationships,
|
|
1714
|
+
state.existingRelationshipMap,
|
|
1715
|
+
state.retainedRelationshipIds
|
|
1716
|
+
);
|
|
1717
|
+
return fragment.payloadReference;
|
|
1718
|
+
}
|
|
1719
|
+
function cloneRelationship3(relationship) {
|
|
1720
|
+
return { ...relationship };
|
|
1721
|
+
}
|
|
1722
|
+
function looksLikeSectionPropertiesXml(xml) {
|
|
1723
|
+
return /^<[^>]*:?sectPr(?:\s|>|\/)/.test(xml.trim());
|
|
1724
|
+
}
|
|
1725
|
+
function escapeXml2(value) {
|
|
1726
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1727
|
+
}
|
|
1728
|
+
function escapeAttribute2(value) {
|
|
1729
|
+
return escapeXml2(value).replace(/"/g, """);
|
|
1730
|
+
}
|
|
1731
|
+
function offsetParagraphBoundary(boundary, offset) {
|
|
1732
|
+
return {
|
|
1733
|
+
...boundary,
|
|
1734
|
+
boundaries: new Map(
|
|
1735
|
+
[...boundary.boundaries.entries()].map(([position, index]) => [
|
|
1736
|
+
position,
|
|
1737
|
+
index + offset
|
|
1738
|
+
])
|
|
1739
|
+
),
|
|
1740
|
+
paragraphStart: boundary.paragraphStart + offset,
|
|
1741
|
+
paragraphStartTagEnd: boundary.paragraphStartTagEnd + offset,
|
|
1742
|
+
paragraphEndTagStart: boundary.paragraphEndTagStart + offset,
|
|
1743
|
+
paragraphEnd: boundary.paragraphEnd + offset,
|
|
1744
|
+
...boundary.paragraphPropertiesStart !== void 0 ? { paragraphPropertiesStart: boundary.paragraphPropertiesStart + offset } : {},
|
|
1745
|
+
...boundary.paragraphPropertiesEnd !== void 0 ? { paragraphPropertiesEnd: boundary.paragraphPropertiesEnd + offset } : {},
|
|
1746
|
+
...boundary.paragraphRunPropertiesStart !== void 0 ? { paragraphRunPropertiesStart: boundary.paragraphRunPropertiesStart + offset } : {},
|
|
1747
|
+
...boundary.paragraphRunPropertiesEnd !== void 0 ? { paragraphRunPropertiesEnd: boundary.paragraphRunPropertiesEnd + offset } : {}
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
function serializeDocumentAttributes(attributes, content) {
|
|
1751
|
+
const merged = {
|
|
1752
|
+
"xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
|
1753
|
+
"xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
|
1754
|
+
...content && documentNeedsW14Namespace(content) ? { "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml" } : {},
|
|
1755
|
+
...attributes ?? {}
|
|
1756
|
+
};
|
|
1757
|
+
return Object.entries(merged).map(([name, value]) => ` ${name}="${escapeAttribute2(value)}"`).join("");
|
|
1758
|
+
}
|
|
1759
|
+
function serializeSectionPropertiesXml(props) {
|
|
1760
|
+
const children = [];
|
|
1761
|
+
if (props.headerReferences) {
|
|
1762
|
+
for (const ref of props.headerReferences) {
|
|
1763
|
+
children.push(`<w:headerReference w:type="${escapeAttribute2(ref.variant)}" r:id="${escapeAttribute2(ref.relationshipId)}"/>`);
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
if (props.footerReferences) {
|
|
1767
|
+
for (const ref of props.footerReferences) {
|
|
1768
|
+
children.push(`<w:footerReference w:type="${escapeAttribute2(ref.variant)}" r:id="${escapeAttribute2(ref.relationshipId)}"/>`);
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
if (props.sectionType) {
|
|
1772
|
+
children.push(`<w:type w:val="${escapeAttribute2(props.sectionType)}"/>`);
|
|
1773
|
+
}
|
|
1774
|
+
if (props.pageSize) {
|
|
1775
|
+
let pgSz = `<w:pgSz w:w="${props.pageSize.width}" w:h="${props.pageSize.height}"`;
|
|
1776
|
+
if (props.pageSize.orientation) {
|
|
1777
|
+
pgSz += ` w:orient="${props.pageSize.orientation}"`;
|
|
1778
|
+
}
|
|
1779
|
+
pgSz += "/>";
|
|
1780
|
+
children.push(pgSz);
|
|
1781
|
+
}
|
|
1782
|
+
if (props.pageMargins) {
|
|
1783
|
+
let pgMar = `<w:pgMar w:top="${props.pageMargins.top}" w:right="${props.pageMargins.right}" w:bottom="${props.pageMargins.bottom}" w:left="${props.pageMargins.left}"`;
|
|
1784
|
+
if (props.pageMargins.header !== void 0) pgMar += ` w:header="${props.pageMargins.header}"`;
|
|
1785
|
+
if (props.pageMargins.footer !== void 0) pgMar += ` w:footer="${props.pageMargins.footer}"`;
|
|
1786
|
+
if (props.pageMargins.gutter !== void 0) pgMar += ` w:gutter="${props.pageMargins.gutter}"`;
|
|
1787
|
+
pgMar += "/>";
|
|
1788
|
+
children.push(pgMar);
|
|
1789
|
+
}
|
|
1790
|
+
if (props.columns) {
|
|
1791
|
+
let cols = "<w:cols";
|
|
1792
|
+
if (props.columns.count !== void 0) cols += ` w:num="${props.columns.count}"`;
|
|
1793
|
+
if (props.columns.space !== void 0) cols += ` w:space="${props.columns.space}"`;
|
|
1794
|
+
if (props.columns.equalWidth !== void 0) cols += ` w:equalWidth="${props.columns.equalWidth ? "1" : "0"}"`;
|
|
1795
|
+
if (props.columns.separator) cols += ` w:sep="1"`;
|
|
1796
|
+
if (props.columns.columns && props.columns.columns.length > 0) {
|
|
1797
|
+
cols += ">";
|
|
1798
|
+
for (const col of props.columns.columns) {
|
|
1799
|
+
let colXml = `<w:col w:w="${col.width}"`;
|
|
1800
|
+
if (col.space !== void 0) colXml += ` w:space="${col.space}"`;
|
|
1801
|
+
colXml += "/>";
|
|
1802
|
+
cols += colXml;
|
|
1803
|
+
}
|
|
1804
|
+
cols += "</w:cols>";
|
|
1805
|
+
} else {
|
|
1806
|
+
cols += "/>";
|
|
1807
|
+
}
|
|
1808
|
+
children.push(cols);
|
|
1809
|
+
}
|
|
1810
|
+
if (props.pageNumbering) {
|
|
1811
|
+
let pgNum = "<w:pgNumType";
|
|
1812
|
+
if (props.pageNumbering.format) pgNum += ` w:fmt="${escapeAttribute2(props.pageNumbering.format)}"`;
|
|
1813
|
+
if (props.pageNumbering.start !== void 0) pgNum += ` w:start="${props.pageNumbering.start}"`;
|
|
1814
|
+
if (props.pageNumbering.chapStyle) pgNum += ` w:chapStyle="${escapeAttribute2(props.pageNumbering.chapStyle)}"`;
|
|
1815
|
+
if (props.pageNumbering.chapSep) pgNum += ` w:chapSep="${escapeAttribute2(props.pageNumbering.chapSep)}"`;
|
|
1816
|
+
pgNum += "/>";
|
|
1817
|
+
children.push(pgNum);
|
|
1818
|
+
}
|
|
1819
|
+
if (props.lineNumbering) {
|
|
1820
|
+
let lineNumbering = "<w:lnNumType";
|
|
1821
|
+
if (props.lineNumbering.countBy !== void 0) {
|
|
1822
|
+
lineNumbering += ` w:countBy="${props.lineNumbering.countBy}"`;
|
|
1823
|
+
}
|
|
1824
|
+
if (props.lineNumbering.start !== void 0) {
|
|
1825
|
+
lineNumbering += ` w:start="${props.lineNumbering.start}"`;
|
|
1826
|
+
}
|
|
1827
|
+
if (props.lineNumbering.distance !== void 0) {
|
|
1828
|
+
lineNumbering += ` w:distance="${props.lineNumbering.distance}"`;
|
|
1829
|
+
}
|
|
1830
|
+
if (props.lineNumbering.restart) {
|
|
1831
|
+
lineNumbering += ` w:restart="${escapeAttribute2(props.lineNumbering.restart)}"`;
|
|
1832
|
+
}
|
|
1833
|
+
lineNumbering += "/>";
|
|
1834
|
+
children.push(lineNumbering);
|
|
1835
|
+
}
|
|
1836
|
+
if (props.pageBorders) {
|
|
1837
|
+
const attrs = [];
|
|
1838
|
+
if (props.pageBorders.offsetFrom) {
|
|
1839
|
+
attrs.push(`w:offsetFrom="${escapeAttribute2(props.pageBorders.offsetFrom)}"`);
|
|
1840
|
+
}
|
|
1841
|
+
if (props.pageBorders.display) {
|
|
1842
|
+
attrs.push(`w:display="${escapeAttribute2(props.pageBorders.display)}"`);
|
|
1843
|
+
}
|
|
1844
|
+
if (props.pageBorders.zOrder) {
|
|
1845
|
+
attrs.push(`w:zOrder="${escapeAttribute2(props.pageBorders.zOrder)}"`);
|
|
1846
|
+
}
|
|
1847
|
+
const borderXml = [
|
|
1848
|
+
serializeBorder("top", props.pageBorders.top),
|
|
1849
|
+
serializeBorder("left", props.pageBorders.left),
|
|
1850
|
+
serializeBorder("bottom", props.pageBorders.bottom),
|
|
1851
|
+
serializeBorder("right", props.pageBorders.right)
|
|
1852
|
+
].filter((entry) => entry.length > 0);
|
|
1853
|
+
if (attrs.length > 0 || borderXml.length > 0) {
|
|
1854
|
+
children.push(
|
|
1855
|
+
`<w:pgBorders${attrs.length > 0 ? ` ${attrs.join(" ")}` : ""}>${borderXml.join("")}</w:pgBorders>`
|
|
1856
|
+
);
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
if (props.titlePage) {
|
|
1860
|
+
children.push("<w:titlePg/>");
|
|
1861
|
+
}
|
|
1862
|
+
if (props.documentGrid) {
|
|
1863
|
+
const attrs = [];
|
|
1864
|
+
if (props.documentGrid.type) {
|
|
1865
|
+
attrs.push(`w:type="${escapeAttribute2(props.documentGrid.type)}"`);
|
|
1866
|
+
}
|
|
1867
|
+
if (props.documentGrid.linePitch !== void 0) {
|
|
1868
|
+
attrs.push(`w:linePitch="${props.documentGrid.linePitch}"`);
|
|
1869
|
+
}
|
|
1870
|
+
if (props.documentGrid.charSpace !== void 0) {
|
|
1871
|
+
attrs.push(`w:charSpace="${props.documentGrid.charSpace}"`);
|
|
1872
|
+
}
|
|
1873
|
+
if (attrs.length > 0) {
|
|
1874
|
+
children.push(`<w:docGrid ${attrs.join(" ")}/>`);
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
if (children.length === 0) {
|
|
1878
|
+
return "<w:sectPr/>";
|
|
1879
|
+
}
|
|
1880
|
+
return `<w:sectPr>${children.join("")}</w:sectPr>`;
|
|
1881
|
+
}
|
|
1882
|
+
function wrapInlineRawXml(rawXml) {
|
|
1883
|
+
const trimmed = rawXml.trimStart();
|
|
1884
|
+
return trimmed.startsWith("<w:r") ? rawXml : `<w:r>${rawXml}</w:r>`;
|
|
1885
|
+
}
|
|
1886
|
+
function documentNeedsW14Namespace(content) {
|
|
1887
|
+
const blockQueue = [...content.children];
|
|
1888
|
+
while (blockQueue.length > 0) {
|
|
1889
|
+
const block = blockQueue.shift();
|
|
1890
|
+
if (!block) {
|
|
1891
|
+
continue;
|
|
1892
|
+
}
|
|
1893
|
+
if (block.type === "paragraph") {
|
|
1894
|
+
for (const child of block.children) {
|
|
1895
|
+
if ((child.type === "text" || child.type === "symbol") && child.marks?.some((mark) => mark.type === "textFill")) {
|
|
1896
|
+
return true;
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
continue;
|
|
1900
|
+
}
|
|
1901
|
+
if (block.type === "table") {
|
|
1902
|
+
for (const row of block.rows) {
|
|
1903
|
+
for (const cell of row.cells) {
|
|
1904
|
+
blockQueue.push(...cell.children);
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
return false;
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
// src/io/export/serialize-comments.ts
|
|
1913
|
+
var DEFAULT_COMMENTS_ROOT_TAG = `<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="w14">`;
|
|
1914
|
+
var DEFAULT_COMMENTS_EXTENDED_ROOT_TAG = `<w15:commentsEx xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml">`;
|
|
1915
|
+
var DEFAULT_COMMENTS_IDS_ROOT_TAG = `<w16cid:commentsIds xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid">`;
|
|
1916
|
+
var DEFAULT_PEOPLE_ROOT_TAG = `<w15:people xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml">`;
|
|
1917
|
+
function serializeMergedCommentsXml(threads, options = {}) {
|
|
1918
|
+
const serializableThreads = threads.filter((thread) => thread.entries.length > 0);
|
|
1919
|
+
const preservedDefinitions = (options.preservedDefinitions ?? []).slice().sort((left, right) => left.order - right.order);
|
|
1920
|
+
const runtimeThreadById = new Map(
|
|
1921
|
+
serializableThreads.map((thread) => [thread.commentId, thread])
|
|
1922
|
+
);
|
|
1923
|
+
const exportCommentIds = options.exportCommentIds ?? createCommentExportIdMap(serializableThreads, preservedDefinitions);
|
|
1924
|
+
const serializableEntries = createSerializableEntries(
|
|
1925
|
+
serializableThreads,
|
|
1926
|
+
preservedDefinitions,
|
|
1927
|
+
exportCommentIds
|
|
1928
|
+
);
|
|
1929
|
+
const serializedEntryByOoxmlId = new Map(
|
|
1930
|
+
serializableEntries.map((entry) => [entry.exportCommentId, entry])
|
|
1931
|
+
);
|
|
1932
|
+
const emittedThreadIds = /* @__PURE__ */ new Set();
|
|
1933
|
+
const mergedComments = [];
|
|
1934
|
+
for (const definition of preservedDefinitions) {
|
|
1935
|
+
const ownedThread = runtimeThreadById.get(definition.commentId);
|
|
1936
|
+
if (ownedThread) {
|
|
1937
|
+
if (emittedThreadIds.has(ownedThread.commentId)) {
|
|
1938
|
+
continue;
|
|
1939
|
+
}
|
|
1940
|
+
mergedComments.push(
|
|
1941
|
+
...serializeThreadEntries(
|
|
1942
|
+
serializableEntries.filter((entry) => entry.thread.commentId === ownedThread.commentId)
|
|
1943
|
+
)
|
|
1944
|
+
);
|
|
1945
|
+
emittedThreadIds.add(ownedThread.commentId);
|
|
1946
|
+
continue;
|
|
1947
|
+
}
|
|
1948
|
+
if (serializedEntryByOoxmlId.has(definition.commentId)) {
|
|
1949
|
+
continue;
|
|
1950
|
+
}
|
|
1951
|
+
mergedComments.push(definition.rawXml);
|
|
1952
|
+
}
|
|
1953
|
+
for (const thread of serializableThreads) {
|
|
1954
|
+
if (emittedThreadIds.has(thread.commentId)) {
|
|
1955
|
+
continue;
|
|
1956
|
+
}
|
|
1957
|
+
mergedComments.push(
|
|
1958
|
+
...serializeThreadEntries(
|
|
1959
|
+
serializableEntries.filter((entry) => entry.thread.commentId === thread.commentId)
|
|
1960
|
+
)
|
|
1961
|
+
);
|
|
1962
|
+
emittedThreadIds.add(thread.commentId);
|
|
1963
|
+
}
|
|
1964
|
+
const mergedExtendedEntries = [
|
|
1965
|
+
...serializePreservedCommentExtensions(
|
|
1966
|
+
preservedDefinitions.filter((definition) => !serializedEntryByOoxmlId.has(definition.commentId))
|
|
1967
|
+
),
|
|
1968
|
+
...serializableEntries.map((entry) => serializeCommentExtension(entry)).filter((xml) => Boolean(xml))
|
|
1969
|
+
];
|
|
1970
|
+
const mergedDurableIds = [
|
|
1971
|
+
...serializePreservedCommentIds(
|
|
1972
|
+
preservedDefinitions.filter((definition) => !serializedEntryByOoxmlId.has(definition.commentId))
|
|
1973
|
+
),
|
|
1974
|
+
...serializableEntries.map((entry) => serializeCommentDurableId(entry)).filter((xml) => Boolean(xml))
|
|
1975
|
+
];
|
|
1976
|
+
const peopleAuthors = /* @__PURE__ */ new Set([
|
|
1977
|
+
...options.peopleAuthors ?? [],
|
|
1978
|
+
...preservedDefinitions.map((definition) => definition.authorId).filter((authorId) => typeof authorId === "string" && authorId.length > 0),
|
|
1979
|
+
...serializableEntries.map((entry) => entry.entry.authorId).filter((authorId) => typeof authorId === "string" && authorId.length > 0)
|
|
1980
|
+
]);
|
|
1981
|
+
return {
|
|
1982
|
+
commentsXml: [
|
|
1983
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
1984
|
+
normalizeCommentsRootTag(options.sourceRootTag ?? DEFAULT_COMMENTS_ROOT_TAG),
|
|
1985
|
+
...mergedComments,
|
|
1986
|
+
`</w:comments>`
|
|
1987
|
+
].join("\n"),
|
|
1988
|
+
serializedCommentIds: serializableThreads.map((thread) => thread.commentId),
|
|
1989
|
+
commentsExtendedXml: mergedExtendedEntries.length > 0 ? [
|
|
1990
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
1991
|
+
options.sourceExtendedRootTag ?? DEFAULT_COMMENTS_EXTENDED_ROOT_TAG,
|
|
1992
|
+
...mergedExtendedEntries,
|
|
1993
|
+
`</w15:commentsEx>`
|
|
1994
|
+
].join("\n") : void 0,
|
|
1995
|
+
commentsIdsXml: mergedDurableIds.length > 0 ? [
|
|
1996
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
1997
|
+
options.sourceIdsRootTag ?? DEFAULT_COMMENTS_IDS_ROOT_TAG,
|
|
1998
|
+
...mergedDurableIds,
|
|
1999
|
+
`</w16cid:commentsIds>`
|
|
2000
|
+
].join("\n") : void 0,
|
|
2001
|
+
peopleXml: peopleAuthors.size > 0 ? [
|
|
2002
|
+
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>`,
|
|
2003
|
+
options.sourcePeopleRootTag ?? DEFAULT_PEOPLE_ROOT_TAG,
|
|
2004
|
+
...[...peopleAuthors].sort((left, right) => left.localeCompare(right)).map(
|
|
2005
|
+
(authorId) => `<w15:person w15:author="${escapeAttribute3(authorId)}" />`
|
|
2006
|
+
),
|
|
2007
|
+
`</w15:people>`
|
|
2008
|
+
].join("\n") : void 0
|
|
2009
|
+
};
|
|
2010
|
+
}
|
|
2011
|
+
function normalizeCommentsRootTag(rootTag) {
|
|
2012
|
+
let normalized = rootTag;
|
|
2013
|
+
if (!/\bxmlns:w14=/u.test(normalized)) {
|
|
2014
|
+
normalized = normalized.replace(
|
|
2015
|
+
/>$/u,
|
|
2016
|
+
` xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml">`
|
|
2017
|
+
);
|
|
2018
|
+
}
|
|
2019
|
+
if (!/\bxmlns:mc=/u.test(normalized)) {
|
|
2020
|
+
normalized = normalized.replace(
|
|
2021
|
+
/>$/u,
|
|
2022
|
+
` xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">`
|
|
2023
|
+
);
|
|
2024
|
+
}
|
|
2025
|
+
if (!/\bmc:Ignorable=/u.test(normalized)) {
|
|
2026
|
+
normalized = normalized.replace(/>$/u, ` mc:Ignorable="w14">`);
|
|
2027
|
+
} else if (!/\bmc:Ignorable="[^"]*\bw14\b/u.test(normalized)) {
|
|
2028
|
+
normalized = normalized.replace(
|
|
2029
|
+
/\bmc:Ignorable="([^"]*)"/u,
|
|
2030
|
+
(_match, value) => `mc:Ignorable="${`${value} w14`.trim().replace(/\s+/gu, " ")}"`
|
|
2031
|
+
);
|
|
2032
|
+
}
|
|
2033
|
+
return normalized;
|
|
2034
|
+
}
|
|
2035
|
+
function serializeCommentAnchorsIntoDocumentXml(documentXml, threads, paragraphs = mapParagraphBoundaries(documentXml), options = {}) {
|
|
2036
|
+
const insertions = /* @__PURE__ */ new Map();
|
|
2037
|
+
const serializedCommentIds = [];
|
|
2038
|
+
const skippedCommentIds = [];
|
|
2039
|
+
const exportCommentIds = options.exportCommentIds ?? createCommentExportIdMap(threads);
|
|
2040
|
+
for (const thread of threads) {
|
|
2041
|
+
const { anchor } = thread;
|
|
2042
|
+
if (anchor.kind !== "range") {
|
|
2043
|
+
skippedCommentIds.push(thread.commentId);
|
|
2044
|
+
continue;
|
|
2045
|
+
}
|
|
2046
|
+
const paragraph = paragraphs.find(
|
|
2047
|
+
(candidate) => anchor.range.from >= candidate.start && anchor.range.to <= candidate.end
|
|
2048
|
+
);
|
|
2049
|
+
if (!paragraph) {
|
|
2050
|
+
skippedCommentIds.push(thread.commentId);
|
|
2051
|
+
continue;
|
|
2052
|
+
}
|
|
2053
|
+
const startIndex = paragraph.boundaries.get(anchor.range.from);
|
|
2054
|
+
const endIndex = paragraph.boundaries.get(anchor.range.to);
|
|
2055
|
+
if (startIndex === void 0 || endIndex === void 0) {
|
|
2056
|
+
skippedCommentIds.push(thread.commentId);
|
|
2057
|
+
continue;
|
|
2058
|
+
}
|
|
2059
|
+
const exportCommentId = exportCommentIds.get(thread.commentId) ?? thread.commentId;
|
|
2060
|
+
pushInsertion(
|
|
2061
|
+
insertions,
|
|
2062
|
+
startIndex,
|
|
2063
|
+
`<w:commentRangeStart w:id="${escapeAttribute3(exportCommentId)}"/>`
|
|
2064
|
+
);
|
|
2065
|
+
pushInsertion(
|
|
2066
|
+
insertions,
|
|
2067
|
+
endIndex,
|
|
2068
|
+
`<w:commentRangeEnd w:id="${escapeAttribute3(exportCommentId)}"/>`
|
|
2069
|
+
);
|
|
2070
|
+
pushInsertion(
|
|
2071
|
+
insertions,
|
|
2072
|
+
endIndex,
|
|
2073
|
+
`<w:r><w:commentReference w:id="${escapeAttribute3(exportCommentId)}"/></w:r>`
|
|
2074
|
+
);
|
|
2075
|
+
serializedCommentIds.push(thread.commentId);
|
|
2076
|
+
}
|
|
2077
|
+
const sortedInsertions = [...insertions.entries()].sort((left, right) => left[0] - right[0]);
|
|
2078
|
+
let cursor = 0;
|
|
2079
|
+
let output = "";
|
|
2080
|
+
for (const [index, snippets] of sortedInsertions) {
|
|
2081
|
+
output += documentXml.slice(cursor, index);
|
|
2082
|
+
output += snippets.join("");
|
|
2083
|
+
cursor = index;
|
|
2084
|
+
}
|
|
2085
|
+
output += documentXml.slice(cursor);
|
|
2086
|
+
return {
|
|
2087
|
+
documentXml: output,
|
|
2088
|
+
serializedCommentIds,
|
|
2089
|
+
skippedCommentIds
|
|
2090
|
+
};
|
|
2091
|
+
}
|
|
2092
|
+
function createCommentExportIdMap(threads, preservedDefinitions = []) {
|
|
2093
|
+
const exportIds = /* @__PURE__ */ new Map();
|
|
2094
|
+
const reservedNumericIds = /* @__PURE__ */ new Set();
|
|
2095
|
+
for (const definition of preservedDefinitions) {
|
|
2096
|
+
const numericId = parseOoxmlNumericId(definition.commentId);
|
|
2097
|
+
if (numericId !== void 0) {
|
|
2098
|
+
reservedNumericIds.add(numericId);
|
|
2099
|
+
}
|
|
2100
|
+
}
|
|
2101
|
+
for (const thread of threads) {
|
|
2102
|
+
const rootEntry = getRootEntry(thread);
|
|
2103
|
+
const preferredValue = thread.metadata?.rootOoxmlCommentId ?? rootEntry?.metadata?.ooxmlCommentId ?? thread.commentId;
|
|
2104
|
+
const numericId = parseOoxmlNumericId(preferredValue);
|
|
2105
|
+
if (numericId !== void 0 && !reservedNumericIds.has(numericId)) {
|
|
2106
|
+
exportIds.set(thread.commentId, String(numericId));
|
|
2107
|
+
reservedNumericIds.add(numericId);
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
let nextGeneratedId = reservedNumericIds.size > 0 ? Math.max(...reservedNumericIds) + 1 : 1;
|
|
2111
|
+
for (const thread of threads) {
|
|
2112
|
+
if (exportIds.has(thread.commentId)) {
|
|
2113
|
+
continue;
|
|
2114
|
+
}
|
|
2115
|
+
while (reservedNumericIds.has(nextGeneratedId)) {
|
|
2116
|
+
nextGeneratedId += 1;
|
|
2117
|
+
}
|
|
2118
|
+
exportIds.set(thread.commentId, String(nextGeneratedId));
|
|
2119
|
+
reservedNumericIds.add(nextGeneratedId);
|
|
2120
|
+
nextGeneratedId += 1;
|
|
2121
|
+
}
|
|
2122
|
+
return exportIds;
|
|
2123
|
+
}
|
|
2124
|
+
function createSerializableEntries(threads, preservedDefinitions, exportCommentIds) {
|
|
2125
|
+
const entries = [];
|
|
2126
|
+
const reservedNumericIds = /* @__PURE__ */ new Set();
|
|
2127
|
+
for (const definition of preservedDefinitions) {
|
|
2128
|
+
const numericId = parseOoxmlNumericId(definition.commentId);
|
|
2129
|
+
if (numericId !== void 0) {
|
|
2130
|
+
reservedNumericIds.add(numericId);
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
for (const thread of threads) {
|
|
2134
|
+
const rootEntry = getRootEntry(thread);
|
|
2135
|
+
if (!rootEntry) {
|
|
2136
|
+
continue;
|
|
2137
|
+
}
|
|
2138
|
+
const rootExportCommentId = exportCommentIds.get(thread.commentId) ?? thread.commentId;
|
|
2139
|
+
const rootNumericId = parseOoxmlNumericId(rootExportCommentId);
|
|
2140
|
+
if (rootNumericId !== void 0) {
|
|
2141
|
+
reservedNumericIds.add(rootNumericId);
|
|
2142
|
+
}
|
|
2143
|
+
const rootParaId = rootEntry.metadata?.paraId ?? thread.metadata?.rootParaId ?? generateParaId(thread.commentId, 0);
|
|
2144
|
+
const rootDurableId = rootEntry.metadata?.durableId ?? createDurableId(rootEntry, rootExportCommentId, 0);
|
|
2145
|
+
entries.push({
|
|
2146
|
+
thread,
|
|
2147
|
+
entry: rootEntry,
|
|
2148
|
+
exportCommentId: rootExportCommentId,
|
|
2149
|
+
paraId: rootParaId,
|
|
2150
|
+
durableId: rootDurableId,
|
|
2151
|
+
isRoot: true
|
|
2152
|
+
});
|
|
2153
|
+
for (let index = 1; index < thread.entries.length; index += 1) {
|
|
2154
|
+
const entry = thread.entries[index];
|
|
2155
|
+
if (!entry) {
|
|
2156
|
+
continue;
|
|
2157
|
+
}
|
|
2158
|
+
const preferredCommentId = entry.metadata?.ooxmlCommentId;
|
|
2159
|
+
let exportCommentId;
|
|
2160
|
+
const preferredNumericId = preferredCommentId ? parseOoxmlNumericId(preferredCommentId) : void 0;
|
|
2161
|
+
if (preferredNumericId !== void 0 && !reservedNumericIds.has(preferredNumericId)) {
|
|
2162
|
+
exportCommentId = String(preferredNumericId);
|
|
2163
|
+
reservedNumericIds.add(preferredNumericId);
|
|
2164
|
+
} else {
|
|
2165
|
+
let nextGeneratedId = reservedNumericIds.size > 0 ? Math.max(...reservedNumericIds) + 1 : 1;
|
|
2166
|
+
while (reservedNumericIds.has(nextGeneratedId)) {
|
|
2167
|
+
nextGeneratedId += 1;
|
|
2168
|
+
}
|
|
2169
|
+
exportCommentId = String(nextGeneratedId);
|
|
2170
|
+
reservedNumericIds.add(nextGeneratedId);
|
|
2171
|
+
}
|
|
2172
|
+
entries.push({
|
|
2173
|
+
thread,
|
|
2174
|
+
entry,
|
|
2175
|
+
exportCommentId,
|
|
2176
|
+
paraId: entry.metadata?.paraId ?? generateParaId(thread.commentId, index),
|
|
2177
|
+
durableId: entry.metadata?.durableId ?? createDurableId(entry, exportCommentId, index),
|
|
2178
|
+
isRoot: false
|
|
2179
|
+
});
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
return entries;
|
|
2183
|
+
}
|
|
2184
|
+
function serializeThreadEntries(entries) {
|
|
2185
|
+
return entries.map((entry) => serializeCommentEntry(entry));
|
|
2186
|
+
}
|
|
2187
|
+
function serializeCommentEntry(entry) {
|
|
2188
|
+
const author = escapeAttribute3(entry.entry.authorId);
|
|
2189
|
+
const createdAt = escapeAttribute3(entry.entry.createdAt);
|
|
2190
|
+
const initials = entry.entry.metadata?.initials;
|
|
2191
|
+
const paragraphXml = serializeCommentParagraphs(entry.entry.body, entry.paraId);
|
|
2192
|
+
return `<w:comment w:id="${escapeAttribute3(entry.exportCommentId)}"${initials ? ` w:initials="${escapeAttribute3(initials)}"` : ""} w:author="${author}" w:date="${createdAt}">${paragraphXml}</w:comment>`;
|
|
2193
|
+
}
|
|
2194
|
+
function serializeCommentParagraphs(body, paraId) {
|
|
2195
|
+
const paragraphs = body.length > 0 ? body.split("\n") : [""];
|
|
2196
|
+
const textId = deriveTextIdFromParaId(paraId);
|
|
2197
|
+
return paragraphs.map((paragraph, index) => {
|
|
2198
|
+
const attributes = index === 0 ? ` w14:paraId="${escapeAttribute3(paraId)}" w14:textId="${escapeAttribute3(textId)}"` : "";
|
|
2199
|
+
return `<w:p${attributes}><w:r>${serializeText2(paragraph)}</w:r></w:p>`;
|
|
2200
|
+
}).join("");
|
|
2201
|
+
}
|
|
2202
|
+
function serializePreservedCommentExtensions(definitions) {
|
|
2203
|
+
return definitions.map(
|
|
2204
|
+
(definition) => definition.paraId ? serializePreservedCommentExtension(definition) : void 0
|
|
2205
|
+
).filter((xml) => Boolean(xml));
|
|
2206
|
+
}
|
|
2207
|
+
function serializePreservedCommentExtension(definition) {
|
|
2208
|
+
const doneValue = definition.isDone ? "true" : "false";
|
|
2209
|
+
return `<w15:commentEx w15:paraId="${escapeAttribute3(definition.paraId)}"${definition.parentParaId ? ` w15:paraIdParent="${escapeAttribute3(definition.parentParaId)}"` : ""} w15:done="${doneValue}" />`;
|
|
2210
|
+
}
|
|
2211
|
+
function serializeCommentExtension(entry) {
|
|
2212
|
+
return `<w15:commentEx w15:paraId="${escapeAttribute3(entry.paraId)}"${entry.isRoot ? "" : ` w15:paraIdParent="${escapeAttribute3(findRootParaId(entry.thread, entry.paraId))}"`} w15:done="${entry.isRoot && entry.thread.status === "resolved" ? "true" : "false"}" />`;
|
|
2213
|
+
}
|
|
2214
|
+
function serializePreservedCommentIds(definitions) {
|
|
2215
|
+
return definitions.map(
|
|
2216
|
+
(definition) => definition.paraId && definition.durableId ? `<w16cid:commentId w16cid:paraId="${escapeAttribute3(definition.paraId)}" w16cid:durableId="${escapeAttribute3(definition.durableId)}" />` : void 0
|
|
2217
|
+
).filter((xml) => Boolean(xml));
|
|
2218
|
+
}
|
|
2219
|
+
function serializeCommentDurableId(entry) {
|
|
2220
|
+
if (!entry.durableId) {
|
|
2221
|
+
return void 0;
|
|
2222
|
+
}
|
|
2223
|
+
return `<w16cid:commentId w16cid:paraId="${escapeAttribute3(entry.paraId)}" w16cid:durableId="${escapeAttribute3(entry.durableId)}" />`;
|
|
2224
|
+
}
|
|
2225
|
+
function getRootEntry(thread) {
|
|
2226
|
+
return thread.entries[0];
|
|
2227
|
+
}
|
|
2228
|
+
function findRootParaId(thread, fallback) {
|
|
2229
|
+
return thread.entries[0]?.metadata?.paraId ?? thread.metadata?.rootParaId ?? fallback;
|
|
2230
|
+
}
|
|
2231
|
+
function generateParaId(seed, index) {
|
|
2232
|
+
const normalized = `${seed}:${index}`;
|
|
2233
|
+
let hash = 0;
|
|
2234
|
+
for (let cursor = 0; cursor < normalized.length; cursor += 1) {
|
|
2235
|
+
hash = hash * 31 + normalized.charCodeAt(cursor) >>> 0;
|
|
2236
|
+
}
|
|
2237
|
+
return hash.toString(16).toUpperCase().padStart(8, "0").slice(-8);
|
|
2238
|
+
}
|
|
2239
|
+
function createDurableId(entry, exportCommentId, index) {
|
|
2240
|
+
const seed = `${entry.entryId}:${exportCommentId}:${index}`;
|
|
2241
|
+
let hash = 0n;
|
|
2242
|
+
for (let cursor = 0; cursor < seed.length; cursor += 1) {
|
|
2243
|
+
hash = hash * 131n + BigInt(seed.charCodeAt(cursor)) & 0xffffffffffffffffn;
|
|
2244
|
+
}
|
|
2245
|
+
return hash.toString(16).toUpperCase().padStart(16, "0").slice(-16);
|
|
2246
|
+
}
|
|
2247
|
+
function deriveTextIdFromParaId(paraId) {
|
|
2248
|
+
return paraId.slice(-8).padStart(8, "0");
|
|
2249
|
+
}
|
|
2250
|
+
function parseOoxmlNumericId(value) {
|
|
2251
|
+
if (!/^-?\d+$/u.test(value)) {
|
|
2252
|
+
return void 0;
|
|
2253
|
+
}
|
|
2254
|
+
const numericId = Number.parseInt(value, 10);
|
|
2255
|
+
return Number.isFinite(numericId) ? numericId : void 0;
|
|
2256
|
+
}
|
|
2257
|
+
function mapParagraphBoundaries(documentXml) {
|
|
2258
|
+
const root = parseXml(documentXml);
|
|
2259
|
+
const documentElement = findChildElement(root, "document");
|
|
2260
|
+
const bodyElement = findChildElement(documentElement, "body");
|
|
2261
|
+
const paragraphs = [];
|
|
2262
|
+
walkBlockNodesForParagraphBoundaries(
|
|
2263
|
+
bodyElement.children,
|
|
2264
|
+
documentXml,
|
|
2265
|
+
paragraphs,
|
|
2266
|
+
0,
|
|
2267
|
+
-1,
|
|
2268
|
+
true
|
|
2269
|
+
);
|
|
2270
|
+
return paragraphs;
|
|
2271
|
+
}
|
|
2272
|
+
function walkBlockNodesForParagraphBoundaries(nodes, documentXml, paragraphs, globalCursor, paragraphIndex, useSurfaceParagraphSeparators) {
|
|
2273
|
+
let nextCursor = globalCursor;
|
|
2274
|
+
let nextParagraphIndex = paragraphIndex;
|
|
2275
|
+
let elementIndex = -1;
|
|
2276
|
+
for (const node of nodes) {
|
|
2277
|
+
if (node.type !== "element") {
|
|
2278
|
+
continue;
|
|
2279
|
+
}
|
|
2280
|
+
elementIndex += 1;
|
|
2281
|
+
const name = localName(node.name);
|
|
2282
|
+
if (name === "p") {
|
|
2283
|
+
if (useSurfaceParagraphSeparators && elementIndex > 0) {
|
|
2284
|
+
nextCursor += 1;
|
|
2285
|
+
}
|
|
2286
|
+
nextParagraphIndex += 1;
|
|
2287
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
2288
|
+
boundaries.set(nextCursor, node.start + openingTagLength(documentXml, node.start));
|
|
2289
|
+
walkParagraphForBoundaries(
|
|
2290
|
+
node,
|
|
2291
|
+
documentXml,
|
|
2292
|
+
boundaries,
|
|
2293
|
+
() => nextCursor,
|
|
2294
|
+
(next) => {
|
|
2295
|
+
nextCursor = next;
|
|
2296
|
+
}
|
|
2297
|
+
);
|
|
2298
|
+
if (!boundaries.has(nextCursor)) {
|
|
2299
|
+
boundaries.set(nextCursor, node.end - 4);
|
|
2300
|
+
}
|
|
2301
|
+
paragraphs.push({
|
|
2302
|
+
paragraphIndex: nextParagraphIndex,
|
|
2303
|
+
start: Math.min(...boundaries.keys()),
|
|
2304
|
+
end: Math.max(...boundaries.keys()),
|
|
2305
|
+
boundaries
|
|
2306
|
+
});
|
|
2307
|
+
continue;
|
|
2308
|
+
}
|
|
2309
|
+
if (name === "tbl") {
|
|
2310
|
+
for (const child of node.children) {
|
|
2311
|
+
if (child.type !== "element" || localName(child.name) !== "tr") {
|
|
2312
|
+
continue;
|
|
2313
|
+
}
|
|
2314
|
+
for (const rowChild of child.children) {
|
|
2315
|
+
if (rowChild.type !== "element" || localName(rowChild.name) !== "tc") {
|
|
2316
|
+
continue;
|
|
2317
|
+
}
|
|
2318
|
+
const result = walkBlockNodesForParagraphBoundaries(
|
|
2319
|
+
rowChild.children,
|
|
2320
|
+
documentXml,
|
|
2321
|
+
paragraphs,
|
|
2322
|
+
nextCursor,
|
|
2323
|
+
nextParagraphIndex,
|
|
2324
|
+
false
|
|
2325
|
+
);
|
|
2326
|
+
nextCursor = result.globalCursor;
|
|
2327
|
+
nextParagraphIndex = result.paragraphIndex;
|
|
2328
|
+
}
|
|
2329
|
+
}
|
|
2330
|
+
continue;
|
|
2331
|
+
}
|
|
2332
|
+
if (name === "sdt") {
|
|
2333
|
+
const sdtContent = findChildElement(node, "sdtContent");
|
|
2334
|
+
const result = walkBlockNodesForParagraphBoundaries(
|
|
2335
|
+
sdtContent.children,
|
|
2336
|
+
documentXml,
|
|
2337
|
+
paragraphs,
|
|
2338
|
+
nextCursor,
|
|
2339
|
+
nextParagraphIndex,
|
|
2340
|
+
false
|
|
2341
|
+
);
|
|
2342
|
+
nextCursor = result.globalCursor;
|
|
2343
|
+
nextParagraphIndex = result.paragraphIndex;
|
|
2344
|
+
continue;
|
|
2345
|
+
}
|
|
2346
|
+
if (name === "customXml") {
|
|
2347
|
+
nextCursor += 1;
|
|
2348
|
+
continue;
|
|
2349
|
+
}
|
|
2350
|
+
nextCursor += 0;
|
|
2351
|
+
}
|
|
2352
|
+
return {
|
|
2353
|
+
globalCursor: nextCursor,
|
|
2354
|
+
paragraphIndex: nextParagraphIndex
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
function walkParagraphForBoundaries(paragraph, sourceXml, boundaries, getCursor, setCursor) {
|
|
2358
|
+
for (const child of paragraph.children) {
|
|
2359
|
+
walkInlineNodeForBoundaries(child, sourceXml, boundaries, getCursor, setCursor);
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
function walkInlineNodeForBoundaries(node, sourceXml, boundaries, getCursor, setCursor) {
|
|
2363
|
+
if (node.type !== "element") {
|
|
2364
|
+
return;
|
|
2365
|
+
}
|
|
2366
|
+
switch (localName(node.name)) {
|
|
2367
|
+
case "r": {
|
|
2368
|
+
if (!boundaries.has(getCursor())) {
|
|
2369
|
+
boundaries.set(getCursor(), node.start);
|
|
2370
|
+
}
|
|
2371
|
+
for (const child of node.children) {
|
|
2372
|
+
walkInlineNodeForBoundaries(child, sourceXml, boundaries, getCursor, setCursor);
|
|
2373
|
+
}
|
|
2374
|
+
boundaries.set(getCursor(), node.end);
|
|
2375
|
+
return;
|
|
2376
|
+
}
|
|
2377
|
+
case "t": {
|
|
2378
|
+
const text = node.children.filter((child) => child.type === "text").map((child) => child.text).join("");
|
|
2379
|
+
setCursor(getCursor() + Array.from(text).length);
|
|
2380
|
+
return;
|
|
2381
|
+
}
|
|
2382
|
+
case "tab":
|
|
2383
|
+
case "br": {
|
|
2384
|
+
const startCursor = getCursor();
|
|
2385
|
+
boundaries.set(startCursor, node.start);
|
|
2386
|
+
const nextCursor = startCursor + 1;
|
|
2387
|
+
boundaries.set(nextCursor, node.end);
|
|
2388
|
+
setCursor(nextCursor);
|
|
2389
|
+
return;
|
|
2390
|
+
}
|
|
2391
|
+
default:
|
|
2392
|
+
for (const child of node.children) {
|
|
2393
|
+
walkInlineNodeForBoundaries(child, sourceXml, boundaries, getCursor, setCursor);
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
function pushInsertion(insertions, index, xml) {
|
|
2398
|
+
const bucket = insertions.get(index);
|
|
2399
|
+
if (bucket) {
|
|
2400
|
+
bucket.push(xml);
|
|
2401
|
+
return;
|
|
2402
|
+
}
|
|
2403
|
+
insertions.set(index, [xml]);
|
|
2404
|
+
}
|
|
2405
|
+
function serializeText2(text) {
|
|
2406
|
+
const preserve = requiresPreservedSpace2(text) ? ` xml:space="preserve"` : "";
|
|
2407
|
+
return `<w:t${preserve}>${escapeXml3(text)}</w:t>`;
|
|
2408
|
+
}
|
|
2409
|
+
function requiresPreservedSpace2(text) {
|
|
2410
|
+
return /^\s/u.test(text) || /\s$/u.test(text) || /\s{2,}/u.test(text);
|
|
2411
|
+
}
|
|
2412
|
+
function escapeXml3(value) {
|
|
2413
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
2414
|
+
}
|
|
2415
|
+
function escapeAttribute3(value) {
|
|
2416
|
+
return escapeXml3(value).replace(/"/g, """);
|
|
2417
|
+
}
|
|
2418
|
+
function openingTagLength(xml, start) {
|
|
2419
|
+
const end = xml.indexOf(">", start);
|
|
2420
|
+
if (end < 0) {
|
|
2421
|
+
throw new Error("Malformed XML: missing tag close.");
|
|
2422
|
+
}
|
|
2423
|
+
return end - start + 1;
|
|
2424
|
+
}
|
|
2425
|
+
function parseXml(xml) {
|
|
2426
|
+
const root = {
|
|
2427
|
+
type: "element",
|
|
2428
|
+
name: "#document",
|
|
2429
|
+
attributes: {},
|
|
2430
|
+
children: [],
|
|
2431
|
+
start: 0,
|
|
2432
|
+
end: xml.length
|
|
2433
|
+
};
|
|
2434
|
+
const stack = [root];
|
|
2435
|
+
const tokenPattern = /<!--[\s\S]*?-->|<\?[\s\S]*?\?>|<!DOCTYPE[\s\S]*?>|<!\[CDATA\[[\s\S]*?\]\]>|<[^>]+>|[^<]+/gu;
|
|
2436
|
+
for (const match of xml.matchAll(tokenPattern)) {
|
|
2437
|
+
const token = match[0] ?? "";
|
|
2438
|
+
const start = match.index ?? 0;
|
|
2439
|
+
const end = start + token.length;
|
|
2440
|
+
if (token.startsWith("<?") || token.startsWith("<!DOCTYPE") || token.startsWith("<!--")) {
|
|
2441
|
+
continue;
|
|
2442
|
+
}
|
|
2443
|
+
if (token.startsWith("<![CDATA[")) {
|
|
2444
|
+
const text2 = token.slice(9, -3);
|
|
2445
|
+
stack[stack.length - 1]?.children.push({
|
|
2446
|
+
type: "text",
|
|
2447
|
+
text: text2,
|
|
2448
|
+
start,
|
|
2449
|
+
end
|
|
2450
|
+
});
|
|
2451
|
+
continue;
|
|
2452
|
+
}
|
|
2453
|
+
if (token.startsWith("</")) {
|
|
2454
|
+
const node = stack.pop();
|
|
2455
|
+
if (!node) {
|
|
2456
|
+
throw new Error("Malformed XML: unexpected closing tag.");
|
|
2457
|
+
}
|
|
2458
|
+
node.end = end;
|
|
2459
|
+
continue;
|
|
2460
|
+
}
|
|
2461
|
+
if (token.startsWith("<")) {
|
|
2462
|
+
const selfClosing = /\/>$/.test(token);
|
|
2463
|
+
const tagBody = token.slice(1, token.length - (selfClosing ? 2 : 1)).trim();
|
|
2464
|
+
const { name, attributes } = parseTag(tagBody);
|
|
2465
|
+
const node = {
|
|
2466
|
+
type: "element",
|
|
2467
|
+
name,
|
|
2468
|
+
attributes,
|
|
2469
|
+
children: [],
|
|
2470
|
+
start,
|
|
2471
|
+
end
|
|
2472
|
+
};
|
|
2473
|
+
stack[stack.length - 1]?.children.push(node);
|
|
2474
|
+
if (!selfClosing) {
|
|
2475
|
+
stack.push(node);
|
|
2476
|
+
}
|
|
2477
|
+
continue;
|
|
2478
|
+
}
|
|
2479
|
+
const text = decodeXmlText(token);
|
|
2480
|
+
if (text.length > 0) {
|
|
2481
|
+
stack[stack.length - 1]?.children.push({
|
|
2482
|
+
type: "text",
|
|
2483
|
+
text,
|
|
2484
|
+
start,
|
|
2485
|
+
end
|
|
2486
|
+
});
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
if (stack.length !== 1) {
|
|
2490
|
+
throw new Error("Malformed XML: unclosed tag.");
|
|
2491
|
+
}
|
|
2492
|
+
return root;
|
|
2493
|
+
}
|
|
2494
|
+
function parseTag(tagBody) {
|
|
2495
|
+
const whitespaceIndex = tagBody.search(/\s/u);
|
|
2496
|
+
const name = whitespaceIndex === -1 ? tagBody : tagBody.slice(0, whitespaceIndex);
|
|
2497
|
+
const rawAttributes = whitespaceIndex === -1 ? "" : tagBody.slice(whitespaceIndex + 1);
|
|
2498
|
+
const attributes = {};
|
|
2499
|
+
const pattern = /([A-Za-z_][A-Za-z0-9:._-]*)\s*=\s*("([^"]*)"|'([^']*)')/gu;
|
|
2500
|
+
for (const match of rawAttributes.matchAll(pattern)) {
|
|
2501
|
+
const key = match[1];
|
|
2502
|
+
const value = match[3] ?? match[4] ?? "";
|
|
2503
|
+
if (key) {
|
|
2504
|
+
attributes[key] = decodeXmlText(value);
|
|
2505
|
+
}
|
|
2506
|
+
}
|
|
2507
|
+
return { name, attributes };
|
|
2508
|
+
}
|
|
2509
|
+
function findChildElement(node, name) {
|
|
2510
|
+
const match = node.children.find(
|
|
2511
|
+
(child) => child.type === "element" && localName(child.name) === name
|
|
2512
|
+
);
|
|
2513
|
+
if (!match) {
|
|
2514
|
+
throw new Error(`Expected XML element ${name}.`);
|
|
2515
|
+
}
|
|
2516
|
+
return match;
|
|
2517
|
+
}
|
|
2518
|
+
function localName(name) {
|
|
2519
|
+
const index = name.indexOf(":");
|
|
2520
|
+
return index === -1 ? name : name.slice(index + 1);
|
|
2521
|
+
}
|
|
2522
|
+
function decodeXmlText(text) {
|
|
2523
|
+
return text.replace(
|
|
2524
|
+
/&(?:#x([0-9A-Fa-f]+)|#([0-9]+)|([A-Za-z]+));/gu,
|
|
2525
|
+
(_, hex, dec, named) => {
|
|
2526
|
+
if (hex) {
|
|
2527
|
+
return String.fromCodePoint(Number.parseInt(hex, 16));
|
|
2528
|
+
}
|
|
2529
|
+
if (dec) {
|
|
2530
|
+
return String.fromCodePoint(Number.parseInt(dec, 10));
|
|
2531
|
+
}
|
|
2532
|
+
switch (named) {
|
|
2533
|
+
case "amp":
|
|
2534
|
+
return "&";
|
|
2535
|
+
case "lt":
|
|
2536
|
+
return "<";
|
|
2537
|
+
case "gt":
|
|
2538
|
+
return ">";
|
|
2539
|
+
case "quot":
|
|
2540
|
+
return '"';
|
|
2541
|
+
case "apos":
|
|
2542
|
+
return "'";
|
|
2543
|
+
default:
|
|
2544
|
+
return `&${named};`;
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
);
|
|
2548
|
+
}
|
|
2549
|
+
|
|
2550
|
+
// src/io/export/split-review-boundaries.ts
|
|
2551
|
+
function splitDocumentAtReviewBoundaries(content, comments, revisions) {
|
|
2552
|
+
const splitPositions = collectSplitPositions(comments, revisions);
|
|
2553
|
+
if (splitPositions.size === 0) {
|
|
2554
|
+
return content;
|
|
2555
|
+
}
|
|
2556
|
+
return {
|
|
2557
|
+
type: "doc",
|
|
2558
|
+
children: splitBlockNodes(content.children, splitPositions, 0, true).children
|
|
2559
|
+
};
|
|
2560
|
+
}
|
|
2561
|
+
function collectSplitPositions(comments, revisions) {
|
|
2562
|
+
const positions = /* @__PURE__ */ new Set();
|
|
2563
|
+
for (const thread of comments) {
|
|
2564
|
+
if (thread.anchor.kind !== "range") {
|
|
2565
|
+
continue;
|
|
2566
|
+
}
|
|
2567
|
+
positions.add(thread.anchor.range.from);
|
|
2568
|
+
positions.add(thread.anchor.range.to);
|
|
2569
|
+
}
|
|
2570
|
+
for (const revision of revisions) {
|
|
2571
|
+
if (revision.status !== "active" || revision.anchor.kind !== "range") {
|
|
2572
|
+
continue;
|
|
2573
|
+
}
|
|
2574
|
+
if (revision.metadata.importedRevisionForm === "paragraph-insertion" || revision.metadata.importedRevisionForm === "paragraph-deletion") {
|
|
2575
|
+
continue;
|
|
2576
|
+
}
|
|
2577
|
+
positions.add(revision.anchor.range.from);
|
|
2578
|
+
positions.add(revision.anchor.range.to);
|
|
2579
|
+
}
|
|
2580
|
+
return positions;
|
|
2581
|
+
}
|
|
2582
|
+
function splitParagraph(paragraph, splitPositions, cursor) {
|
|
2583
|
+
const children = [];
|
|
2584
|
+
let nextCursor = cursor;
|
|
2585
|
+
for (const child of paragraph.children) {
|
|
2586
|
+
if (child.type === "text") {
|
|
2587
|
+
const split = splitTextNode(child, splitPositions, nextCursor);
|
|
2588
|
+
children.push(...split.children);
|
|
2589
|
+
nextCursor = split.cursor;
|
|
2590
|
+
continue;
|
|
2591
|
+
}
|
|
2592
|
+
if (child.type === "hyperlink") {
|
|
2593
|
+
const split = splitHyperlinkNode(child, splitPositions, nextCursor);
|
|
2594
|
+
children.push(...split.children);
|
|
2595
|
+
nextCursor = split.cursor;
|
|
2596
|
+
continue;
|
|
2597
|
+
}
|
|
2598
|
+
children.push(child);
|
|
2599
|
+
nextCursor += measureInlineNodeForReviewBoundaries(child);
|
|
2600
|
+
}
|
|
2601
|
+
return {
|
|
2602
|
+
paragraph: {
|
|
2603
|
+
...paragraph,
|
|
2604
|
+
children
|
|
2605
|
+
},
|
|
2606
|
+
cursor: nextCursor
|
|
2607
|
+
};
|
|
2608
|
+
}
|
|
2609
|
+
function splitBlockNodes(blocks, splitPositions, cursor, useSurfaceParagraphSeparators) {
|
|
2610
|
+
const children = [];
|
|
2611
|
+
let nextCursor = cursor;
|
|
2612
|
+
for (const [index, block] of blocks.entries()) {
|
|
2613
|
+
if (block.type === "paragraph") {
|
|
2614
|
+
if (useSurfaceParagraphSeparators && index > 0) {
|
|
2615
|
+
nextCursor += 1;
|
|
2616
|
+
}
|
|
2617
|
+
const next = splitParagraph(block, splitPositions, nextCursor);
|
|
2618
|
+
children.push(next.paragraph);
|
|
2619
|
+
nextCursor = next.cursor;
|
|
2620
|
+
continue;
|
|
2621
|
+
}
|
|
2622
|
+
if (block.type === "table") {
|
|
2623
|
+
const next = splitTableAtReviewBoundaries(block, splitPositions, nextCursor);
|
|
2624
|
+
children.push(next.table);
|
|
2625
|
+
nextCursor = next.cursor;
|
|
2626
|
+
continue;
|
|
2627
|
+
}
|
|
2628
|
+
if (block.type === "sdt") {
|
|
2629
|
+
const next = splitBlockNodes(block.children, splitPositions, nextCursor, false);
|
|
2630
|
+
children.push({
|
|
2631
|
+
...block,
|
|
2632
|
+
children: next.children
|
|
2633
|
+
});
|
|
2634
|
+
nextCursor = next.cursor;
|
|
2635
|
+
continue;
|
|
2636
|
+
}
|
|
2637
|
+
if (block.type === "custom_xml") {
|
|
2638
|
+
children.push(block);
|
|
2639
|
+
nextCursor += 1;
|
|
2640
|
+
continue;
|
|
2641
|
+
}
|
|
2642
|
+
children.push(block);
|
|
2643
|
+
nextCursor += 1;
|
|
2644
|
+
}
|
|
2645
|
+
return {
|
|
2646
|
+
children,
|
|
2647
|
+
cursor: nextCursor
|
|
2648
|
+
};
|
|
2649
|
+
}
|
|
2650
|
+
function splitTableAtReviewBoundaries(table, splitPositions, cursor) {
|
|
2651
|
+
let nextCursor = cursor;
|
|
2652
|
+
const rows = table.rows.map((row) => ({
|
|
2653
|
+
...row,
|
|
2654
|
+
cells: row.cells.map((cell) => {
|
|
2655
|
+
const next = splitBlockNodes(cell.children, splitPositions, nextCursor, false);
|
|
2656
|
+
nextCursor = next.cursor;
|
|
2657
|
+
return {
|
|
2658
|
+
...cell,
|
|
2659
|
+
children: next.children
|
|
2660
|
+
};
|
|
2661
|
+
})
|
|
2662
|
+
}));
|
|
2663
|
+
return {
|
|
2664
|
+
table: {
|
|
2665
|
+
...table,
|
|
2666
|
+
rows
|
|
2667
|
+
},
|
|
2668
|
+
cursor: nextCursor
|
|
2669
|
+
};
|
|
2670
|
+
}
|
|
2671
|
+
function splitHyperlinkNode(node, splitPositions, cursor) {
|
|
2672
|
+
let nextCursor = cursor;
|
|
2673
|
+
const groups = [[]];
|
|
2674
|
+
for (const child of node.children) {
|
|
2675
|
+
if (child.type === "text") {
|
|
2676
|
+
const split = splitTextNode(child, splitPositions, nextCursor);
|
|
2677
|
+
for (const piece of split.children) {
|
|
2678
|
+
groups[groups.length - 1]?.push(piece);
|
|
2679
|
+
nextCursor += Array.from(piece.text).length;
|
|
2680
|
+
if (splitPositions.has(nextCursor)) {
|
|
2681
|
+
groups.push([]);
|
|
2682
|
+
}
|
|
2683
|
+
}
|
|
2684
|
+
continue;
|
|
2685
|
+
}
|
|
2686
|
+
groups[groups.length - 1]?.push(child);
|
|
2687
|
+
nextCursor += measureInlineNodeForReviewBoundaries(child);
|
|
2688
|
+
if (splitPositions.has(nextCursor)) {
|
|
2689
|
+
groups.push([]);
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
const hyperlinks = groups.filter((children) => children.length > 0).map((children) => ({
|
|
2693
|
+
type: "hyperlink",
|
|
2694
|
+
href: node.href,
|
|
2695
|
+
children
|
|
2696
|
+
}));
|
|
2697
|
+
return {
|
|
2698
|
+
children: hyperlinks.length > 0 ? hyperlinks : [node],
|
|
2699
|
+
cursor: nextCursor
|
|
2700
|
+
};
|
|
2701
|
+
}
|
|
2702
|
+
function splitTextNode(node, splitPositions, cursor) {
|
|
2703
|
+
const codepoints = Array.from(node.text);
|
|
2704
|
+
if (codepoints.length === 0) {
|
|
2705
|
+
return { children: [], cursor };
|
|
2706
|
+
}
|
|
2707
|
+
const boundaries = /* @__PURE__ */ new Set([0, codepoints.length]);
|
|
2708
|
+
for (let index = 1; index < codepoints.length; index += 1) {
|
|
2709
|
+
if (splitPositions.has(cursor + index)) {
|
|
2710
|
+
boundaries.add(index);
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
const ordered = [...boundaries].sort((left, right) => left - right);
|
|
2714
|
+
const children = [];
|
|
2715
|
+
for (let index = 0; index < ordered.length - 1; index += 1) {
|
|
2716
|
+
const start = ordered[index] ?? 0;
|
|
2717
|
+
const end = ordered[index + 1] ?? codepoints.length;
|
|
2718
|
+
const text = codepoints.slice(start, end).join("");
|
|
2719
|
+
if (text.length === 0) {
|
|
2720
|
+
continue;
|
|
2721
|
+
}
|
|
2722
|
+
children.push({
|
|
2723
|
+
type: "text",
|
|
2724
|
+
text,
|
|
2725
|
+
...node.marks && node.marks.length > 0 ? { marks: node.marks } : {}
|
|
2726
|
+
});
|
|
2727
|
+
}
|
|
2728
|
+
return {
|
|
2729
|
+
children: children.length > 0 ? children : [node],
|
|
2730
|
+
cursor: cursor + codepoints.length
|
|
2731
|
+
};
|
|
2732
|
+
}
|
|
2733
|
+
function measureInlineNodeForReviewBoundaries(node) {
|
|
2734
|
+
switch (node.type) {
|
|
2735
|
+
case "text":
|
|
2736
|
+
return Array.from(node.text).length;
|
|
2737
|
+
case "bookmark_start":
|
|
2738
|
+
case "bookmark_end":
|
|
2739
|
+
return 0;
|
|
2740
|
+
case "hyperlink":
|
|
2741
|
+
return node.children.reduce(
|
|
2742
|
+
(size, child) => size + measureInlineNodeForReviewBoundaries(child),
|
|
2743
|
+
0
|
|
2744
|
+
);
|
|
2745
|
+
case "field": {
|
|
2746
|
+
const childWidth = node.children.reduce(
|
|
2747
|
+
(size, child) => size + measureInlineNodeForReviewBoundaries(child),
|
|
2748
|
+
0
|
|
2749
|
+
);
|
|
2750
|
+
return childWidth > 0 ? childWidth : 1;
|
|
2751
|
+
}
|
|
2752
|
+
case "tab":
|
|
2753
|
+
case "hard_break":
|
|
2754
|
+
case "column_break":
|
|
2755
|
+
case "footnote_ref":
|
|
2756
|
+
case "image":
|
|
2757
|
+
case "opaque_inline":
|
|
2758
|
+
case "chart_preview":
|
|
2759
|
+
case "smartart_preview":
|
|
2760
|
+
case "shape":
|
|
2761
|
+
case "wordart":
|
|
2762
|
+
case "vml_shape":
|
|
2763
|
+
case "symbol":
|
|
2764
|
+
return 1;
|
|
2765
|
+
default:
|
|
2766
|
+
return 1;
|
|
2767
|
+
}
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
// src/io/ooxml/revision-boundaries.ts
|
|
2771
|
+
function mapRevisionBoundaries(documentXml) {
|
|
2772
|
+
const root = parseXmlWithPositions(documentXml);
|
|
2773
|
+
const documentElement = findRequiredChildElement(root, "document");
|
|
2774
|
+
const bodyElement = findRequiredChildElement(documentElement, "body");
|
|
2775
|
+
const paragraphs = [];
|
|
2776
|
+
let cursor = 0;
|
|
2777
|
+
let paragraphIndex = -1;
|
|
2778
|
+
let previousWasParagraph = false;
|
|
2779
|
+
for (const child of bodyElement.children) {
|
|
2780
|
+
if (child.type !== "element") {
|
|
2781
|
+
continue;
|
|
2782
|
+
}
|
|
2783
|
+
if (localName2(child.name) !== "p") {
|
|
2784
|
+
cursor += 1;
|
|
2785
|
+
previousWasParagraph = false;
|
|
2786
|
+
continue;
|
|
2787
|
+
}
|
|
2788
|
+
if (previousWasParagraph) {
|
|
2789
|
+
cursor += 1;
|
|
2790
|
+
}
|
|
2791
|
+
paragraphIndex += 1;
|
|
2792
|
+
const boundaries = /* @__PURE__ */ new Map();
|
|
2793
|
+
boundaries.set(cursor, child.openingTagEnd);
|
|
2794
|
+
const paragraphProperties = findChildElement2(child, "pPr");
|
|
2795
|
+
const paragraphRunProperties = paragraphProperties ? findChildElement2(paragraphProperties, "rPr") : void 0;
|
|
2796
|
+
walkStoryNodesForBoundaries(
|
|
2797
|
+
child.children,
|
|
2798
|
+
boundaries,
|
|
2799
|
+
() => cursor,
|
|
2800
|
+
(next) => {
|
|
2801
|
+
cursor = next;
|
|
2802
|
+
}
|
|
2803
|
+
);
|
|
2804
|
+
boundaries.set(cursor, child.closingTagStart);
|
|
2805
|
+
paragraphs.push({
|
|
2806
|
+
paragraphIndex,
|
|
2807
|
+
start: Math.min(...boundaries.keys()),
|
|
2808
|
+
end: Math.max(...boundaries.keys()),
|
|
2809
|
+
boundaries,
|
|
2810
|
+
paragraphStart: child.start,
|
|
2811
|
+
paragraphStartTagEnd: child.openingTagEnd,
|
|
2812
|
+
paragraphEndTagStart: child.closingTagStart,
|
|
2813
|
+
paragraphEnd: child.end,
|
|
2814
|
+
...paragraphProperties ? {
|
|
2815
|
+
paragraphPropertiesStart: paragraphProperties.start,
|
|
2816
|
+
paragraphPropertiesEnd: paragraphProperties.end
|
|
2817
|
+
} : {},
|
|
2818
|
+
...paragraphRunProperties ? {
|
|
2819
|
+
paragraphRunPropertiesStart: paragraphRunProperties.start,
|
|
2820
|
+
paragraphRunPropertiesEnd: paragraphRunProperties.end
|
|
2821
|
+
} : {}
|
|
2822
|
+
});
|
|
2823
|
+
previousWasParagraph = true;
|
|
2824
|
+
}
|
|
2825
|
+
return paragraphs;
|
|
2826
|
+
}
|
|
2827
|
+
function walkStoryNodesForBoundaries(nodes, boundaries, getCursor, setCursor) {
|
|
2828
|
+
for (const node of nodes) {
|
|
2829
|
+
walkStoryNodeForBoundaries(node, boundaries, getCursor, setCursor);
|
|
2830
|
+
}
|
|
2831
|
+
}
|
|
2832
|
+
function walkStoryNodeForBoundaries(node, boundaries, getCursor, setCursor) {
|
|
2833
|
+
if (node.type !== "element") {
|
|
2834
|
+
return;
|
|
2835
|
+
}
|
|
2836
|
+
switch (localName2(node.name)) {
|
|
2837
|
+
case "pPr":
|
|
2838
|
+
return;
|
|
2839
|
+
case "r":
|
|
2840
|
+
boundaries.set(getCursor(), node.start);
|
|
2841
|
+
for (const child of node.children) {
|
|
2842
|
+
walkStoryNodeForBoundaries(child, boundaries, getCursor, setCursor);
|
|
2843
|
+
}
|
|
2844
|
+
boundaries.set(getCursor(), node.end);
|
|
2845
|
+
return;
|
|
2846
|
+
case "ins":
|
|
2847
|
+
case "del":
|
|
2848
|
+
case "moveFrom":
|
|
2849
|
+
case "moveTo":
|
|
2850
|
+
case "hyperlink":
|
|
2851
|
+
case "smartTag":
|
|
2852
|
+
case "sdtContent":
|
|
2853
|
+
case "customXml":
|
|
2854
|
+
for (const child of node.children) {
|
|
2855
|
+
walkStoryNodeForBoundaries(child, boundaries, getCursor, setCursor);
|
|
2856
|
+
}
|
|
2857
|
+
return;
|
|
2858
|
+
case "t":
|
|
2859
|
+
case "delText":
|
|
2860
|
+
case "instrText":
|
|
2861
|
+
case "delInstrText": {
|
|
2862
|
+
const text = node.children.filter((child) => child.type === "text").map((child) => child.text).join("");
|
|
2863
|
+
if (text.length === 0) {
|
|
2864
|
+
return;
|
|
2865
|
+
}
|
|
2866
|
+
if (!boundaries.has(getCursor())) {
|
|
2867
|
+
boundaries.set(getCursor(), node.start);
|
|
2868
|
+
}
|
|
2869
|
+
setCursor(getCursor() + text.length);
|
|
2870
|
+
boundaries.set(getCursor(), node.end);
|
|
2871
|
+
return;
|
|
2872
|
+
}
|
|
2873
|
+
case "tab":
|
|
2874
|
+
case "br":
|
|
2875
|
+
case "cr":
|
|
2876
|
+
if (!boundaries.has(getCursor())) {
|
|
2877
|
+
boundaries.set(getCursor(), node.start);
|
|
2878
|
+
}
|
|
2879
|
+
setCursor(getCursor() + 1);
|
|
2880
|
+
boundaries.set(getCursor(), node.end);
|
|
2881
|
+
return;
|
|
2882
|
+
default:
|
|
2883
|
+
for (const child of node.children) {
|
|
2884
|
+
walkStoryNodeForBoundaries(child, boundaries, getCursor, setCursor);
|
|
2885
|
+
}
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
function parseXmlWithPositions(xml) {
|
|
2889
|
+
const root = {
|
|
2890
|
+
type: "element",
|
|
2891
|
+
name: "__root__",
|
|
2892
|
+
attributes: {},
|
|
2893
|
+
children: [],
|
|
2894
|
+
start: 0,
|
|
2895
|
+
end: xml.length,
|
|
2896
|
+
openingTagEnd: 0,
|
|
2897
|
+
closingTagStart: xml.length
|
|
2898
|
+
};
|
|
2899
|
+
const stack = [root];
|
|
2900
|
+
let cursor = 0;
|
|
2901
|
+
while (cursor < xml.length) {
|
|
2902
|
+
if (xml.startsWith("<!--", cursor)) {
|
|
2903
|
+
const end = xml.indexOf("-->", cursor);
|
|
2904
|
+
cursor = end >= 0 ? end + 3 : xml.length;
|
|
2905
|
+
continue;
|
|
2906
|
+
}
|
|
2907
|
+
if (xml.startsWith("<?", cursor)) {
|
|
2908
|
+
const end = xml.indexOf("?>", cursor);
|
|
2909
|
+
cursor = end >= 0 ? end + 2 : xml.length;
|
|
2910
|
+
continue;
|
|
2911
|
+
}
|
|
2912
|
+
if (xml.startsWith("<![CDATA[", cursor)) {
|
|
2913
|
+
const end = xml.indexOf("]]>", cursor);
|
|
2914
|
+
const textEnd = end >= 0 ? end : xml.length;
|
|
2915
|
+
stack[stack.length - 1]?.children.push({
|
|
2916
|
+
type: "text",
|
|
2917
|
+
text: xml.slice(cursor + 9, textEnd),
|
|
2918
|
+
start: cursor,
|
|
2919
|
+
end: textEnd
|
|
2920
|
+
});
|
|
2921
|
+
cursor = end >= 0 ? end + 3 : xml.length;
|
|
2922
|
+
continue;
|
|
2923
|
+
}
|
|
2924
|
+
if (xml[cursor] !== "<") {
|
|
2925
|
+
const nextTag = xml.indexOf("<", cursor);
|
|
2926
|
+
const end = nextTag >= 0 ? nextTag : xml.length;
|
|
2927
|
+
const text = decodeXmlEntities2(xml.slice(cursor, end));
|
|
2928
|
+
if (text.length > 0) {
|
|
2929
|
+
stack[stack.length - 1]?.children.push({
|
|
2930
|
+
type: "text",
|
|
2931
|
+
text,
|
|
2932
|
+
start: cursor,
|
|
2933
|
+
end
|
|
2934
|
+
});
|
|
2935
|
+
}
|
|
2936
|
+
cursor = end;
|
|
2937
|
+
continue;
|
|
2938
|
+
}
|
|
2939
|
+
if (xml[cursor + 1] === "/") {
|
|
2940
|
+
const tagEnd2 = xml.indexOf(">", cursor);
|
|
2941
|
+
if (tagEnd2 < 0) {
|
|
2942
|
+
throw new Error("Malformed XML: unterminated closing tag.");
|
|
2943
|
+
}
|
|
2944
|
+
const node2 = stack.pop();
|
|
2945
|
+
if (!node2 || stack.length === 0) {
|
|
2946
|
+
throw new Error("Malformed XML: unexpected closing tag.");
|
|
2947
|
+
}
|
|
2948
|
+
node2.closingTagStart = cursor;
|
|
2949
|
+
node2.end = tagEnd2 + 1;
|
|
2950
|
+
cursor = tagEnd2 + 1;
|
|
2951
|
+
continue;
|
|
2952
|
+
}
|
|
2953
|
+
const tagEnd = xml.indexOf(">", cursor);
|
|
2954
|
+
if (tagEnd < 0) {
|
|
2955
|
+
throw new Error("Malformed XML: unterminated opening tag.");
|
|
2956
|
+
}
|
|
2957
|
+
const rawTag = xml.slice(cursor + 1, tagEnd);
|
|
2958
|
+
const selfClosing = /\/\s*$/.test(rawTag);
|
|
2959
|
+
const normalizedTag = selfClosing ? rawTag.replace(/\/\s*$/, "") : rawTag;
|
|
2960
|
+
const parts = normalizedTag.trim().split(/\s+/, 1);
|
|
2961
|
+
const name = parts[0];
|
|
2962
|
+
if (!name) {
|
|
2963
|
+
throw new Error("Malformed XML: empty tag name.");
|
|
2964
|
+
}
|
|
2965
|
+
const attributes = parseAttributes2(normalizedTag.slice(name.length));
|
|
2966
|
+
const node = {
|
|
2967
|
+
type: "element",
|
|
2968
|
+
name,
|
|
2969
|
+
attributes,
|
|
2970
|
+
children: [],
|
|
2971
|
+
start: cursor,
|
|
2972
|
+
end: selfClosing ? tagEnd + 1 : xml.length,
|
|
2973
|
+
openingTagEnd: tagEnd + 1,
|
|
2974
|
+
closingTagStart: tagEnd + 1
|
|
2975
|
+
};
|
|
2976
|
+
stack[stack.length - 1]?.children.push(node);
|
|
2977
|
+
cursor = tagEnd + 1;
|
|
2978
|
+
if (!selfClosing) {
|
|
2979
|
+
stack.push(node);
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
if (stack.length !== 1) {
|
|
2983
|
+
throw new Error("Malformed XML: unclosed element.");
|
|
2984
|
+
}
|
|
2985
|
+
return root;
|
|
2986
|
+
}
|
|
2987
|
+
function findRequiredChildElement(node, childLocalName) {
|
|
2988
|
+
const child = findChildElement2(node, childLocalName);
|
|
2989
|
+
if (!child) {
|
|
2990
|
+
throw new Error(`Expected <${childLocalName}> element.`);
|
|
2991
|
+
}
|
|
2992
|
+
return child;
|
|
2993
|
+
}
|
|
2994
|
+
function findChildElement2(node, childLocalName) {
|
|
2995
|
+
return node.children.find(
|
|
2996
|
+
(entry) => entry.type === "element" && localName2(entry.name) === childLocalName
|
|
2997
|
+
);
|
|
2998
|
+
}
|
|
2999
|
+
function localName2(name) {
|
|
3000
|
+
const separatorIndex = name.indexOf(":");
|
|
3001
|
+
return separatorIndex >= 0 ? name.slice(separatorIndex + 1) : name;
|
|
3002
|
+
}
|
|
3003
|
+
function parseAttributes2(source) {
|
|
3004
|
+
const attributes = {};
|
|
3005
|
+
const attributePattern = /([A-Za-z_][A-Za-z0-9:._-]*)\s*=\s*("([^"]*)"|'([^']*)')/g;
|
|
3006
|
+
for (const match of source.matchAll(attributePattern)) {
|
|
3007
|
+
const key = match[1];
|
|
3008
|
+
if (!key) {
|
|
3009
|
+
continue;
|
|
3010
|
+
}
|
|
3011
|
+
const rawValue = match[3] ?? match[4] ?? "";
|
|
3012
|
+
attributes[key] = decodeXmlEntities2(rawValue);
|
|
3013
|
+
}
|
|
3014
|
+
return attributes;
|
|
3015
|
+
}
|
|
3016
|
+
function decodeXmlEntities2(value) {
|
|
3017
|
+
return value.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&/g, "&");
|
|
3018
|
+
}
|
|
3019
|
+
|
|
3020
|
+
// src/io/export/serialize-runtime-revisions.ts
|
|
3021
|
+
function serializeRuntimeRevisionsIntoDocumentXml(documentXml, revisions, boundaries = mapRevisionBoundaries(documentXml)) {
|
|
3022
|
+
const replacements = [];
|
|
3023
|
+
const serializedRevisionIds = [];
|
|
3024
|
+
const skippedRevisionIds = [];
|
|
3025
|
+
const paragraphMarkers = /* @__PURE__ */ new Map();
|
|
3026
|
+
for (const revision of revisions) {
|
|
3027
|
+
if (revision.status !== "active" || revision.anchor.kind !== "range") {
|
|
3028
|
+
continue;
|
|
3029
|
+
}
|
|
3030
|
+
if (revision.kind !== "insertion" && revision.kind !== "deletion") {
|
|
3031
|
+
continue;
|
|
3032
|
+
}
|
|
3033
|
+
const form = revision.metadata.importedRevisionForm;
|
|
3034
|
+
if (form === "paragraph-insertion" || form === "paragraph-deletion") {
|
|
3035
|
+
const paragraphBoundary = findParagraphBoundaryForAnchor(boundaries, revision);
|
|
3036
|
+
if (!paragraphBoundary) {
|
|
3037
|
+
skippedRevisionIds.push(revision.revisionId);
|
|
3038
|
+
continue;
|
|
3039
|
+
}
|
|
3040
|
+
const entry = paragraphMarkers.get(paragraphBoundary.paragraphIndex) ?? {
|
|
3041
|
+
boundary: paragraphBoundary,
|
|
3042
|
+
markers: [],
|
|
3043
|
+
revisionIds: []
|
|
3044
|
+
};
|
|
3045
|
+
entry.markers.push(createParagraphRevisionMarker(revision));
|
|
3046
|
+
entry.revisionIds.push(revision.revisionId);
|
|
3047
|
+
paragraphMarkers.set(paragraphBoundary.paragraphIndex, entry);
|
|
3048
|
+
serializedRevisionIds.push(revision.revisionId);
|
|
3049
|
+
continue;
|
|
3050
|
+
}
|
|
3051
|
+
const rangeReplacement = createRangeRevisionReplacement(documentXml, boundaries, revision);
|
|
3052
|
+
if (!rangeReplacement) {
|
|
3053
|
+
skippedRevisionIds.push(revision.revisionId);
|
|
3054
|
+
continue;
|
|
3055
|
+
}
|
|
3056
|
+
replacements.push(rangeReplacement);
|
|
3057
|
+
serializedRevisionIds.push(revision.revisionId);
|
|
3058
|
+
}
|
|
3059
|
+
for (const { boundary, markers, revisionIds } of paragraphMarkers.values()) {
|
|
3060
|
+
const paragraphInsertion = createParagraphRevisionInsertion(
|
|
3061
|
+
documentXml,
|
|
3062
|
+
boundary,
|
|
3063
|
+
markers
|
|
3064
|
+
);
|
|
3065
|
+
if (!paragraphInsertion) {
|
|
3066
|
+
skippedRevisionIds.push(...revisionIds);
|
|
3067
|
+
continue;
|
|
3068
|
+
}
|
|
3069
|
+
replacements.push(paragraphInsertion);
|
|
3070
|
+
}
|
|
3071
|
+
return {
|
|
3072
|
+
documentXml: applyReplacements(documentXml, replacements),
|
|
3073
|
+
serializedRevisionIds,
|
|
3074
|
+
skippedRevisionIds
|
|
3075
|
+
};
|
|
3076
|
+
}
|
|
3077
|
+
function createRangeRevisionReplacement(documentXml, boundaries, revision) {
|
|
3078
|
+
const { anchor } = revision;
|
|
3079
|
+
if (anchor.kind !== "range") {
|
|
3080
|
+
return void 0;
|
|
3081
|
+
}
|
|
3082
|
+
const paragraphBoundary = findParagraphBoundaryForRange(boundaries, anchor.range.from, anchor.range.to);
|
|
3083
|
+
if (!paragraphBoundary) {
|
|
3084
|
+
return void 0;
|
|
3085
|
+
}
|
|
3086
|
+
const startIndex = paragraphBoundary.boundaries.get(anchor.range.from);
|
|
3087
|
+
const endIndex = paragraphBoundary.boundaries.get(anchor.range.to);
|
|
3088
|
+
if (startIndex === void 0 || endIndex === void 0 || endIndex < startIndex) {
|
|
3089
|
+
return void 0;
|
|
3090
|
+
}
|
|
3091
|
+
const xml = documentXml.slice(startIndex, endIndex);
|
|
3092
|
+
const attributes = serializeRevisionAttributes(revision);
|
|
3093
|
+
return {
|
|
3094
|
+
start: startIndex,
|
|
3095
|
+
end: endIndex,
|
|
3096
|
+
replacement: revision.kind === "insertion" ? `<w:ins${attributes}>${xml}</w:ins>` : `<w:del${attributes}>${convertRunsToDeletedContent(xml)}</w:del>`
|
|
3097
|
+
};
|
|
3098
|
+
}
|
|
3099
|
+
function createParagraphRevisionMarker(revision) {
|
|
3100
|
+
const markerName = revision.kind === "insertion" ? "w:ins" : "w:del";
|
|
3101
|
+
return `<${markerName}${serializeRevisionAttributes(revision)}/>`;
|
|
3102
|
+
}
|
|
3103
|
+
function createParagraphRevisionInsertion(documentXml, paragraphBoundary, markers) {
|
|
3104
|
+
const paragraphXml = documentXml.slice(
|
|
3105
|
+
paragraphBoundary.paragraphStart,
|
|
3106
|
+
paragraphBoundary.paragraphEnd
|
|
3107
|
+
);
|
|
3108
|
+
const markerXml = markers.join("");
|
|
3109
|
+
const paragraphRunPropertiesInsertionIndex = findClosingTagInsertionIndex(
|
|
3110
|
+
documentXml,
|
|
3111
|
+
paragraphBoundary.paragraphRunPropertiesStart,
|
|
3112
|
+
paragraphBoundary.paragraphRunPropertiesEnd,
|
|
3113
|
+
"w:rPr"
|
|
3114
|
+
);
|
|
3115
|
+
if (paragraphRunPropertiesInsertionIndex !== void 0) {
|
|
3116
|
+
return {
|
|
3117
|
+
start: paragraphRunPropertiesInsertionIndex,
|
|
3118
|
+
end: paragraphRunPropertiesInsertionIndex,
|
|
3119
|
+
replacement: markerXml
|
|
3120
|
+
};
|
|
3121
|
+
}
|
|
3122
|
+
const paragraphPropertiesInsertionIndex = findClosingTagInsertionIndex(
|
|
3123
|
+
documentXml,
|
|
3124
|
+
paragraphBoundary.paragraphPropertiesStart,
|
|
3125
|
+
paragraphBoundary.paragraphPropertiesEnd,
|
|
3126
|
+
"w:pPr"
|
|
3127
|
+
);
|
|
3128
|
+
if (paragraphPropertiesInsertionIndex !== void 0) {
|
|
3129
|
+
return {
|
|
3130
|
+
start: paragraphPropertiesInsertionIndex,
|
|
3131
|
+
end: paragraphPropertiesInsertionIndex,
|
|
3132
|
+
replacement: `<w:rPr>${markerXml}</w:rPr>`
|
|
3133
|
+
};
|
|
3134
|
+
}
|
|
3135
|
+
if (!/<w:p[\s>]/u.test(paragraphXml)) {
|
|
3136
|
+
return void 0;
|
|
3137
|
+
}
|
|
3138
|
+
return {
|
|
3139
|
+
start: paragraphBoundary.paragraphStartTagEnd,
|
|
3140
|
+
end: paragraphBoundary.paragraphStartTagEnd,
|
|
3141
|
+
replacement: `<w:pPr><w:rPr>${markerXml}</w:rPr></w:pPr>`
|
|
3142
|
+
};
|
|
3143
|
+
}
|
|
3144
|
+
function findParagraphBoundaryForRange(boundaries, from, to) {
|
|
3145
|
+
return boundaries.find(
|
|
3146
|
+
(boundary) => from >= boundary.start && to <= boundary.end
|
|
3147
|
+
);
|
|
3148
|
+
}
|
|
3149
|
+
function findParagraphBoundaryForAnchor(boundaries, revision) {
|
|
3150
|
+
const anchor = revision.anchor.kind === "range" ? revision.anchor.range.from : void 0;
|
|
3151
|
+
if (anchor === void 0) {
|
|
3152
|
+
return void 0;
|
|
3153
|
+
}
|
|
3154
|
+
return boundaries.find(
|
|
3155
|
+
(boundary) => boundary.end === anchor || anchor >= boundary.start && anchor <= boundary.end
|
|
3156
|
+
);
|
|
3157
|
+
}
|
|
3158
|
+
function serializeRevisionAttributes(revision) {
|
|
3159
|
+
const attributes = {
|
|
3160
|
+
"w:id": revision.metadata.ooxmlRevisionId ?? sanitizeRevisionId(revision.revisionId),
|
|
3161
|
+
"w:author": revision.authorId,
|
|
3162
|
+
"w:date": revision.createdAt
|
|
3163
|
+
};
|
|
3164
|
+
return Object.entries(attributes).filter(([, value]) => value && value.length > 0).map(([name, value]) => ` ${name}="${escapeAttribute4(value)}"`).join("");
|
|
3165
|
+
}
|
|
3166
|
+
function findClosingTagInsertionIndex(documentXml, start, end, tagName) {
|
|
3167
|
+
if (start === void 0 || end === void 0) {
|
|
3168
|
+
return void 0;
|
|
3169
|
+
}
|
|
3170
|
+
const closingTag = `</${tagName}>`;
|
|
3171
|
+
const closingIndex = documentXml.lastIndexOf(closingTag, end);
|
|
3172
|
+
if (closingIndex < start) {
|
|
3173
|
+
return void 0;
|
|
3174
|
+
}
|
|
3175
|
+
return closingIndex;
|
|
3176
|
+
}
|
|
3177
|
+
function sanitizeRevisionId(revisionId) {
|
|
3178
|
+
const numericTail = /(\d+)$/.exec(revisionId)?.[1];
|
|
3179
|
+
return numericTail ?? revisionId.replace(/[^A-Za-z0-9._-]/g, "-");
|
|
3180
|
+
}
|
|
3181
|
+
function convertRunsToDeletedContent(xml) {
|
|
3182
|
+
return xml.replace(/<(\/?)w:t\b/g, "<$1w:delText").replace(/<(\/?)w:instrText\b/g, "<$1w:delInstrText");
|
|
3183
|
+
}
|
|
3184
|
+
function applyReplacements(documentXml, replacements) {
|
|
3185
|
+
const sorted = replacements.slice().sort((left, right) => right.start - left.start || right.end - left.end);
|
|
3186
|
+
let output = documentXml;
|
|
3187
|
+
for (const replacement of sorted) {
|
|
3188
|
+
output = output.slice(0, replacement.start) + replacement.replacement + output.slice(replacement.end);
|
|
3189
|
+
}
|
|
3190
|
+
return output;
|
|
3191
|
+
}
|
|
3192
|
+
function escapeAttribute4(value) {
|
|
3193
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
3194
|
+
}
|
|
3195
|
+
|
|
3196
|
+
export {
|
|
3197
|
+
DOCX_MIME_TYPE,
|
|
3198
|
+
DOCX_DOCUMENT_CONTENT_TYPE,
|
|
3199
|
+
writeDocxPackage,
|
|
3200
|
+
readOpcPackage,
|
|
3201
|
+
createExportSession,
|
|
3202
|
+
DOCX_NULL_NUMBERING_INSTANCE_ID,
|
|
3203
|
+
createSyntheticDocxNullNumberingCatalog,
|
|
3204
|
+
WORD_NUMBERING_CONTENT_TYPE,
|
|
3205
|
+
serializeNumberingXml,
|
|
3206
|
+
hasSerializableNumberingEntries,
|
|
3207
|
+
serializeMainDocument,
|
|
3208
|
+
mapRevisionBoundaries,
|
|
3209
|
+
parseXmlWithPositions,
|
|
3210
|
+
findRequiredChildElement,
|
|
3211
|
+
findChildElement2 as findChildElement,
|
|
3212
|
+
localName2 as localName,
|
|
3213
|
+
serializeMergedCommentsXml,
|
|
3214
|
+
serializeCommentAnchorsIntoDocumentXml,
|
|
3215
|
+
createCommentExportIdMap,
|
|
3216
|
+
mapParagraphBoundaries,
|
|
3217
|
+
splitDocumentAtReviewBoundaries,
|
|
3218
|
+
serializeRuntimeRevisionsIntoDocumentXml
|
|
3219
|
+
};
|
|
3220
|
+
//# sourceMappingURL=chunk-WGBAKP3Q.js.map
|