@stll/folio-core 0.75.0 → 0.76.0
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/ai-edits/headless.d.ts +2 -1
- package/dist/ai-edits/headless.js +18 -12
- package/dist/ai-edits/pending-suggestions.js +2 -3
- package/dist/ai-edits/read.js +9 -3
- package/dist/ai-edits/types.d.ts +9 -4
- package/dist/canonical/batchCommentReferences.js +2 -1
- package/dist/canonical/canonicalBatchReview.js +5 -8
- package/dist/canonical/canonicalComments.js +8 -2
- package/dist/canonical/canonicalCompilerRefusal.d.ts +2 -4
- package/dist/canonical/canonicalCompilerRefusal.js +20 -14
- package/dist/canonical/canonicalPublicOperations.js +203 -71
- package/dist/canonical/canonicalSession.js +3 -1
- package/dist/controller/canonicalClipboard.d.ts +1 -1
- package/dist/controller/canonicalClipboard.js +71 -17
- package/dist/controller/canonicalContentControlValue.js +7 -7
- package/dist/controller/canonicalReview.d.ts +21 -0
- package/dist/controller/canonicalReview.js +44 -9
- package/dist/controller/canonicalStructure.d.ts +1 -2
- package/dist/controller/canonicalStructure.js +51 -24
- package/dist/controller/fontReadiness.js +50 -23
- package/dist/controller/hiddenEditorManager.js +5 -20
- package/dist/docx/bulletMarkers.d.ts +8 -0
- package/dist/docx/bulletMarkers.js +9 -1
- package/dist/docx/canonicalSessionInput.js +25 -11
- package/dist/docx/ensureParaIds.d.ts +31 -0
- package/dist/docx/ensureParaIds.js +57 -32
- package/dist/docx/ensureUniqueControlIds.d.ts +13 -0
- package/dist/docx/ensureUniqueControlIds.js +47 -29
- package/dist/docx/ensureUniqueRevisionIds.d.ts +13 -0
- package/dist/docx/ensureUniqueRevisionIds.js +52 -29
- package/dist/docx/server/templatePlan/apply.js +62 -8
- package/dist/docx/server/templatePlan/blocks.d.ts +55 -3
- package/dist/docx/server/templatePlan/blocks.js +182 -6
- package/dist/docx/server/templatePlan/builders.d.ts +16 -1
- package/dist/docx/server/templatePlan/builders.js +31 -3
- package/dist/docx/server/templatePlan/effects.d.ts +27 -0
- package/dist/docx/server/templatePlan/effects.js +188 -0
- package/dist/docx/server/templatePlan/emit.d.ts +2 -1
- package/dist/docx/server/templatePlan/emit.js +44 -2
- package/dist/docx/server/templatePlan/inspect.js +21 -1
- package/dist/docx/server/templatePlan/labels.js +16 -1
- package/dist/docx/server/templatePlan/plan.d.ts +12 -3
- package/dist/docx/server/templatePlan/plan.js +36 -12
- package/dist/docx/server/templatePlan/planShape.js +47 -22
- package/dist/docx/server/templatePlan/revisions.d.ts +46 -0
- package/dist/docx/server/templatePlan/revisions.js +407 -0
- package/dist/docx/server/templatePlan/story.d.ts +15 -1
- package/dist/docx/server/templatePlan/story.js +50 -2
- package/dist/docx/server/templatePlan/types.d.ts +217 -1
- package/dist/docx/server/templatePlan/verify.js +117 -8
- package/dist/docx/server/templatePlan/xml.d.ts +3 -0
- package/dist/docx/server/templatePlan/xml.js +4 -1
- package/dist/internal/revisionResolutionStep.d.ts +2 -31
- package/dist/internal/revisionResolutionStep.js +2 -171
- package/dist/prosemirror/commands/comments.d.ts +6 -234
- package/dist/prosemirror/commands/comments.js +5 -2464
- package/dist/prosemirror/commands/index.d.ts +4 -2
- package/dist/prosemirror/commands/index.js +4 -2
- package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +1 -11
- package/dist/prosemirror/commands/tableCellMergeResolution.js +3 -154
- package/dist/prosemirror/extensions/marks/HyperlinkExtension.js +6 -8
- package/dist/prosemirror/hyperlinkRemoval.d.ts +11 -8
- package/dist/prosemirror/hyperlinkRemoval.js +24 -15
- package/dist/prosemirror/paraIds.d.ts +1 -14
- package/dist/prosemirror/paraIds.js +1 -14
- package/dist/prosemirror/paragraphMarkJoin.d.ts +0 -16
- package/dist/prosemirror/paragraphMarkJoin.js +3 -46
- package/dist/prosemirror/projectionReview.d.ts +49 -0
- package/dist/prosemirror/projectionReview.js +292 -0
- package/dist/prosemirror/rebaseParagraphRunFormatting.d.ts +1 -13
- package/dist/prosemirror/rebaseParagraphRunFormatting.js +1 -16
- package/dist/prosemirror/replacedAnnotations.d.ts +2 -5
- package/dist/prosemirror/replacedAnnotations.js +1 -10
- package/dist/prosemirror/revisionCarriers.d.ts +9 -1
- package/dist/prosemirror/revisionCarriers.js +15 -1
- package/dist/prosemirror/revisionRangeQueries.d.ts +32 -0
- package/dist/prosemirror/revisionRangeQueries.js +76 -0
- package/dist/prosemirror/runFormattingMarkNames.d.ts +1 -1
- package/dist/prosemirror/runFormattingMarkNames.js +1 -1
- package/dist/prosemirror/suggestionQueries.d.ts +114 -0
- package/dist/prosemirror/suggestionQueries.js +320 -0
- package/dist/prosemirror/tableGridMutation.d.ts +1 -16
- package/dist/prosemirror/tableGridMutation.js +1 -78
- package/dist/prosemirror/tableRowRevisionOwnership.d.ts +18 -0
- package/dist/prosemirror/tableRowRevisionOwnership.js +7 -0
- package/dist/prosemirror/utils/extractTrackedChanges.js +14 -4
- package/dist/server.d.ts +2 -2
- package/dist/types/canonicalCapabilities.d.ts +1 -25
- package/dist/types/canonicalCapabilities.js +1 -23
- package/package.json +2 -2
- package/dist/internal/headlessRevisionResolutionGuard.d.ts +0 -3
- package/dist/internal/headlessRevisionResolutionGuard.js +0 -18
|
@@ -19,7 +19,6 @@ import { createEmptyDoc, toProseDoc } from "../prosemirror/conversion/toProseDoc
|
|
|
19
19
|
import { registerEditorCommandOwner } from "../prosemirror/executeEditorCommand.js";
|
|
20
20
|
import { ensureBaseDirectionInState } from "../prosemirror/extensions/features/AutoBidiDetectionExtension.js";
|
|
21
21
|
import { toggleMarkForAllScripts, toggleUnderlineMark } from "../prosemirror/extensions/marks/markUtils.js";
|
|
22
|
-
import { HyperlinkRemovalRefusal } from "../prosemirror/hyperlinkRemoval.js";
|
|
23
22
|
import { REVISION_MARK_NAMES } from "../prosemirror/keptRangeBoundaries.js";
|
|
24
23
|
import { adoptNativeSelection, createNativeNavigationPlugin, createNativeSelectionRefreshPlugin, isCompositionKeyDown } from "../prosemirror/nativeSelection.js";
|
|
25
24
|
import { NoteReferenceReplayDefect, noteReferenceTransactionIssue } from "../prosemirror/noteReferenceOccurrences.js";
|
|
@@ -27,7 +26,7 @@ import { ensureParaIdsInDoc } from "../prosemirror/paraIds.js";
|
|
|
27
26
|
import { createDocumentNumberingPlugin } from "../prosemirror/plugins/documentNumbering.js";
|
|
28
27
|
import { createDocumentStyleContextPlugin, createDocumentStylesPlugin } from "../prosemirror/plugins/documentStyles.js";
|
|
29
28
|
import { schema } from "../prosemirror/schema/index.js";
|
|
30
|
-
import { CANONICAL_CONDITION,
|
|
29
|
+
import { CANONICAL_CONDITION, CANONICAL_RULE } from "../types/canonicalCapabilities.js";
|
|
31
30
|
import { readModel } from "../types/documentModel.js";
|
|
32
31
|
import { INTERNAL_CLIPBOARD_TYPE } from "../utils/clipboardFiles.js";
|
|
33
32
|
import { NO_ANONYMIZATION_TERMS, createAnonymizationHighlights } from "./anonymizationHighlights.js";
|
|
@@ -497,21 +496,7 @@ const createHiddenEditorManager = (hostDeps) => {
|
|
|
497
496
|
refuse("Composition must finish before formatting.", { condition: CANONICAL_CONDITION.composing });
|
|
498
497
|
return false;
|
|
499
498
|
}
|
|
500
|
-
const
|
|
501
|
-
const described = Result.try({
|
|
502
|
-
try: () => getCanonicalCommandIntents(command, commandState),
|
|
503
|
-
catch: (error) => error
|
|
504
|
-
});
|
|
505
|
-
if (described.isErr()) {
|
|
506
|
-
if (!(described.error instanceof HyperlinkRemovalRefusal)) throw described.error;
|
|
507
|
-
const refusal = new CanonicalSessionRefusalError({
|
|
508
|
-
ground: { gap: CANONICAL_GAP.commands },
|
|
509
|
-
message: described.error.message
|
|
510
|
-
});
|
|
511
|
-
refuse(refusal.message, refusal.ground);
|
|
512
|
-
return false;
|
|
513
|
-
}
|
|
514
|
-
const intents = described.value;
|
|
499
|
+
const intents = getCanonicalCommandIntents(command, view.state);
|
|
515
500
|
if (intents === void 0) return void 0;
|
|
516
501
|
if (intents.length > 0 && intents.every((intent) => intent.type === "selection")) return command(view.state, (transaction) => view?.dispatch(transaction), view);
|
|
517
502
|
syncCanonicalMode();
|
|
@@ -616,7 +601,7 @@ const createHiddenEditorManager = (hostDeps) => {
|
|
|
616
601
|
if (mark !== void 0) command = name === "toggleUnderline" ? toggleUnderlineMark(mark) : toggleMarkForAllScripts(mark, name === "toggleBold" ? "bold" : "italic");
|
|
617
602
|
}
|
|
618
603
|
if (command === void 0) {
|
|
619
|
-
refuse("The formatting command is unavailable.", {
|
|
604
|
+
refuse("The formatting command is unavailable.", { condition: CANONICAL_CONDITION.invalidRequest });
|
|
620
605
|
return;
|
|
621
606
|
}
|
|
622
607
|
if (executeCanonicalCommand(command) === void 0) command(view.state, (transaction) => view?.dispatch(transaction), view);
|
|
@@ -626,7 +611,7 @@ const createHiddenEditorManager = (hostDeps) => {
|
|
|
626
611
|
syncCanonicalMode();
|
|
627
612
|
if (type === "deleteTable") {
|
|
628
613
|
const command = deps.getExtensionManager()?.getCommand("deleteTable")?.();
|
|
629
|
-
if (command === void 0) refuse("Deleting the table is unavailable.", {
|
|
614
|
+
if (command === void 0) refuse("Deleting the table is unavailable.", { condition: CANONICAL_CONDITION.invalidRequest });
|
|
630
615
|
else executeCanonicalCommand(command);
|
|
631
616
|
return;
|
|
632
617
|
}
|
|
@@ -1391,7 +1376,7 @@ const createHiddenEditorManager = (hostDeps) => {
|
|
|
1391
1376
|
const command = deps.getExtensionManager()?.getCommand("deleteTable")?.();
|
|
1392
1377
|
const intents = command === void 0 ? void 0 : getCanonicalCommandIntents(command, storyView.state);
|
|
1393
1378
|
if (intents === void 0) {
|
|
1394
|
-
refuse("Deleting the table is unavailable.", {
|
|
1379
|
+
refuse("Deleting the table is unavailable.", { condition: CANONICAL_CONDITION.invalidRequest });
|
|
1395
1380
|
return false;
|
|
1396
1381
|
}
|
|
1397
1382
|
prepared = prepareCanonicalCommands(session, storyView.state, intents, story);
|
|
@@ -20,4 +20,12 @@ export declare const bulletMarkerFontName: (formatting: {
|
|
|
20
20
|
* symbol-font code.
|
|
21
21
|
*/
|
|
22
22
|
export declare const convertBulletToUnicode: (bulletChar: string, fontName?: string) => string;
|
|
23
|
+
/**
|
|
24
|
+
* The text face a bullet level's mark is a character of, when it is one: the
|
|
25
|
+
* mark then looks as it does only in that face (the customary `o` of a
|
|
26
|
+
* monospace face). Undefined for a level that names no face, for a symbol
|
|
27
|
+
* face, and for a code a text face has no character for, which is read as
|
|
28
|
+
* the shape it stands in for.
|
|
29
|
+
*/
|
|
30
|
+
export declare const bulletTextFace: (bulletChar: string, fontName: string | undefined) => string | undefined;
|
|
23
31
|
export {};
|
|
@@ -23,4 +23,12 @@ const convertBulletToUnicode = (bulletChar, fontName) => {
|
|
|
23
23
|
if (charCode < 32 || charCode >= 127 && charCode < 160) return "•";
|
|
24
24
|
return bulletChar;
|
|
25
25
|
};
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* The text face a bullet level's mark is a character of, when it is one: the
|
|
28
|
+
* mark then looks as it does only in that face (the customary `o` of a
|
|
29
|
+
* monospace face). Undefined for a level that names no face, for a symbol
|
|
30
|
+
* face, and for a code a text face has no character for, which is read as
|
|
31
|
+
* the shape it stands in for.
|
|
32
|
+
*/
|
|
33
|
+
const bulletTextFace = (bulletChar, fontName) => fontName !== void 0 && !isOoxmlSymbolFont(fontName) && bulletChar !== "" && convertBulletToUnicode(bulletChar, fontName) === bulletChar ? fontName : void 0;
|
|
34
|
+
export { bulletMarkerFontName, bulletTextFace, convertBulletToUnicode };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { toArrayBuffer } from "../utils/docxInput.js";
|
|
2
2
|
import { DOCX_CONTAINER_TYPES, detectDocxContainerType } from "./encryption/containerFormat.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { isDigitallySignedPackage, planPackageParaIds, signedPackageMutationError, writePackageParts } from "./ensureParaIds.js";
|
|
4
|
+
import { uniqueControlIdUpdates } from "./ensureUniqueControlIds.js";
|
|
5
|
+
import { uniqueRevisionIdUpdates } from "./ensureUniqueRevisionIds.js";
|
|
6
6
|
import { keepNormalizedSourceParts } from "./normalizedSourceParts.js";
|
|
7
7
|
import { MalformedPackageRelationshipsError } from "./packageRelationshipsRoot.js";
|
|
8
8
|
import { parseDocx } from "./parser.js";
|
|
@@ -30,6 +30,27 @@ const prepareCanonicalDocxInput = async (input) => {
|
|
|
30
30
|
const source = await prepareCanonicalDocxSource(input);
|
|
31
31
|
return source.isErr() ? Result.err(source.error) : Result.ok(source.value.docx);
|
|
32
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* Every id normalization of a package over one read of its parts: paragraph
|
|
35
|
+
* ids, then content control ids, then revision ids, each pass reading the
|
|
36
|
+
* texts the one before wrote. The package is written once, if at all.
|
|
37
|
+
*/
|
|
38
|
+
const normalizeCanonicalPackage = async (bytes) => {
|
|
39
|
+
const paragraphs = await planPackageParaIds(bytes);
|
|
40
|
+
const signed = isDigitallySignedPackage(paragraphs.entries);
|
|
41
|
+
if (signed && paragraphs.updates.size > 0) throw signedPackageMutationError();
|
|
42
|
+
const texts = new Map(paragraphs.partTexts);
|
|
43
|
+
const updates = new Map(paragraphs.updates);
|
|
44
|
+
for (const [path, text] of updates) texts.set(path, text);
|
|
45
|
+
if (!signed) for (const pass of [uniqueControlIdUpdates, uniqueRevisionIdUpdates]) for (const [path, text] of pass(texts)?.updates ?? []) {
|
|
46
|
+
texts.set(path, text);
|
|
47
|
+
updates.set(path, text);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
docx: updates.size === 0 ? new Uint8Array(bytes) : await writePackageParts(bytes, updates),
|
|
51
|
+
mintedParaIds: new Set(paragraphs.mintedParaIds)
|
|
52
|
+
};
|
|
53
|
+
};
|
|
33
54
|
/** {@link prepareCanonicalDocxInput}, with the paragraph ids it minted. */
|
|
34
55
|
const prepareCanonicalDocxSource = async (input) => {
|
|
35
56
|
const bytes = await Result.tryPromise({
|
|
@@ -42,14 +63,7 @@ const prepareCanonicalDocxSource = async (input) => {
|
|
|
42
63
|
if (bytes.isErr()) return Result.err(bytes.error);
|
|
43
64
|
if (detectDocxContainerType(bytes.value) === DOCX_CONTAINER_TYPES.CFB) return Result.err(new CanonicalDocxInputError({ message: "Password-protected documents are unavailable in the experimental canonical session." }));
|
|
44
65
|
return Result.tryPromise({
|
|
45
|
-
try:
|
|
46
|
-
const paragraphs = await ensureParaIds(bytes.value);
|
|
47
|
-
const { docx } = await ensureUniqueRevisionIds((await ensureUniqueControlIds(paragraphs.docx)).docx);
|
|
48
|
-
return {
|
|
49
|
-
docx,
|
|
50
|
-
mintedParaIds: new Set(paragraphs.mintedParaIds)
|
|
51
|
-
};
|
|
52
|
-
},
|
|
66
|
+
try: () => normalizeCanonicalPackage(bytes.value),
|
|
53
67
|
catch: (cause) => malformedPartCause(cause) ?? new CanonicalDocxInputError({
|
|
54
68
|
message: cause instanceof Error ? cause.message : "Canonical document normalization failed.",
|
|
55
69
|
cause
|
|
@@ -74,6 +74,8 @@ export declare const paragraphMintKeys: (xml: string) => readonly string[] | und
|
|
|
74
74
|
* keyed on the ordinal alone.
|
|
75
75
|
*/
|
|
76
76
|
export declare const mintParaId: (context: MintContext, partPath: string, ordinal: number, keys: readonly string[] | undefined) => string;
|
|
77
|
+
/** Whether a package with these part paths carries a digital signature. */
|
|
78
|
+
export declare const isDigitallySignedPackage: (entries: readonly string[]) => boolean;
|
|
77
79
|
type PackageStoryPartsOptions = {
|
|
78
80
|
documentPartName: string;
|
|
79
81
|
/** The main part's relationships part, when the package has one. */
|
|
@@ -108,6 +110,35 @@ export type UnnamedParagraphIds = {
|
|
|
108
110
|
* it. A repeated authored id is left for its readers to judge.
|
|
109
111
|
*/
|
|
110
112
|
export declare const assignPackageParaIds: (options: AssignPackageParaIdsOptions) => ReadonlyMap<string, UnnamedParagraphIds>;
|
|
113
|
+
/**
|
|
114
|
+
* A package's XML parts as one bounded read decoded them, and the texts that
|
|
115
|
+
* write the paragraph ids {@link ensureParaIds} would write.
|
|
116
|
+
*/
|
|
117
|
+
export type PackageParaIdPlan = {
|
|
118
|
+
/** The package's part paths. */
|
|
119
|
+
entries: readonly string[];
|
|
120
|
+
/** Each XML part the read decoded, by path, as the package holds it. */
|
|
121
|
+
partTexts: ReadonlyMap<string, string>;
|
|
122
|
+
/** The part texts that write the ids, by path. */
|
|
123
|
+
updates: ReadonlyMap<string, string>;
|
|
124
|
+
/** Every id the texts write, in scan order. */
|
|
125
|
+
mintedParaIds: readonly string[];
|
|
126
|
+
assigned: number;
|
|
127
|
+
deduplicated: number;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Read a `.docx` buffer's XML parts once and plan the paragraph ids
|
|
131
|
+
* {@link ensureParaIds} writes, without writing them. Failures are surfaced
|
|
132
|
+
* as {@link EnsureParaIdsError}, as {@link ensureParaIds} surfaces them.
|
|
133
|
+
*/
|
|
134
|
+
export declare const planPackageParaIds: (docx: Uint8Array | ArrayBuffer) => Promise<PackageParaIdPlan>;
|
|
135
|
+
/** The error for a normalization that would invalidate a package's signatures. */
|
|
136
|
+
export declare const signedPackageMutationError: () => EnsureParaIdsError;
|
|
137
|
+
/**
|
|
138
|
+
* `docx` with each part of `updates` written as its text, in one write of the
|
|
139
|
+
* package. Every other entry keeps its stored bytes.
|
|
140
|
+
*/
|
|
141
|
+
export declare const writePackageParts: (docx: Uint8Array | ArrayBuffer, updates: ReadonlyMap<string, string>) => Promise<Uint8Array>;
|
|
111
142
|
/**
|
|
112
143
|
* Backfill `w14:paraId` on every paragraph of a `.docx` buffer. All package,
|
|
113
144
|
* XML, and compression failures are surfaced as {@link EnsureParaIdsError}.
|
|
@@ -538,7 +538,8 @@ const relatedStoryParts = (rels, relsPath, entries) => {
|
|
|
538
538
|
/** OPC part names are case-insensitive; compare lowercased. */
|
|
539
539
|
const matchesTargetPattern = (path) => TARGET_PART_PATTERNS.some((pattern) => pattern.test(path.toLowerCase()));
|
|
540
540
|
const toUint8Array = (docx) => docx instanceof Uint8Array ? docx : new Uint8Array(docx);
|
|
541
|
-
|
|
541
|
+
/** Whether a package with these part paths carries a digital signature. */
|
|
542
|
+
const isDigitallySignedPackage = (entries) => entries.some((path) => path.toLowerCase().startsWith(DIGITAL_SIGNATURE_PART_PREFIX));
|
|
542
543
|
/** The parts besides the main one whose paragraphs get ids. */
|
|
543
544
|
const packageStoryParts = ({ documentPartName, documentRels, entries }) => {
|
|
544
545
|
const storyParts = relatedStoryParts(documentRels, relationshipsPathFor(documentPartName), entries);
|
|
@@ -618,12 +619,7 @@ const packageParaIdUpdates = (partTexts, parts) => {
|
|
|
618
619
|
deduplicated
|
|
619
620
|
};
|
|
620
621
|
};
|
|
621
|
-
|
|
622
|
-
* Backfill `w14:paraId` on every paragraph of a `.docx` buffer. See the
|
|
623
|
-
* module doc for the exact contract. Throws {@link EnsureParaIdsError} when
|
|
624
|
-
* the buffer is not a WordprocessingML package or a part is malformed.
|
|
625
|
-
*/
|
|
626
|
-
const ensureParaIdsInternal = async (docx, options) => {
|
|
622
|
+
const planPackageParaIdsInternal = async (docx) => {
|
|
627
623
|
const archive = await loadDocxArchive(docx);
|
|
628
624
|
const relsPath = archive.entries.find((name) => name.toLowerCase() === PACKAGE_RELATIONSHIPS_PART);
|
|
629
625
|
const rels = relsPath === void 0 ? null : await archive.readEntryString(relsPath);
|
|
@@ -646,30 +642,67 @@ const ensureParaIdsInternal = async (docx, options) => {
|
|
|
646
642
|
const text = await archive.readEntryString(name);
|
|
647
643
|
if (text !== null) partTexts.set(name, text);
|
|
648
644
|
}
|
|
649
|
-
|
|
645
|
+
return {
|
|
646
|
+
entries: archive.entries,
|
|
650
647
|
partTexts,
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
assigned: 0,
|
|
657
|
-
deduplicated: 0,
|
|
658
|
-
alreadyComplete: true,
|
|
659
|
-
mintedParaIds: []
|
|
648
|
+
...packageParaIdUpdates(partTexts, scanPackageParaIds({
|
|
649
|
+
partTexts,
|
|
650
|
+
documentPartName,
|
|
651
|
+
isTargetPart
|
|
652
|
+
}))
|
|
660
653
|
};
|
|
654
|
+
};
|
|
655
|
+
/** Run `normalize`, surfacing every failure as an {@link EnsureParaIdsError}. */
|
|
656
|
+
const withEnsureParaIdsErrors = async (normalize) => {
|
|
657
|
+
try {
|
|
658
|
+
return await normalize();
|
|
659
|
+
} catch (error) {
|
|
660
|
+
if (error instanceof EnsureParaIdsError) throw error;
|
|
661
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
662
|
+
throw createEnsureParaIdsError(`Failed to normalize paragraph IDs: ${message}`, error);
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
/**
|
|
666
|
+
* Read a `.docx` buffer's XML parts once and plan the paragraph ids
|
|
667
|
+
* {@link ensureParaIds} writes, without writing them. Failures are surfaced
|
|
668
|
+
* as {@link EnsureParaIdsError}, as {@link ensureParaIds} surfaces them.
|
|
669
|
+
*/
|
|
670
|
+
const planPackageParaIds = (docx) => withEnsureParaIdsErrors(() => planPackageParaIdsInternal(docx));
|
|
671
|
+
/** The error for a normalization that would invalidate a package's signatures. */
|
|
672
|
+
const signedPackageMutationError = () => new EnsureParaIdsError({
|
|
673
|
+
reason: ENSURE_PARA_IDS_REASONS.SIGNED_PACKAGE,
|
|
674
|
+
message: "Refusing to normalize a digitally signed package because rewriting OOXML invalidates its signatures. Warn the user and pass allowSignedPackageMutation only if invalidation is acceptable."
|
|
675
|
+
});
|
|
676
|
+
/**
|
|
677
|
+
* `docx` with each part of `updates` written as its text, in one write of the
|
|
678
|
+
* package. Every other entry keeps its stored bytes.
|
|
679
|
+
*/
|
|
680
|
+
const writePackageParts = async (docx, updates) => {
|
|
661
681
|
const zip = await ZipArchive.read(docx);
|
|
662
|
-
if (hasDigitalSignatureParts(zip) && options.allowSignedPackageMutation !== true) throw new EnsureParaIdsError({
|
|
663
|
-
reason: ENSURE_PARA_IDS_REASONS.SIGNED_PACKAGE,
|
|
664
|
-
message: "Refusing to normalize a digitally signed package because rewriting OOXML invalidates its signatures. Warn the user and pass allowSignedPackageMutation only if invalidation is acceptable."
|
|
665
|
-
});
|
|
666
682
|
for (const [partPath, content] of updates) {
|
|
667
683
|
const sourceEntry = zip.entry(partPath);
|
|
668
684
|
if (sourceEntry === void 0) throw createEnsureParaIdsError(`Package part disappeared during normalization: ${partPath}`);
|
|
669
685
|
zip.set(partPath, content, { modified: sourceEntry.modified });
|
|
670
686
|
}
|
|
687
|
+
return await zip.generate({ level: 6 });
|
|
688
|
+
};
|
|
689
|
+
/**
|
|
690
|
+
* Backfill `w14:paraId` on every paragraph of a `.docx` buffer. See the
|
|
691
|
+
* module doc for the exact contract. Throws {@link EnsureParaIdsError} when
|
|
692
|
+
* the buffer is not a WordprocessingML package or a part is malformed.
|
|
693
|
+
*/
|
|
694
|
+
const ensureParaIdsInternal = async (docx, options) => {
|
|
695
|
+
const { entries, updates, mintedParaIds, assigned, deduplicated } = await planPackageParaIdsInternal(docx);
|
|
696
|
+
if (updates.size === 0) return {
|
|
697
|
+
docx: toUint8Array(docx),
|
|
698
|
+
assigned: 0,
|
|
699
|
+
deduplicated: 0,
|
|
700
|
+
alreadyComplete: true,
|
|
701
|
+
mintedParaIds: []
|
|
702
|
+
};
|
|
703
|
+
if (isDigitallySignedPackage(entries) && options.allowSignedPackageMutation !== true) throw signedPackageMutationError();
|
|
671
704
|
return {
|
|
672
|
-
docx: await
|
|
705
|
+
docx: await writePackageParts(docx, updates),
|
|
673
706
|
assigned,
|
|
674
707
|
deduplicated,
|
|
675
708
|
alreadyComplete: false,
|
|
@@ -680,13 +713,5 @@ const ensureParaIdsInternal = async (docx, options) => {
|
|
|
680
713
|
* Backfill `w14:paraId` on every paragraph of a `.docx` buffer. All package,
|
|
681
714
|
* XML, and compression failures are surfaced as {@link EnsureParaIdsError}.
|
|
682
715
|
*/
|
|
683
|
-
const ensureParaIds =
|
|
684
|
-
|
|
685
|
-
return await ensureParaIdsInternal(docx, options);
|
|
686
|
-
} catch (error) {
|
|
687
|
-
if (error instanceof EnsureParaIdsError) throw error;
|
|
688
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
689
|
-
throw createEnsureParaIdsError(`Failed to normalize paragraph IDs: ${message}`, error);
|
|
690
|
-
}
|
|
691
|
-
};
|
|
692
|
-
export { ENSURE_PARA_IDS_REASONS, EnsureParaIdsError, assignPackageParaIds, collectExistingParaIds, ensureParaIds, isWordprocessingElement, mintParaId, packageStoryParts, paragraphMintKeys, storyParagraphSlots };
|
|
716
|
+
const ensureParaIds = (docx, options = {}) => withEnsureParaIdsErrors(() => ensureParaIdsInternal(docx, options));
|
|
717
|
+
export { ENSURE_PARA_IDS_REASONS, EnsureParaIdsError, assignPackageParaIds, collectExistingParaIds, ensureParaIds, isDigitallySignedPackage, isWordprocessingElement, mintParaId, packageStoryParts, paragraphMintKeys, planPackageParaIds, signedPackageMutationError, storyParagraphSlots, writePackageParts };
|
|
@@ -12,6 +12,19 @@
|
|
|
12
12
|
* under the same ids for older readers, and the parser reads one of the two,
|
|
13
13
|
* so a fallback's ids are reserved but never counted as a repeat.
|
|
14
14
|
*/
|
|
15
|
+
/** Part texts that renumber repeated ids, and how many they renumber. */
|
|
16
|
+
export type PackageControlIdUpdates = {
|
|
17
|
+
/** The rewritten text of each part that repeats an id, by path. */
|
|
18
|
+
updates: Map<string, string>;
|
|
19
|
+
reminted: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The part texts that give each content control after the first carrying an
|
|
23
|
+
* id an id of its own; `undefined` when a part does not scan, since it may
|
|
24
|
+
* hold ids a new one would repeat. The body's controls win the
|
|
25
|
+
* first-occurrence rule.
|
|
26
|
+
*/
|
|
27
|
+
export declare const uniqueControlIdUpdates: (partTexts: ReadonlyMap<string, string>) => PackageControlIdUpdates | undefined;
|
|
15
28
|
export type EnsureUniqueControlIdsResult = {
|
|
16
29
|
/** The normalized `.docx`; the input bytes when nothing repeated. */
|
|
17
30
|
docx: Uint8Array;
|
|
@@ -46,33 +46,25 @@ const controlIdsIn = (xml) => {
|
|
|
46
46
|
}
|
|
47
47
|
}).status === "scanned" ? out : void 0;
|
|
48
48
|
};
|
|
49
|
+
/** The story parts of `paths`, the body first, then the rest in a fixed order. */
|
|
50
|
+
const storyPartsInOrder = (paths) => [...paths].filter((path) => STORY_PART_PATTERN.test(path)).sort((a, b) => {
|
|
51
|
+
const body = (path) => path.toLowerCase() === "word/document.xml" ? 0 : 1;
|
|
52
|
+
return body(a) - body(b) || a.localeCompare(b);
|
|
53
|
+
});
|
|
49
54
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
55
|
+
* The part texts that give each content control after the first carrying an
|
|
56
|
+
* id an id of its own; `undefined` when a part does not scan, since it may
|
|
57
|
+
* hold ids a new one would repeat. The body's controls win the
|
|
58
|
+
* first-occurrence rule.
|
|
52
59
|
*/
|
|
53
|
-
const
|
|
54
|
-
const zip = await ZipArchive.read(docx);
|
|
55
|
-
const entries = zip.entries();
|
|
56
|
-
if (entries.some(({ path }) => path.toLowerCase().startsWith(DIGITAL_SIGNATURE_PART_PREFIX))) return {
|
|
57
|
-
docx,
|
|
58
|
-
reminted: 0
|
|
59
|
-
};
|
|
60
|
-
const parts = entries.map(({ path }) => path).filter((path) => STORY_PART_PATTERN.test(path)).sort((a, b) => {
|
|
61
|
-
const body = (path) => path.toLowerCase() === "word/document.xml" ? 0 : 1;
|
|
62
|
-
return body(a) - body(b) || a.localeCompare(b);
|
|
63
|
-
});
|
|
64
|
-
const texts = /* @__PURE__ */ new Map();
|
|
60
|
+
const uniqueControlIdUpdates = (partTexts) => {
|
|
65
61
|
const ids = /* @__PURE__ */ new Map();
|
|
66
62
|
const taken = /* @__PURE__ */ new Set();
|
|
67
|
-
for (const path of
|
|
68
|
-
const xml =
|
|
69
|
-
if (
|
|
63
|
+
for (const path of storyPartsInOrder(partTexts.keys())) {
|
|
64
|
+
const xml = partTexts.get(path);
|
|
65
|
+
if (!xml.includes("sdtPr")) continue;
|
|
70
66
|
const found = controlIdsIn(xml);
|
|
71
|
-
if (found === void 0) return
|
|
72
|
-
docx,
|
|
73
|
-
reminted: 0
|
|
74
|
-
};
|
|
75
|
-
texts.set(path, xml);
|
|
67
|
+
if (found === void 0) return void 0;
|
|
76
68
|
ids.set(path, found);
|
|
77
69
|
for (const { value } of found) taken.add(value);
|
|
78
70
|
}
|
|
@@ -83,6 +75,7 @@ const ensureUniqueControlIds = async (docx) => {
|
|
|
83
75
|
return next;
|
|
84
76
|
};
|
|
85
77
|
const seen = /* @__PURE__ */ new Set();
|
|
78
|
+
const updates = /* @__PURE__ */ new Map();
|
|
86
79
|
let reminted = 0;
|
|
87
80
|
for (const [path, found] of ids) {
|
|
88
81
|
const edits = [];
|
|
@@ -99,19 +92,44 @@ const ensureUniqueControlIds = async (docx) => {
|
|
|
99
92
|
});
|
|
100
93
|
}
|
|
101
94
|
if (edits.length === 0) continue;
|
|
102
|
-
const patched = spliceXml(
|
|
95
|
+
const patched = spliceXml(partTexts.get(path), edits);
|
|
103
96
|
if (patched === null) continue;
|
|
104
|
-
|
|
105
|
-
zip.set(path, patched, source ? { modified: source.modified } : {});
|
|
97
|
+
updates.set(path, patched);
|
|
106
98
|
reminted += edits.length;
|
|
107
99
|
}
|
|
108
|
-
|
|
109
|
-
|
|
100
|
+
return {
|
|
101
|
+
updates,
|
|
110
102
|
reminted
|
|
111
103
|
};
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Give each content control after the first carrying an id an id of its own.
|
|
107
|
+
* A signed package, or a part that does not scan, is left as it is.
|
|
108
|
+
*/
|
|
109
|
+
const ensureUniqueControlIds = async (docx) => {
|
|
110
|
+
const zip = await ZipArchive.read(docx);
|
|
111
|
+
const entries = zip.entries();
|
|
112
|
+
if (entries.some(({ path }) => path.toLowerCase().startsWith(DIGITAL_SIGNATURE_PART_PREFIX))) return {
|
|
113
|
+
docx,
|
|
114
|
+
reminted: 0
|
|
115
|
+
};
|
|
116
|
+
const texts = /* @__PURE__ */ new Map();
|
|
117
|
+
for (const path of storyPartsInOrder(entries.map((entry) => entry.path))) {
|
|
118
|
+
const xml = await zip.entry(path)?.text();
|
|
119
|
+
if (xml !== void 0) texts.set(path, xml);
|
|
120
|
+
}
|
|
121
|
+
const found = uniqueControlIdUpdates(texts);
|
|
122
|
+
if (found === void 0 || found.reminted === 0) return {
|
|
123
|
+
docx,
|
|
124
|
+
reminted: 0
|
|
125
|
+
};
|
|
126
|
+
for (const [path, patched] of found.updates) {
|
|
127
|
+
const source = zip.entry(path);
|
|
128
|
+
zip.set(path, patched, source ? { modified: source.modified } : {});
|
|
129
|
+
}
|
|
112
130
|
return {
|
|
113
131
|
docx: await zip.generate({ level: 6 }),
|
|
114
|
-
reminted
|
|
132
|
+
reminted: found.reminted
|
|
115
133
|
};
|
|
116
134
|
};
|
|
117
|
-
export { ensureUniqueControlIds };
|
|
135
|
+
export { ensureUniqueControlIds, uniqueControlIdUpdates };
|
|
@@ -15,6 +15,19 @@
|
|
|
15
15
|
* under the same ids for older readers, and the parser reads one of the two,
|
|
16
16
|
* so a fallback's ids are reserved but never counted as a repeat.
|
|
17
17
|
*/
|
|
18
|
+
/** Part texts that renumber repeated ids, and how many they renumber. */
|
|
19
|
+
export type PackageRevisionIdUpdates = {
|
|
20
|
+
/** The rewritten text of each part that repeats an id, by path. */
|
|
21
|
+
updates: Map<string, string>;
|
|
22
|
+
reminted: number;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The part texts that give each revision record after the first carrying an
|
|
26
|
+
* id an id of its own; `undefined` when a part does not scan, since it may
|
|
27
|
+
* hold ids a new one would repeat. The body's records win the
|
|
28
|
+
* first-occurrence rule.
|
|
29
|
+
*/
|
|
30
|
+
export declare const uniqueRevisionIdUpdates: (partTexts: ReadonlyMap<string, string>) => PackageRevisionIdUpdates | undefined;
|
|
18
31
|
export type EnsureUniqueRevisionIdsResult = {
|
|
19
32
|
/** The normalized `.docx`; the input bytes when nothing repeated. */
|
|
20
33
|
docx: Uint8Array;
|
|
@@ -69,33 +69,30 @@ const revisionIdsIn = (xml) => {
|
|
|
69
69
|
}).status === "scanned" ? out : void 0;
|
|
70
70
|
};
|
|
71
71
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
72
|
+
* A start tag naming a revision record under any prefix. A part none matches
|
|
73
|
+
* holds no record, so it needs no scan.
|
|
74
74
|
*/
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
75
|
+
const REVISION_RECORD_TAG = new RegExp(`<(?:[^\\s/>:]+:)?(?:${[...REVISION_RECORDS].join("|")})[\\s/>]`, "u");
|
|
76
|
+
/** The story parts of `paths`, the body first, then the rest in a fixed order. */
|
|
77
|
+
const storyPartsInOrder = (paths) => [...paths].filter((path) => STORY_PART_PATTERN.test(path)).sort((a, b) => {
|
|
78
|
+
const body = (path) => path.toLowerCase() === "word/document.xml" ? 0 : 1;
|
|
79
|
+
return body(a) - body(b) || a.localeCompare(b);
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* The part texts that give each revision record after the first carrying an
|
|
83
|
+
* id an id of its own; `undefined` when a part does not scan, since it may
|
|
84
|
+
* hold ids a new one would repeat. The body's records win the
|
|
85
|
+
* first-occurrence rule.
|
|
86
|
+
*/
|
|
87
|
+
const uniqueRevisionIdUpdates = (partTexts) => {
|
|
87
88
|
const ids = /* @__PURE__ */ new Map();
|
|
88
89
|
const taken = /* @__PURE__ */ new Set();
|
|
89
|
-
for (const path of
|
|
90
|
-
const xml =
|
|
91
|
-
if (xml
|
|
90
|
+
for (const path of storyPartsInOrder(partTexts.keys())) {
|
|
91
|
+
const xml = partTexts.get(path);
|
|
92
|
+
if (!REVISION_RECORD_TAG.test(xml)) continue;
|
|
92
93
|
const found = revisionIdsIn(xml);
|
|
93
|
-
if (found === void 0) return
|
|
94
|
-
docx,
|
|
95
|
-
reminted: 0
|
|
96
|
-
};
|
|
94
|
+
if (found === void 0) return void 0;
|
|
97
95
|
if (found.length === 0) continue;
|
|
98
|
-
texts.set(path, xml);
|
|
99
96
|
ids.set(path, found);
|
|
100
97
|
for (const { value } of found) taken.add(value);
|
|
101
98
|
}
|
|
@@ -106,6 +103,7 @@ const ensureUniqueRevisionIds = async (docx) => {
|
|
|
106
103
|
return next;
|
|
107
104
|
};
|
|
108
105
|
const seen = /* @__PURE__ */ new Set();
|
|
106
|
+
const updates = /* @__PURE__ */ new Map();
|
|
109
107
|
let reminted = 0;
|
|
110
108
|
for (const [path, found] of ids) {
|
|
111
109
|
const edits = [];
|
|
@@ -122,19 +120,44 @@ const ensureUniqueRevisionIds = async (docx) => {
|
|
|
122
120
|
});
|
|
123
121
|
}
|
|
124
122
|
if (edits.length === 0) continue;
|
|
125
|
-
const patched = spliceXml(
|
|
123
|
+
const patched = spliceXml(partTexts.get(path), edits);
|
|
126
124
|
if (patched === null) continue;
|
|
127
|
-
|
|
128
|
-
zip.set(path, patched, source ? { modified: source.modified } : {});
|
|
125
|
+
updates.set(path, patched);
|
|
129
126
|
reminted += edits.length;
|
|
130
127
|
}
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
return {
|
|
129
|
+
updates,
|
|
133
130
|
reminted
|
|
134
131
|
};
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Give each revision record after the first carrying an id an id of its own.
|
|
135
|
+
* A signed package, or a part that does not scan, is left as it is.
|
|
136
|
+
*/
|
|
137
|
+
const ensureUniqueRevisionIds = async (docx) => {
|
|
138
|
+
const zip = await ZipArchive.read(docx);
|
|
139
|
+
const entries = zip.entries();
|
|
140
|
+
if (entries.some(({ path }) => path.toLowerCase().startsWith(DIGITAL_SIGNATURE_PART_PREFIX))) return {
|
|
141
|
+
docx,
|
|
142
|
+
reminted: 0
|
|
143
|
+
};
|
|
144
|
+
const texts = /* @__PURE__ */ new Map();
|
|
145
|
+
for (const path of storyPartsInOrder(entries.map((entry) => entry.path))) {
|
|
146
|
+
const xml = await zip.entry(path)?.text();
|
|
147
|
+
if (xml !== void 0) texts.set(path, xml);
|
|
148
|
+
}
|
|
149
|
+
const found = uniqueRevisionIdUpdates(texts);
|
|
150
|
+
if (found === void 0 || found.reminted === 0) return {
|
|
151
|
+
docx,
|
|
152
|
+
reminted: 0
|
|
153
|
+
};
|
|
154
|
+
for (const [path, patched] of found.updates) {
|
|
155
|
+
const source = zip.entry(path);
|
|
156
|
+
zip.set(path, patched, source ? { modified: source.modified } : {});
|
|
157
|
+
}
|
|
135
158
|
return {
|
|
136
159
|
docx: await zip.generate({ level: 6 }),
|
|
137
|
-
reminted
|
|
160
|
+
reminted: found.reminted
|
|
138
161
|
};
|
|
139
162
|
};
|
|
140
|
-
export { ensureUniqueRevisionIds };
|
|
163
|
+
export { ensureUniqueRevisionIds, uniqueRevisionIdUpdates };
|