Repository navigation
Expand file tree
/
Copy pathdocumentTextWorker.ts
More file actions
675 lines (664 loc) · 24.9 KB
/
Copy pathdocumentTextWorker.ts
File metadata and controls
675 lines (664 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// @effect-diagnostics nodeBuiltinImport:off -- isolated CLI worker; no server runtime is loaded here.
import * as NodeFSP from "node:fs/promises";
import * as NodePath from "node:path";
import * as NodeUtil from "node:util";
import { characterEntities } from "character-entities";
import { SaxesParser } from "saxes";
import * as Yauzl from "yauzl";
const MAX_INPUT = 16 * 1024 * 1024;
const MAX_EXPANDED = 32 * 1024 * 1024;
const MAX_OUTPUT = 1_000_000;
const CELL_START = Symbol("cell-start");
const CELL_END = Symbol("cell-end");
type TextPart = string | typeof CELL_START | typeof CELL_END | { fallback: string };
const TRUNCATED = "\n\n[Extracted text truncated at document extraction limits.]";
// Parts the document never displays: tracked deletions and moves, field codes, phonetic guides,
// paragraph properties such as tab-stop definitions.
const HIDDEN = new Set([
"script",
"style",
"binary-data",
"rPh",
"delText",
"moveFrom",
"instrText",
"delInstrText",
"tracked-changes",
"pPr",
]);
// @effect-diagnostics-next-line extendsNativeError:off - this worker loads no Effect runtime; Truncation is a local throw sentinel.
class Truncation extends Error {}
interface XmlNode {
name: string;
attrs: Record<string, string>;
children: Array<XmlNode | string>;
spaceCount?: number;
hasText?: boolean;
hasTextChoice?: boolean;
}
function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
const root: XmlNode = { name: "root", attrs: {}, children: [] };
const stack = [root];
let nodes = 0;
const parser = new SaxesParser();
if (htmlEntities) Object.assign(parser.ENTITIES, characterEntities);
parser.on("opentag", (tag) => {
if (++nodes > 200_000 || stack.length > 128) throw new Truncation("XML structure limit");
const node: XmlNode = {
name: tag.name.split(":").at(-1)!,
attrs: Object.fromEntries(
Object.entries(tag.attributes).flatMap(([key, value]) => [
[key, String(value)],
[key.split(":").at(-1)!, String(value)],
]),
),
children: [],
};
if (odfSpaces && node.name === "s") node.spaceCount = repeatCount(node.attrs.c);
stack.at(-1)!.children.push(node);
stack.push(node);
});
parser.on("closetag", () => {
const node = stack.pop()!;
const parent = stack.at(-1)!;
// Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
// Keep one when no Choice carries text, such as a chart with a VML caption.
if (node.name === "Fallback" && parent.hasTextChoice) {
parent.children.pop();
return;
}
if (!node.hasText) return;
parent.hasText = true;
if (node.name === "Choice") parent.hasTextChoice = true;
});
const text = (value: string) => {
const node = stack.at(-1)!;
node.children.push(value);
if (value.trim() !== "") node.hasText = true;
};
parser.on("text", text);
parser.on("cdata", text);
parser.write(source).close();
return root;
}
const children = (node: XmlNode) =>
node.children.filter((child): child is XmlNode => typeof child !== "string");
function* all(node: XmlNode, name: string | readonly string[]): Generator<XmlNode, void> {
for (const child of children(node)) {
if (typeof name === "string" ? child.name === name : name.includes(child.name)) yield child;
else yield* all(child, name);
}
}
function* text(node: XmlNode): Generator<string, void> {
if (HIDDEN.has(node.name)) return;
if (["tab", "cell"].includes(node.name)) yield "\t";
else if (["br", "line-break", "cr"].includes(node.name)) yield "\n";
else if (node.spaceCount !== undefined) {
yield " ".repeat(Math.min(MAX_OUTPUT, node.spaceCount));
if (node.spaceCount > MAX_OUTPUT) throw new Truncation("Space repeat limit");
} else
for (const child of node.children) {
if (typeof child === "string") yield child;
else yield* text(child);
}
}
function* paragraphs(node: XmlNode): Generator<TextPart, void> {
if (HIDDEN.has(node.name)) return;
for (const child of node.children) {
if (typeof child === "string") yield child;
else if (["br", "line-break", "cr"].includes(child.name)) yield "\n";
else if (["tbl", "table"].includes(child.name)) yield* table(child);
else if (["p", "h"].includes(child.name) || /^h[1-6]$/.test(child.name)) {
const style = all(child, "pStyle").next().value?.attrs.val ?? "";
const level =
child.name === "h"
? Number(child.attrs["outline-level"]) || 1
: /^h[1-6]$/.test(child.name)
? Number(child.name[1])
: Number(style.match(/^Heading([1-6])$/i)?.[1]) || 0;
if (level) yield `${"#".repeat(Math.min(6, level))} `;
yield* text(child);
yield "\n";
} else {
yield* paragraphs(child);
if (["li", "div", "section"].includes(child.name)) yield "\n";
}
}
}
function* inCell(parts: Iterable<TextPart>): Generator<TextPart, void> {
yield CELL_START;
yield* parts;
yield CELL_END;
}
function repeatCount(value: string | undefined): number {
if (value === undefined) return 1;
if (!/^[0-9]+$/.test(value) || Number(value) < 1) throw new Error("Invalid repeat count");
return Number(value);
}
function odfValue(cell: XmlNode): string {
const type = cell.attrs["office:value-type"] ?? cell.attrs["value-type"];
const attr = (name: string) => cell.attrs[`office:${name}`] ?? cell.attrs[name] ?? "";
switch (type) {
case "void":
return "";
case "string":
return attr("string-value");
case "boolean":
return attr("boolean-value");
case "date":
return attr("date-value");
case "time":
return attr("time-value");
case "currency":
return attr("value").trim() ? `${attr("value")} ${attr("currency")}`.trim() : "";
case "percentage":
return attr("value").trim() ? `${attr("value")} [ODF value-type=percentage]` : "";
default:
return attr("value");
}
}
function hasCellText(cell: XmlNode): boolean {
if (odfValue(cell).trim()) return true;
// Space markers can have enormous repeat counts; emptiness never requires expanding them.
if (HIDDEN.has(cell.name) || cell.spaceCount !== undefined) return false;
return cell.children.some((child) =>
typeof child === "string" ? !!child.trim() : hasCellText(child),
);
}
function* table(node: XmlNode): Generator<TextPart, void> {
let rowCount = 0;
for (const row of all(node, ["tr", "table-row"])) {
const cells = children(row).filter((cell) =>
["tc", "table-cell", "covered-table-cell", "td", "th"].includes(cell.name),
);
// ODS writers pad sheets with repeated empty rows and trailing cells.
while (cells.length && !hasCellText(cells.at(-1)!)) cells.pop();
if (!cells.length) continue;
const repeat = repeatCount(row.attrs["number-rows-repeated"]);
for (let index = 0; index < repeat; index++) {
if (++rowCount > 10_000) throw new Truncation("Row limit");
let column = 0;
for (const cell of cells) {
const count = repeatCount(cell.attrs["number-columns-repeated"]);
for (let i = 0; i < count; i++) {
if (column >= 512) throw new Truncation("Column limit");
if (column++) yield "\t";
yield CELL_START;
yield* paragraphs(cell);
yield { fallback: odfValue(cell) };
yield CELL_END;
}
}
yield "\n";
}
}
}
async function archive(path: string): Promise<Map<string, string>> {
const zip = await new Promise<Yauzl.ZipFile>((resolve, reject) =>
Yauzl.open(
path,
{ lazyEntries: true, validateEntrySizes: true, strictFileNames: true },
(error, result) => (error || !result ? reject(error) : resolve(result)),
),
);
const files = new Map<string, string>();
let expanded = 0;
let count = 0;
try {
await new Promise<void>((resolve, reject) => {
zip.on("error", reject);
zip.on("end", resolve);
zip.on("entry", (entry: Yauzl.Entry) => {
if (++count > 2048 || zip.entryCount > 2048) {
reject(new Truncation("Archive limit"));
return;
}
if (entry.generalPurposeBitFlag & 1) {
reject(new Error("Encrypted archive"));
return;
}
if (!/\.(xml|rels|xhtml|html|htm|opf|svg)$/i.test(entry.fileName)) {
zip.readEntry();
return;
}
if (
entry.uncompressedSize > MAX_EXPANDED ||
entry.uncompressedSize > Math.max(1024, entry.compressedSize * 200)
) {
reject(new Truncation("Archive limit"));
return;
}
expanded += entry.uncompressedSize;
if (expanded > MAX_EXPANDED) {
reject(new Truncation("Expanded byte limit"));
return;
}
zip.openReadStream(entry, (error, stream) => {
if (error || !stream) {
reject(error);
return;
}
const buffers: Buffer[] = [];
let size = 0;
stream.on("error", reject);
stream.on("data", (chunk: Buffer) => {
size += chunk.length;
if (size > entry.uncompressedSize || size > MAX_EXPANDED) {
stream.destroy(new Truncation("Entry byte limit"));
return;
}
buffers.push(chunk);
});
stream.on("end", () => {
const data = Buffer.concat(buffers);
// XML's BOM and initial '<' bytes distinguish both UTF-16 byte orders.
const encoding =
(data[0] === 0xff && data[1] === 0xfe) || (data[0] === 0x3c && data[1] === 0)
? "utf-16le"
: (data[0] === 0xfe && data[1] === 0xff) || (data[0] === 0 && data[1] === 0x3c)
? "utf-16be"
: "utf-8";
files.set(entry.fileName, new TextDecoder(encoding).decode(data));
zip.readEntry();
});
});
});
zip.readEntry();
});
} finally {
zip.close();
}
return files;
}
function* notebook(source: string): Generator<string, void> {
const book: unknown = JSON.parse(source);
if (!book || typeof book !== "object" || !("cells" in book) || !Array.isArray(book.cells))
throw new Error("Invalid notebook");
function* lines(value: unknown): Generator<string, void> {
if (typeof value === "string") yield value;
else if (Array.isArray(value))
for (const line of value) {
if (typeof line === "string") yield line;
}
}
for (const [index, cell] of book.cells.entries()) {
if (index >= 10_000) throw new Truncation("Notebook cell limit");
if (!cell || typeof cell !== "object") continue;
const kind = "cell_type" in cell && cell.cell_type === "code" ? "code" : "markdown";
yield `## Cell ${index + 1} (${kind})\n`;
if ("source" in cell) yield* lines(cell.source);
if ("outputs" in cell && Array.isArray(cell.outputs)) {
yield "\nOutput:\n";
for (const output of cell.outputs) {
if (!output || typeof output !== "object") continue;
if ("text" in output) yield* lines(output.text);
else if (
"data" in output &&
output.data &&
typeof output.data === "object" &&
"text/plain" in output.data
)
yield* lines(output.data["text/plain"]);
else if ("output_type" in output && output.output_type === "error") {
if ("ename" in output) yield* lines(output.ename);
if ("evalue" in output) {
yield ": ";
yield* lines(output.evalue);
}
yield "\n";
if ("traceback" in output)
for (const line of lines(output.traceback)) {
yield NodeUtil.stripVTControlCharacters(line);
yield "\n";
}
}
yield "\n";
}
}
yield "\n\n";
}
}
function* rtf(source: string): Generator<string, void> {
if (!source.startsWith("{\\rtf")) throw new Error("Invalid RTF document");
let state = { skip: false, uc: 1, decoder: new TextDecoder("windows-1252") };
const stack: Array<typeof state> = [];
const symbols = new Map([
["endash", "–"],
["emdash", characterEntities.mdash],
["enspace", "\u2002"],
["emspace", "\u2003"],
["qmspace", "\u2005"],
["lquote", "‘"],
["rquote", "’"],
["ldblquote", "“"],
["rdblquote", "”"],
["bullet", "•"],
["~", "\u00a0"],
["_", "\u2011"],
["-", ""],
]);
let fallback = 0;
for (let i = 0; i < source.length; i++) {
const char = source[i]!;
if (char === "{" || char === "}") fallback = 0;
if (char === "{") {
stack.push({ ...state });
if (stack.length > 128) throw new Truncation("RTF depth limit");
} else if (char === "}") state = stack.pop() ?? state;
else if (char === "\\") {
const rest = source.slice(i + 1, i + 80);
const hex = /^'([0-9a-f]{2})/i.exec(rest);
const word = /^([a-z]+)(-?\d+)? ?/i.exec(rest);
if (hex) {
if (fallback) fallback--;
else if (!state.skip)
yield state.decoder.decode(Uint8Array.of(Number.parseInt(hex[1]!, 16)));
i += 3;
} else if (word) {
i += word[0].length;
const control = word[1]!;
const value = Number(word[2]);
if (control === "bin") {
if (!Number.isSafeInteger(value) || value < 0)
throw new Error("Invalid RTF binary length");
i += value;
}
if (fallback) {
fallback--;
continue;
}
if (
[
"fonttbl",
"colortbl",
"stylesheet",
"info",
"pict",
"object",
"fldinst",
"header",
"footer",
].includes(control)
)
state.skip = true;
if (control === "ansicpg") {
// Per-byte decoding only supports single-byte Windows code pages.
if (value !== 874 && (value < 1250 || value > 1258))
throw new Error("Unsupported RTF code page");
state.decoder = new TextDecoder(`windows-${value}`, { fatal: true });
} else if (control === "uc") {
if (!Number.isSafeInteger(value) || value < 0)
throw new Error("Invalid RTF fallback count");
state.uc = value;
} else if (control === "u" && !state.skip) {
yield String.fromCharCode(value < 0 ? value + 65536 : value);
fallback = state.uc;
} else if (!state.skip && ["par", "line", "row"].includes(control)) yield "\n";
else if (!state.skip && ["tab", "cell"].includes(control)) yield "\t";
else if (!state.skip && symbols.has(control)) yield symbols.get(control)!;
} else {
i++;
if (fallback) fallback--;
else if (rest[0] === "*") state.skip = true;
// A backslash before a line break is \par; TextEdit ends paragraphs this way.
else if (!state.skip && (rest[0] === "\n" || rest[0] === "\r")) yield "\n";
else if (!state.skip && ["\\", "{", "}"].includes(rest[0]!)) yield rest[0]!;
else if (!state.skip && symbols.has(rest[0]!)) yield symbols.get(rest[0]!)!;
}
} else if (!state.skip && char !== "\n" && char !== "\r") {
if (fallback) fallback--;
else
yield char.charCodeAt(0) < 128
? char
: state.decoder.decode(Uint8Array.of(char.charCodeAt(0)));
}
}
}
async function* document(path: string, extension: string): AsyncGenerator<TextPart, void> {
if ((await NodeFSP.stat(path)).size > MAX_INPUT) throw new Truncation("Input byte limit");
if (extension === ".ipynb") yield* notebook(await NodeFSP.readFile(path, "utf8"));
else if (extension === ".rtf") yield* rtf((await NodeFSP.readFile(path)).toString("latin1"));
else {
const files = await archive(path);
const notes: string[] = [];
const skip = (name: string) =>
notes.push(`\n[Skipped document part: ${name} (missing or invalid XML).]\n`);
const read = (name: string): XmlNode => {
try {
const data = files.get(name);
if (!data) throw new Error("Missing document part");
return xml(data, [".odt", ".ods", ".odp"].includes(extension), extension === ".epub");
} catch (error) {
if (error instanceof Truncation) throw error;
skip(name);
return { name: "root", attrs: {}, children: [] };
}
};
const relations = (name: string) =>
new Map(
[...all(read(name), "Relationship")]
.filter((rel) => rel.attrs.TargetMode !== "External")
.map((rel) => [rel.attrs.Id, rel.attrs.Target]),
);
if (extension === ".docx") yield* paragraphs(read("word/document.xml"));
else if ([".odt", ".ods", ".odp"].includes(extension)) {
const content = read("content.xml");
if (extension === ".odt") yield* paragraphs(content);
else {
let index = 0;
for (const part of all(content, extension === ".ods" ? "table" : "page")) {
yield extension === ".ods"
? `\n\n## Sheet: ${part.attrs.name ?? "Untitled"}\n`
: `\n\n## Slide ${++index}\n`;
if (extension === ".ods") yield* table(part);
else yield* paragraphs(part);
}
}
} else if (extension === ".xlsx") {
const workbook = read("xl/workbook.xml");
const dateSystem = ["1", "true"].includes(
all(workbook, "workbookPr").next().value?.attrs.date1904 ?? "",
)
? "1904"
: "1900";
const styles = files.has("xl/styles.xml") ? read("xl/styles.xml") : undefined;
const customFormats = new Map(
styles
? [...all(styles, "numFmt")].map((node) => [node.attrs.numFmtId, node.attrs.formatCode])
: [],
);
const cellFormats = styles ? all(styles, "cellXfs").next().value : undefined;
const formats = cellFormats ? children(cellFormats) : [];
const strings = files.has("xl/sharedStrings.xml")
? [...all(read("xl/sharedStrings.xml"), "si")]
: [];
const populatedStrings = strings.map(hasCellText);
const rels = relations("xl/_rels/workbook.xml.rels");
for (const sheet of all(workbook, "sheet")) {
yield `\n## Sheet: ${sheet.attrs.name}\n`;
const target = rels.get(sheet.attrs["r:id"] ?? sheet.attrs.id);
if (!target) {
skip(`sheet relationship ${sheet.attrs.name}`);
continue;
}
const name = target.startsWith("/")
? target.slice(1)
: NodePath.posix.normalize(`xl/${target}`);
const populated = (cell: XmlNode) => {
const value = all(cell, "v").next().value;
if (cell.attrs.t === "s") {
const raw = value?.children.find((child) => typeof child === "string")?.trim();
return !!raw && !!populatedStrings[Number(raw)];
}
if (cell.attrs.t === "inlineStr") return hasCellText(cell);
if (value && hasCellText(value)) return true;
for (const formula of all(cell, "f")) if (hasCellText(formula)) return true;
return false;
};
let rowCount = 0;
for (const row of all(read(name), "row")) {
const cells: Array<XmlNode | undefined> = [];
let nextColumn = 0;
let columnsTruncated = false;
for (const cell of children(row).filter((node) => node.name === "c")) {
const letters = cell.attrs.r?.match(/^[A-Z]+/)?.[0];
const column = letters
? [...letters].reduce((n, char) => n * 26 + char.charCodeAt(0) - 64, 0) - 1
: nextColumn;
nextColumn = column + 1;
if (!populated(cell)) continue;
if (column >= 512) {
columnsTruncated = true;
continue;
}
cells[column] = cell;
}
if (!cells.length && !columnsTruncated) continue;
if (++rowCount > 10_000) throw new Truncation("Row limit");
for (const [column, cell] of cells.entries()) {
if (column) yield "\t";
if (!cell) continue;
const value = all(cell, "v").next().value;
const raw = value?.children.find((child) => typeof child === "string") ?? "";
if (cell.attrs.t === "s") {
const shared = strings[Number(raw)];
if (shared) yield* inCell(text(shared));
} else if (cell.attrs.t === "inlineStr") yield* inCell(text(cell));
else {
if (value && hasCellText(value)) yield* inCell(text(value));
else for (const formula of all(cell, "f")) yield* inCell(text(formula));
const formatId = formats[Number(cell.attrs.s ?? 0)]?.attrs.numFmtId;
const format = formatId ? customFormats.get(formatId) : undefined;
if (
(cell.attrs.t === undefined || cell.attrs.t === "n") &&
raw.trim() &&
formatId &&
formatId !== "0"
)
yield ` [Excel numFmtId=${formatId}${format ? `; format=${format}` : ""}; date system ${dateSystem}]`;
}
}
yield "\n";
if (columnsTruncated) throw new Truncation("Column limit");
}
}
} else if (extension === ".pptx") {
const rels = relations("ppt/_rels/presentation.xml.rels");
let index = 0;
for (const slide of all(read("ppt/presentation.xml"), "sldId")) {
yield `\n\n## Slide ${++index}\n`;
const target = rels.get(slide.attrs["r:id"] ?? slide.attrs.id);
if (!target) {
skip(`slide relationship ${index}`);
continue;
}
const name = target.startsWith("/")
? target.slice(1)
: NodePath.posix.normalize(`ppt/${target}`);
yield* paragraphs(read(name));
const slideRels = NodePath.posix.join(
NodePath.posix.dirname(name),
"_rels",
`${NodePath.posix.basename(name)}.rels`,
);
if (!files.has(slideRels)) continue;
for (const rel of all(read(slideRels), "Relationship")) {
const target = rel.attrs.Target;
if (
!rel.attrs.Type?.endsWith("/notesSlide") ||
rel.attrs.TargetMode === "External" ||
!target
)
continue;
yield "Notes:\n";
yield* paragraphs(
read(
target.startsWith("/")
? target.slice(1)
: NodePath.posix.join(NodePath.posix.dirname(name), target),
),
);
}
}
} else if (extension === ".epub") {
const rootfile = all(read("META-INF/container.xml"), "rootfile").next().value?.attrs[
"full-path"
];
if (!rootfile) {
if (!notes.length) skip("EPUB package");
yield* notes;
return;
}
const packageXml = read(rootfile);
const items = new Map(
[...all(packageXml, "item")].map((item) => [item.attrs.id, item.attrs.href]),
);
let index = 0;
for (const item of all(packageXml, "itemref")) {
yield `\n\n## Chapter ${++index}\n`;
const href = items.get(item.attrs.idref);
if (!href) {
skip(`chapter ${index}`);
continue;
}
let decoded: string;
try {
decoded = decodeURIComponent(href.split("#")[0]!);
} catch {
skip(`chapter ${index}`);
continue;
}
const name = NodePath.posix.normalize(
NodePath.posix.join(NodePath.posix.dirname(rootfile), decoded),
);
yield* paragraphs(read(name));
}
} else throw new Error("Unsupported document");
yield* notes;
}
}
export async function extractDocumentText(path: string, extension: string): Promise<string> {
let result = "";
let remaining = MAX_OUTPUT;
let cellDepth = 0;
let started = false;
let space = false;
let truncated = false;
try {
// Charge raw text before normalization so repeated whitespace also exhausts the budget.
for await (const part of document(path, extension)) {
if (part === CELL_START) {
if (cellDepth++ === 0) {
started = false;
space = false;
} else if (started) space = true;
continue;
}
if (part === CELL_END) {
cellDepth--;
if (cellDepth > 0 && started) space = true;
continue;
}
if (typeof part !== "string" && started) continue;
const raw = typeof part === "string" ? part : part.fallback;
let value = raw.slice(0, remaining).replaceAll("\0", "");
remaining -= Math.min(raw.length, remaining);
if (cellDepth) {
value = value.replaceAll(/\s+/g, " ");
const trimmed = value.trim();
if (trimmed) {
result += (started && (space || value.startsWith(" ")) ? " " : "") + trimmed;
started = true;
}
space = value.endsWith(" ") || (!value && space);
} else result += value;
if (remaining === 0) throw new Truncation("Output budget");
}
} catch (error) {
if (!(error instanceof Truncation)) throw error;
truncated = true;
}
result = result.replaceAll(/\n(?:[ \t]*\n){2,}/g, "\n\n").trim();
if (!result && !truncated) throw new Error("No text extracted");
return result + (truncated ? TRUNCATED : "");
}