llm-large-files 0.7.0 → 0.8.1
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 +370 -1
- package/README.md +12 -8
- package/dist/aggregate.d.ts +103 -0
- package/dist/aggregate.d.ts.map +1 -0
- package/dist/aggregate.js +651 -0
- package/dist/aggregate.js.map +1 -0
- package/dist/analyze.d.ts +3 -1
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +109 -14
- package/dist/analyze.js.map +1 -1
- package/dist/anthropic/adapter.d.ts.map +1 -1
- package/dist/anthropic/adapter.js +4 -1
- package/dist/anthropic/adapter.js.map +1 -1
- package/dist/ask.d.ts +3 -3
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +72 -17
- package/dist/ask.js.map +1 -1
- package/dist/decode.d.ts +19 -0
- package/dist/decode.d.ts.map +1 -0
- package/dist/decode.js +97 -0
- package/dist/decode.js.map +1 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +65 -0
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +6 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +5 -1
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +23 -0
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +12 -18
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +82 -746
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +9 -5
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +5 -1
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +11 -2
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +20 -3
- package/dist/gemini/generate.js.map +1 -1
- package/dist/groups.d.ts +54 -0
- package/dist/groups.d.ts.map +1 -0
- package/dist/groups.js +136 -0
- package/dist/groups.js.map +1 -0
- package/dist/html.d.ts +11 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +1112 -0
- package/dist/html.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +57 -14
- package/dist/library.js.map +1 -1
- package/dist/limiter.d.ts +2 -1
- package/dist/limiter.d.ts.map +1 -1
- package/dist/limiter.js +3 -2
- package/dist/limiter.js.map +1 -1
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +8 -1
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +69 -15
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +38 -0
- package/dist/node/ingest-child.d.ts.map +1 -0
- package/dist/node/ingest-child.js +42 -0
- package/dist/node/ingest-child.js.map +1 -0
- package/dist/node/isolated.d.ts +88 -0
- package/dist/node/isolated.d.ts.map +1 -0
- package/dist/node/isolated.js +381 -0
- package/dist/node/isolated.js.map +1 -0
- package/dist/office.d.ts +105 -0
- package/dist/office.d.ts.map +1 -0
- package/dist/office.js +1092 -0
- package/dist/office.js.map +1 -0
- package/dist/openai/adapter.d.ts.map +1 -1
- package/dist/openai/adapter.js +15 -1
- package/dist/openai/adapter.js.map +1 -1
- package/dist/pdf-pages.d.ts.map +1 -1
- package/dist/pdf-pages.js +2 -6
- package/dist/pdf-pages.js.map +1 -1
- package/dist/peers.d.ts +8 -0
- package/dist/peers.d.ts.map +1 -0
- package/dist/peers.js +32 -0
- package/dist/peers.js.map +1 -0
- package/dist/plan.d.ts +3 -1
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +17 -1
- package/dist/plan.js.map +1 -1
- package/dist/quality.d.ts +16 -0
- package/dist/quality.d.ts.map +1 -0
- package/dist/quality.js +157 -0
- package/dist/quality.js.map +1 -0
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +4 -2
- package/dist/ranged.js.map +1 -1
- package/dist/search.d.ts +99 -22
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +744 -140
- package/dist/search.js.map +1 -1
- package/dist/text-index.d.ts +3 -1
- package/dist/text-index.d.ts.map +1 -1
- package/dist/text-index.js +3 -1
- package/dist/text-index.js.map +1 -1
- package/dist/text.d.ts +27 -3
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +225 -23
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts +9 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +457 -16
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +23 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/word-styles.d.ts +102 -0
- package/dist/word-styles.d.ts.map +1 -0
- package/dist/word-styles.js +419 -0
- package/dist/word-styles.js.map +1 -0
- package/dist/xlsx.d.ts +23 -0
- package/dist/xlsx.d.ts.map +1 -0
- package/dist/xlsx.js +511 -0
- package/dist/xlsx.js.map +1 -0
- package/dist/xml-stream.d.ts +55 -0
- package/dist/xml-stream.d.ts.map +1 -0
- package/dist/xml-stream.js +235 -0
- package/dist/xml-stream.js.map +1 -0
- package/docs/README.md +8 -8
- package/docs/getting-started.md +1 -1
- package/docs/guides/costs-and-limits.md +7 -5
- package/docs/guides/deployment.md +95 -29
- package/docs/guides/errors-and-reliability.md +23 -10
- package/docs/guides/pdfs.md +127 -74
- package/docs/guides/storage.md +1 -1
- package/docs/guides/testing.md +5 -1
- package/docs/guides/web-app.md +10 -9
- package/docs/how-it-works.md +80 -50
- package/docs/reference/ask.md +26 -8
- package/docs/reference/errors-and-retries.md +5 -3
- package/docs/reference/events.md +14 -6
- package/docs/reference/gemini.md +6 -2
- package/docs/reference/library.md +221 -79
- package/docs/reference/node.md +201 -23
- package/docs/reference/openai.md +1 -1
- package/docs/reference/tools.md +269 -23
- package/docs/security.md +114 -69
- package/docs/troubleshooting.md +40 -20
- package/package.json +6 -2
- package/src/aggregate.ts +674 -0
- package/src/analyze.ts +117 -13
- package/src/anthropic/adapter.ts +4 -1
- package/src/ask.ts +73 -17
- package/src/decode.ts +89 -0
- package/src/errors.ts +108 -0
- package/src/events.ts +10 -1
- package/src/evidence.ts +23 -0
- package/src/extract.ts +79 -701
- package/src/gemini/adapter.ts +9 -5
- package/src/gemini/endpoints.ts +12 -2
- package/src/gemini/generate.ts +19 -3
- package/src/groups.ts +162 -0
- package/src/html.ts +1071 -0
- package/src/index.ts +2 -1
- package/src/library.ts +52 -14
- package/src/limiter.ts +6 -1
- package/src/model.ts +8 -1
- package/src/node/handlers.ts +66 -16
- package/src/node/index.ts +2 -0
- package/src/node/ingest-child.ts +61 -0
- package/src/node/isolated.ts +464 -0
- package/src/office.ts +1047 -0
- package/src/openai/adapter.ts +14 -1
- package/src/pdf-pages.ts +2 -5
- package/src/peers.ts +29 -0
- package/src/plan.ts +19 -1
- package/src/quality.ts +146 -0
- package/src/ranged.ts +4 -2
- package/src/search.ts +738 -146
- package/src/text-index.ts +4 -2
- package/src/text.ts +214 -23
- package/src/tools.ts +440 -16
- package/src/types.ts +20 -3
- package/src/word-styles.ts +420 -0
- package/src/xlsx.ts +478 -0
- package/src/xml-stream.ts +224 -0
package/src/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Public types. Everything a consumer touches is defined here, so the package can be
|
|
2
2
|
// published without exposing internal module layout.
|
|
3
3
|
|
|
4
|
-
export type DocumentFormat = "text" | "markdown" | "log" | "json" | "csv" | "pdf" | "docx" | "pptx";
|
|
4
|
+
export type DocumentFormat = "text" | "markdown" | "log" | "json" | "csv" | "pdf" | "docx" | "pptx" | "xlsx" | "html";
|
|
5
5
|
|
|
6
6
|
export type OutlineEntry = {
|
|
7
7
|
/** 1-based line number in the extracted text. */
|
|
@@ -69,6 +69,23 @@ export type DocumentMeta = {
|
|
|
69
69
|
originalKept?: boolean;
|
|
70
70
|
outline: OutlineEntry[];
|
|
71
71
|
logProfile?: LogProfile;
|
|
72
|
+
/**
|
|
73
|
+
* The pages of a PDF that have no text at all: scans, photos, charts. Up to 200 are listed.
|
|
74
|
+
* Absent when there are none, and on documents from before 0.8.1.
|
|
75
|
+
*/
|
|
76
|
+
pagesWithoutText?: number[];
|
|
77
|
+
/**
|
|
78
|
+
* The pages of a PDF whose text looks garbled: few letters and digits among their characters, or
|
|
79
|
+
* many replacement characters (a font without a Unicode map). Up to 200 are listed. Absent when
|
|
80
|
+
* there are none, and on documents from before 0.8.1.
|
|
81
|
+
*/
|
|
82
|
+
garbledPages?: number[];
|
|
83
|
+
/**
|
|
84
|
+
* The scripts the text is written in, by their share of its letters in percent ("Hebrew" 81,
|
|
85
|
+
* "Latin" 19), from its first 2 MB, those of 5% or more. Absent for a text in Latin letters alone,
|
|
86
|
+
* one with few letters, and documents from before 0.8.1.
|
|
87
|
+
*/
|
|
88
|
+
scripts?: Array<{ script: string; percent: number }>;
|
|
72
89
|
createdAt: string;
|
|
73
90
|
expiresAt: string;
|
|
74
91
|
};
|
|
@@ -235,8 +252,8 @@ export type ToolOutput = { result: ToolResult; files: ToolFile[] };
|
|
|
235
252
|
export type ToolCallLimits = {
|
|
236
253
|
/**
|
|
237
254
|
* The most bytes the files of this call may have together. Give the room left in your request,
|
|
238
|
-
* times 3/4 when files are sent as base64. Defaults to 3/8 of `maxRequestBytes`
|
|
239
|
-
*
|
|
255
|
+
* times 3/4 when files are sent as base64. Defaults to 3/8 of (`maxRequestBytes` − 262,144), the
|
|
256
|
+
* 256 KB the library keeps free in a request taken off first: 3,651,696 bytes for the default budget.
|
|
240
257
|
*/
|
|
241
258
|
maxFileBytes?: number;
|
|
242
259
|
/** The most pages this call may show. Lower it as the pages already shown use up your token budget. */
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { sliceWhole } from "./text.js";
|
|
2
|
+
import { readXml, type XmlAttributes } from "./xml-stream.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Word's paragraph styles and list numbering, read from `word/styles.xml` and
|
|
6
|
+
* `word/numbering.xml`, so that a paragraph's text can be written with the number Word shows before
|
|
7
|
+
* it ("2.1", "(a)", "ג.") and a heading as a Markdown heading. Without them, "clause 2.2" of a
|
|
8
|
+
* contract cannot be found, and the document has no outline.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** What a paragraph says of itself in its properties (`w:pPr`). */
|
|
12
|
+
export type ParagraphStyle = {
|
|
13
|
+
styleId?: string;
|
|
14
|
+
/** `w:numPr`: the list and its level. A `numId` of "0" takes numbering off. */
|
|
15
|
+
numId?: string;
|
|
16
|
+
ilvl?: number;
|
|
17
|
+
/** `w:outlineLvl`, 0-based: 0 is a top-level heading, 9 is body text. */
|
|
18
|
+
outlineLevel?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A level of a list: its start, its number format, its number text cut into the text between the
|
|
23
|
+
* numbers and the level of each number (`"%1.%2"` is `[0, ".", 1]`), and whether it writes the
|
|
24
|
+
* numbers of all levels in Arabic digits (`w:isLgl`, Word's "legal style numbering").
|
|
25
|
+
*/
|
|
26
|
+
type Level = { start: number; format: string; parts: Array<string | number>; legal: boolean };
|
|
27
|
+
/**
|
|
28
|
+
* A list definition (`w:abstractNum`): its levels, the list style it defines (`w:styleLink`), and
|
|
29
|
+
* the list style it takes its levels from instead (`w:numStyleLink`). Word writes a list made with
|
|
30
|
+
* a list style, as its "1 / 1.1 / 1.1.1" or "Article / Section", as a definition with no levels.
|
|
31
|
+
*/
|
|
32
|
+
type ListDefinition = { levels: Level[]; styleLink?: string; numStyleLink?: string };
|
|
33
|
+
/** A list (`w:num`): its definition, and the levels it starts at another number. */
|
|
34
|
+
type Num = { abstractId: string; starts: Map<number, number> };
|
|
35
|
+
type StyleInfo = { name: string; basedOn?: string; numId?: string; ilvl?: number; outlineLevel?: number };
|
|
36
|
+
/** What a paragraph style and the styles it is based on give its paragraphs: a list and level, a heading level, an outline level. */
|
|
37
|
+
type Resolved = { numId?: string; ilvl?: number; heading?: number; outline?: number };
|
|
38
|
+
/** A list as it numbers paragraphs: the definition whose counts it shares, its levels, and its start overrides. */
|
|
39
|
+
type List = { key: string; levels: Level[]; starts: Map<number, number> };
|
|
40
|
+
/**
|
|
41
|
+
* A paragraph counted in its list, as `WordNumbering.count` gives it to `labelOf`: the levels of
|
|
42
|
+
* its list, its level, and the counts of the list.
|
|
43
|
+
*/
|
|
44
|
+
type Counted ={ levels: Level[]; level: number; counts: Array<number | undefined> };
|
|
45
|
+
|
|
46
|
+
/** Levels of numbering a list may have in Word. */
|
|
47
|
+
const LEVELS = 9;
|
|
48
|
+
/**
|
|
49
|
+
* The characters of a level's number text kept, and of the number written from it: Word's dialog
|
|
50
|
+
* takes far fewer. Each `%n` of the text may be a Roman numeral of 15 letters, and the number is
|
|
51
|
+
* written before every paragraph of the list.
|
|
52
|
+
*/
|
|
53
|
+
const MAX_LABEL = 64;
|
|
54
|
+
/** Links from a list definition to a list style, and on to the definition of that style, followed at most: Word makes one. */
|
|
55
|
+
const MAX_LIST_LINKS = 4;
|
|
56
|
+
const NOTHING: Resolved = {};
|
|
57
|
+
|
|
58
|
+
const attribute = (attributes: XmlAttributes, name: string) => attributes[`w:${name}`] ?? attributes[name];
|
|
59
|
+
const integerOf = (value: string | undefined): number | undefined => {
|
|
60
|
+
if (value === undefined) return undefined;
|
|
61
|
+
const number = Number.parseInt(value, 10);
|
|
62
|
+
return Number.isFinite(number) ? number : undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** A start value of a list, within what Word allows (0 to 32,767): a file may say anything. */
|
|
66
|
+
const startOf = (value: string | undefined): number | undefined => {
|
|
67
|
+
const number = integerOf(value);
|
|
68
|
+
return number === undefined ? undefined : Math.min(32_767, Math.max(0, number));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Reads `w:style` elements: names, the style each is based on, and the numbering and outline level
|
|
73
|
+
* in its paragraph properties. Paragraph styles and list styles (`w:type="numbering"`, whose list a
|
|
74
|
+
* list definition may take its levels from) are kept apart.
|
|
75
|
+
*/
|
|
76
|
+
function readStyles(xml: string | undefined): { styles: Map<string, StyleInfo>; listStyles: Map<string, StyleInfo> } {
|
|
77
|
+
const styles = new Map<string, StyleInfo>();
|
|
78
|
+
const listStyles = new Map<string, StyleInfo>();
|
|
79
|
+
if (!xml) return { styles, listStyles };
|
|
80
|
+
let current: StyleInfo | undefined;
|
|
81
|
+
let inParagraphProperties = 0;
|
|
82
|
+
let inChange = 0;
|
|
83
|
+
readXml(xml, {
|
|
84
|
+
open(name, attributes, empty) {
|
|
85
|
+
if (name === "w:style") {
|
|
86
|
+
const id = attribute(attributes, "styleId");
|
|
87
|
+
const type = attribute(attributes, "type");
|
|
88
|
+
const kept = type === undefined || type === "paragraph" ? styles : type === "numbering" ? listStyles : undefined;
|
|
89
|
+
current = id && kept ? { name: "" } : undefined;
|
|
90
|
+
if (current && id) kept?.set(id, current);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (!current) return;
|
|
94
|
+
if (name === "w:pPrChange" && !empty) inChange++;
|
|
95
|
+
if (inChange > 0) return;
|
|
96
|
+
if (name === "w:pPr" && !empty) inParagraphProperties++;
|
|
97
|
+
else if (name === "w:name") current.name = (attribute(attributes, "val") ?? "").toLowerCase();
|
|
98
|
+
else if (name === "w:basedOn") current.basedOn = attribute(attributes, "val");
|
|
99
|
+
else if (inParagraphProperties > 0 && name === "w:numId") current.numId = attribute(attributes, "val");
|
|
100
|
+
else if (inParagraphProperties > 0 && name === "w:ilvl") current.ilvl = integerOf(attribute(attributes, "val"));
|
|
101
|
+
else if (inParagraphProperties > 0 && name === "w:outlineLvl") current.outlineLevel = integerOf(attribute(attributes, "val"));
|
|
102
|
+
},
|
|
103
|
+
close(name) {
|
|
104
|
+
if (name === "w:style") current = undefined;
|
|
105
|
+
else if (name === "w:pPrChange" && inChange > 0) inChange--;
|
|
106
|
+
else if (name === "w:pPr" && inParagraphProperties > 0) inParagraphProperties--;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
return { styles, listStyles };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** `"%1.%2."` as `[0, ".", 1, "."]`: the text of a level, with the level of each number in it. */
|
|
113
|
+
function partsOf(text: string): Array<string | number> {
|
|
114
|
+
const parts: Array<string | number> = [];
|
|
115
|
+
let from = 0;
|
|
116
|
+
for (const match of text.matchAll(/%([1-9])/g)) {
|
|
117
|
+
if (match.index > from) parts.push(text.slice(from, match.index));
|
|
118
|
+
parts.push(Number(match[1]) - 1);
|
|
119
|
+
from = match.index + match[0].length;
|
|
120
|
+
}
|
|
121
|
+
if (from < text.length) parts.push(text.slice(from));
|
|
122
|
+
return parts;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Reads `w:abstractNum` and `w:num` elements. */
|
|
126
|
+
function readNumbering(xml: string | undefined): { abstracts: Map<string, ListDefinition>; nums: Map<string, Num> } {
|
|
127
|
+
const abstracts = new Map<string, ListDefinition>();
|
|
128
|
+
const nums = new Map<string, Num>();
|
|
129
|
+
if (!xml) return { abstracts, nums };
|
|
130
|
+
let abstract: ListDefinition | undefined;
|
|
131
|
+
let level: Level | undefined;
|
|
132
|
+
let num: Num | undefined;
|
|
133
|
+
let overrideLevel: number | undefined;
|
|
134
|
+
readXml(xml, {
|
|
135
|
+
open(name, attributes, empty) {
|
|
136
|
+
if (name === "w:abstractNum" && !empty) {
|
|
137
|
+
abstract = { levels: [] };
|
|
138
|
+
const id = attribute(attributes, "abstractNumId");
|
|
139
|
+
if (id !== undefined) abstracts.set(id, abstract);
|
|
140
|
+
} else if (abstract && name === "w:lvl" && !empty) {
|
|
141
|
+
const index = integerOf(attribute(attributes, "ilvl")) ?? 0;
|
|
142
|
+
level = { start: 1, format: "decimal", parts: [index, "."], legal: false };
|
|
143
|
+
if (index >= 0 && index < LEVELS) abstract.levels[index] = level;
|
|
144
|
+
} else if (level && name === "w:start") {
|
|
145
|
+
level.start = startOf(attribute(attributes, "val")) ?? 1;
|
|
146
|
+
} else if (level && name === "w:numFmt") {
|
|
147
|
+
level.format = attribute(attributes, "val") ?? "decimal";
|
|
148
|
+
} else if (level && name === "w:lvlText") {
|
|
149
|
+
// Word writes a few characters here ("%1.%2."); a long one would be copied before every paragraph.
|
|
150
|
+
level.parts = partsOf(sliceWhole(attribute(attributes, "val") ?? "", MAX_LABEL));
|
|
151
|
+
} else if (level && name === "w:isLgl") {
|
|
152
|
+
level.legal = !["0", "false", "off"].includes(attribute(attributes, "val") ?? "");
|
|
153
|
+
} else if (abstract && !level && name === "w:styleLink") {
|
|
154
|
+
abstract.styleLink = attribute(attributes, "val");
|
|
155
|
+
} else if (abstract && !level && name === "w:numStyleLink") {
|
|
156
|
+
abstract.numStyleLink = attribute(attributes, "val");
|
|
157
|
+
} else if (name === "w:num" && !empty) {
|
|
158
|
+
num = { abstractId: "", starts: new Map() };
|
|
159
|
+
const id = attribute(attributes, "numId");
|
|
160
|
+
if (id !== undefined) nums.set(id, num);
|
|
161
|
+
} else if (num && name === "w:abstractNumId") {
|
|
162
|
+
num.abstractId = attribute(attributes, "val") ?? "";
|
|
163
|
+
} else if (num && name === "w:lvlOverride") {
|
|
164
|
+
overrideLevel = integerOf(attribute(attributes, "ilvl"));
|
|
165
|
+
} else if (num && overrideLevel !== undefined && overrideLevel >= 0 && overrideLevel < LEVELS && name === "w:startOverride") {
|
|
166
|
+
const start = startOf(attribute(attributes, "val"));
|
|
167
|
+
if (start !== undefined) num.starts.set(overrideLevel, start);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
close(name) {
|
|
171
|
+
if (name === "w:abstractNum") abstract = undefined;
|
|
172
|
+
else if (name === "w:lvl") level = undefined;
|
|
173
|
+
else if (name === "w:num") num = undefined;
|
|
174
|
+
else if (name === "w:lvlOverride") overrideLevel = undefined;
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
return { abstracts, nums };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const ROMAN: Array<[number, string]> = [[1000, "m"], [900, "cm"], [500, "d"], [400, "cd"], [100, "c"], [90, "xc"], [50, "l"], [40, "xl"], [10, "x"], [9, "ix"], [5, "v"], [4, "iv"], [1, "i"]];
|
|
181
|
+
|
|
182
|
+
function roman(value: number): string {
|
|
183
|
+
// Roman numerals end at 3999; past it Word writes the number.
|
|
184
|
+
if (value < 1 || value > 3_999) return String(value);
|
|
185
|
+
let rest = value;
|
|
186
|
+
let out = "";
|
|
187
|
+
for (const [size, letters] of ROMAN) {
|
|
188
|
+
while (rest >= size) {
|
|
189
|
+
out += letters;
|
|
190
|
+
rest -= size;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** a, b, … z, aa, bb, …: Word repeats the letter. */
|
|
197
|
+
function letters(value: number): string {
|
|
198
|
+
// Past "zzzzzzzzzz" (260) the letters would only grow: the number is written.
|
|
199
|
+
if (value < 1 || value > 260) return String(value);
|
|
200
|
+
const letter = String.fromCharCode(97 + ((value - 1) % 26));
|
|
201
|
+
return letter.repeat(Math.floor((value - 1) / 26) + 1);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const HEBREW_ONES = ["", "א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט"];
|
|
205
|
+
const HEBREW_TENS = ["", "י", "כ", "ל", "מ", "נ", "ס", "ע", "פ", "צ"];
|
|
206
|
+
const HEBREW_HUNDREDS = ["", "ק", "ר", "ש", "ת"];
|
|
207
|
+
const HEBREW_LETTERS = "אבגדהוזחטיכלמנסעפצקרשת";
|
|
208
|
+
|
|
209
|
+
/** Hebrew numerals (hebrew1): א, ב, … י, יא, … טו and טז for 15 and 16, as Word writes them. */
|
|
210
|
+
function hebrewNumeral(value: number): string {
|
|
211
|
+
if (value < 1) return String(value);
|
|
212
|
+
let rest = value % 1000;
|
|
213
|
+
let out = "";
|
|
214
|
+
while (rest >= 400) {
|
|
215
|
+
out += "ת";
|
|
216
|
+
rest -= 400;
|
|
217
|
+
}
|
|
218
|
+
out += HEBREW_HUNDREDS[Math.floor(rest / 100)]!;
|
|
219
|
+
rest %= 100;
|
|
220
|
+
if (rest === 15) return `${out}טו`;
|
|
221
|
+
if (rest === 16) return `${out}טז`;
|
|
222
|
+
return out + HEBREW_TENS[Math.floor(rest / 10)]! + HEBREW_ONES[rest % 10]!;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** A counter in a list format of Word. Formats it does not know are written as numbers. */
|
|
226
|
+
export function formatNumber(value: number, format: string): string {
|
|
227
|
+
switch (format) {
|
|
228
|
+
case "lowerLetter": return letters(value);
|
|
229
|
+
case "upperLetter": return letters(value).toUpperCase();
|
|
230
|
+
case "lowerRoman": return roman(value);
|
|
231
|
+
case "upperRoman": return roman(value).toUpperCase();
|
|
232
|
+
case "decimalZero": return value < 10 ? `0${value}` : String(value);
|
|
233
|
+
case "hebrew1": return hebrewNumeral(value);
|
|
234
|
+
case "hebrew2": return value < 1 ? String(value) : HEBREW_LETTERS[(value - 1) % HEBREW_LETTERS.length]!;
|
|
235
|
+
case "none": return "";
|
|
236
|
+
default: return String(value);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Private-use characters: the bullets of the Symbol and Wingdings fonts. */
|
|
241
|
+
const PRIVATE_USE = /[-]/u;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Numbers the paragraphs of one document in order, as Word does: one count for each list and level,
|
|
245
|
+
* shared by the lists made from one definition (or from one list style), restarted by a list that
|
|
246
|
+
* says where to start, and the deeper levels started again when a level counts up.
|
|
247
|
+
*
|
|
248
|
+
* Every paragraph of a list is counted, an empty one too, but its number is written only when it
|
|
249
|
+
* has text (`count`, then `labelOf`), and what a style gives is worked out once a style: a file of
|
|
250
|
+
* 312,500 empty paragraphs of a style based on 19 others, in a list whose number was 480
|
|
251
|
+
* characters long, took 25 seconds for 45 KB.
|
|
252
|
+
*/
|
|
253
|
+
export class WordNumbering {
|
|
254
|
+
private readonly styles: Map<string, StyleInfo>;
|
|
255
|
+
private readonly listStyles: Map<string, StyleInfo>;
|
|
256
|
+
private readonly abstracts: Map<string, ListDefinition>;
|
|
257
|
+
private readonly nums: Map<string, Num>;
|
|
258
|
+
/** The list definition that defines each list style (`w:styleLink`). */
|
|
259
|
+
private readonly styleLinks = new Map<string, string>();
|
|
260
|
+
/** The counts of each list definition, by level: undefined for a level not counted since it was last started again. */
|
|
261
|
+
private readonly counts = new Map<string, Array<number | undefined>>();
|
|
262
|
+
/** Lists whose start override has been applied. */
|
|
263
|
+
private readonly started = new Set<string>();
|
|
264
|
+
/** What each paragraph style gives, worked out at its first paragraph: the styles of a document do not change. */
|
|
265
|
+
private readonly resolved = new Map<string, Resolved>();
|
|
266
|
+
/** Each list found so far, by its `numId`; null for one with no levels. */
|
|
267
|
+
private readonly lists = new Map<string, List | null>();
|
|
268
|
+
|
|
269
|
+
constructor(stylesXml: string | undefined, numberingXml: string | undefined) {
|
|
270
|
+
({ styles: this.styles, listStyles: this.listStyles } = readStyles(stylesXml));
|
|
271
|
+
({ abstracts: this.abstracts, nums: this.nums } = readNumbering(numberingXml));
|
|
272
|
+
for (const [id, definition] of this.abstracts) {
|
|
273
|
+
if (definition.styleLink !== undefined && !this.styleLinks.has(definition.styleLink)) this.styleLinks.set(definition.styleLink, id);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** A style and the styles it is based on, nearest first, at most 20 deep. */
|
|
278
|
+
private *chain(styleId: string | undefined): Generator<StyleInfo> {
|
|
279
|
+
const seen = new Set<string>();
|
|
280
|
+
for (let id = styleId; id !== undefined && !seen.has(id) && seen.size < 20; id = this.styles.get(id)?.basedOn) {
|
|
281
|
+
seen.add(id);
|
|
282
|
+
const style = this.styles.get(id);
|
|
283
|
+
if (!style) return;
|
|
284
|
+
yield style;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* What a paragraph style gives: the nearest list, level and outline level in its chain, and the
|
|
290
|
+
* level of its nearest heading style ("heading 2", or a style whose name Word gives in English
|
|
291
|
+
* even in a Hebrew document) or title style. Kept only for the styles the file defines, which
|
|
292
|
+
* bounds what is kept.
|
|
293
|
+
*/
|
|
294
|
+
private resolve(styleId: string | undefined): Resolved {
|
|
295
|
+
if (styleId === undefined || !this.styles.has(styleId)) return NOTHING;
|
|
296
|
+
let resolved = this.resolved.get(styleId);
|
|
297
|
+
if (resolved) return resolved;
|
|
298
|
+
resolved = {};
|
|
299
|
+
for (const style of this.chain(styleId)) {
|
|
300
|
+
resolved.numId ??= style.numId;
|
|
301
|
+
resolved.ilvl ??= style.ilvl;
|
|
302
|
+
resolved.outline ??= style.outlineLevel;
|
|
303
|
+
if (resolved.heading === undefined) {
|
|
304
|
+
const named = /^heading (\d)$/.exec(style.name);
|
|
305
|
+
if (named) resolved.heading = Math.min(6, Math.max(1, Number(named[1])));
|
|
306
|
+
else if (style.name === "title") resolved.heading = 1;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
this.resolved.set(styleId, resolved);
|
|
310
|
+
return resolved;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* The level of a heading, 1 to 6, for a paragraph of a heading style, of the title style, or with
|
|
315
|
+
* an outline level. Undefined for body text.
|
|
316
|
+
*/
|
|
317
|
+
headingLevel(paragraph: ParagraphStyle): number | undefined {
|
|
318
|
+
const style = this.resolve(paragraph.styleId);
|
|
319
|
+
if (style.heading !== undefined) return style.heading;
|
|
320
|
+
const outline = paragraph.outlineLevel ?? style.outline;
|
|
321
|
+
return outline !== undefined && outline >= 0 && outline < 6 ? outline + 1 : undefined;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** The list a `numId` names, found at its first paragraph; undefined when the file defines none, or it has no levels. */
|
|
325
|
+
private list(numId: string): List | undefined {
|
|
326
|
+
const known = this.lists.get(numId);
|
|
327
|
+
if (known !== undefined) return known ?? undefined;
|
|
328
|
+
// A numId the file does not define is not kept: the paragraphs may name any number of them.
|
|
329
|
+
const num = this.nums.get(numId);
|
|
330
|
+
if (!num) return undefined;
|
|
331
|
+
const found = this.definitionOf(num.abstractId);
|
|
332
|
+
const list = found ? { key: found.id, levels: found.definition.levels, starts: num.starts } : null;
|
|
333
|
+
this.lists.set(numId, list);
|
|
334
|
+
return list ?? undefined;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The list definition whose levels and counts a definition uses: itself, or for one that takes
|
|
339
|
+
* them from a list style (`w:numStyleLink`), the definition of that style: the one that names it
|
|
340
|
+
* in `w:styleLink`, or that of the list in the style's own properties. A file may link them in a
|
|
341
|
+
* circle, so a few links are followed; a definition linked back to itself keeps its own levels.
|
|
342
|
+
*/
|
|
343
|
+
private definitionOf(abstractId: string): { id: string; definition: ListDefinition } | undefined {
|
|
344
|
+
let id = abstractId;
|
|
345
|
+
for (let link = 0; link <= MAX_LIST_LINKS; link++) {
|
|
346
|
+
const definition = this.abstracts.get(id);
|
|
347
|
+
if (!definition) return undefined;
|
|
348
|
+
const style = definition.numStyleLink;
|
|
349
|
+
if (style === undefined) return { id, definition };
|
|
350
|
+
const styleNumId = this.listStyles.get(style)?.numId;
|
|
351
|
+
const linked = this.styleLinks.get(style) ?? (styleNumId === undefined ? undefined : this.nums.get(styleNumId)?.abstractId);
|
|
352
|
+
if (linked === undefined || linked === id) return { id, definition };
|
|
353
|
+
id = linked;
|
|
354
|
+
}
|
|
355
|
+
return undefined;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Counts a paragraph in its list, as Word counts every paragraph of a list, an empty one too. It
|
|
360
|
+
* gives what `labelOf` needs to write the paragraph's number, until the next paragraph is
|
|
361
|
+
* counted, or undefined for a paragraph of no list.
|
|
362
|
+
*/
|
|
363
|
+
count(paragraph: ParagraphStyle): Counted | undefined {
|
|
364
|
+
const style = this.resolve(paragraph.styleId);
|
|
365
|
+
const numId = paragraph.numId ?? style.numId;
|
|
366
|
+
if (numId === undefined || numId === "0") return undefined;
|
|
367
|
+
const list = this.list(numId);
|
|
368
|
+
if (!list) return undefined;
|
|
369
|
+
const level = Math.min(LEVELS - 1, Math.max(0, paragraph.ilvl ?? style.ilvl ?? 0));
|
|
370
|
+
const definition = list.levels[level];
|
|
371
|
+
if (!definition) return undefined;
|
|
372
|
+
let counts = this.counts.get(list.key);
|
|
373
|
+
if (!counts) {
|
|
374
|
+
counts = new Array<number | undefined>(LEVELS).fill(undefined);
|
|
375
|
+
this.counts.set(list.key, counts);
|
|
376
|
+
}
|
|
377
|
+
if (!this.started.has(numId)) {
|
|
378
|
+
this.started.add(numId);
|
|
379
|
+
for (const [overridden, start] of list.starts) counts[overridden] = start - 1;
|
|
380
|
+
}
|
|
381
|
+
const count = counts[level];
|
|
382
|
+
counts[level] = count === undefined ? definition.start : count + 1;
|
|
383
|
+
for (let deeper = level + 1; deeper < LEVELS; deeper++) counts[deeper] = undefined;
|
|
384
|
+
return { levels: list.levels, level, counts };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* The number Word writes before a paragraph that `count` has just counted ("2.1.", "(a)", "-"
|
|
389
|
+
* for a bullet), or undefined. It is cut after `MAX_LABEL` characters.
|
|
390
|
+
*/
|
|
391
|
+
labelOf({ levels, level, counts }: Counted): string | undefined {
|
|
392
|
+
const definition = levels[level];
|
|
393
|
+
if (!definition) return undefined;
|
|
394
|
+
if (definition.format === "bullet") return "-";
|
|
395
|
+
let text = "";
|
|
396
|
+
for (const part of definition.parts) {
|
|
397
|
+
if (typeof part === "string") {
|
|
398
|
+
text += part;
|
|
399
|
+
} else {
|
|
400
|
+
const own = levels[part];
|
|
401
|
+
const format = own?.format ?? "decimal";
|
|
402
|
+
// A legal level writes the number of every level in Arabic digits. Word keeps the zero of
|
|
403
|
+
// decimalZero: "Section 1.01" of its "Article I. / Section 1.01" list is a legal level.
|
|
404
|
+
text += formatNumber(counts[part] ?? own?.start ?? 1, definition.legal && format !== "decimalZero" ? "decimal" : format);
|
|
405
|
+
}
|
|
406
|
+
if (text.length >= MAX_LABEL) {
|
|
407
|
+
text = sliceWhole(text, MAX_LABEL);
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (PRIVATE_USE.test(text)) return "-";
|
|
412
|
+
return text.trim() || undefined;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** Counts a paragraph (`count`), and gives the number Word writes before it (`labelOf`), or undefined. */
|
|
416
|
+
label(paragraph: ParagraphStyle): string | undefined {
|
|
417
|
+
const counted = this.count(paragraph);
|
|
418
|
+
return counted && this.labelOf(counted);
|
|
419
|
+
}
|
|
420
|
+
}
|