@shuji-bonji/pdf-reader-mcp 0.8.0 → 0.9.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 +94 -0
- package/README.ja.md +37 -7
- package/README.md +41 -9
- package/dist/services/actual-text-service.d.ts +99 -0
- package/dist/services/actual-text-service.d.ts.map +1 -0
- package/dist/services/actual-text-service.js +196 -0
- package/dist/services/actual-text-service.js.map +1 -0
- package/dist/services/content-stream-service.d.ts +70 -0
- package/dist/services/content-stream-service.d.ts.map +1 -0
- package/dist/services/content-stream-service.js +524 -0
- package/dist/services/content-stream-service.js.map +1 -0
- package/dist/services/pdfjs-service.d.ts +69 -24
- package/dist/services/pdfjs-service.d.ts.map +1 -1
- package/dist/services/pdfjs-service.js +163 -177
- package/dist/services/pdfjs-service.js.map +1 -1
- package/dist/services/struct-tree-service.d.ts +21 -62
- package/dist/services/struct-tree-service.d.ts.map +1 -1
- package/dist/services/struct-tree-service.js +132 -132
- package/dist/services/struct-tree-service.js.map +1 -1
- package/dist/services/struct-tree-walker.d.ts +93 -0
- package/dist/services/struct-tree-walker.d.ts.map +1 -0
- package/dist/services/struct-tree-walker.js +177 -0
- package/dist/services/struct-tree-walker.js.map +1 -0
- package/dist/tools/tier1/read-text.d.ts.map +1 -1
- package/dist/tools/tier1/read-text.js +5 -1
- package/dist/tools/tier1/read-text.js.map +1 -1
- package/dist/tools/tier1/search-text.d.ts.map +1 -1
- package/dist/tools/tier1/search-text.js +22 -1
- package/dist/tools/tier1/search-text.js.map +1 -1
- package/dist/tools/tier2/extract-tables.d.ts.map +1 -1
- package/dist/tools/tier2/extract-tables.js +15 -9
- package/dist/tools/tier2/extract-tables.js.map +1 -1
- package/dist/types.d.ts +17 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/formatter.d.ts +12 -0
- package/dist/utils/formatter.d.ts.map +1 -1
- package/dist/utils/formatter.js +49 -3
- package/dist/utils/formatter.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structure-tree walking over pdf-lib objects.
|
|
3
|
+
*
|
|
4
|
+
* Split out of `struct-tree-service` so that consumers which need only the
|
|
5
|
+
* *shape* of the structure tree do not pull in pdfjs. `struct-tree-service`
|
|
6
|
+
* combines this with pdfjs text; `actual-text-service` needs the walk alone,
|
|
7
|
+
* and importing it from a module that in turn imports pdfjs-service would make
|
|
8
|
+
* the two services circular.
|
|
9
|
+
*
|
|
10
|
+
* Walks the document's logical structure hierarchy from the catalog's
|
|
11
|
+
* `StructTreeRoot`, depth-first — which is exactly how ISO 32000-2 §14.8.2.5
|
|
12
|
+
* defines logical content order:
|
|
13
|
+
*
|
|
14
|
+
* > Logical content order - the ordering for semantic purposes - shall be
|
|
15
|
+
* > defined by a depth-first traversal of the document's logical structure
|
|
16
|
+
* > hierarchy.
|
|
17
|
+
*
|
|
18
|
+
* ## Why not `page.getStructTree()`?
|
|
19
|
+
*
|
|
20
|
+
* pdfjs only offers a per-page view. Merging those per page - which is what
|
|
21
|
+
* `extract_tables` and `inspect_tags` do - cannot produce logical content order,
|
|
22
|
+
* because §14.8.2.5 NOTE 2 allows a single logical object to span pages:
|
|
23
|
+
*
|
|
24
|
+
* > A logical object can extend over more than one PDF page ...
|
|
25
|
+
*
|
|
26
|
+
* pdfjs's per-page nodes carry no element identity (`role` + `children` only),
|
|
27
|
+
* so once merged there is no way to recover that page 1's `L` and page 2's `L`
|
|
28
|
+
* are the same list. Walking the document tree keeps them whole.
|
|
29
|
+
*/
|
|
30
|
+
import { type PDFDocument } from 'pdf-lib';
|
|
31
|
+
/**
|
|
32
|
+
* A marked-content reference: the text this structure element owns on one page.
|
|
33
|
+
*
|
|
34
|
+
* `pageObjNum` is the object number of the `/Pg` page, which is what pdfjs's
|
|
35
|
+
* marked-content id is built from (see `pdfjsMarkedContentId`).
|
|
36
|
+
*/
|
|
37
|
+
export interface ContentRef {
|
|
38
|
+
pageObjNum: number;
|
|
39
|
+
mcid: number;
|
|
40
|
+
}
|
|
41
|
+
/** A node in the document's logical structure hierarchy. */
|
|
42
|
+
export interface StructElement {
|
|
43
|
+
/** The structure type, `/S` (e.g. `H1`, `P`, `Table`). */
|
|
44
|
+
role: string;
|
|
45
|
+
/**
|
|
46
|
+
* `/ActualText` — a **character-level replacement** for the content
|
|
47
|
+
* (ISO 32000-2 §14.9.4: "shall be used as a replacement, not a description").
|
|
48
|
+
* When present it supersedes the glyphs; it is not a description.
|
|
49
|
+
*/
|
|
50
|
+
actualText: string | null;
|
|
51
|
+
/**
|
|
52
|
+
* `/Alt` — an **alternate description** for content that "does not translate
|
|
53
|
+
* naturally into text" (§14.9.3), e.g. a Figure. This is a description *of*
|
|
54
|
+
* the content, not the content, so it is kept apart from the text and must
|
|
55
|
+
* never be reported as the element's text.
|
|
56
|
+
*/
|
|
57
|
+
alt: string | null;
|
|
58
|
+
/** `/Lang`, if this element overrides the document language (§14.9.2). */
|
|
59
|
+
lang: string | null;
|
|
60
|
+
/** Marked-content references owned directly by this element, in order. */
|
|
61
|
+
contentRefs: ContentRef[];
|
|
62
|
+
/** Child structure elements, in document order. */
|
|
63
|
+
children: StructElement[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Build the pdfjs marked-content id for a `/Pg` + `/MCID` pair.
|
|
67
|
+
*
|
|
68
|
+
* pdfjs names marked content `p{pageObjectNumber}R_mc{mcid}` — verified against
|
|
69
|
+
* pdfjs-dist in `tests/tier1/struct-tree-service.test.ts`. Note it drops the
|
|
70
|
+
* generation number (`p7R`, not `p7_0R`).
|
|
71
|
+
*
|
|
72
|
+
* This format is pdfjs's internal convention, not a published contract, so the
|
|
73
|
+
* test asserts it against real pdfjs output: if a pdfjs upgrade renames these,
|
|
74
|
+
* the test fails rather than the tool silently returning empty text. (D-2 was
|
|
75
|
+
* caused by hardcoding pdfjs constants without such a guard.)
|
|
76
|
+
*/
|
|
77
|
+
export declare function pdfjsMarkedContentId(ref: ContentRef): string;
|
|
78
|
+
/** Resolve a value that may be an indirect reference. */
|
|
79
|
+
export declare function deref(doc: PDFDocument, obj: unknown): unknown;
|
|
80
|
+
/**
|
|
81
|
+
* Walk the document's structure tree from the catalog.
|
|
82
|
+
*
|
|
83
|
+
* Returns the top-level structure elements in document order, or `null` when the
|
|
84
|
+
* catalog has no `StructTreeRoot` (an untagged document, or one whose structure
|
|
85
|
+
* tree is unreachable).
|
|
86
|
+
*
|
|
87
|
+
* Note this reads only `StructTreeRoot`; it needs neither `/ParentTree` nor
|
|
88
|
+
* `/StructParents`, which pdfjs's per-page `getStructTree()` does require.
|
|
89
|
+
*/
|
|
90
|
+
export declare function walkStructTree(doc: PDFDocument): StructElement[] | null;
|
|
91
|
+
/** Collect every content reference under an element, in document order. */
|
|
92
|
+
export declare function collectContentRefs(element: StructElement, into?: ContentRef[]): ContentRef[];
|
|
93
|
+
//# sourceMappingURL=struct-tree-walker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"struct-tree-walker.d.ts","sourceRoot":"","sources":["../../src/services/struct-tree-walker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,EAGL,KAAK,WAAW,EAMjB,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,4DAA4D;AAC5D,MAAM,WAAW,aAAa;IAC5B,0DAA0D;IAC1D,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B;;;;;OAKG;IACH,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,0EAA0E;IAC1E,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,0EAA0E;IAC1E,WAAW,EAAE,UAAU,EAAE,CAAC;IAC1B,mDAAmD;IACnD,QAAQ,EAAE,aAAa,EAAE,CAAC;CAC3B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAE5D;AAED,yDAAyD;AACzD,wBAAgB,KAAK,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAE7D;AAwGD;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,WAAW,GAAG,aAAa,EAAE,GAAG,IAAI,CAavE;AAED,2EAA2E;AAC3E,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,aAAa,EAAE,IAAI,GAAE,UAAU,EAAO,GAAG,UAAU,EAAE,CAIhG"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structure-tree walking over pdf-lib objects.
|
|
3
|
+
*
|
|
4
|
+
* Split out of `struct-tree-service` so that consumers which need only the
|
|
5
|
+
* *shape* of the structure tree do not pull in pdfjs. `struct-tree-service`
|
|
6
|
+
* combines this with pdfjs text; `actual-text-service` needs the walk alone,
|
|
7
|
+
* and importing it from a module that in turn imports pdfjs-service would make
|
|
8
|
+
* the two services circular.
|
|
9
|
+
*
|
|
10
|
+
* Walks the document's logical structure hierarchy from the catalog's
|
|
11
|
+
* `StructTreeRoot`, depth-first — which is exactly how ISO 32000-2 §14.8.2.5
|
|
12
|
+
* defines logical content order:
|
|
13
|
+
*
|
|
14
|
+
* > Logical content order - the ordering for semantic purposes - shall be
|
|
15
|
+
* > defined by a depth-first traversal of the document's logical structure
|
|
16
|
+
* > hierarchy.
|
|
17
|
+
*
|
|
18
|
+
* ## Why not `page.getStructTree()`?
|
|
19
|
+
*
|
|
20
|
+
* pdfjs only offers a per-page view. Merging those per page - which is what
|
|
21
|
+
* `extract_tables` and `inspect_tags` do - cannot produce logical content order,
|
|
22
|
+
* because §14.8.2.5 NOTE 2 allows a single logical object to span pages:
|
|
23
|
+
*
|
|
24
|
+
* > A logical object can extend over more than one PDF page ...
|
|
25
|
+
*
|
|
26
|
+
* pdfjs's per-page nodes carry no element identity (`role` + `children` only),
|
|
27
|
+
* so once merged there is no way to recover that page 1's `L` and page 2's `L`
|
|
28
|
+
* are the same list. Walking the document tree keeps them whole.
|
|
29
|
+
*/
|
|
30
|
+
import { PDFArray, PDFDict, PDFHexString, PDFName, PDFNumber, PDFRef, PDFString, } from 'pdf-lib';
|
|
31
|
+
/**
|
|
32
|
+
* Build the pdfjs marked-content id for a `/Pg` + `/MCID` pair.
|
|
33
|
+
*
|
|
34
|
+
* pdfjs names marked content `p{pageObjectNumber}R_mc{mcid}` — verified against
|
|
35
|
+
* pdfjs-dist in `tests/tier1/struct-tree-service.test.ts`. Note it drops the
|
|
36
|
+
* generation number (`p7R`, not `p7_0R`).
|
|
37
|
+
*
|
|
38
|
+
* This format is pdfjs's internal convention, not a published contract, so the
|
|
39
|
+
* test asserts it against real pdfjs output: if a pdfjs upgrade renames these,
|
|
40
|
+
* the test fails rather than the tool silently returning empty text. (D-2 was
|
|
41
|
+
* caused by hardcoding pdfjs constants without such a guard.)
|
|
42
|
+
*/
|
|
43
|
+
export function pdfjsMarkedContentId(ref) {
|
|
44
|
+
return `p${ref.pageObjNum}R_mc${ref.mcid}`;
|
|
45
|
+
}
|
|
46
|
+
/** Resolve a value that may be an indirect reference. */
|
|
47
|
+
export function deref(doc, obj) {
|
|
48
|
+
return obj instanceof PDFRef ? doc.context.lookup(obj) : obj;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Read a text-ish value (`PDFString` / `PDFHexString`) from a dictionary.
|
|
52
|
+
*
|
|
53
|
+
* **Encrypted documents yield nothing.** ISO 32000-2 §7.6.2 encrypts *strings
|
|
54
|
+
* and streams* and nothing else, so an encrypted file's structure tree still
|
|
55
|
+
* walks correctly — the names, numbers and references are all in the clear —
|
|
56
|
+
* but every string in it is ciphertext, and pdf-lib does not decrypt (it is
|
|
57
|
+
* loaded with `ignoreEncryption`, which ignores, not decrypts).
|
|
58
|
+
*
|
|
59
|
+
* Returning the ciphertext would be far worse than returning nothing: measured
|
|
60
|
+
* on `PDF32000_2008.pdf` (owner-password encrypted, fully readable in any
|
|
61
|
+
* viewer), the tree carries **18026** `/ActualText` entries, every one of them
|
|
62
|
+
* bytes like `&)ð`. Since #18 those entries *replace* page text, so this
|
|
63
|
+
* would have substituted garbage for the body of every such document.
|
|
64
|
+
*/
|
|
65
|
+
function textEntry(doc, dict, key) {
|
|
66
|
+
if (doc.isEncrypted)
|
|
67
|
+
return null;
|
|
68
|
+
const value = deref(doc, dict.get(PDFName.of(key)));
|
|
69
|
+
if (value instanceof PDFString || value instanceof PDFHexString)
|
|
70
|
+
return value.decodeText();
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
/** Normalise `/K` — it may be absent, a single object, or an array. */
|
|
74
|
+
function kidsOf(doc, dict) {
|
|
75
|
+
const k = deref(doc, dict.get(PDFName.of('K')));
|
|
76
|
+
if (k === undefined || k === null)
|
|
77
|
+
return [];
|
|
78
|
+
if (k instanceof PDFArray)
|
|
79
|
+
return k.asArray();
|
|
80
|
+
return [k];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Walk one structure element.
|
|
84
|
+
*
|
|
85
|
+
* `/K` is polymorphic (§14.7.2 Table 355) and every form has to be handled:
|
|
86
|
+
*
|
|
87
|
+
* - **integer** — an MCID on the element's own (possibly inherited) `/Pg`
|
|
88
|
+
* - **MCR dict** — an MCID with its own `/Pg`, which is how an element points
|
|
89
|
+
* at content on a page other than its own. This is the page-spanning case.
|
|
90
|
+
* - **OBJR dict** — a reference to an annotation or form field. Skipped: it
|
|
91
|
+
* owns no page text.
|
|
92
|
+
* - **StructElem dict** — a child element, recursed into
|
|
93
|
+
*
|
|
94
|
+
* `/Pg` is inherited: an element without one uses its nearest ancestor's.
|
|
95
|
+
*/
|
|
96
|
+
function walkElement(doc, dict, inheritedPg) {
|
|
97
|
+
const s = deref(doc, dict.get(PDFName.of('S')));
|
|
98
|
+
if (!(s instanceof PDFName))
|
|
99
|
+
return null;
|
|
100
|
+
const pgValue = dict.get(PDFName.of('Pg'));
|
|
101
|
+
const pg = pgValue instanceof PDFRef ? pgValue : inheritedPg;
|
|
102
|
+
const element = {
|
|
103
|
+
role: s.decodeText(),
|
|
104
|
+
actualText: textEntry(doc, dict, 'ActualText'),
|
|
105
|
+
alt: textEntry(doc, dict, 'Alt'),
|
|
106
|
+
lang: textEntry(doc, dict, 'Lang'),
|
|
107
|
+
contentRefs: [],
|
|
108
|
+
children: [],
|
|
109
|
+
};
|
|
110
|
+
for (const kid of kidsOf(doc, dict)) {
|
|
111
|
+
// An MCID written directly, on this element's page.
|
|
112
|
+
if (kid instanceof PDFNumber) {
|
|
113
|
+
if (pg)
|
|
114
|
+
element.contentRefs.push({ pageObjNum: pg.objectNumber, mcid: kid.asNumber() });
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const resolved = deref(doc, kid);
|
|
118
|
+
if (!(resolved instanceof PDFDict))
|
|
119
|
+
continue;
|
|
120
|
+
const type = resolved.get(PDFName.of('Type'));
|
|
121
|
+
const typeName = type instanceof PDFName ? type.decodeText() : null;
|
|
122
|
+
if (typeName === 'MCR') {
|
|
123
|
+
const mcid = deref(doc, resolved.get(PDFName.of('MCID')));
|
|
124
|
+
const mcrPgValue = resolved.get(PDFName.of('Pg'));
|
|
125
|
+
const mcrPg = mcrPgValue instanceof PDFRef ? mcrPgValue : pg;
|
|
126
|
+
if (mcid instanceof PDFNumber && mcrPg) {
|
|
127
|
+
element.contentRefs.push({ pageObjNum: mcrPg.objectNumber, mcid: mcid.asNumber() });
|
|
128
|
+
}
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
// OBJR points at an annotation or form field, which carries no page text.
|
|
132
|
+
if (typeName === 'OBJR')
|
|
133
|
+
continue;
|
|
134
|
+
// Anything with an /S is a child structure element. Checking /S rather than
|
|
135
|
+
// Type == StructElem on purpose: Type is optional in practice and plenty of
|
|
136
|
+
// producers omit it.
|
|
137
|
+
if (resolved.get(PDFName.of('S'))) {
|
|
138
|
+
const child = walkElement(doc, resolved, pg);
|
|
139
|
+
if (child)
|
|
140
|
+
element.children.push(child);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return element;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Walk the document's structure tree from the catalog.
|
|
147
|
+
*
|
|
148
|
+
* Returns the top-level structure elements in document order, or `null` when the
|
|
149
|
+
* catalog has no `StructTreeRoot` (an untagged document, or one whose structure
|
|
150
|
+
* tree is unreachable).
|
|
151
|
+
*
|
|
152
|
+
* Note this reads only `StructTreeRoot`; it needs neither `/ParentTree` nor
|
|
153
|
+
* `/StructParents`, which pdfjs's per-page `getStructTree()` does require.
|
|
154
|
+
*/
|
|
155
|
+
export function walkStructTree(doc) {
|
|
156
|
+
const root = deref(doc, doc.catalog.get(PDFName.of('StructTreeRoot')));
|
|
157
|
+
if (!(root instanceof PDFDict))
|
|
158
|
+
return null;
|
|
159
|
+
const elements = [];
|
|
160
|
+
for (const kid of kidsOf(doc, root)) {
|
|
161
|
+
const resolved = deref(doc, kid);
|
|
162
|
+
if (resolved instanceof PDFDict && resolved.get(PDFName.of('S'))) {
|
|
163
|
+
const element = walkElement(doc, resolved, undefined);
|
|
164
|
+
if (element)
|
|
165
|
+
elements.push(element);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return elements;
|
|
169
|
+
}
|
|
170
|
+
/** Collect every content reference under an element, in document order. */
|
|
171
|
+
export function collectContentRefs(element, into = []) {
|
|
172
|
+
into.push(...element.contentRefs);
|
|
173
|
+
for (const child of element.children)
|
|
174
|
+
collectContentRefs(child, into);
|
|
175
|
+
return into;
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=struct-tree-walker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"struct-tree-walker.js","sourceR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Root":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAOzE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"read-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAOzE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAmExD"}
|
|
@@ -13,7 +13,11 @@ export function registerReadText(server) {
|
|
|
13
13
|
|
|
14
14
|
Text is extracted page by page, sorted by vertical position (top to bottom) then horizontal position (left to right), providing natural reading order.
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
\`/ActualText\` replacements (ISO 32000-2 §14.9.4) are resolved, on both of the paths that clause defines: the \`/ActualText\` of a structure element, and the one in a \`Span\` marked-content property list — the latter occurs in untagged documents too. So a word carried as ActualText (ligature substitutes, hyphenation fixes) reads here the way a person viewing the page sees it, not in its glyph form.
|
|
17
|
+
|
|
18
|
+
For **tagged** PDFs, \`extract_structured_text\` is still the better tool when order matters: it returns text in logical content order (ISO 32000-2 §14.8.2.5), which this tool does not — read_text sorts by coordinate. Tables in tagged PDFs are best read with \`extract_tables\`.
|
|
19
|
+
|
|
20
|
+
For **untagged** multi-column PDFs (e.g. older 新旧対照表 PDFs that lack a structure tree), pass \`split_columns: 2\` or \`3\` to bucket items by X-coordinate left-to-right.
|
|
17
21
|
|
|
18
22
|
For Japanese form-style PDFs (帳票・様式) where U+3000 fullwidth spaces are used as visual indentation, pass \`compact_whitespace: true\` to collapse runs of whitespace to a single ASCII space. Cuts 20–40% of token consumption without losing content.
|
|
19
23
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAsB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAErF,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IAChD,MAAM,CAAC,YAAY,CACjB,WAAW,EACX;QACE,KAAK,EAAE,eAAe;QACtB,WAAW,EAAE
|
|
1
|
+
{"version":3,"file":"read-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/read-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAsB,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAErF,MAAM,UAAU,gBAAgB,CAAC,MAAiB;IAChD,MAAM,CAAC,YAAY,CACjB,WAAW,EACX;QACE,KAAK,EAAE,eAAe;QACtB,WAAW,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;yFAyBsE;QACnF,WAAW,EAAE,cAAc;QAC3B,WAAW,EAAE;YACX,YAAY,EAAE,IAAI;YAClB,eAAe,EAAE,KAAK;YACtB,cAAc,EAAE,IAAI;YACpB,aAAa,EAAE,KAAK;SACrB;KACF,EACD,KAAK,EAAE,MAAqB,EAAE,EAAE;QAC9B,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,WAAW,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,EAAE;gBAC9D,YAAY,EAAE,MAAM,CAAC,aAAa;gBAClC,iBAAiB,EAAE,MAAM,CAAC,kBAAkB;aAC7C,CAAC,CAAC;YAEH,IAAI,IAAY,CAAC;YACjB,IAAI,MAAM,CAAC,eAAe,KAAK,cAAc,CAAC,IAAI,EAAE,CAAC;gBACnD,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;YACxC,CAAC;iBAAM,CAAC;gBACN,IAAI,GAAG,uBAAuB,CAAC,KAAK,CAAC,CAAC;YACxC,CAAC;YAED,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;YAEnD,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;aACtD,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,GAAG,GAAG,qBAAqB,CAAC,KAAK,CAAC,CAAC;YACzC,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;gBACxE,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAQzE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"search-text.d.ts","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAQzE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAwF1D"}
|
|
@@ -13,6 +13,8 @@ export function registerSearchText(server) {
|
|
|
13
13
|
|
|
14
14
|
Case-insensitive search across all or specified pages. Each match includes the page number, the matched text, and configurable surrounding context.
|
|
15
15
|
|
|
16
|
+
The search runs over the same text \`read_text\` returns, so \`/ActualText\` replacements (ISO 32000-2 §14.9.4) match: a word carried as ActualText (ligature substitutes, hyphenation fixes) is found under the spelling a viewer shows, not under its glyph form. Rarely, a page's marked content cannot be aligned with the extracted text and the replacement is left unresolved; when that happens on a search with no hits, the result carries a \`note\` naming those pages.
|
|
17
|
+
|
|
16
18
|
Args:
|
|
17
19
|
- file_path (string): Absolute path to a local PDF file
|
|
18
20
|
- query (string): Text to search for (case-insensitive, 1-500 chars)
|
|
@@ -36,7 +38,7 @@ Examples:
|
|
|
36
38
|
},
|
|
37
39
|
}, async (params) => {
|
|
38
40
|
try {
|
|
39
|
-
const allMatches = await searchText(params.file_path, params.query, params.context_chars, params.pages);
|
|
41
|
+
const { matches: allMatches, unresolvedPages, unresolvedReason, } = await searchText(params.file_path, params.query, params.context_chars, params.pages);
|
|
40
42
|
const truncated = allMatches.length > params.max_results;
|
|
41
43
|
const matches = allMatches.slice(0, params.max_results);
|
|
42
44
|
const result = {
|
|
@@ -45,6 +47,25 @@ Examples:
|
|
|
45
47
|
matches,
|
|
46
48
|
truncated,
|
|
47
49
|
};
|
|
50
|
+
// #18 resolves /ActualText, so the blanket #15 warning no longer
|
|
51
|
+
// applies — but the resolution can still be skipped. Keep saying so,
|
|
52
|
+
// now only where it is actually true, and say which of the two reasons
|
|
53
|
+
// it is: they call for different next steps, and pointing an encrypted
|
|
54
|
+
// document at extract_structured_text would waste the caller's time
|
|
55
|
+
// (that tool reads its replacement text through pdf-lib as well).
|
|
56
|
+
if (allMatches.length === 0 && unresolvedPages.length > 0) {
|
|
57
|
+
result.note =
|
|
58
|
+
unresolvedReason === 'encrypted'
|
|
59
|
+
? 'No matches. This document is encrypted, so replacement text (/ActualText, ' +
|
|
60
|
+
'ISO 32000-2 §14.9.4) could not be read: §7.6.2 encrypts strings and streams, ' +
|
|
61
|
+
'and this server does not decrypt them. What was searched is the glyphs as ' +
|
|
62
|
+
'drawn. No other tool here will do better — decrypt the file first if you ' +
|
|
63
|
+
'need the replacement text.'
|
|
64
|
+
: 'No matches. Replacement text (/ActualText, ISO 32000-2 §14.9.4) was resolved, ' +
|
|
65
|
+
`except on page(s) ${unresolvedPages.join(', ')}, whose content stream could not ` +
|
|
66
|
+
'be aligned with the extracted text. A Span-level replacement there would be ' +
|
|
67
|
+
'invisible to this search — try extract_structured_text if the document is tagged.';
|
|
68
|
+
}
|
|
48
69
|
const text = params.response_format === ResponseFormat.JSON
|
|
49
70
|
? JSON.stringify(result, null, 2)
|
|
50
71
|
: formatSearchResultMarkdown(result);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAwB,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAE7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAEtE,MAAM,UAAU,kBAAkB,CAAC,MAAiB;IAClD,MAAM,CAAC,YAAY,CACjB,aAAa,EACb;QACE,KAAK,EAAE,iBAAiB;QACxB,WAAW,EAAE
|
|
1
|
+
{"version":3,"file":"search-text.js","sourceRoot":"","sources":["../../../src/tools/tier1/search-text.ts"],"names":[],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SAAS,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"extract-tables.d.ts","sourceRoot":"","sources":["../../../src/tools/tier2/extract-tables.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAOzE,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAkE7D"}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { ResponseFormat } from '../../constants.js';
|
|
10
10
|
import { ExtractTablesSchema } from '../../schemas/tier2.js';
|
|
11
|
-
import { extractTables } from '../../services/
|
|
11
|
+
import { extractTables } from '../../services/struct-tree-service.js';
|
|
12
12
|
import { handleStructuredError } from '../../utils/error-handler.js';
|
|
13
13
|
import { formatTablesMarkdown, truncateIfNeeded } from '../../utils/formatter.js';
|
|
14
14
|
export function registerExtractTables(server) {
|
|
@@ -17,10 +17,15 @@ export function registerExtractTables(server) {
|
|
|
17
17
|
description: `Extract every \`<Table>\` subtree from a Tagged PDF as a structured row/cell list,
|
|
18
18
|
optionally rendered as Markdown tables.
|
|
19
19
|
|
|
20
|
-
How it works: walks the
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
How it works: walks the document's StructTreeRoot depth-first (the same walker
|
|
21
|
+
as \`extract_structured_text\` / \`inspect_tags\`) and pulls cell text for each
|
|
22
|
+
\`<TR>\` → \`<TH>/<TD>\`, then collapses kerning whitespace (e.g. "消 費 税 法" →
|
|
23
|
+
"消費税法"). This sidesteps reading-order extraction's failure mode on
|
|
24
|
+
multi-column tables (typical of 新旧対照表 PDFs).
|
|
25
|
+
|
|
26
|
+
A Table that continues across a page break is ONE table (ISO 32000-2 §14.8.2.5
|
|
27
|
+
NOTE 2) — \`pages\` is an array, and a table touching the requested page range is
|
|
28
|
+
returned whole. Cell text honours \`/ActualText\` replacements (§14.9.4).
|
|
24
29
|
|
|
25
30
|
Args:
|
|
26
31
|
- file_path (string): Absolute path to a local PDF file
|
|
@@ -29,15 +34,16 @@ Args:
|
|
|
29
34
|
|
|
30
35
|
Returns:
|
|
31
36
|
Markdown — \`# Extracted Tables\` summary block followed by one
|
|
32
|
-
\`##
|
|
37
|
+
\`## Table N — Page(s) …\` section per table with a GFM table.
|
|
33
38
|
|
|
34
|
-
JSON — \`{ isTagged, tables: [{
|
|
35
|
-
totalTables, pagesScanned, note? }\`.
|
|
39
|
+
JSON — \`{ isTagged, tables: [{ pages, index, headerRows, bodyRows, footerRows }],
|
|
40
|
+
totalTables, pagesScanned, note? }\`. \`index\` is the table's 1-based position
|
|
41
|
+
in logical content order, document-wide.
|
|
36
42
|
|
|
37
43
|
Limitations:
|
|
38
44
|
- Untagged PDFs return an empty result and a \`note\`.
|
|
39
45
|
- colspan/rowspan are not honoured (cells are listed in source order).
|
|
40
|
-
- Nested tables are
|
|
46
|
+
- Nested tables are not emitted separately (their text appears in the outer cell).
|
|
41
47
|
|
|
42
48
|
Examples:
|
|
43
49
|
- Pull 新旧対照表 from a kaisei tsutatsu PDF for diffing
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract-tables.js","sourceRoot":"","sources":["../../../src/tools/tier2/extract-tables.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAA2B,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"extract-tables.js","sourceRoot":"","sources":["../../../src/tools/tier2/extract-tables.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAA2B,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AACtF,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAAE,q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AC;IACH,CAAC,CACF,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -38,6 +38,13 @@ export interface SearchResult {
|
|
|
38
38
|
totalMatches: number;
|
|
39
39
|
matches: SearchMatch[];
|
|
40
40
|
truncated: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Set when the search found nothing but the document is tagged (#15).
|
|
43
|
+
* search_text sees raw glyphs only — a tagged document may carry the
|
|
44
|
+
* searched words in `/ActualText` replacements (ISO 32000-2 §14.9.4),
|
|
45
|
+
* which only `extract_structured_text` resolves.
|
|
46
|
+
*/
|
|
47
|
+
note?: string;
|
|
41
48
|
}
|
|
42
49
|
/** An extracted image from a PDF page */
|
|
43
50
|
export interface ExtractedImage {
|
|
@@ -206,9 +213,16 @@ export interface TableRow {
|
|
|
206
213
|
}
|
|
207
214
|
/** A single table extracted from a Tagged PDF's structure tree. */
|
|
208
215
|
export interface ExtractedTable {
|
|
209
|
-
/**
|
|
210
|
-
|
|
211
|
-
|
|
216
|
+
/**
|
|
217
|
+
* 1-based page numbers the table's content lives on, ascending (#14).
|
|
218
|
+
*
|
|
219
|
+
* An array, not a number: a Table StructElem that continues across a page
|
|
220
|
+
* break is ONE table (§14.8.2.5 NOTE 2), and since the walker moved to the
|
|
221
|
+
* document's StructTreeRoot it is reported whole. Was `page: number` when
|
|
222
|
+
* extraction was per-page — that sliced spanning tables into fragments.
|
|
223
|
+
*/
|
|
224
|
+
pages: number[];
|
|
225
|
+
/** 1-based index of the table in logical content order, document-wide. */
|
|
212
226
|
index: number;
|
|
213
227
|
/** Rows from `<THead>`. Empty if the table has no explicit header section. */
|
|
214
228
|
headerRows: TableRow[];
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,6CAA6C;AAC7C,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,GA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AG,QAAQ,CAAC;CAC5B;AAED,+BAA+B;AAC/B,MAAM,WAAW,mBAAmB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,kBAAkB,EAAE,CAAC;IAC5B,cAAc,EAAE;QACd,WAAW,EAAE,MAAM,EAAE,CAAC;QACtB,WAAW,EAAE,MAAM,EAAE,CAAC;QACtB,MAAM,EAAE,MAAM,EAAE,CAAC;KAClB,CAAC;IACF,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -33,6 +33,18 @@ export declare function formatStructureMarkdown(analysis: StructureAnalysis): st
|
|
|
33
33
|
* Format tags analysis as Markdown.
|
|
34
34
|
*/
|
|
35
35
|
export declare function formatTagsMarkdown(analysis: TagsAnalysis): string;
|
|
36
|
+
/**
|
|
37
|
+
* Render a page-number list compactly (#17).
|
|
38
|
+
*
|
|
39
|
+
* Consecutive runs collapse to `start–end`; gaps are comma-separated:
|
|
40
|
+
* `[1,2,3]` → `"1–3"`, `[1,2,3,7,9,10]` → `"1–3, 7, 9–10"`.
|
|
41
|
+
*
|
|
42
|
+
* Without this, an element spanning a whole document (`Document` in a
|
|
43
|
+
* 1000-page PDF) renders every page number and bloats the response by tens
|
|
44
|
+
* of kilobytes. A side benefit: pages absent from the structure tree show up
|
|
45
|
+
* as visible gaps. Display only — JSON keeps the full array.
|
|
46
|
+
*/
|
|
47
|
+
export declare function formatPageList(pages: number[]): string;
|
|
36
48
|
/**
|
|
37
49
|
* Format structured text as Markdown.
|
|
38
50
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/utils/formatter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EACV,mBAAmB,EAEnB,aAAa,EACb,kBAAkB,EAClB,QAAQ,EACR,WAAW,EACX,UAAU,EACV,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EAC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;GAEG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM,CAyC3E;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,YAAY,GAAG,MAAM,CA0BjE;AAaD;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAmBtD;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,oBAAoB,GAAG,MAAM,CAyCjF;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,aAAa,GAAG,MAAM,CA+BnE;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,QAAQ,EAAE,mBAAmB,GAAG,MAAM,CAgC/E;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,kBAAkB,GAAG,MAAM,CA6B7E;AAID;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIpD;AA4BD;;GAEG;AACH,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,CAuB/E;AAED;;GAEG;AACH,wBAAgB,gCAAgC,CAAC,MAAM,EAAE,kBAAkB,GAAG,MAAM,CA2CnF;AAED;;GAEG;AACH,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,mBAAmB,GAAG,MAAM,CAqClF;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,sBAAsB,GAAG,MAAM,CAuB3E"}
|
package/dist/utils/formatter.js
CHANGED
|
@@ -72,6 +72,9 @@ export function formatSearchResultMarkdown(result) {
|
|
|
72
72
|
if (result.truncated) {
|
|
73
73
|
lines.push('---', '⚠️ Results truncated. Use page range parameters to narrow the search.');
|
|
74
74
|
}
|
|
75
|
+
if (result.note) {
|
|
76
|
+
lines.push('', '## Note', '', `> ${result.note}`);
|
|
77
|
+
}
|
|
75
78
|
return lines.join('\n');
|
|
76
79
|
}
|
|
77
80
|
/**
|
|
@@ -166,6 +169,48 @@ export function formatTagsMarkdown(analysis) {
|
|
|
166
169
|
renderTagTree(analysis.rootTag, lines, 0, 4);
|
|
167
170
|
return lines.join('\n');
|
|
168
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Escape a table cell for GitHub-flavoured Markdown (#16).
|
|
174
|
+
*
|
|
175
|
+
* Same treatment as extract_tables' cells: `|` would end the cell, and a raw
|
|
176
|
+
* newline would end the row. Applied at FORMAT time only — the JSON output
|
|
177
|
+
* carries the raw text.
|
|
178
|
+
*/
|
|
179
|
+
function escapeMarkdownCell(text) {
|
|
180
|
+
return text.replace(/\|/g, '\\|').replace(/\n/g, ' ');
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Render a page-number list compactly (#17).
|
|
184
|
+
*
|
|
185
|
+
* Consecutive runs collapse to `start–end`; gaps are comma-separated:
|
|
186
|
+
* `[1,2,3]` → `"1–3"`, `[1,2,3,7,9,10]` → `"1–3, 7, 9–10"`.
|
|
187
|
+
*
|
|
188
|
+
* Without this, an element spanning a whole document (`Document` in a
|
|
189
|
+
* 1000-page PDF) renders every page number and bloats the response by tens
|
|
190
|
+
* of kilobytes. A side benefit: pages absent from the structure tree show up
|
|
191
|
+
* as visible gaps. Display only — JSON keeps the full array.
|
|
192
|
+
*/
|
|
193
|
+
export function formatPageList(pages) {
|
|
194
|
+
if (pages.length === 0)
|
|
195
|
+
return '';
|
|
196
|
+
const parts = [];
|
|
197
|
+
let runStart = pages[0];
|
|
198
|
+
let prev = pages[0];
|
|
199
|
+
const flush = () => {
|
|
200
|
+
parts.push(runStart === prev ? `${runStart}` : `${runStart}–${prev}`);
|
|
201
|
+
};
|
|
202
|
+
for (const p of pages.slice(1)) {
|
|
203
|
+
if (p === prev + 1) {
|
|
204
|
+
prev = p;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
flush();
|
|
208
|
+
runStart = p;
|
|
209
|
+
prev = p;
|
|
210
|
+
}
|
|
211
|
+
flush();
|
|
212
|
+
return parts.join(', ');
|
|
213
|
+
}
|
|
169
214
|
/**
|
|
170
215
|
* Format structured text as Markdown.
|
|
171
216
|
*
|
|
@@ -187,7 +232,7 @@ export function formatStructuredTextMarkdown(result) {
|
|
|
187
232
|
const indent = ' '.repeat(element.depth);
|
|
188
233
|
const parts = [`${indent}- **${element.role}**`];
|
|
189
234
|
if (element.pages.length > 1)
|
|
190
|
-
parts.push(`(pages ${element.pages
|
|
235
|
+
parts.push(`(pages ${formatPageList(element.pages)})`);
|
|
191
236
|
else if (element.pages.length === 1)
|
|
192
237
|
parts.push(`(page ${element.pages[0]})`);
|
|
193
238
|
if (element.label)
|
|
@@ -202,7 +247,7 @@ export function formatStructuredTextMarkdown(result) {
|
|
|
202
247
|
if (element.rows) {
|
|
203
248
|
lines.push('');
|
|
204
249
|
for (const [rowIndex, row] of element.rows.entries()) {
|
|
205
|
-
lines.push(`${indent} | ${row.map((c) => c.text).join(' | ')} |`);
|
|
250
|
+
lines.push(`${indent} | ${row.map((c) => escapeMarkdownCell(c.text)).join(' | ')} |`);
|
|
206
251
|
if (rowIndex === 0 && row.some((c) => c.isHeader)) {
|
|
207
252
|
lines.push(`${indent} |${row.map(() => '---').join('|')}|`);
|
|
208
253
|
}
|
|
@@ -450,7 +495,8 @@ export function formatTablesMarkdown(result) {
|
|
|
450
495
|
return lines.join('\n');
|
|
451
496
|
}
|
|
452
497
|
for (const table of result.tables) {
|
|
453
|
-
|
|
498
|
+
const where = table.pages.length === 1 ? `Page ${table.pages[0]}` : `Pages ${formatPageList(table.pages)}`;
|
|
499
|
+
lines.push('', `## Table ${table.index} — ${where}`, '');
|
|
454
500
|
lines.push(renderTableAsMarkdown(table));
|
|
455
501
|
}
|
|
456
502
|
return lines.join('\n');
|