@stll/folio-core 0.72.0 → 0.73.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/README.md +12 -6
- package/dist/ai-edits/apply.d.ts +6 -114
- package/dist/ai-edits/apply.js +9 -131
- package/dist/ai-edits/applyTypes.d.ts +85 -0
- package/dist/ai-edits/applyTypes.js +24 -0
- package/dist/ai-edits/clean-text.d.ts +7 -1
- package/dist/ai-edits/clean-text.js +23 -2
- package/dist/ai-edits/finalSectionReview.d.ts +1 -1
- package/dist/ai-edits/headless.d.ts +29 -147
- package/dist/ai-edits/headless.js +213 -1634
- package/dist/ai-edits/index.d.ts +3 -3
- package/dist/ai-edits/index.js +2 -3
- package/dist/ai-edits/pending-suggestions.d.ts +14 -4
- package/dist/ai-edits/pending-suggestions.js +183 -90
- package/dist/ai-edits/result-validation.js +1 -1
- package/dist/ai-edits/retired-comment-receipts.d.ts +1 -1
- package/dist/ai-edits/snapshot.js +8 -3
- package/dist/ai-edits/table-cell-mutations.d.ts +1 -1
- package/dist/ai-edits/table-cell-mutations.js +1 -1
- package/dist/ai-edits/table-geometry.d.ts +1 -1
- package/dist/ai-edits/table-geometry.js +1 -1
- package/dist/ai-edits/table-row-column-mutations.d.ts +1 -1
- package/dist/ai-edits/table-row-column-mutations.js +1 -1
- package/dist/ai-edits/tableNodes.d.ts +38 -0
- package/dist/ai-edits/tableNodes.js +86 -0
- package/dist/ai-edits/tracked-batch-fidelity.d.ts +1 -1
- package/dist/ai-edits/undefinedReferences.d.ts +7 -0
- package/dist/canonical/batchCommentReferences.js +1 -0
- package/dist/canonical/canonicalInlineProjection.js +1 -1
- package/dist/canonical/canonicalPublicOperations.js +228 -64
- package/dist/canonical/canonicalSession.d.ts +9 -1
- package/dist/canonical/canonicalSession.js +95 -15
- package/dist/compare/compare.d.ts +1 -1
- package/dist/compare/inline-atoms-model.d.ts +1 -1
- package/dist/compare/inline-atoms.d.ts +2 -2
- package/dist/compare/inline-provenance-model.d.ts +1 -1
- package/dist/compare/inline-provenance.d.ts +2 -2
- package/dist/compat/eigenpal.d.ts +3 -3
- package/dist/compat/eigenpal.js +2 -3
- package/dist/controller/canonicalClipboard.d.ts +13 -3
- package/dist/controller/canonicalClipboard.js +185 -49
- package/dist/controller/canonicalClipboardResources.d.ts +9 -1
- package/dist/controller/canonicalClipboardResources.js +22 -2
- package/dist/controller/canonicalReview.d.ts +1 -1
- package/dist/controller/canonicalReview.js +1 -1
- package/dist/controller/canonicalStructure.js +1 -1
- package/dist/controller/canonicalWatermark.d.ts +10 -0
- package/dist/controller/canonicalWatermark.js +37 -0
- package/dist/controller/hiddenEditorApi.d.ts +1 -1
- package/dist/controller/hiddenEditorManager.d.ts +4 -3
- package/dist/controller/hiddenEditorManager.js +75 -15
- package/dist/document-operations-apply.d.ts +15 -0
- package/dist/document-operations-apply.js +280 -0
- package/dist/document-operations.d.ts +1 -14
- package/dist/document-operations.js +2 -279
- package/dist/docx/modelValidation.js +1 -1
- package/dist/docx/paragraphParser.js +2 -2
- package/dist/docx/paragraphPropertySource.d.ts +1 -1
- package/dist/docx/reviewResolutionProvenance.d.ts +3 -2
- package/dist/docx/reviewResolutionProvenance.js +32 -9
- package/dist/docx/revisionIdNormalization.js +30 -7
- package/dist/docx/rezip.js +1 -1
- package/dist/docx/serializer/borderSerializer.js +1 -1
- package/dist/docx/serializer/commentSerializer.js +1 -1
- package/dist/docx/serializer/documentSerializer.js +1 -1
- package/dist/docx/serializer/paragraphSerializer.js +3 -3
- package/dist/docx/serializer/runSerializer.js +1 -1
- package/dist/docx/serializer/sectionPropertiesSerializer.js +1 -1
- package/dist/docx/serializer/tableSerializer.js +5 -37
- package/dist/docx/serializer/textFormattingSerializer.js +1 -1
- package/dist/docx/serializer/trackedChangeAttributes.js +1 -1
- package/dist/docx/server/templatePlan/apply.d.ts +15 -0
- package/dist/docx/server/templatePlan/apply.js +600 -0
- package/dist/docx/server/templatePlan/blocks.d.ts +97 -0
- package/dist/docx/server/templatePlan/blocks.js +355 -0
- package/dist/docx/server/templatePlan/builders.d.ts +72 -0
- package/dist/docx/server/templatePlan/builders.js +237 -0
- package/dist/docx/server/templatePlan/emit.d.ts +118 -0
- package/dist/docx/server/templatePlan/emit.js +466 -0
- package/dist/docx/server/templatePlan/emphasis.d.ts +62 -0
- package/dist/docx/server/templatePlan/emphasis.js +173 -0
- package/dist/docx/server/templatePlan/inspect.d.ts +22 -0
- package/dist/docx/server/templatePlan/inspect.js +177 -0
- package/dist/docx/server/templatePlan/labels.d.ts +36 -0
- package/dist/docx/server/templatePlan/labels.js +126 -0
- package/dist/docx/server/templatePlan/package.d.ts +34 -0
- package/dist/docx/server/templatePlan/package.js +99 -0
- package/dist/docx/server/templatePlan/plan.d.ts +143 -0
- package/dist/docx/server/templatePlan/plan.js +841 -0
- package/dist/docx/server/templatePlan/planShape.d.ts +9 -0
- package/dist/docx/server/templatePlan/planShape.js +161 -0
- package/dist/docx/server/templatePlan/resources.d.ts +41 -0
- package/dist/docx/server/templatePlan/resources.js +136 -0
- package/dist/docx/server/templatePlan/story.d.ts +128 -0
- package/dist/docx/server/templatePlan/story.js +440 -0
- package/dist/docx/server/templatePlan/types.d.ts +406 -0
- package/dist/docx/server/templatePlan/types.js +28 -0
- package/dist/docx/server/templatePlan/verify.d.ts +40 -0
- package/dist/docx/server/templatePlan/verify.js +968 -0
- package/dist/docx/server/templatePlan/xml.d.ts +69 -0
- package/dist/docx/server/templatePlan/xml.js +119 -0
- package/dist/docx/settingsHeaderFooterUpdate.d.ts +9 -0
- package/dist/docx/settingsHeaderFooterUpdate.js +45 -10
- package/dist/docx/storyBlockReplay.js +1 -1
- package/dist/docx/structuralXmlPatch.js +9 -1
- package/dist/docx/tableGridXml.d.ts +30 -0
- package/dist/docx/tableGridXml.js +54 -0
- package/dist/docx/tableParser.js +7 -0
- package/dist/docx/xmlParser.js +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -3
- package/dist/internal/paragraphFormattingSerialization.js +1 -1
- package/dist/internal/wholeStoryRevisionResolution.js +1 -1
- package/dist/managers/ContextMenuManager.js +2 -1
- package/dist/managers/editorShortcuts.d.ts +1 -1
- package/dist/prosemirror/anchoredTextBoxes.d.ts +1 -1
- package/dist/prosemirror/canonicalCommands.d.ts +1 -1
- package/dist/prosemirror/canonicalSelectionRange.d.ts +1 -1
- package/dist/prosemirror/canonicalTableCommands.js +2 -2
- package/dist/prosemirror/commands/comments.js +3 -3
- package/dist/prosemirror/commands/contentControls.d.ts +1 -1
- package/dist/prosemirror/commands/formatPainter.d.ts +1 -1
- package/dist/prosemirror/commands/formatting.d.ts +1 -1
- package/dist/prosemirror/commands/pastePlainText.d.ts +1 -1
- package/dist/prosemirror/commands/sectionBreak.js +1 -1
- package/dist/prosemirror/commands/tableCellMergeResolution.d.ts +1 -1
- package/dist/prosemirror/commands/tableMergeFoldDecisions.d.ts +1 -1
- package/dist/prosemirror/commentRecords.d.ts +1 -1
- package/dist/prosemirror/contentControlRevisions.d.ts +1 -1
- package/dist/prosemirror/conversion/fromProseDoc.js +1 -1
- package/dist/prosemirror/documentStructureCounts.d.ts +24 -0
- package/dist/prosemirror/documentStructureCounts.js +76 -0
- package/dist/prosemirror/extensions/ExtensionManager.d.ts +1 -1
- package/dist/prosemirror/extensions/StarterKit.d.ts +1 -1
- package/dist/prosemirror/extensions/core/HistoryExtension.d.ts +1 -7
- package/dist/prosemirror/extensions/core/HistoryExtension.js +5 -13
- package/dist/prosemirror/extensions/core/ParagraphExtension.js +1 -1
- package/dist/prosemirror/extensions/core/historyCommands.d.ts +16 -0
- package/dist/prosemirror/extensions/core/historyCommands.js +19 -0
- package/dist/prosemirror/extensions/features/AutoBidiDetectionExtension.js +1 -1
- package/dist/prosemirror/extensions/features/ParaIdAllocatorExtension.d.ts +2 -35
- package/dist/prosemirror/extensions/features/ParaIdAllocatorExtension.js +4 -142
- package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.d.ts +3 -87
- package/dist/prosemirror/extensions/features/ParagraphChangeTrackerExtension.js +7 -162
- package/dist/prosemirror/extensions/features/pastedCellSections.d.ts +1 -1
- package/dist/prosemirror/extensions/features/pastedHeadingStyles.js +1 -1
- package/dist/prosemirror/extensions/marks/markUtils.d.ts +1 -1
- package/dist/prosemirror/extensions/nodes/TableExtension.d.ts +1 -1
- package/dist/prosemirror/extensions/nodes/TableExtension.js +2 -2
- package/dist/prosemirror/extensions/types.d.ts +1 -1
- package/dist/prosemirror/findReplaceSelection.d.ts +1 -1
- package/dist/prosemirror/hyperlinkRemoval.d.ts +1 -1
- package/dist/prosemirror/index.d.ts +1 -1
- package/dist/prosemirror/index.js +1 -1
- package/dist/prosemirror/insertionGaps.d.ts +1 -1
- package/dist/prosemirror/keptRangeBoundaries.d.ts +1 -1
- package/dist/prosemirror/listNumbering.d.ts +1 -1
- package/dist/prosemirror/lockedControls.d.ts +1 -1
- package/dist/prosemirror/markupViewProjection.d.ts +1 -1
- package/dist/prosemirror/noteReferenceOccurrences.d.ts +1 -1
- package/dist/prosemirror/noteReferenceReview.js +1 -1
- package/dist/prosemirror/paraIds.d.ts +46 -0
- package/dist/prosemirror/paraIds.js +152 -0
- package/dist/prosemirror/paragraphChangeTrackerMeta.d.ts +97 -0
- package/dist/prosemirror/paragraphChangeTrackerMeta.js +87 -0
- package/dist/prosemirror/paragraphMarkJoin.d.ts +1 -1
- package/dist/prosemirror/paragraphPropertyCarry.d.ts +1 -1
- package/dist/prosemirror/pastedCommentCopies.d.ts +1 -1
- package/dist/prosemirror/pastedCommentCopies.js +1 -1
- package/dist/prosemirror/plugins/createDocScanPlugin.d.ts +1 -1
- package/dist/prosemirror/plugins/documentNumbering.d.ts +1 -1
- package/dist/prosemirror/plugins/suggestionMode.d.ts +1 -1
- package/dist/prosemirror/plugins/templateDirectives.d.ts +1 -1
- package/dist/prosemirror/rebaseParagraphRunFormatting.d.ts +1 -1
- package/dist/prosemirror/rebaseParagraphRuns.d.ts +1 -1
- package/dist/prosemirror/rejoinRunCarriers.d.ts +1 -1
- package/dist/prosemirror/replacedAnnotations.d.ts +1 -1
- package/dist/prosemirror/replacedAnnotations.js +16 -1
- package/dist/prosemirror/revisionCarriers.d.ts +1 -1
- package/dist/prosemirror/runFormattingInlineCarriers.d.ts +1 -1
- package/dist/prosemirror/sectionMarkEdits.d.ts +1 -1
- package/dist/prosemirror/selectionBounds.d.ts +10 -0
- package/dist/prosemirror/selectionBounds.js +22 -0
- package/dist/prosemirror/storyListNumbering.d.ts +1 -1
- package/dist/prosemirror/tableCellPaste.d.ts +1 -1
- package/dist/prosemirror/tableGridMutation.d.ts +1 -1
- package/dist/prosemirror/textInput.d.ts +1 -1
- package/dist/server.d.ts +7 -2
- package/dist/server.js +6 -1
- package/dist/types/canonicalCapabilities.d.ts +8 -41
- package/dist/types/canonicalCapabilities.js +6 -34
- package/package.json +4 -4
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** The block manifest of a DOCX: its main story as addressable, hash-pinned blocks. */
|
|
2
|
+
import { document_d_exports } from "../../../types/document.js";
|
|
3
|
+
import { NumberingMap } from "../../numberingParser.js";
|
|
4
|
+
import { StyleMap } from "../../styleParser.js";
|
|
5
|
+
import { XmlElement } from "../../xmlParser.js";
|
|
6
|
+
import { Emphasis } from "./emphasis.js";
|
|
7
|
+
import { LabelCounter, ParagraphLabel } from "./labels.js";
|
|
8
|
+
import { LoadedPackage } from "./package.js";
|
|
9
|
+
import { ParagraphRead, StoryState } from "./story.js";
|
|
10
|
+
import { DocxBlock, DocxBlockManifest, TemplatePlanError } from "./types.js";
|
|
11
|
+
import { RawPart } from "./xml.js";
|
|
12
|
+
import { Result } from "better-result";
|
|
13
|
+
export declare const blockHash: (text: string, label: string | null) => string;
|
|
14
|
+
export type AnalyzedBlock = {
|
|
15
|
+
block: DocxBlock;
|
|
16
|
+
element: XmlElement;
|
|
17
|
+
/** Null for a table. */
|
|
18
|
+
read: ParagraphRead | null;
|
|
19
|
+
label: ParagraphLabel | null;
|
|
20
|
+
/** The table directly holding the block, when it is nested. */
|
|
21
|
+
parentId: string | null;
|
|
22
|
+
/** The top-level block the block is, or sits in. */
|
|
23
|
+
topLevelId: string;
|
|
24
|
+
};
|
|
25
|
+
export type AnalyzedStory = {
|
|
26
|
+
topLevel: AnalyzedBlock[];
|
|
27
|
+
/** Every block in document order, nested ones included. */
|
|
28
|
+
order: AnalyzedBlock[];
|
|
29
|
+
byId: Map<string, AnalyzedBlock>;
|
|
30
|
+
/** Paragraph blocks by the ordinal their read carries. */
|
|
31
|
+
paragraphs: AnalyzedBlock[];
|
|
32
|
+
state: StoryState;
|
|
33
|
+
/** `w:sectPr` elements in document order, the body's own last. */
|
|
34
|
+
sections: XmlElement[];
|
|
35
|
+
/** Names of block-level elements the walk has no reading for. */
|
|
36
|
+
unreadBlocks: string[];
|
|
37
|
+
warnings: string[];
|
|
38
|
+
};
|
|
39
|
+
type StoryResources = {
|
|
40
|
+
styles: StyleMap;
|
|
41
|
+
defaultStyleId: string | null;
|
|
42
|
+
numbering: NumberingMap | null;
|
|
43
|
+
};
|
|
44
|
+
/** The heading level a style outlines its paragraphs at, 0 being the highest. */
|
|
45
|
+
export declare const styleOutlineLevel: (style: document_d_exports.Style | undefined) => number | null;
|
|
46
|
+
type AnalyzeStoryOptions = {
|
|
47
|
+
container: XmlElement;
|
|
48
|
+
resources: StoryResources;
|
|
49
|
+
counter?: LabelCounter;
|
|
50
|
+
};
|
|
51
|
+
/** Read the blocks under `container` into ids, text, labels and hashes. */
|
|
52
|
+
export declare const analyzeStory: ({ container, resources, counter }: AnalyzeStoryOptions) => AnalyzedStory;
|
|
53
|
+
export type AnalyzedNote = {
|
|
54
|
+
id: string;
|
|
55
|
+
element: XmlElement;
|
|
56
|
+
text: string;
|
|
57
|
+
paragraphs: ParagraphRead[];
|
|
58
|
+
/** Whether the note holds a table or another block that is not a paragraph. */
|
|
59
|
+
hasOtherBlocks: boolean;
|
|
60
|
+
};
|
|
61
|
+
export type AnalyzedNotes = {
|
|
62
|
+
kind: "footnotes" | "endnotes";
|
|
63
|
+
path: string | null;
|
|
64
|
+
part: RawPart | null;
|
|
65
|
+
/** Notes with content; separators are left out. */
|
|
66
|
+
notes: AnalyzedNote[];
|
|
67
|
+
byId: Map<string, AnalyzedNote>;
|
|
68
|
+
/** Every `w:id` the part uses, separators included. */
|
|
69
|
+
usedIds: number[];
|
|
70
|
+
state: StoryState;
|
|
71
|
+
};
|
|
72
|
+
export type AnalyzedDocument = {
|
|
73
|
+
pkg: LoadedPackage;
|
|
74
|
+
document: RawPart;
|
|
75
|
+
body: XmlElement;
|
|
76
|
+
story: AnalyzedStory;
|
|
77
|
+
styles: StyleMap;
|
|
78
|
+
/** The emphasis the document defaults give every run. */
|
|
79
|
+
defaultEmphasis: Emphasis;
|
|
80
|
+
stylesPath: string | null;
|
|
81
|
+
stylesXml: string | null;
|
|
82
|
+
defaultStyleId: string | null;
|
|
83
|
+
numbering: NumberingMap | null;
|
|
84
|
+
numberingPath: string | null;
|
|
85
|
+
numberingXml: string | null;
|
|
86
|
+
footnotes: AnalyzedNotes;
|
|
87
|
+
endnotes: AnalyzedNotes;
|
|
88
|
+
manifest: DocxBlockManifest;
|
|
89
|
+
};
|
|
90
|
+
/** Open a package and analyse its main story, notes, styles and numbering. */
|
|
91
|
+
export declare const analyzeDocument: (bytes: Uint8Array) => Promise<Result<AnalyzedDocument, TemplatePlanError>>;
|
|
92
|
+
/**
|
|
93
|
+
* Read a DOCX into the blocks a template plan addresses: ids, visible text,
|
|
94
|
+
* rendered list labels, styles and a hash per block, plus its notes.
|
|
95
|
+
*/
|
|
96
|
+
export declare const analyzeDocxBlocks: (docx: Uint8Array) => Promise<Result<DocxBlockManifest, TemplatePlanError>>;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { parseNumbering } from "../../numberingParser.js";
|
|
2
|
+
import { parseStylesPackage } from "../../styleParser.js";
|
|
3
|
+
import { defaultsBaseline } from "./emphasis.js";
|
|
4
|
+
import { createLabelCounter } from "./labels.js";
|
|
5
|
+
import { guarded, loadPackage, readPartText, sha256Hex } from "./package.js";
|
|
6
|
+
import { createStoryState, endStory, readParagraph, walkBlocks } from "./story.js";
|
|
7
|
+
import { TemplatePlanError } from "./types.js";
|
|
8
|
+
import { elementChildren, isW, readRawPart, wAttr, wChild } from "./xml.js";
|
|
9
|
+
import { Result } from "better-result";
|
|
10
|
+
// src/docx/server/templatePlan/blocks.ts
|
|
11
|
+
/** The block manifest of a DOCX: its main story as addressable, hash-pinned blocks. */
|
|
12
|
+
const BLOCK_HASH_LENGTH = 12;
|
|
13
|
+
const blockHash = (text, label) => sha256Hex(`${text}\u0000${label ?? ""}`).slice(0, BLOCK_HASH_LENGTH);
|
|
14
|
+
/** The heading level a style outlines its paragraphs at, 0 being the highest. */
|
|
15
|
+
const styleOutlineLevel = (style) => {
|
|
16
|
+
const outline = style?.pPr?.outlineLevel;
|
|
17
|
+
return outline?.kind === "heading" ? outline.level : null;
|
|
18
|
+
};
|
|
19
|
+
const styleNameOf = (style) => style?.name ?? null;
|
|
20
|
+
/** Read the blocks under `container` into ids, text, labels and hashes. */
|
|
21
|
+
const analyzeStory = ({ container, resources, counter = createLabelCounter(resources) }) => {
|
|
22
|
+
const state = createStoryState();
|
|
23
|
+
const story = {
|
|
24
|
+
topLevel: [],
|
|
25
|
+
order: [],
|
|
26
|
+
byId: /* @__PURE__ */ new Map(),
|
|
27
|
+
paragraphs: [],
|
|
28
|
+
state,
|
|
29
|
+
sections: [],
|
|
30
|
+
unreadBlocks: [],
|
|
31
|
+
warnings: []
|
|
32
|
+
};
|
|
33
|
+
const tables = [];
|
|
34
|
+
let index = 0;
|
|
35
|
+
let sectionIndex = 0;
|
|
36
|
+
const unread = /* @__PURE__ */ new Map();
|
|
37
|
+
const register = (analyzed) => {
|
|
38
|
+
story.order.push(analyzed);
|
|
39
|
+
story.byId.set(analyzed.block.id, analyzed);
|
|
40
|
+
const parent = tables.at(-1);
|
|
41
|
+
if (parent === void 0) {
|
|
42
|
+
story.topLevel.push(analyzed);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
parent.cells.at(-1)?.blockIds.push(analyzed.block.id);
|
|
46
|
+
for (const open of tables) open.nested.push(analyzed.block);
|
|
47
|
+
};
|
|
48
|
+
const noteCellText = (text) => {
|
|
49
|
+
tables.at(-1)?.cellTexts.at(-1)?.push(text);
|
|
50
|
+
};
|
|
51
|
+
walkBlocks(container, {
|
|
52
|
+
paragraph: (element) => {
|
|
53
|
+
const read = readParagraph(element, state);
|
|
54
|
+
const styleId = read.styleId;
|
|
55
|
+
const style = resources.styles.get(styleId ?? resources.defaultStyleId ?? "");
|
|
56
|
+
const label = counter.next({
|
|
57
|
+
styleId,
|
|
58
|
+
numPr: read.numPr
|
|
59
|
+
});
|
|
60
|
+
const id = `p${index}`;
|
|
61
|
+
const block = {
|
|
62
|
+
id,
|
|
63
|
+
index,
|
|
64
|
+
kind: "paragraph",
|
|
65
|
+
text: read.text,
|
|
66
|
+
label: label.label,
|
|
67
|
+
numbering: label.numbering,
|
|
68
|
+
styleId,
|
|
69
|
+
styleName: styleNameOf(style),
|
|
70
|
+
outlineLevel: read.outlineLevel ?? styleOutlineLevel(style),
|
|
71
|
+
sectionIndex,
|
|
72
|
+
hash: blockHash(read.text, label.label),
|
|
73
|
+
...read.paraId === null ? {} : { paraId: read.paraId },
|
|
74
|
+
inlineObjects: read.inlineObjects
|
|
75
|
+
};
|
|
76
|
+
index += 1;
|
|
77
|
+
const analyzed = {
|
|
78
|
+
block,
|
|
79
|
+
element,
|
|
80
|
+
read,
|
|
81
|
+
label,
|
|
82
|
+
parentId: tables.at(-1)?.analyzed.block.id ?? null,
|
|
83
|
+
topLevelId: tables.at(0)?.analyzed.block.id ?? id
|
|
84
|
+
};
|
|
85
|
+
register(analyzed);
|
|
86
|
+
story.paragraphs[read.ordinal] = analyzed;
|
|
87
|
+
noteCellText(read.text);
|
|
88
|
+
if (read.hasSectionBreak) {
|
|
89
|
+
const sectPr = wChild(wChild(element, "pPr"), "sectPr");
|
|
90
|
+
if (sectPr !== null) story.sections.push(sectPr);
|
|
91
|
+
sectionIndex += 1;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
tableStart: (element) => {
|
|
95
|
+
const id = `t${index}`;
|
|
96
|
+
const properties = wChild(element, "tblPr");
|
|
97
|
+
const styleId = wAttr(wChild(properties, "tblStyle"), "val");
|
|
98
|
+
const block = {
|
|
99
|
+
id,
|
|
100
|
+
index,
|
|
101
|
+
kind: "table",
|
|
102
|
+
text: "",
|
|
103
|
+
label: null,
|
|
104
|
+
numbering: null,
|
|
105
|
+
styleId,
|
|
106
|
+
styleName: styleNameOf(resources.styles.get(styleId ?? "")),
|
|
107
|
+
outlineLevel: null,
|
|
108
|
+
sectionIndex,
|
|
109
|
+
hash: "",
|
|
110
|
+
inlineObjects: 0,
|
|
111
|
+
cells: [],
|
|
112
|
+
nested: []
|
|
113
|
+
};
|
|
114
|
+
index += 1;
|
|
115
|
+
const analyzed = {
|
|
116
|
+
block,
|
|
117
|
+
element,
|
|
118
|
+
read: null,
|
|
119
|
+
label: null,
|
|
120
|
+
parentId: tables.at(-1)?.analyzed.block.id ?? null,
|
|
121
|
+
topLevelId: tables.at(0)?.analyzed.block.id ?? id
|
|
122
|
+
};
|
|
123
|
+
register(analyzed);
|
|
124
|
+
tables.push({
|
|
125
|
+
analyzed,
|
|
126
|
+
cells: [],
|
|
127
|
+
nested: [],
|
|
128
|
+
cellTexts: []
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
cellStart: (row, col) => {
|
|
132
|
+
const open = tables.at(-1);
|
|
133
|
+
if (open === void 0) return;
|
|
134
|
+
open.cells.push({
|
|
135
|
+
row,
|
|
136
|
+
col,
|
|
137
|
+
blockIds: []
|
|
138
|
+
});
|
|
139
|
+
open.cellTexts.push([]);
|
|
140
|
+
},
|
|
141
|
+
tableEnd: () => {
|
|
142
|
+
const open = tables.pop();
|
|
143
|
+
if (open === void 0) return;
|
|
144
|
+
const rows = /* @__PURE__ */ new Map();
|
|
145
|
+
open.cells.forEach(({ row }, cellIndex) => {
|
|
146
|
+
const texts = rows.get(row) ?? [];
|
|
147
|
+
texts.push((open.cellTexts[cellIndex] ?? []).join("\n"));
|
|
148
|
+
rows.set(row, texts);
|
|
149
|
+
});
|
|
150
|
+
const { block } = open.analyzed;
|
|
151
|
+
block.text = [...rows.values()].map((cells) => cells.join(" ")).join("\n");
|
|
152
|
+
block.cells = open.cells;
|
|
153
|
+
block.nested = open.nested;
|
|
154
|
+
let inlineObjects = 0;
|
|
155
|
+
for (const nested of open.nested) if (nested.kind === "paragraph") inlineObjects += nested.inlineObjects;
|
|
156
|
+
block.inlineObjects = inlineObjects;
|
|
157
|
+
block.hash = blockHash(block.text, null);
|
|
158
|
+
noteCellText(block.text);
|
|
159
|
+
},
|
|
160
|
+
unread: (element) => {
|
|
161
|
+
const name = element.name ?? "";
|
|
162
|
+
unread.set(name, (unread.get(name) ?? 0) + 1);
|
|
163
|
+
},
|
|
164
|
+
tracked: () => {
|
|
165
|
+
state.trackedChanges += 1;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
const bodySection = wChild(container, "sectPr");
|
|
169
|
+
if (bodySection !== null) story.sections.push(bodySection);
|
|
170
|
+
for (const [name, count] of unread) {
|
|
171
|
+
story.unreadBlocks.push(name);
|
|
172
|
+
story.warnings.push(`${count} block-level ${name} element(s) are not read as blocks.`);
|
|
173
|
+
}
|
|
174
|
+
if (state.open.length > 0 || state.strayFieldEnds > 0) story.warnings.push("The story holds complex field marks that do not pair up; text after an unclosed field reads as field code.");
|
|
175
|
+
return story;
|
|
176
|
+
};
|
|
177
|
+
const NOTE_ELEMENT = {
|
|
178
|
+
footnotes: "footnote",
|
|
179
|
+
endnotes: "endnote"
|
|
180
|
+
};
|
|
181
|
+
const analyzeNotes = (kind, path, xml) => {
|
|
182
|
+
const notes = {
|
|
183
|
+
kind,
|
|
184
|
+
path,
|
|
185
|
+
part: null,
|
|
186
|
+
notes: [],
|
|
187
|
+
byId: /* @__PURE__ */ new Map(),
|
|
188
|
+
usedIds: [],
|
|
189
|
+
state: createStoryState()
|
|
190
|
+
};
|
|
191
|
+
if (path === null || xml === null) return notes;
|
|
192
|
+
const part = readRawPart(path, xml);
|
|
193
|
+
if (part === null) throw new TemplatePlanError({
|
|
194
|
+
message: `The notes part ${path} could not be read.`,
|
|
195
|
+
code: "xml-unreadable",
|
|
196
|
+
part: path
|
|
197
|
+
});
|
|
198
|
+
notes.part = part;
|
|
199
|
+
for (const element of elementChildren(part.root)) {
|
|
200
|
+
const id = wAttr(element, "id");
|
|
201
|
+
if (id === null) continue;
|
|
202
|
+
const numericId = Number.parseInt(id, 10);
|
|
203
|
+
if (Number.isInteger(numericId)) notes.usedIds.push(numericId);
|
|
204
|
+
const type = wAttr(element, "type");
|
|
205
|
+
if (!isW(element, NOTE_ELEMENT[kind]) || type !== null && type !== "normal") continue;
|
|
206
|
+
const paragraphs = [];
|
|
207
|
+
let hasOtherBlocks = false;
|
|
208
|
+
walkBlocks(element, {
|
|
209
|
+
paragraph: (paragraph) => {
|
|
210
|
+
paragraphs.push(readParagraph(paragraph, notes.state));
|
|
211
|
+
},
|
|
212
|
+
tableStart: () => {
|
|
213
|
+
hasOtherBlocks = true;
|
|
214
|
+
},
|
|
215
|
+
cellStart: () => {},
|
|
216
|
+
tableEnd: () => {},
|
|
217
|
+
unread: () => {
|
|
218
|
+
hasOtherBlocks = true;
|
|
219
|
+
},
|
|
220
|
+
tracked: () => {
|
|
221
|
+
notes.state.trackedChanges += 1;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
endStory(notes.state);
|
|
225
|
+
const note = {
|
|
226
|
+
id,
|
|
227
|
+
element,
|
|
228
|
+
text: paragraphs.map(({ text }) => text).join("\n"),
|
|
229
|
+
paragraphs,
|
|
230
|
+
hasOtherBlocks
|
|
231
|
+
};
|
|
232
|
+
notes.notes.push(note);
|
|
233
|
+
notes.byId.set(id, note);
|
|
234
|
+
}
|
|
235
|
+
return notes;
|
|
236
|
+
};
|
|
237
|
+
const noteList = ({ notes }) => notes.map(({ id, text }) => ({
|
|
238
|
+
id,
|
|
239
|
+
text
|
|
240
|
+
}));
|
|
241
|
+
const countComments = (xml) => {
|
|
242
|
+
if (xml === null) return 0;
|
|
243
|
+
const part = readRawPart("comments", xml);
|
|
244
|
+
return part === null ? 0 : elementChildren(part.root).filter(({ name }) => (name ?? "").endsWith("comment")).length;
|
|
245
|
+
};
|
|
246
|
+
/** Open a package and analyse its main story, notes, styles and numbering. */
|
|
247
|
+
const analyzeDocument = async (bytes) => {
|
|
248
|
+
const loaded = await loadPackage(bytes);
|
|
249
|
+
if (Result.isError(loaded)) return loaded;
|
|
250
|
+
const pkg = loaded.value;
|
|
251
|
+
const document = readRawPart(pkg.documentPath, pkg.documentXml);
|
|
252
|
+
const body = document === null ? null : wChild(document.root, "body");
|
|
253
|
+
if (document === null || body === null) return Result.err(new TemplatePlanError({
|
|
254
|
+
message: "The main document part has no readable body.",
|
|
255
|
+
code: "xml-unreadable",
|
|
256
|
+
part: pkg.documentPath
|
|
257
|
+
}));
|
|
258
|
+
for (const kind of [
|
|
259
|
+
"styles",
|
|
260
|
+
"numbering",
|
|
261
|
+
"footnotes",
|
|
262
|
+
"endnotes"
|
|
263
|
+
]) {
|
|
264
|
+
const missing = pkg.missingPartPath(kind);
|
|
265
|
+
if (missing !== null) return Result.err(new TemplatePlanError({
|
|
266
|
+
message: `The main document names the part ${missing}, which the package does not hold.`,
|
|
267
|
+
code: "part-missing",
|
|
268
|
+
part: missing
|
|
269
|
+
}));
|
|
270
|
+
}
|
|
271
|
+
const stylesPath = pkg.partPath("styles");
|
|
272
|
+
const numberingPath = pkg.partPath("numbering");
|
|
273
|
+
const footnotesPath = pkg.partPath("footnotes");
|
|
274
|
+
const endnotesPath = pkg.partPath("endnotes");
|
|
275
|
+
const texts = [];
|
|
276
|
+
for (const path of [
|
|
277
|
+
stylesPath,
|
|
278
|
+
numberingPath,
|
|
279
|
+
footnotesPath,
|
|
280
|
+
endnotesPath,
|
|
281
|
+
pkg.partPath("comments")
|
|
282
|
+
]) {
|
|
283
|
+
const text = await readPartText(pkg, path);
|
|
284
|
+
if (Result.isError(text)) return text;
|
|
285
|
+
texts.push(text.value);
|
|
286
|
+
}
|
|
287
|
+
const [stylesXml = null, numberingXml = null, footnotesXml = null, endnotesXml = null, commentsXml = null] = texts;
|
|
288
|
+
const read = Result.try({
|
|
289
|
+
try: () => {
|
|
290
|
+
const stylesPackage = stylesXml === null ? null : parseStylesPackage(stylesXml, null);
|
|
291
|
+
const styles = stylesPackage?.styles ?? /* @__PURE__ */ new Map();
|
|
292
|
+
const defaultEmphasis = defaultsBaseline(stylesPackage?.styleDefinitions.docDefaults?.rPr);
|
|
293
|
+
const defaultStyleId = [...styles.values()].find((style) => style.type === "paragraph" && style.default === true)?.styleId ?? null;
|
|
294
|
+
const numbering = numberingXml === null ? null : parseNumbering(numberingXml);
|
|
295
|
+
return {
|
|
296
|
+
styles,
|
|
297
|
+
defaultEmphasis,
|
|
298
|
+
defaultStyleId,
|
|
299
|
+
numbering,
|
|
300
|
+
story: analyzeStory({
|
|
301
|
+
container: body,
|
|
302
|
+
resources: {
|
|
303
|
+
styles,
|
|
304
|
+
defaultStyleId,
|
|
305
|
+
numbering
|
|
306
|
+
}
|
|
307
|
+
}),
|
|
308
|
+
footnotes: analyzeNotes("footnotes", footnotesPath, footnotesXml),
|
|
309
|
+
endnotes: analyzeNotes("endnotes", endnotesPath, endnotesXml)
|
|
310
|
+
};
|
|
311
|
+
},
|
|
312
|
+
catch: (cause) => cause instanceof TemplatePlanError ? cause : new TemplatePlanError({
|
|
313
|
+
message: "The DOCX parts could not be analysed.",
|
|
314
|
+
code: "xml-unreadable",
|
|
315
|
+
cause
|
|
316
|
+
})
|
|
317
|
+
});
|
|
318
|
+
if (Result.isError(read)) return read;
|
|
319
|
+
const { styles, defaultEmphasis, defaultStyleId, numbering, story, footnotes, endnotes } = read.value;
|
|
320
|
+
const warnings = [...story.warnings];
|
|
321
|
+
const formattingRevisions = story.state.formattingRevisions + footnotes.state.formattingRevisions + endnotes.state.formattingRevisions;
|
|
322
|
+
if (formattingRevisions > 0) warnings.push(`${formattingRevisions} tracked formatting change(s) are not counted as tracked changes.`);
|
|
323
|
+
const manifest = {
|
|
324
|
+
sha256: pkg.sha256,
|
|
325
|
+
blocks: story.topLevel.map(({ block }) => block),
|
|
326
|
+
footnotes: noteList(footnotes),
|
|
327
|
+
endnotes: noteList(endnotes),
|
|
328
|
+
comments: countComments(commentsXml),
|
|
329
|
+
trackedChanges: story.state.trackedChanges + footnotes.state.trackedChanges + endnotes.state.trackedChanges,
|
|
330
|
+
warnings
|
|
331
|
+
};
|
|
332
|
+
return Result.ok({
|
|
333
|
+
pkg,
|
|
334
|
+
document,
|
|
335
|
+
body,
|
|
336
|
+
story,
|
|
337
|
+
styles,
|
|
338
|
+
defaultEmphasis,
|
|
339
|
+
stylesPath,
|
|
340
|
+
stylesXml,
|
|
341
|
+
defaultStyleId,
|
|
342
|
+
numbering,
|
|
343
|
+
numberingPath,
|
|
344
|
+
numberingXml,
|
|
345
|
+
footnotes,
|
|
346
|
+
endnotes,
|
|
347
|
+
manifest
|
|
348
|
+
});
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
* Read a DOCX into the blocks a template plan addresses: ids, visible text,
|
|
352
|
+
* rendered list labels, styles and a hash per block, plus its notes.
|
|
353
|
+
*/
|
|
354
|
+
const analyzeDocxBlocks = async (docx) => await guarded(async () => (await analyzeDocument(docx)).map(({ manifest }) => manifest));
|
|
355
|
+
export { analyzeDocument, analyzeDocxBlocks, analyzeStory, blockHash, styleOutlineLevel };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { XmlSplice } from "../../selectiveXmlPatch.js";
|
|
2
|
+
import { XmlElement } from "../../xmlParser.js";
|
|
3
|
+
import { PartPrefixes, RawPart } from "./xml.js";
|
|
4
|
+
/**
|
|
5
|
+
* A hidden bookmark name in the form a cross-reference target takes: `_Ref`
|
|
6
|
+
* and nine digits, derived from `seed` and stepped past the names in `taken`.
|
|
7
|
+
* The name is added to `taken`.
|
|
8
|
+
*/
|
|
9
|
+
export declare const mintRefBookmarkName: (seed: string, taken: Set<string>) => string;
|
|
10
|
+
/** The `w:id` values `w:bookmarkStart` elements under `root` already use. */
|
|
11
|
+
export declare const bookmarkIdsOf: (root: XmlElement) => number[];
|
|
12
|
+
export type BookmarkWrap = {
|
|
13
|
+
id: number;
|
|
14
|
+
name: string;
|
|
15
|
+
};
|
|
16
|
+
type BookmarkMarkupOptions = BookmarkWrap & {
|
|
17
|
+
prefix: string;
|
|
18
|
+
};
|
|
19
|
+
export declare const bookmarkStartXml: ({ prefix, id, name }: BookmarkMarkupOptions) => string;
|
|
20
|
+
export declare const bookmarkEndXml: ({ prefix, id }: Pick<BookmarkMarkupOptions, "prefix" | "id">) => string;
|
|
21
|
+
type WrapParagraphOptions = BookmarkWrap & {
|
|
22
|
+
/** One serialized `w:p`. */
|
|
23
|
+
paragraphXml: string;
|
|
24
|
+
prefixes: PartPrefixes;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* `paragraphXml` with its content, everything after `w:pPr`, inside a
|
|
28
|
+
* bookmark. Null when the markup is not one paragraph.
|
|
29
|
+
*/
|
|
30
|
+
export declare const wrapParagraphContentInBookmark: ({ paragraphXml, prefixes, id, name }: WrapParagraphOptions) => string | null;
|
|
31
|
+
type ComplexFieldOptions = {
|
|
32
|
+
prefix: string;
|
|
33
|
+
/** The field code between the field characters, e.g. ` REF _Ref000000001 \n \h `. */
|
|
34
|
+
instruction: string;
|
|
35
|
+
/** Serialized runs holding the cached result. */
|
|
36
|
+
resultRunsXml: string;
|
|
37
|
+
/** A serialized `w:rPr` the field's own runs carry, or the empty string. */
|
|
38
|
+
runPropertiesXml: string;
|
|
39
|
+
};
|
|
40
|
+
/** A complex field: begin, instruction, separate, the cached result, end. */
|
|
41
|
+
export declare const complexFieldXml: ({ prefix, instruction, resultRunsXml, runPropertiesXml }: ComplexFieldOptions) => string;
|
|
42
|
+
/**
|
|
43
|
+
* The field code of a REF to `bookmark` with `switches` (`\n \t \h`). Null
|
|
44
|
+
* when the switches hold anything but single-letter switches.
|
|
45
|
+
*/
|
|
46
|
+
export declare const refFieldInstruction: (bookmark: string, switches: string) => string | null;
|
|
47
|
+
/** The bookmark a REF, PAGEREF or NOTEREF instruction names. */
|
|
48
|
+
export declare const fieldBookmarkOf: (instruction: string) => string | null;
|
|
49
|
+
/**
|
|
50
|
+
* `settingsXml` with `w:updateFields` set (`true`) or absent (`false`), at its
|
|
51
|
+
* place in the schema's child order. Null when the part is not a settings part.
|
|
52
|
+
*/
|
|
53
|
+
export declare const setUpdateFieldsOnOpen: (settingsXml: string, enabled: boolean) => string | null;
|
|
54
|
+
export type FrozenFieldCount = {
|
|
55
|
+
kind: string;
|
|
56
|
+
count: number;
|
|
57
|
+
};
|
|
58
|
+
export type FreezeFieldsResult = {
|
|
59
|
+
/** Splices that remove the field machinery and keep the cached result. */
|
|
60
|
+
splices: XmlSplice[];
|
|
61
|
+
frozen: FrozenFieldCount[];
|
|
62
|
+
};
|
|
63
|
+
/** Drop splices that lie inside an earlier one, so none overlap. */
|
|
64
|
+
export declare const withoutContained: (splices: readonly XmlSplice[]) => XmlSplice[];
|
|
65
|
+
/**
|
|
66
|
+
* Replace every field of the given kinds under `part` by its cached result:
|
|
67
|
+
* a simple field keeps its runs, a complex field loses its field characters
|
|
68
|
+
* and its code. A field nested in a kept result is judged on its own; one
|
|
69
|
+
* nested in another field's code is part of that code.
|
|
70
|
+
*/
|
|
71
|
+
export declare const freezeFields: (part: RawPart, kinds: ReadonlySet<string>) => FreezeFieldsResult;
|
|
72
|
+
export {};
|