docrev 0.11.3 → 0.12.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/CHANGELOG.md +22 -0
- package/LICENSE +21 -0
- package/dist/bin/rev.js +29 -21
- package/dist/bin/rev.js.map +1 -1
- package/dist/lib/build.d.ts +43 -0
- package/dist/lib/build.d.ts.map +1 -1
- package/dist/lib/build.js +175 -5
- package/dist/lib/build.js.map +1 -1
- package/dist/lib/commands/build.d.ts.map +1 -1
- package/dist/lib/commands/build.js +39 -125
- package/dist/lib/commands/build.js.map +1 -1
- package/dist/lib/commands/comments.js +3 -3
- package/dist/lib/commands/comments.js.map +1 -1
- package/dist/lib/commands/context.d.ts +12 -1
- package/dist/lib/commands/context.d.ts.map +1 -1
- package/dist/lib/commands/context.js +34 -1
- package/dist/lib/commands/context.js.map +1 -1
- package/dist/lib/commands/merge-resolve.js +1 -1
- package/dist/lib/commands/merge-resolve.js.map +1 -1
- package/dist/lib/commands/quality.js +2 -2
- package/dist/lib/commands/quality.js.map +1 -1
- package/dist/lib/commands/sections.js +1 -1
- package/dist/lib/commands/sections.js.map +1 -1
- package/dist/lib/commands/text-ops.js +1 -1
- package/dist/lib/commands/text-ops.js.map +1 -1
- package/dist/lib/commands/utilities.d.ts.map +1 -1
- package/dist/lib/commands/utilities.js +5 -2
- package/dist/lib/commands/utilities.js.map +1 -1
- package/dist/lib/commands/word-tools.js +2 -2
- package/dist/lib/commands/word-tools.js.map +1 -1
- package/dist/lib/crossref.d.ts.map +1 -1
- package/dist/lib/crossref.js +12 -41
- package/dist/lib/crossref.js.map +1 -1
- package/dist/lib/csl.d.ts.map +1 -1
- package/dist/lib/csl.js +14 -22
- package/dist/lib/csl.js.map +1 -1
- package/dist/lib/diff-engine.js +1 -1
- package/dist/lib/diff-engine.js.map +1 -1
- package/dist/lib/equations.d.ts.map +1 -1
- package/dist/lib/equations.js +12 -3
- package/dist/lib/equations.js.map +1 -1
- package/dist/lib/errors.d.ts.map +1 -1
- package/dist/lib/errors.js +1 -30
- package/dist/lib/errors.js.map +1 -1
- package/dist/lib/image-registry.d.ts.map +1 -1
- package/dist/lib/image-registry.js +4 -0
- package/dist/lib/image-registry.js.map +1 -1
- package/dist/lib/import.d.ts +1 -1
- package/dist/lib/import.d.ts.map +1 -1
- package/dist/lib/import.js +1 -1
- package/dist/lib/import.js.map +1 -1
- package/dist/lib/input.d.ts +0 -11
- package/dist/lib/input.d.ts.map +1 -1
- package/dist/lib/input.js +0 -35
- package/dist/lib/input.js.map +1 -1
- package/dist/lib/merge.d.ts.map +1 -1
- package/dist/lib/merge.js +9 -1
- package/dist/lib/merge.js.map +1 -1
- package/dist/lib/ooxml.d.ts +22 -0
- package/dist/lib/ooxml.d.ts.map +1 -1
- package/dist/lib/ooxml.js +130 -0
- package/dist/lib/ooxml.js.map +1 -1
- package/dist/lib/orcid.d.ts.map +1 -1
- package/dist/lib/orcid.js +16 -7
- package/dist/lib/orcid.js.map +1 -1
- package/dist/lib/plugins.d.ts.map +1 -1
- package/dist/lib/plugins.js +8 -4
- package/dist/lib/plugins.js.map +1 -1
- package/dist/lib/protect-restore.d.ts.map +1 -1
- package/dist/lib/protect-restore.js +7 -5
- package/dist/lib/protect-restore.js.map +1 -1
- package/dist/lib/restore-references.d.ts.map +1 -1
- package/dist/lib/restore-references.js +25 -3
- package/dist/lib/restore-references.js.map +1 -1
- package/dist/lib/schema.d.ts.map +1 -1
- package/dist/lib/schema.js +1 -19
- package/dist/lib/schema.js.map +1 -1
- package/dist/lib/slides.js +1 -1
- package/dist/lib/slides.js.map +1 -1
- package/dist/lib/spelling.js +1 -1
- package/dist/lib/spelling.js.map +1 -1
- package/dist/lib/tui.js +0 -1
- package/dist/lib/tui.js.map +1 -1
- package/dist/lib/utils.d.ts +14 -0
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +41 -0
- package/dist/lib/utils.js.map +1 -1
- package/dist/lib/word-extraction.d.ts +5 -5
- package/dist/lib/word-extraction.d.ts.map +1 -1
- package/dist/lib/word-extraction.js +25 -106
- package/dist/lib/word-extraction.js.map +1 -1
- package/dist/lib/word.d.ts +14 -38
- package/dist/lib/word.d.ts.map +1 -1
- package/dist/lib/word.js +24 -179
- package/dist/lib/word.js.map +1 -1
- package/dist/lib/wordcomments.d.ts.map +1 -1
- package/dist/lib/wordcomments.js +9 -7
- package/dist/lib/wordcomments.js.map +1 -1
- package/package.json +17 -6
- package/.gitattributes +0 -1
- package/PLAN-tables-and-postprocess.md +0 -850
- package/bin/rev.ts +0 -145
- package/docs-src/build.py +0 -113
- package/docs-src/extra.css +0 -208
- package/docs-src/md-to-html.lua +0 -6
- package/docs-src/template.html +0 -116
- package/eslint.config.js +0 -27
- package/lib/anchor-match.ts +0 -307
- package/lib/annotations.ts +0 -695
- package/lib/build.ts +0 -2047
- package/lib/citations.ts +0 -160
- package/lib/commands/build.ts +0 -885
- package/lib/commands/citations.ts +0 -515
- package/lib/commands/comments.ts +0 -1077
- package/lib/commands/context.ts +0 -185
- package/lib/commands/core.ts +0 -328
- package/lib/commands/doi.ts +0 -451
- package/lib/commands/file-ops.ts +0 -372
- package/lib/commands/history.ts +0 -320
- package/lib/commands/index.ts +0 -87
- package/lib/commands/init.ts +0 -259
- package/lib/commands/merge-resolve.ts +0 -378
- package/lib/commands/preview.ts +0 -178
- package/lib/commands/project-info.ts +0 -244
- package/lib/commands/quality.ts +0 -517
- package/lib/commands/response.ts +0 -454
- package/lib/commands/section-boundaries.ts +0 -82
- package/lib/commands/sections.ts +0 -451
- package/lib/commands/sync.ts +0 -689
- package/lib/commands/text-ops.ts +0 -449
- package/lib/commands/utilities.ts +0 -448
- package/lib/commands/verify-anchors.ts +0 -272
- package/lib/commands/word-tools.ts +0 -340
- package/lib/comment-realign.ts +0 -99
- package/lib/config.ts +0 -84
- package/lib/crossref.ts +0 -781
- package/lib/csl.ts +0 -191
- package/lib/dependencies.ts +0 -132
- package/lib/diff-engine.ts +0 -465
- package/lib/doi-cache.ts +0 -115
- package/lib/doi.ts +0 -879
- package/lib/equations.ts +0 -506
- package/lib/errors.ts +0 -350
- package/lib/format.ts +0 -541
- package/lib/git.ts +0 -326
- package/lib/grammar.ts +0 -303
- package/lib/image-registry.ts +0 -180
- package/lib/import.ts +0 -932
- package/lib/input.ts +0 -174
- package/lib/journals.ts +0 -543
- package/lib/macro-filter.lua +0 -201
- package/lib/macros.ts +0 -273
- package/lib/merge.ts +0 -633
- package/lib/ooxml.ts +0 -768
- package/lib/orcid.ts +0 -144
- package/lib/pdf-comments.ts +0 -263
- package/lib/pdf-import.ts +0 -524
- package/lib/plugins.ts +0 -362
- package/lib/postprocess.ts +0 -188
- package/lib/pptx-color-filter.lua +0 -37
- package/lib/pptx-template.ts +0 -469
- package/lib/pptx-themes/academic.pptx +0 -0
- package/lib/pptx-themes/corporate.pptx +0 -0
- package/lib/pptx-themes/dark.pptx +0 -0
- package/lib/pptx-themes/default.pptx +0 -0
- package/lib/pptx-themes/minimal.pptx +0 -0
- package/lib/pptx-themes/plant.pptx +0 -0
- package/lib/pptx-themes.ts +0 -483
- package/lib/protect-restore.ts +0 -520
- package/lib/rate-limiter.ts +0 -127
- package/lib/response.ts +0 -200
- package/lib/restore-references.ts +0 -240
- package/lib/review.ts +0 -327
- package/lib/schema.ts +0 -494
- package/lib/scientific-words.ts +0 -73
- package/lib/sections.ts +0 -428
- package/lib/slides.ts +0 -756
- package/lib/spelling.ts +0 -334
- package/lib/templates.ts +0 -526
- package/lib/themes.ts +0 -742
- package/lib/trackchanges.ts +0 -166
- package/lib/tui.ts +0 -450
- package/lib/types.ts +0 -546
- package/lib/undo.ts +0 -250
- package/lib/utils.ts +0 -69
- package/lib/variables.ts +0 -179
- package/lib/word-extraction.ts +0 -525
- package/lib/word.ts +0 -526
- package/lib/wordcomments.ts +0 -789
- package/mkdocs.yml +0 -64
- package/scripts/postbuild.js +0 -47
- package/tsconfig.json +0 -26
- package/types/index.d.ts +0 -531
package/lib/ooxml.ts
DELETED
|
@@ -1,768 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parser-backed OOXML layer.
|
|
3
|
-
*
|
|
4
|
-
* A `.docx` is a zip of XML parts. Reading those parts with regexes breaks on
|
|
5
|
-
* the variability real Word emits: marker elements carry attributes beyond
|
|
6
|
-
* `w:id`, anchor text spans many runs, the WordprocessingML namespace can be
|
|
7
|
-
* bound to a prefix other than `w`, runs are separated by `<w:tab/>`/`<w:br/>`,
|
|
8
|
-
* and prose lives in parts other than `document.xml` (footnotes, endnotes,
|
|
9
|
-
* headers, footers).
|
|
10
|
-
*
|
|
11
|
-
* This module tokenizes the XML structurally and walks it once into an ordered
|
|
12
|
-
* flow of paragraphs, runs, text, track-change spans, and comment-range
|
|
13
|
-
* markers. Every reader (text extraction, comment anchors, headings) and the
|
|
14
|
-
* comment injector derive from that single walk — there is no second engine to
|
|
15
|
-
* drift from.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import AdmZip from 'adm-zip';
|
|
19
|
-
|
|
20
|
-
export const WML_NS = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
|
|
21
|
-
|
|
22
|
-
// =============================================================================
|
|
23
|
-
// Package access
|
|
24
|
-
// =============================================================================
|
|
25
|
-
|
|
26
|
-
/** Open a `.docx` package. Throws a clear error if it is not a valid zip. */
|
|
27
|
-
export function openDocx(docxPath: string): AdmZip {
|
|
28
|
-
try {
|
|
29
|
-
return new AdmZip(docxPath);
|
|
30
|
-
} catch (err: any) {
|
|
31
|
-
throw new Error(`Invalid Word document (not a valid .docx file): ${err.message}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Read a single part as UTF-8 text, or `null` when the part is absent. */
|
|
36
|
-
export function readPartText(zip: AdmZip, partName: string): string | null {
|
|
37
|
-
const entry = zip.getEntry(partName);
|
|
38
|
-
if (!entry) return null;
|
|
39
|
-
return entry.getData().toString('utf8');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The WordprocessingML parts that carry running prose, in reading order:
|
|
44
|
-
* the main story first, then footnotes, endnotes, headers, and footers.
|
|
45
|
-
* A reader that opens only `document.xml` loses everything else.
|
|
46
|
-
*/
|
|
47
|
-
export function listProseParts(zip: AdmZip): string[] {
|
|
48
|
-
const parts: string[] = [];
|
|
49
|
-
if (zip.getEntry('word/document.xml')) parts.push('word/document.xml');
|
|
50
|
-
for (const fixed of ['word/footnotes.xml', 'word/endnotes.xml']) {
|
|
51
|
-
if (zip.getEntry(fixed)) parts.push(fixed);
|
|
52
|
-
}
|
|
53
|
-
const headerFooter = zip
|
|
54
|
-
.getEntries()
|
|
55
|
-
.map((e) => e.entryName)
|
|
56
|
-
.filter((name) => /^word\/(header|footer)\d*\.xml$/.test(name))
|
|
57
|
-
.sort();
|
|
58
|
-
parts.push(...headerFooter);
|
|
59
|
-
return parts;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// =============================================================================
|
|
63
|
-
// Entity coding
|
|
64
|
-
// =============================================================================
|
|
65
|
-
|
|
66
|
-
/** Decode the XML entities that appear in `.docx` text and attribute values. */
|
|
67
|
-
export function decodeXmlEntities(text: string): string {
|
|
68
|
-
return text
|
|
69
|
-
.replace(/</g, '<')
|
|
70
|
-
.replace(/>/g, '>')
|
|
71
|
-
.replace(/"/g, '"')
|
|
72
|
-
.replace(/'/g, "'")
|
|
73
|
-
.replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(parseInt(code, 10)))
|
|
74
|
-
.replace(/&#x([0-9a-fA-F]+);/g, (_, code) => String.fromCodePoint(parseInt(code, 16)))
|
|
75
|
-
.replace(/&/g, '&'); // ampersand last so decoded text is not re-decoded
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Encode text for placement inside an XML element (`<w:t>`). */
|
|
79
|
-
export function encodeXmlText(text: string): string {
|
|
80
|
-
return text
|
|
81
|
-
.replace(/&/g, '&')
|
|
82
|
-
.replace(/</g, '<')
|
|
83
|
-
.replace(/>/g, '>');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Encode text for placement inside a double-quoted XML attribute value. */
|
|
87
|
-
export function encodeXmlAttr(text: string): string {
|
|
88
|
-
return encodeXmlText(text).replace(/"/g, '"');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// =============================================================================
|
|
92
|
-
// Tokenizer
|
|
93
|
-
// =============================================================================
|
|
94
|
-
|
|
95
|
-
export interface XmlAttr {
|
|
96
|
-
/** Qualified name as written, e.g. `w:id`. */
|
|
97
|
-
name: string;
|
|
98
|
-
prefix: string;
|
|
99
|
-
local: string;
|
|
100
|
-
/** Entity-decoded value. */
|
|
101
|
-
value: string;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export type XmlTokenKind =
|
|
105
|
-
| 'open'
|
|
106
|
-
| 'close'
|
|
107
|
-
| 'selfclose'
|
|
108
|
-
| 'text'
|
|
109
|
-
| 'comment'
|
|
110
|
-
| 'cdata'
|
|
111
|
-
| 'decl'
|
|
112
|
-
| 'pi';
|
|
113
|
-
|
|
114
|
-
export interface XmlToken {
|
|
115
|
-
kind: XmlTokenKind;
|
|
116
|
-
/** Byte offset of `<` (or text start) in the source. */
|
|
117
|
-
start: number;
|
|
118
|
-
/** Byte offset just past `>` (or text end) in the source. */
|
|
119
|
-
end: number;
|
|
120
|
-
/** Exact source slice [start, end). */
|
|
121
|
-
raw: string;
|
|
122
|
-
name?: string;
|
|
123
|
-
prefix?: string;
|
|
124
|
-
local?: string;
|
|
125
|
-
attrs?: XmlAttr[];
|
|
126
|
-
/** Entity-decoded text, for `text` and `cdata` tokens. */
|
|
127
|
-
text?: string;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function splitQName(name: string): { prefix: string; local: string } {
|
|
131
|
-
const colon = name.indexOf(':');
|
|
132
|
-
return colon === -1
|
|
133
|
-
? { prefix: '', local: name }
|
|
134
|
-
: { prefix: name.slice(0, colon), local: name.slice(colon + 1) };
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const WS = /\s/;
|
|
138
|
-
|
|
139
|
-
function parseTag(raw: string, start: number, end: number): XmlToken {
|
|
140
|
-
const isClose = raw.startsWith('</');
|
|
141
|
-
const selfClose = !isClose && raw.endsWith('/>');
|
|
142
|
-
const body = raw.slice(isClose ? 2 : 1, selfClose ? raw.length - 2 : raw.length - 1);
|
|
143
|
-
|
|
144
|
-
let p = 0;
|
|
145
|
-
while (p < body.length && !WS.test(body[p]!) && body[p] !== '/') p++;
|
|
146
|
-
const name = body.slice(0, p);
|
|
147
|
-
const { prefix, local } = splitQName(name);
|
|
148
|
-
|
|
149
|
-
const attrs: XmlAttr[] = [];
|
|
150
|
-
while (p < body.length) {
|
|
151
|
-
while (p < body.length && WS.test(body[p]!)) p++;
|
|
152
|
-
if (p >= body.length) break;
|
|
153
|
-
const nameStart = p;
|
|
154
|
-
while (p < body.length && body[p] !== '=' && !WS.test(body[p]!)) p++;
|
|
155
|
-
const attrName = body.slice(nameStart, p);
|
|
156
|
-
while (p < body.length && WS.test(body[p]!)) p++;
|
|
157
|
-
if (body[p] !== '=') {
|
|
158
|
-
// Valueless attribute (not used by WML, but keep the lexer honest).
|
|
159
|
-
if (attrName) {
|
|
160
|
-
const q = splitQName(attrName);
|
|
161
|
-
attrs.push({ name: attrName, prefix: q.prefix, local: q.local, value: '' });
|
|
162
|
-
}
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
p++; // skip '='
|
|
166
|
-
while (p < body.length && WS.test(body[p]!)) p++;
|
|
167
|
-
const quote = body[p];
|
|
168
|
-
let value = '';
|
|
169
|
-
if (quote === '"' || quote === "'") {
|
|
170
|
-
p++;
|
|
171
|
-
const valueStart = p;
|
|
172
|
-
while (p < body.length && body[p] !== quote) p++;
|
|
173
|
-
value = body.slice(valueStart, p);
|
|
174
|
-
p++; // skip closing quote
|
|
175
|
-
} else {
|
|
176
|
-
const valueStart = p;
|
|
177
|
-
while (p < body.length && !WS.test(body[p]!)) p++;
|
|
178
|
-
value = body.slice(valueStart, p);
|
|
179
|
-
}
|
|
180
|
-
const q = splitQName(attrName);
|
|
181
|
-
attrs.push({ name: attrName, prefix: q.prefix, local: q.local, value: decodeXmlEntities(value) });
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return {
|
|
185
|
-
kind: isClose ? 'close' : selfClose ? 'selfclose' : 'open',
|
|
186
|
-
start,
|
|
187
|
-
end,
|
|
188
|
-
raw,
|
|
189
|
-
name,
|
|
190
|
-
prefix,
|
|
191
|
-
local,
|
|
192
|
-
attrs,
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Tokenize an XML string into an ordered, offset-tagged token stream.
|
|
198
|
-
* Quote-aware so a raw `>` inside an attribute value does not end a tag early.
|
|
199
|
-
*/
|
|
200
|
-
export function tokenizeXml(xml: string): XmlToken[] {
|
|
201
|
-
const tokens: XmlToken[] = [];
|
|
202
|
-
const n = xml.length;
|
|
203
|
-
let i = 0;
|
|
204
|
-
|
|
205
|
-
while (i < n) {
|
|
206
|
-
const lt = xml.indexOf('<', i);
|
|
207
|
-
if (lt === -1) {
|
|
208
|
-
tokens.push({ kind: 'text', start: i, end: n, raw: xml.slice(i, n), text: decodeXmlEntities(xml.slice(i, n)) });
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
if (lt > i) {
|
|
212
|
-
tokens.push({ kind: 'text', start: i, end: lt, raw: xml.slice(i, lt), text: decodeXmlEntities(xml.slice(i, lt)) });
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (xml.startsWith('<!--', lt)) {
|
|
216
|
-
const close = xml.indexOf('-->', lt + 4);
|
|
217
|
-
const end = close === -1 ? n : close + 3;
|
|
218
|
-
tokens.push({ kind: 'comment', start: lt, end, raw: xml.slice(lt, end) });
|
|
219
|
-
i = end;
|
|
220
|
-
continue;
|
|
221
|
-
}
|
|
222
|
-
if (xml.startsWith('<![CDATA[', lt)) {
|
|
223
|
-
const close = xml.indexOf(']]>', lt + 9);
|
|
224
|
-
const end = close === -1 ? n : close + 3;
|
|
225
|
-
tokens.push({
|
|
226
|
-
kind: 'cdata',
|
|
227
|
-
start: lt,
|
|
228
|
-
end,
|
|
229
|
-
raw: xml.slice(lt, end),
|
|
230
|
-
text: xml.slice(lt + 9, close === -1 ? n : close),
|
|
231
|
-
});
|
|
232
|
-
i = end;
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
if (xml.startsWith('<?', lt)) {
|
|
236
|
-
const close = xml.indexOf('?>', lt + 2);
|
|
237
|
-
const end = close === -1 ? n : close + 2;
|
|
238
|
-
tokens.push({ kind: xml.startsWith('<?xml', lt) ? 'decl' : 'pi', start: lt, end, raw: xml.slice(lt, end) });
|
|
239
|
-
i = end;
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// Element tag: find the closing '>' that is not inside a quoted value.
|
|
244
|
-
let j = lt + 1;
|
|
245
|
-
let quote = '';
|
|
246
|
-
while (j < n) {
|
|
247
|
-
const ch = xml[j];
|
|
248
|
-
if (quote) {
|
|
249
|
-
if (ch === quote) quote = '';
|
|
250
|
-
} else if (ch === '"' || ch === "'") {
|
|
251
|
-
quote = ch;
|
|
252
|
-
} else if (ch === '>') {
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
j++;
|
|
256
|
-
}
|
|
257
|
-
const end = j < n ? j + 1 : n;
|
|
258
|
-
tokens.push(parseTag(xml.slice(lt, end), lt, end));
|
|
259
|
-
i = end;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return tokens;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// =============================================================================
|
|
266
|
-
// Namespace resolution
|
|
267
|
-
// =============================================================================
|
|
268
|
-
|
|
269
|
-
export interface NsContext {
|
|
270
|
-
uriForPrefix(prefix: string): string | undefined;
|
|
271
|
-
/** True when an element token lives in the WordprocessingML namespace. */
|
|
272
|
-
isWml(token: XmlToken): boolean;
|
|
273
|
-
/** Value of the WML-namespaced attribute with the given local name, if present. */
|
|
274
|
-
wmlAttr(token: XmlToken, local: string): string | undefined;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Build a namespace context from a token stream. Declarations are read from
|
|
279
|
-
* every element (the root carries them in practice), so the WML namespace is
|
|
280
|
-
* matched by URI rather than by assuming the `w` prefix.
|
|
281
|
-
*/
|
|
282
|
-
export function resolveNamespaces(tokens: XmlToken[]): NsContext {
|
|
283
|
-
const prefixToUri = new Map<string, string>();
|
|
284
|
-
for (const tok of tokens) {
|
|
285
|
-
if (tok.kind !== 'open' && tok.kind !== 'selfclose') continue;
|
|
286
|
-
for (const attr of tok.attrs ?? []) {
|
|
287
|
-
if (attr.name === 'xmlns') prefixToUri.set('', attr.value);
|
|
288
|
-
else if (attr.prefix === 'xmlns') prefixToUri.set(attr.local, attr.value);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const uriForPrefix = (prefix: string) => prefixToUri.get(prefix);
|
|
293
|
-
const isWml = (token: XmlToken) => prefixToUri.get(token.prefix ?? '') === WML_NS;
|
|
294
|
-
const wmlAttr = (token: XmlToken, local: string) => {
|
|
295
|
-
for (const attr of token.attrs ?? []) {
|
|
296
|
-
if (attr.local !== local) continue;
|
|
297
|
-
// An unprefixed attribute is in no namespace; a prefixed one resolves
|
|
298
|
-
// through the same table. Marker ids share their element's prefix.
|
|
299
|
-
if (attr.prefix === '' || prefixToUri.get(attr.prefix) === WML_NS) return attr.value;
|
|
300
|
-
}
|
|
301
|
-
return undefined;
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
return { uriForPrefix, isWml, wmlAttr };
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// =============================================================================
|
|
308
|
-
// Ordered document flow
|
|
309
|
-
// =============================================================================
|
|
310
|
-
|
|
311
|
-
export type FlowItem =
|
|
312
|
-
| { kind: 'text'; text: string }
|
|
313
|
-
| { kind: 'paraStart'; style: string | null; level: number; xmlStart: number }
|
|
314
|
-
| { kind: 'paraEnd' }
|
|
315
|
-
| { kind: 'runStart'; xmlStart: number; xmlContentStart: number }
|
|
316
|
-
| { kind: 'runEnd'; xmlEnd: number }
|
|
317
|
-
| { kind: 'commentStart'; id: string; xmlStart: number; xmlEnd: number }
|
|
318
|
-
| { kind: 'commentEnd'; id: string; xmlStart: number; xmlEnd: number }
|
|
319
|
-
| { kind: 'commentRef'; id: string }
|
|
320
|
-
| { kind: 'insStart' }
|
|
321
|
-
| { kind: 'insEnd' }
|
|
322
|
-
| { kind: 'delStart' }
|
|
323
|
-
| { kind: 'delEnd' };
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Walk a part's XML once into an ordered flow. Text-bearing items appear in
|
|
327
|
-
* document order interleaved with paragraph, run, track-change, and
|
|
328
|
-
* comment-range boundaries, each tagged with the source offsets a later
|
|
329
|
-
* injection step needs.
|
|
330
|
-
*/
|
|
331
|
-
export function walkBody(xml: string, ns?: NsContext): FlowItem[] {
|
|
332
|
-
const tokens = tokenizeXml(xml);
|
|
333
|
-
const nsCtx = ns ?? resolveNamespaces(tokens);
|
|
334
|
-
const flow: FlowItem[] = [];
|
|
335
|
-
|
|
336
|
-
// Track nesting so content is read precisely: literal text only counts
|
|
337
|
-
// inside `<w:t>`/`<w:delText>` (never `<w:instrText>` field codes), and
|
|
338
|
-
// `<w:tab/>`/`<w:br/>` only count inside a run (never a `<w:pPr>` tab-stop
|
|
339
|
-
// definition).
|
|
340
|
-
let runDepth = 0;
|
|
341
|
-
let inPPr = 0;
|
|
342
|
-
let textDepth = 0;
|
|
343
|
-
let currentPara: Extract<FlowItem, { kind: 'paraStart' }> | null = null;
|
|
344
|
-
|
|
345
|
-
const local = (t: XmlToken) => (nsCtx.isWml(t) ? t.local : undefined);
|
|
346
|
-
|
|
347
|
-
for (let idx = 0; idx < tokens.length; idx++) {
|
|
348
|
-
const tok = tokens[idx]!;
|
|
349
|
-
|
|
350
|
-
if (tok.kind === 'text') {
|
|
351
|
-
if (textDepth > 0 && tok.text) flow.push({ kind: 'text', text: tok.text });
|
|
352
|
-
continue;
|
|
353
|
-
}
|
|
354
|
-
if (tok.kind === 'cdata') {
|
|
355
|
-
if (textDepth > 0 && tok.text) flow.push({ kind: 'text', text: tok.text });
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
if (tok.kind !== 'open' && tok.kind !== 'close' && tok.kind !== 'selfclose') continue;
|
|
359
|
-
|
|
360
|
-
const ln = local(tok);
|
|
361
|
-
if (ln === undefined) continue;
|
|
362
|
-
|
|
363
|
-
if (tok.kind === 'open') {
|
|
364
|
-
switch (ln) {
|
|
365
|
-
case 'p':
|
|
366
|
-
currentPara = { kind: 'paraStart', style: null, level: 0, xmlStart: tok.start };
|
|
367
|
-
flow.push(currentPara);
|
|
368
|
-
break;
|
|
369
|
-
case 'pPr':
|
|
370
|
-
inPPr++;
|
|
371
|
-
break;
|
|
372
|
-
case 't':
|
|
373
|
-
case 'delText':
|
|
374
|
-
textDepth++;
|
|
375
|
-
break;
|
|
376
|
-
case 'r':
|
|
377
|
-
runDepth++;
|
|
378
|
-
flow.push({ kind: 'runStart', xmlStart: tok.start, xmlContentStart: tok.end });
|
|
379
|
-
break;
|
|
380
|
-
case 'ins':
|
|
381
|
-
flow.push({ kind: 'insStart' });
|
|
382
|
-
break;
|
|
383
|
-
case 'del':
|
|
384
|
-
flow.push({ kind: 'delStart' });
|
|
385
|
-
break;
|
|
386
|
-
default:
|
|
387
|
-
break;
|
|
388
|
-
}
|
|
389
|
-
} else if (tok.kind === 'close') {
|
|
390
|
-
switch (ln) {
|
|
391
|
-
case 'p':
|
|
392
|
-
flow.push({ kind: 'paraEnd' });
|
|
393
|
-
currentPara = null;
|
|
394
|
-
break;
|
|
395
|
-
case 'pPr':
|
|
396
|
-
if (inPPr > 0) inPPr--;
|
|
397
|
-
break;
|
|
398
|
-
case 't':
|
|
399
|
-
case 'delText':
|
|
400
|
-
if (textDepth > 0) textDepth--;
|
|
401
|
-
break;
|
|
402
|
-
case 'r':
|
|
403
|
-
if (runDepth > 0) runDepth--;
|
|
404
|
-
flow.push({ kind: 'runEnd', xmlEnd: tok.start });
|
|
405
|
-
break;
|
|
406
|
-
case 'ins':
|
|
407
|
-
flow.push({ kind: 'insEnd' });
|
|
408
|
-
break;
|
|
409
|
-
case 'del':
|
|
410
|
-
flow.push({ kind: 'delEnd' });
|
|
411
|
-
break;
|
|
412
|
-
default:
|
|
413
|
-
break;
|
|
414
|
-
}
|
|
415
|
-
} else {
|
|
416
|
-
// self-close
|
|
417
|
-
switch (ln) {
|
|
418
|
-
case 'pStyle': {
|
|
419
|
-
const val = nsCtx.wmlAttr(tok, 'val');
|
|
420
|
-
if (currentPara && inPPr > 0 && val) {
|
|
421
|
-
currentPara.style = val;
|
|
422
|
-
const m = val.match(/(\d+)/);
|
|
423
|
-
if (/heading/i.test(val)) currentPara.level = m ? parseInt(m[1]!, 10) : 0;
|
|
424
|
-
}
|
|
425
|
-
break;
|
|
426
|
-
}
|
|
427
|
-
case 'tab':
|
|
428
|
-
if (runDepth > 0 && inPPr === 0) flow.push({ kind: 'text', text: '\t' });
|
|
429
|
-
break;
|
|
430
|
-
case 'br':
|
|
431
|
-
case 'cr':
|
|
432
|
-
if (runDepth > 0 && inPPr === 0) flow.push({ kind: 'text', text: '\n' });
|
|
433
|
-
break;
|
|
434
|
-
case 'commentRangeStart': {
|
|
435
|
-
const id = nsCtx.wmlAttr(tok, 'id');
|
|
436
|
-
if (id !== undefined) flow.push({ kind: 'commentStart', id, xmlStart: tok.start, xmlEnd: tok.end });
|
|
437
|
-
break;
|
|
438
|
-
}
|
|
439
|
-
case 'commentRangeEnd': {
|
|
440
|
-
const id = nsCtx.wmlAttr(tok, 'id');
|
|
441
|
-
if (id !== undefined) flow.push({ kind: 'commentEnd', id, xmlStart: tok.start, xmlEnd: tok.end });
|
|
442
|
-
break;
|
|
443
|
-
}
|
|
444
|
-
case 'commentReference': {
|
|
445
|
-
const id = nsCtx.wmlAttr(tok, 'id');
|
|
446
|
-
if (id !== undefined) flow.push({ kind: 'commentRef', id });
|
|
447
|
-
break;
|
|
448
|
-
}
|
|
449
|
-
default:
|
|
450
|
-
break;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
return flow;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
// =============================================================================
|
|
459
|
-
// Derived readers
|
|
460
|
-
// =============================================================================
|
|
461
|
-
|
|
462
|
-
export interface CommentRange {
|
|
463
|
-
id: string;
|
|
464
|
-
/** Concatenated anchor text between the range markers. */
|
|
465
|
-
anchor: string;
|
|
466
|
-
/** Start offset of the anchor in `text`. */
|
|
467
|
-
start: number;
|
|
468
|
-
/** End offset of the anchor in `text`. */
|
|
469
|
-
end: number;
|
|
470
|
-
/** True when the range encloses no text (a zero-width / point anchor). */
|
|
471
|
-
isEmpty: boolean;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
export interface DocTextModel {
|
|
475
|
-
/** Plain text: runs concatenated, paragraphs joined with nothing between. */
|
|
476
|
-
text: string;
|
|
477
|
-
/** Comment ranges with offsets into `text`. */
|
|
478
|
-
comments: CommentRange[];
|
|
479
|
-
/** Heading paragraphs with their offset into `text`. */
|
|
480
|
-
headings: Array<{ style: string; level: number; text: string; position: number }>;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Build the plain-text model used for comment-anchor matching. Runs are
|
|
485
|
-
* concatenated and paragraphs are joined with no separator, matching the
|
|
486
|
-
* coordinate system the placement engine expects, but namespace-aware and
|
|
487
|
-
* robust to marker-attribute variation and multi-run anchors.
|
|
488
|
-
*/
|
|
489
|
-
export function buildDocTextModel(xml: string): DocTextModel {
|
|
490
|
-
const flow = walkBody(xml);
|
|
491
|
-
let text = '';
|
|
492
|
-
const startOffsets = new Map<string, number>();
|
|
493
|
-
const endOffsets = new Map<string, number>();
|
|
494
|
-
const comments: CommentRange[] = [];
|
|
495
|
-
const headings: DocTextModel['headings'] = [];
|
|
496
|
-
|
|
497
|
-
let paraStartOffset = 0;
|
|
498
|
-
let paraStyle: string | null = null;
|
|
499
|
-
let paraLevel = 0;
|
|
500
|
-
let paraText = '';
|
|
501
|
-
|
|
502
|
-
for (const item of flow) {
|
|
503
|
-
switch (item.kind) {
|
|
504
|
-
case 'text':
|
|
505
|
-
text += item.text;
|
|
506
|
-
paraText += item.text;
|
|
507
|
-
break;
|
|
508
|
-
case 'paraStart':
|
|
509
|
-
paraStartOffset = text.length;
|
|
510
|
-
paraStyle = item.style;
|
|
511
|
-
paraLevel = item.level;
|
|
512
|
-
paraText = '';
|
|
513
|
-
break;
|
|
514
|
-
case 'paraEnd':
|
|
515
|
-
if (paraStyle && /heading/i.test(paraStyle) && paraText.trim()) {
|
|
516
|
-
headings.push({ style: paraStyle, level: paraLevel, text: paraText.trim(), position: paraStartOffset });
|
|
517
|
-
}
|
|
518
|
-
break;
|
|
519
|
-
case 'commentStart':
|
|
520
|
-
if (!startOffsets.has(item.id)) startOffsets.set(item.id, text.length);
|
|
521
|
-
break;
|
|
522
|
-
case 'commentEnd':
|
|
523
|
-
if (!endOffsets.has(item.id)) endOffsets.set(item.id, text.length);
|
|
524
|
-
break;
|
|
525
|
-
default:
|
|
526
|
-
break;
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
// The paragraph style can be set after `paraStart` (pStyle lives early in
|
|
531
|
-
// the paragraph), so headings are recorded at paraEnd above.
|
|
532
|
-
|
|
533
|
-
for (const [id, start] of startOffsets) {
|
|
534
|
-
const end = endOffsets.get(id);
|
|
535
|
-
if (end === undefined) continue;
|
|
536
|
-
const anchor = text.slice(start, Math.max(start, end));
|
|
537
|
-
comments.push({ id, anchor: anchor.trim(), start, end: Math.max(start, end), isEmpty: !anchor.trim() });
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
return { text, comments, headings };
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
// =============================================================================
|
|
544
|
-
// Run index (for comment injection)
|
|
545
|
-
// =============================================================================
|
|
546
|
-
|
|
547
|
-
export interface TextRunSlot {
|
|
548
|
-
/** Source offset of the enclosing run's `<w:r>`. */
|
|
549
|
-
runOpenStart: number;
|
|
550
|
-
/** Source offset just past the enclosing run's `</w:r>`. */
|
|
551
|
-
runCloseEnd: number;
|
|
552
|
-
/** Raw `<w:rPr>...</w:rPr>` of the enclosing run, or '' when it has none. */
|
|
553
|
-
rPr: string;
|
|
554
|
-
/** Raw opening `<w:t ...>` tag of this text element. */
|
|
555
|
-
tOpenTag: string;
|
|
556
|
-
/** Source offset just past `<w:t ...>`. */
|
|
557
|
-
contentStart: number;
|
|
558
|
-
/** Source offset of `</w:t>`. */
|
|
559
|
-
contentEnd: number;
|
|
560
|
-
/** Raw (still XML-encoded) text between the `<w:t>` tags. */
|
|
561
|
-
content: string;
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
interface RunFrame {
|
|
565
|
-
openStart: number;
|
|
566
|
-
rPr: string;
|
|
567
|
-
rPrOpenStart: number;
|
|
568
|
-
pending: TextRunSlot[];
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* Index every `<w:t>` text element with its enclosing run's exact boundaries
|
|
573
|
-
* and run properties, derived from the token nesting rather than by scanning
|
|
574
|
-
* backwards for the nearest `<w:r`. This locates the correct run even when the
|
|
575
|
-
* text sits inside a hyperlink, a field, or an `mc:AlternateContent` block, and
|
|
576
|
-
* naturally ignores marker-like text that lives in attributes (it is not inside
|
|
577
|
-
* a `<w:t>`).
|
|
578
|
-
*/
|
|
579
|
-
export function indexTextRuns(xml: string): TextRunSlot[] {
|
|
580
|
-
const tokens = tokenizeXml(xml);
|
|
581
|
-
const ns = resolveNamespaces(tokens);
|
|
582
|
-
const slots: TextRunSlot[] = [];
|
|
583
|
-
const runStack: RunFrame[] = [];
|
|
584
|
-
|
|
585
|
-
let tOpenTag = '';
|
|
586
|
-
let tContentStart = -1;
|
|
587
|
-
let inT = false;
|
|
588
|
-
|
|
589
|
-
for (const tok of tokens) {
|
|
590
|
-
if (tok.kind === 'open' || tok.kind === 'close' || tok.kind === 'selfclose') {
|
|
591
|
-
const ln = ns.isWml(tok) ? tok.local : undefined;
|
|
592
|
-
const top = runStack[runStack.length - 1];
|
|
593
|
-
|
|
594
|
-
if (tok.kind === 'open') {
|
|
595
|
-
if (ln === 'r') {
|
|
596
|
-
runStack.push({ openStart: tok.start, rPr: '', rPrOpenStart: -1, pending: [] });
|
|
597
|
-
} else if (ln === 'rPr' && top) {
|
|
598
|
-
top.rPrOpenStart = tok.start;
|
|
599
|
-
} else if (ln === 't' && top) {
|
|
600
|
-
tOpenTag = tok.raw;
|
|
601
|
-
tContentStart = tok.end;
|
|
602
|
-
inT = true;
|
|
603
|
-
}
|
|
604
|
-
} else if (tok.kind === 'close') {
|
|
605
|
-
if (ln === 'rPr' && top && top.rPrOpenStart >= 0) {
|
|
606
|
-
top.rPr = xml.slice(top.rPrOpenStart, tok.end);
|
|
607
|
-
top.rPrOpenStart = -1;
|
|
608
|
-
} else if (ln === 't' && top && inT) {
|
|
609
|
-
top.pending.push({
|
|
610
|
-
runOpenStart: top.openStart,
|
|
611
|
-
runCloseEnd: -1,
|
|
612
|
-
rPr: top.rPr,
|
|
613
|
-
tOpenTag,
|
|
614
|
-
contentStart: tContentStart,
|
|
615
|
-
contentEnd: tok.start,
|
|
616
|
-
content: xml.slice(tContentStart, tok.start),
|
|
617
|
-
});
|
|
618
|
-
inT = false;
|
|
619
|
-
} else if (ln === 'r') {
|
|
620
|
-
const frame = runStack.pop();
|
|
621
|
-
if (frame) {
|
|
622
|
-
for (const slot of frame.pending) {
|
|
623
|
-
slot.runCloseEnd = tok.end;
|
|
624
|
-
slots.push(slot);
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
slots.sort((a, b) => a.contentStart - b.contentStart);
|
|
633
|
-
return slots;
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
// =============================================================================
|
|
637
|
-
// Comment bodies + threading
|
|
638
|
-
// =============================================================================
|
|
639
|
-
|
|
640
|
-
export interface ExtractedComment {
|
|
641
|
-
id: string;
|
|
642
|
-
author: string;
|
|
643
|
-
/** Full ISO date string as written, or '' when absent. */
|
|
644
|
-
date: string;
|
|
645
|
-
text: string;
|
|
646
|
-
/** Parent comment id for a reply, resolved from commentsExtended.xml. */
|
|
647
|
-
parentId?: string;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
/** First attribute with the given local name, regardless of namespace prefix. */
|
|
651
|
-
function attrByLocal(token: XmlToken, local: string): string | undefined {
|
|
652
|
-
for (const attr of token.attrs ?? []) {
|
|
653
|
-
if (attr.local === local) return attr.value;
|
|
654
|
-
}
|
|
655
|
-
return undefined;
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
/**
|
|
659
|
-
* Extract comment bodies from `word/comments.xml` and reply threading from
|
|
660
|
-
* `word/commentsExtended.xml`. Structural elements are matched by local name
|
|
661
|
-
* in the WordprocessingML namespace, so a document that binds WML to a prefix
|
|
662
|
-
* other than `w` still yields its comments.
|
|
663
|
-
*/
|
|
664
|
-
export function extractComments(zip: AdmZip): ExtractedComment[] {
|
|
665
|
-
const commentsXml = readPartText(zip, 'word/comments.xml');
|
|
666
|
-
if (commentsXml === null) return [];
|
|
667
|
-
|
|
668
|
-
const tokens = tokenizeXml(commentsXml);
|
|
669
|
-
const ns = resolveNamespaces(tokens);
|
|
670
|
-
const wmlLocal = (t: XmlToken) => (ns.isWml(t) ? t.local : undefined);
|
|
671
|
-
|
|
672
|
-
const comments: ExtractedComment[] = [];
|
|
673
|
-
const paraIdToCommentId = new Map<string, string>();
|
|
674
|
-
|
|
675
|
-
let current: ExtractedComment | null = null;
|
|
676
|
-
let textDepth = 0;
|
|
677
|
-
|
|
678
|
-
for (const tok of tokens) {
|
|
679
|
-
if (tok.kind === 'text' || tok.kind === 'cdata') {
|
|
680
|
-
if (current && textDepth > 0 && tok.text) current.text += tok.text;
|
|
681
|
-
continue;
|
|
682
|
-
}
|
|
683
|
-
const ln =
|
|
684
|
-
tok.kind === 'open' || tok.kind === 'close' || tok.kind === 'selfclose'
|
|
685
|
-
? wmlLocal(tok)
|
|
686
|
-
: undefined;
|
|
687
|
-
if (ln === undefined) continue;
|
|
688
|
-
|
|
689
|
-
// Word stores the threading key (w14:paraId) on each comment paragraph;
|
|
690
|
-
// a reply's second paragraph is an empty self-closed <w:p/>, so read it on
|
|
691
|
-
// open and self-close alike.
|
|
692
|
-
if ((tok.kind === 'open' || tok.kind === 'selfclose') && ln === 'p' && current) {
|
|
693
|
-
const paraId = attrByLocal(tok, 'paraId');
|
|
694
|
-
if (paraId && current.id) paraIdToCommentId.set(paraId, current.id);
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
if (tok.kind === 'open') {
|
|
698
|
-
if (ln === 'comment') {
|
|
699
|
-
current = {
|
|
700
|
-
id: ns.wmlAttr(tok, 'id') ?? '',
|
|
701
|
-
author: ns.wmlAttr(tok, 'author') ?? 'Unknown',
|
|
702
|
-
date: ns.wmlAttr(tok, 'date') ?? '',
|
|
703
|
-
text: '',
|
|
704
|
-
};
|
|
705
|
-
} else if ((ln === 't' || ln === 'delText') && current) {
|
|
706
|
-
textDepth++;
|
|
707
|
-
}
|
|
708
|
-
} else if (tok.kind === 'close') {
|
|
709
|
-
if (ln === 'comment' && current) {
|
|
710
|
-
current.text = current.text.trim();
|
|
711
|
-
comments.push(current);
|
|
712
|
-
current = null;
|
|
713
|
-
textDepth = 0;
|
|
714
|
-
} else if ((ln === 't' || ln === 'delText') && textDepth > 0) {
|
|
715
|
-
textDepth--;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
// Resolve reply links from commentsExtended.xml (absent for non-Word sources).
|
|
721
|
-
const extendedXml = readPartText(zip, 'word/commentsExtended.xml');
|
|
722
|
-
if (extendedXml && paraIdToCommentId.size > 0) {
|
|
723
|
-
const parentByCommentId = new Map<string, string>();
|
|
724
|
-
for (const tok of tokenizeXml(extendedXml)) {
|
|
725
|
-
if (tok.kind !== 'selfclose' && tok.kind !== 'open') continue;
|
|
726
|
-
if (tok.local !== 'commentEx') continue;
|
|
727
|
-
const paraId = attrByLocal(tok, 'paraId');
|
|
728
|
-
const parentParaId = attrByLocal(tok, 'paraIdParent');
|
|
729
|
-
if (!paraId || !parentParaId) continue;
|
|
730
|
-
const childId = paraIdToCommentId.get(paraId);
|
|
731
|
-
const parentId = paraIdToCommentId.get(parentParaId);
|
|
732
|
-
if (childId && parentId && childId !== parentId) {
|
|
733
|
-
parentByCommentId.set(childId, parentId);
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
for (const c of comments) {
|
|
737
|
-
const parent = parentByCommentId.get(c.id);
|
|
738
|
-
if (parent) c.parentId = parent;
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
return comments;
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
/** Parts that can carry comment ranges, in reading order. */
|
|
746
|
-
export const COMMENT_PARTS = ['word/document.xml', 'word/footnotes.xml', 'word/endnotes.xml'];
|
|
747
|
-
|
|
748
|
-
/**
|
|
749
|
-
* Comment ranges across every part that can hold them — not just
|
|
750
|
-
* `document.xml`, so a comment anchored in a footnote or endnote is found
|
|
751
|
-
* rather than silently lost. Positions are offsets into the concatenated
|
|
752
|
-
* `fullDocText`.
|
|
753
|
-
*/
|
|
754
|
-
export function buildCommentAnchorModel(zip: AdmZip): { fullDocText: string; comments: CommentRange[] } {
|
|
755
|
-
let fullDocText = '';
|
|
756
|
-
const comments: CommentRange[] = [];
|
|
757
|
-
for (const part of COMMENT_PARTS) {
|
|
758
|
-
const xml = readPartText(zip, part);
|
|
759
|
-
if (xml === null) continue;
|
|
760
|
-
const model = buildDocTextModel(xml);
|
|
761
|
-
const offset = fullDocText.length;
|
|
762
|
-
fullDocText += model.text;
|
|
763
|
-
for (const c of model.comments) {
|
|
764
|
-
comments.push({ id: c.id, anchor: c.anchor, start: c.start + offset, end: c.end + offset, isEmpty: c.isEmpty });
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
return { fullDocText, comments };
|
|
768
|
-
}
|