@effortlessmotion/pdf-to-md 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +157 -0
- package/bin/cli.js +9 -0
- package/dist/cjs/analyze.d.cts +35 -0
- package/dist/cjs/analyze.d.ts +35 -0
- package/dist/cjs/analyze.js +620 -0
- package/dist/cjs/analyze.js.map +1 -0
- package/dist/cjs/audit.d.cts +38 -0
- package/dist/cjs/audit.d.ts +38 -0
- package/dist/cjs/audit.js +252 -0
- package/dist/cjs/audit.js.map +1 -0
- package/dist/cjs/cli/audit.d.cts +3 -0
- package/dist/cjs/cli/audit.d.ts +3 -0
- package/dist/cjs/cli/audit.js +269 -0
- package/dist/cjs/cli/audit.js.map +1 -0
- package/dist/cjs/cli/extract.d.cts +3 -0
- package/dist/cjs/cli/extract.d.ts +3 -0
- package/dist/cjs/cli/extract.js +704 -0
- package/dist/cjs/cli/extract.js.map +1 -0
- package/dist/cjs/cli/index.d.cts +5 -0
- package/dist/cjs/cli/index.d.ts +5 -0
- package/dist/cjs/cli/index.js +58 -0
- package/dist/cjs/cli/index.js.map +1 -0
- package/dist/cjs/cli/render.d.cts +3 -0
- package/dist/cjs/cli/render.d.ts +3 -0
- package/dist/cjs/cli/render.js +361 -0
- package/dist/cjs/cli/render.js.map +1 -0
- package/dist/cjs/cli/types.d.cts +10 -0
- package/dist/cjs/cli/types.d.ts +10 -0
- package/dist/cjs/cli/types.js +7 -0
- package/dist/cjs/cli/types.js.map +1 -0
- package/dist/cjs/cli.d.cts +2 -0
- package/dist/cjs/cli.d.ts +2 -0
- package/dist/cjs/cli.js +345 -0
- package/dist/cjs/cli.js.map +1 -0
- package/dist/cjs/collect.d.cts +6 -0
- package/dist/cjs/collect.d.ts +6 -0
- package/dist/cjs/collect.js +370 -0
- package/dist/cjs/collect.js.map +1 -0
- package/dist/cjs/dict.d.cts +8 -0
- package/dist/cjs/dict.d.ts +8 -0
- package/dist/cjs/dict.js +55 -0
- package/dist/cjs/dict.js.map +1 -0
- package/dist/cjs/emit.d.cts +27 -0
- package/dist/cjs/emit.d.ts +27 -0
- package/dist/cjs/emit.js +187 -0
- package/dist/cjs/emit.js.map +1 -0
- package/dist/cjs/engines/tesseract.d.cts +30 -0
- package/dist/cjs/engines/tesseract.d.ts +30 -0
- package/dist/cjs/engines/tesseract.js +650 -0
- package/dist/cjs/engines/tesseract.js.map +1 -0
- package/dist/cjs/errors.d.cts +21 -0
- package/dist/cjs/errors.d.ts +21 -0
- package/dist/cjs/errors.js +124 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/extract.d.cts +88 -0
- package/dist/cjs/extract.d.ts +88 -0
- package/dist/cjs/extract.js +436 -0
- package/dist/cjs/extract.js.map +1 -0
- package/dist/cjs/geometry.d.cts +63 -0
- package/dist/cjs/geometry.d.ts +63 -0
- package/dist/cjs/geometry.js +521 -0
- package/dist/cjs/geometry.js.map +1 -0
- package/dist/cjs/index.d.cts +15 -0
- package/dist/cjs/index.d.ts +15 -0
- package/dist/cjs/index.js +56 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/lines.d.cts +3 -0
- package/dist/cjs/lines.d.ts +3 -0
- package/dist/cjs/lines.js +181 -0
- package/dist/cjs/lines.js.map +1 -0
- package/dist/cjs/markdown.d.cts +2 -0
- package/dist/cjs/markdown.d.ts +2 -0
- package/dist/cjs/markdown.js +222 -0
- package/dist/cjs/markdown.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/page-marker.d.cts +16 -0
- package/dist/cjs/page-marker.d.ts +16 -0
- package/dist/cjs/page-marker.js +85 -0
- package/dist/cjs/page-marker.js.map +1 -0
- package/dist/cjs/pdf-open.d.cts +12 -0
- package/dist/cjs/pdf-open.d.ts +12 -0
- package/dist/cjs/pdf-open.js +257 -0
- package/dist/cjs/pdf-open.js.map +1 -0
- package/dist/cjs/raster.d.cts +26 -0
- package/dist/cjs/raster.d.ts +26 -0
- package/dist/cjs/raster.js +262 -0
- package/dist/cjs/raster.js.map +1 -0
- package/dist/cjs/render-analysis.d.cts +25 -0
- package/dist/cjs/render-analysis.d.ts +25 -0
- package/dist/cjs/render-analysis.js +319 -0
- package/dist/cjs/render-analysis.js.map +1 -0
- package/dist/cjs/report.d.cts +12 -0
- package/dist/cjs/report.d.ts +12 -0
- package/dist/cjs/report.js +392 -0
- package/dist/cjs/report.js.map +1 -0
- package/dist/cjs/resolve.d.cts +1 -0
- package/dist/cjs/resolve.d.ts +1 -0
- package/dist/cjs/resolve.js +18 -0
- package/dist/cjs/resolve.js.map +1 -0
- package/dist/cjs/types.d.cts +171 -0
- package/dist/cjs/types.d.ts +171 -0
- package/dist/cjs/types.js +9 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/validate.d.cts +14 -0
- package/dist/cjs/validate.d.ts +14 -0
- package/dist/cjs/validate.js +96 -0
- package/dist/cjs/validate.js.map +1 -0
- package/dist/esm/analyze.d.ts +35 -0
- package/dist/esm/analyze.js +270 -0
- package/dist/esm/analyze.js.map +1 -0
- package/dist/esm/audit.d.ts +38 -0
- package/dist/esm/audit.js +113 -0
- package/dist/esm/audit.js.map +1 -0
- package/dist/esm/cli/audit.d.ts +3 -0
- package/dist/esm/cli/audit.js +20 -0
- package/dist/esm/cli/audit.js.map +1 -0
- package/dist/esm/cli/extract.d.ts +3 -0
- package/dist/esm/cli/extract.js +313 -0
- package/dist/esm/cli/extract.js.map +1 -0
- package/dist/esm/cli/index.d.ts +5 -0
- package/dist/esm/cli/index.js +7 -0
- package/dist/esm/cli/index.js.map +1 -0
- package/dist/esm/cli/render.d.ts +3 -0
- package/dist/esm/cli/render.js +77 -0
- package/dist/esm/cli/render.js.map +1 -0
- package/dist/esm/cli/types.d.ts +10 -0
- package/dist/esm/cli/types.js +3 -0
- package/dist/esm/cli/types.js.map +1 -0
- package/dist/esm/cli.d.ts +2 -0
- package/dist/esm/cli.js +90 -0
- package/dist/esm/cli.js.map +1 -0
- package/dist/esm/collect.d.ts +6 -0
- package/dist/esm/collect.js +80 -0
- package/dist/esm/collect.js.map +1 -0
- package/dist/esm/dict.d.ts +8 -0
- package/dist/esm/dict.js +28 -0
- package/dist/esm/dict.js.map +1 -0
- package/dist/esm/emit.d.ts +27 -0
- package/dist/esm/emit.js +131 -0
- package/dist/esm/emit.js.map +1 -0
- package/dist/esm/engines/tesseract.d.ts +30 -0
- package/dist/esm/engines/tesseract.js +190 -0
- package/dist/esm/engines/tesseract.js.map +1 -0
- package/dist/esm/errors.d.ts +21 -0
- package/dist/esm/errors.js +9 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/extract.d.ts +88 -0
- package/dist/esm/extract.js +147 -0
- package/dist/esm/extract.js.map +1 -0
- package/dist/esm/geometry.d.ts +63 -0
- package/dist/esm/geometry.js +279 -0
- package/dist/esm/geometry.js.map +1 -0
- package/dist/esm/index.d.ts +15 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lines.d.ts +3 -0
- package/dist/esm/lines.js +67 -0
- package/dist/esm/lines.js.map +1 -0
- package/dist/esm/markdown.d.ts +2 -0
- package/dist/esm/markdown.js +153 -0
- package/dist/esm/markdown.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-marker.d.ts +16 -0
- package/dist/esm/page-marker.js +40 -0
- package/dist/esm/page-marker.js.map +1 -0
- package/dist/esm/pdf-open.d.ts +12 -0
- package/dist/esm/pdf-open.js +61 -0
- package/dist/esm/pdf-open.js.map +1 -0
- package/dist/esm/raster.d.ts +26 -0
- package/dist/esm/raster.js +60 -0
- package/dist/esm/raster.js.map +1 -0
- package/dist/esm/render-analysis.d.ts +25 -0
- package/dist/esm/render-analysis.js +115 -0
- package/dist/esm/render-analysis.js.map +1 -0
- package/dist/esm/report.d.ts +12 -0
- package/dist/esm/report.js +182 -0
- package/dist/esm/report.js.map +1 -0
- package/dist/esm/resolve.d.ts +1 -0
- package/dist/esm/resolve.js +7 -0
- package/dist/esm/resolve.js.map +1 -0
- package/dist/esm/types.d.ts +171 -0
- package/dist/esm/types.js +6 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/validate.d.ts +14 -0
- package/dist/esm/validate.js +56 -0
- package/dist/esm/validate.js.map +1 -0
- package/package.json +67 -0
- package/skills/pdf-to-md/SKILL.md +102 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// report.ts — QA report over an extraction, machine-readable with --json.
|
|
2
|
+
import { countPageMarkers, isPageMarkerLine, splitByPageMarker } from './page-marker.js';
|
|
3
|
+
// < SPARSE warns only; < AUTO_OCR means the pages are certainly images (measured:
|
|
4
|
+
// an image-only 100-pager yields ~4 chars/page) and the auto-OCR fallback fires.
|
|
5
|
+
export const SPARSE_CHARS_PER_PAGE = 200;
|
|
6
|
+
export const AUTO_OCR_CHARS_PER_PAGE = 20;
|
|
7
|
+
/** A manually-bumped integer, deliberately not the npm package version —
|
|
8
|
+
* bump whenever a change can alter output for identical input. See `CHANGELOG.md` for history. */ export const OUTPUT_VERSION = 1;
|
|
9
|
+
// A body paragraph line may end in sentence/clause punctuation, a closing
|
|
10
|
+
// quote/bracket, an ellipsis, or a digit (chart/label lines like "1/3" legitimately end in a number).
|
|
11
|
+
const TERMINAL_PUNCT = /[.!?:;"”'’)\]…0-9]$/;
|
|
12
|
+
// Lines that are not body prose at all: headings (incl. "### pN" markers),
|
|
13
|
+
// floats/blockquotes, table rows, and list items — excluded before TERMINAL_PUNCT runs.
|
|
14
|
+
const NON_PROSE_LINE = /^[#>|*-]/;
|
|
15
|
+
// A `> [review: reason] text` line (src/emit.ts). Shared by pageStats (per
|
|
16
|
+
// page) and both top-level report builders (whole doc) so the counts can't drift apart.
|
|
17
|
+
const REVIEW_LINE = /^> \[review: /gm;
|
|
18
|
+
// Dangling lines split by length: long ones are the invisible column-break/splice
|
|
19
|
+
// signal; short ones are usually a missed heading or an unjoined label.
|
|
20
|
+
const DANGLING_LONG_MIN = 60;
|
|
21
|
+
/** Below this per-word confidence (0-100, tesseract.js's `confidence`), a
|
|
22
|
+
* word counts as low-confidence. Measured: junk max 43 vs prose min 76, cut 60, 2026-08-17. */ export const LOW_CONFIDENCE_THRESHOLD = 60;
|
|
23
|
+
// Cap on `lowConfidenceSample`'s length per page — a few representative
|
|
24
|
+
// offenders, not the whole page re-emitted a second time.
|
|
25
|
+
export const LOW_CONFIDENCE_SAMPLE_CAP = 12;
|
|
26
|
+
// Grouped from the flat recognized-word list (confidence is per-word, never
|
|
27
|
+
// merged into a line — see Word's comment). `measured` is tracked separately from `low` so a page with nothing to measure reports ABSENT, not a lying zero.
|
|
28
|
+
function lowConfidenceByPage(words) {
|
|
29
|
+
const byPage = new Map();
|
|
30
|
+
for (const w of words){
|
|
31
|
+
var _byPage_get;
|
|
32
|
+
if (w.confidence === undefined) continue;
|
|
33
|
+
const entry = (_byPage_get = byPage.get(w.page)) !== null && _byPage_get !== void 0 ? _byPage_get : {
|
|
34
|
+
measured: 0,
|
|
35
|
+
low: 0,
|
|
36
|
+
lowWords: []
|
|
37
|
+
};
|
|
38
|
+
entry.measured++;
|
|
39
|
+
if (w.confidence < LOW_CONFIDENCE_THRESHOLD) {
|
|
40
|
+
entry.low++;
|
|
41
|
+
entry.lowWords.push({
|
|
42
|
+
text: w.text,
|
|
43
|
+
confidence: w.confidence
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
byPage.set(w.page, entry);
|
|
47
|
+
}
|
|
48
|
+
const result = new Map();
|
|
49
|
+
for (const [page, entry] of byPage){
|
|
50
|
+
// Sort ascending by confidence BEFORE capping, so the cap keeps the
|
|
51
|
+
// worst offenders regardless of appearance order.
|
|
52
|
+
const sample = entry.lowWords.slice().sort((a, b)=>a.confidence - b.confidence).slice(0, LOW_CONFIDENCE_SAMPLE_CAP).map((w)=>w.text);
|
|
53
|
+
result.set(page, {
|
|
54
|
+
measured: entry.measured,
|
|
55
|
+
low: entry.low,
|
|
56
|
+
sample
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
// Counts of body paragraph LINES ending WITHOUT terminal punctuation, split
|
|
62
|
+
// by length: `long` is the invisible column-break/splice signal; `short` is usually a missed heading or unjoined label.
|
|
63
|
+
function danglingCounts(body) {
|
|
64
|
+
let long = 0;
|
|
65
|
+
let short = 0;
|
|
66
|
+
for (const raw of body.split('\n')){
|
|
67
|
+
const line = raw.trim();
|
|
68
|
+
if (!line || NON_PROSE_LINE.test(line)) continue;
|
|
69
|
+
if (TERMINAL_PUNCT.test(line)) continue;
|
|
70
|
+
if (line.length >= DANGLING_LONG_MIN) long++;
|
|
71
|
+
else short++;
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
long,
|
|
75
|
+
short
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
// One page's own measured stats, derivable from its body text alone —
|
|
79
|
+
// shared by `pageStats` and any future caller that already has a page's body text in hand.
|
|
80
|
+
function measurePage(body) {
|
|
81
|
+
const heads = body.match(/^#{1,6}\s*(.+)$/gm) || [];
|
|
82
|
+
const dangling = danglingCounts(body);
|
|
83
|
+
return {
|
|
84
|
+
chars: body.length,
|
|
85
|
+
headings: heads.length,
|
|
86
|
+
junkHeadings: heads.filter((h)=>!/[A-Za-z]{3}/.test(h)).length,
|
|
87
|
+
floats: (body.match(/^> \[floats\]/gm) || []).length,
|
|
88
|
+
paragraphs: (body.match(/^(?![#>-])\S.*$/gm) || []).length,
|
|
89
|
+
danglingLong: dangling.long,
|
|
90
|
+
danglingShort: dangling.short,
|
|
91
|
+
reviewBlocks: (body.match(REVIEW_LINE) || []).length
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
// Per-page QA rows, one per "### pN" block. The text path always passes
|
|
95
|
+
// markdown with markers forced on (`statsMd`) regardless of `--page-markers`, so pageStats is always populated.
|
|
96
|
+
//
|
|
97
|
+
// `hybridPages` (text path only) marks a row `largeImages: true` when that
|
|
98
|
+
// page used its text layer but also carries a large embedded image; omitted entirely on the OCR path, which has no equivalent signal.
|
|
99
|
+
function pageStats(md, hybridPages) {
|
|
100
|
+
const rows = [];
|
|
101
|
+
for (const { page, body } of splitByPageMarker(md)){
|
|
102
|
+
const row = {
|
|
103
|
+
page,
|
|
104
|
+
...measurePage(body)
|
|
105
|
+
};
|
|
106
|
+
if (hybridPages) row.largeImages = hybridPages.includes(page);
|
|
107
|
+
rows.push(row);
|
|
108
|
+
}
|
|
109
|
+
return rows;
|
|
110
|
+
}
|
|
111
|
+
export function buildReport({ numPages, md, statsMd, stats, bodyH, headingSizes, hybridPages = [] }) {
|
|
112
|
+
const heads = (md.match(/^#{1,6}\s*(.+)$/gm) || []).filter((h)=>!isPageMarkerLine(h)); // page markers are provenance, not structure
|
|
113
|
+
const perPage = Math.round(md.length / numPages);
|
|
114
|
+
const perPageStats = pageStats(statsMd, hybridPages);
|
|
115
|
+
return {
|
|
116
|
+
outputVersion: OUTPUT_VERSION,
|
|
117
|
+
path: 'text',
|
|
118
|
+
pages: numPages,
|
|
119
|
+
bodyFontHeight: bodyH,
|
|
120
|
+
headingSizes: headingSizes.slice(0, 6),
|
|
121
|
+
chars: md.length,
|
|
122
|
+
charsPerPage: perPage,
|
|
123
|
+
sparse: perPage < SPARSE_CHARS_PER_PAGE,
|
|
124
|
+
headings: heads.length,
|
|
125
|
+
junkHeadings: heads.filter((h)=>!/[A-Za-z]{3}/.test(h)).length,
|
|
126
|
+
listItems: stats.listItems,
|
|
127
|
+
paragraphs: stats.paragraphs,
|
|
128
|
+
joinedPairs: stats.joinedPairs,
|
|
129
|
+
callouts: stats.callouts,
|
|
130
|
+
closedHyphens: stats.closedHyphens,
|
|
131
|
+
danglingLong: perPageStats.reduce((sum, r)=>sum + r.danglingLong, 0),
|
|
132
|
+
danglingShort: perPageStats.reduce((sum, r)=>sum + r.danglingShort, 0),
|
|
133
|
+
reviewBlocks: perPageStats.reduce((sum, r)=>sum + r.reviewBlocks, 0),
|
|
134
|
+
// The text path never sees per-word confidence at all; 0 here means
|
|
135
|
+
// "nothing to measure" (see PageStatsRow's field doc), not "measured 0".
|
|
136
|
+
lowConfidenceWords: 0,
|
|
137
|
+
hybridPages,
|
|
138
|
+
pageStats: perPageStats
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// QA report for the OCR path: measured from the emitted markdown text.
|
|
142
|
+
// `fallback` records that the text layer was tried first and found empty.
|
|
143
|
+
export function buildOcrReport({ md, fallback, words = [] }) {
|
|
144
|
+
const numPages = countPageMarkers(md);
|
|
145
|
+
const heads = (md.match(/^#{1,6}\s*(.+)$/gm) || []).filter((h)=>!isPageMarkerLine(h));
|
|
146
|
+
const perPage = numPages ? Math.round(md.length / numPages) : 0;
|
|
147
|
+
const perPageStats = pageStats(md);
|
|
148
|
+
// Attach the low-confidence detector's per-page results after the fact:
|
|
149
|
+
// pageStats() has no confidence data (it reads markdown text only), so join it in here by page number.
|
|
150
|
+
const lowConf = lowConfidenceByPage(words);
|
|
151
|
+
for (const row of perPageStats){
|
|
152
|
+
const entry = lowConf.get(row.page);
|
|
153
|
+
if (entry && entry.measured > 0) {
|
|
154
|
+
row.lowConfidenceWords = entry.low;
|
|
155
|
+
if (entry.sample.length) row.lowConfidenceSample = entry.sample;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
outputVersion: OUTPUT_VERSION,
|
|
160
|
+
path: 'ocr',
|
|
161
|
+
pages: numPages,
|
|
162
|
+
ocrFallback: !!fallback,
|
|
163
|
+
chars: md.length,
|
|
164
|
+
charsPerPage: perPage,
|
|
165
|
+
sparse: perPage < SPARSE_CHARS_PER_PAGE,
|
|
166
|
+
headings: heads.length,
|
|
167
|
+
junkHeadings: heads.filter((h)=>!/[A-Za-z]{3}/.test(h)).length,
|
|
168
|
+
paragraphs: (md.match(/^(?![#>-])\S.*$/gm) || []).length,
|
|
169
|
+
// Exclude BOTH "[floats]" and "[review: ...]" lines — a review marker is
|
|
170
|
+
// a labeled unstructured block, not a genuine callout.
|
|
171
|
+
callouts: (md.match(/^> (?!\[floats\]|\[review:)/gm) || []).length,
|
|
172
|
+
floats: (md.match(/^> \[floats\]/gm) || []).length,
|
|
173
|
+
danglingLong: perPageStats.reduce((sum, r)=>sum + r.danglingLong, 0),
|
|
174
|
+
danglingShort: perPageStats.reduce((sum, r)=>sum + r.danglingShort, 0),
|
|
175
|
+
reviewBlocks: perPageStats.reduce((sum, r)=>sum + r.reviewBlocks, 0),
|
|
176
|
+
lowConfidenceWords: perPageStats.reduce((sum, r)=>{
|
|
177
|
+
var _r_lowConfidenceWords;
|
|
178
|
+
return sum + ((_r_lowConfidenceWords = r.lowConfidenceWords) !== null && _r_lowConfidenceWords !== void 0 ? _r_lowConfidenceWords : 0);
|
|
179
|
+
}, 0),
|
|
180
|
+
pageStats: perPageStats
|
|
181
|
+
};
|
|
182
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/report.ts"],"sourcesContent":["// report.ts — QA report over an extraction, machine-readable with --json.\nimport { countPageMarkers, isPageMarkerLine, splitByPageMarker } from './page-marker.ts';\nimport type { BuildOcrReportInput, BuildReportInput, PageStatsRow, Report, WordConfidenceInput } from './types.ts';\n\n// < SPARSE warns only; < AUTO_OCR means the pages are certainly images (measured:\n// an image-only 100-pager yields ~4 chars/page) and the auto-OCR fallback fires.\nexport const SPARSE_CHARS_PER_PAGE = 200;\nexport const AUTO_OCR_CHARS_PER_PAGE = 20;\n\n/** A manually-bumped integer, deliberately not the npm package version —\n * bump whenever a change can alter output for identical input. See `CHANGELOG.md` for history. */\nexport const OUTPUT_VERSION = 1;\n\n// A body paragraph line may end in sentence/clause punctuation, a closing\n// quote/bracket, an ellipsis, or a digit (chart/label lines like \"1/3\" legitimately end in a number).\nconst TERMINAL_PUNCT = /[.!?:;\"”'’)\\]…0-9]$/;\n\n// Lines that are not body prose at all: headings (incl. \"### pN\" markers),\n// floats/blockquotes, table rows, and list items — excluded before TERMINAL_PUNCT runs.\nconst NON_PROSE_LINE = /^[#>|*-]/;\n\n// A `> [review: reason] text` line (src/emit.ts). Shared by pageStats (per\n// page) and both top-level report builders (whole doc) so the counts can't drift apart.\nconst REVIEW_LINE = /^> \\[review: /gm;\n\n// Dangling lines split by length: long ones are the invisible column-break/splice\n// signal; short ones are usually a missed heading or an unjoined label.\nconst DANGLING_LONG_MIN = 60;\n\n/** Below this per-word confidence (0-100, tesseract.js's `confidence`), a\n * word counts as low-confidence. Measured: junk max 43 vs prose min 76, cut 60, 2026-08-17. */\nexport const LOW_CONFIDENCE_THRESHOLD = 60;\n\n// Cap on `lowConfidenceSample`'s length per page — a few representative\n// offenders, not the whole page re-emitted a second time.\nexport const LOW_CONFIDENCE_SAMPLE_CAP = 12;\n\ninterface LowConfidenceStats {\n measured: number; // words on this page that carried a confidence value at all\n low: number; // of those, how many scored below LOW_CONFIDENCE_THRESHOLD\n sample: string[]; // worst-confidence-first, capped list of the actual low-confidence tokens\n}\n\n// Grouped from the flat recognized-word list (confidence is per-word, never\n// merged into a line — see Word's comment). `measured` is tracked separately from `low` so a page with nothing to measure reports ABSENT, not a lying zero.\nfunction lowConfidenceByPage(words: WordConfidenceInput[]): Map<number, LowConfidenceStats> {\n const byPage = new Map<number, { measured: number; low: number; lowWords: { text: string; confidence: number }[] }>();\n for (const w of words) {\n if (w.confidence === undefined) continue;\n const entry = byPage.get(w.page) ?? { measured: 0, low: 0, lowWords: [] };\n entry.measured++;\n if (w.confidence < LOW_CONFIDENCE_THRESHOLD) {\n entry.low++;\n entry.lowWords.push({ text: w.text, confidence: w.confidence });\n }\n byPage.set(w.page, entry);\n }\n const result = new Map<number, LowConfidenceStats>();\n for (const [page, entry] of byPage) {\n // Sort ascending by confidence BEFORE capping, so the cap keeps the\n // worst offenders regardless of appearance order.\n const sample = entry.lowWords\n .slice()\n .sort((a, b) => a.confidence - b.confidence)\n .slice(0, LOW_CONFIDENCE_SAMPLE_CAP)\n .map((w) => w.text);\n result.set(page, { measured: entry.measured, low: entry.low, sample });\n }\n return result;\n}\n\ninterface DanglingCounts {\n long: number;\n short: number;\n}\n\n// Counts of body paragraph LINES ending WITHOUT terminal punctuation, split\n// by length: `long` is the invisible column-break/splice signal; `short` is usually a missed heading or unjoined label.\nfunction danglingCounts(body: string): DanglingCounts {\n let long = 0;\n let short = 0;\n for (const raw of body.split('\\n')) {\n const line = raw.trim();\n if (!line || NON_PROSE_LINE.test(line)) continue;\n if (TERMINAL_PUNCT.test(line)) continue;\n if (line.length >= DANGLING_LONG_MIN) long++;\n else short++;\n }\n return { long, short };\n}\n\n// One page's own measured stats, derivable from its body text alone —\n// shared by `pageStats` and any future caller that already has a page's body text in hand.\nfunction measurePage(body: string): Omit<PageStatsRow, 'page' | 'largeImages' | 'lowConfidenceWords' | 'lowConfidenceSample'> {\n const heads = body.match(/^#{1,6}\\s*(.+)$/gm) || [];\n const dangling = danglingCounts(body);\n return {\n chars: body.length,\n headings: heads.length,\n junkHeadings: heads.filter((h) => !/[A-Za-z]{3}/.test(h)).length,\n floats: (body.match(/^> \\[floats\\]/gm) || []).length,\n paragraphs: (body.match(/^(?![#>-])\\S.*$/gm) || []).length,\n danglingLong: dangling.long,\n danglingShort: dangling.short,\n reviewBlocks: (body.match(REVIEW_LINE) || []).length,\n };\n}\n\n// Per-page QA rows, one per \"### pN\" block. The text path always passes\n// markdown with markers forced on (`statsMd`) regardless of `--page-markers`, so pageStats is always populated.\n//\n// `hybridPages` (text path only) marks a row `largeImages: true` when that\n// page used its text layer but also carries a large embedded image; omitted entirely on the OCR path, which has no equivalent signal.\nfunction pageStats(md: string, hybridPages?: number[]): PageStatsRow[] {\n const rows: PageStatsRow[] = [];\n for (const { page, body } of splitByPageMarker(md)) {\n const row: PageStatsRow = { page, ...measurePage(body) };\n if (hybridPages) row.largeImages = hybridPages.includes(page);\n rows.push(row);\n }\n return rows;\n}\n\nexport function buildReport({ numPages, md, statsMd, stats, bodyH, headingSizes, hybridPages = [] }: BuildReportInput): Report {\n const heads = (md.match(/^#{1,6}\\s*(.+)$/gm) || []).filter((h) => !isPageMarkerLine(h)); // page markers are provenance, not structure\n const perPage = Math.round(md.length / numPages);\n const perPageStats = pageStats(statsMd, hybridPages);\n return {\n outputVersion: OUTPUT_VERSION,\n path: 'text',\n pages: numPages,\n bodyFontHeight: bodyH,\n headingSizes: headingSizes.slice(0, 6),\n chars: md.length,\n charsPerPage: perPage,\n sparse: perPage < SPARSE_CHARS_PER_PAGE,\n headings: heads.length,\n junkHeadings: heads.filter((h) => !/[A-Za-z]{3}/.test(h)).length,\n listItems: stats.listItems,\n paragraphs: stats.paragraphs,\n joinedPairs: stats.joinedPairs,\n callouts: stats.callouts,\n closedHyphens: stats.closedHyphens,\n danglingLong: perPageStats.reduce((sum, r) => sum + r.danglingLong, 0),\n danglingShort: perPageStats.reduce((sum, r) => sum + r.danglingShort, 0),\n reviewBlocks: perPageStats.reduce((sum, r) => sum + r.reviewBlocks, 0),\n // The text path never sees per-word confidence at all; 0 here means\n // \"nothing to measure\" (see PageStatsRow's field doc), not \"measured 0\".\n lowConfidenceWords: 0,\n hybridPages,\n pageStats: perPageStats,\n };\n}\n\n// QA report for the OCR path: measured from the emitted markdown text.\n// `fallback` records that the text layer was tried first and found empty.\nexport function buildOcrReport({ md, fallback, words = [] }: BuildOcrReportInput): Report {\n const numPages = countPageMarkers(md);\n const heads = (md.match(/^#{1,6}\\s*(.+)$/gm) || []).filter((h) => !isPageMarkerLine(h));\n const perPage = numPages ? Math.round(md.length / numPages) : 0;\n const perPageStats = pageStats(md);\n // Attach the low-confidence detector's per-page results after the fact:\n // pageStats() has no confidence data (it reads markdown text only), so join it in here by page number.\n const lowConf = lowConfidenceByPage(words);\n for (const row of perPageStats) {\n const entry = lowConf.get(row.page);\n if (entry && entry.measured > 0) {\n row.lowConfidenceWords = entry.low;\n if (entry.sample.length) row.lowConfidenceSample = entry.sample;\n }\n }\n return {\n outputVersion: OUTPUT_VERSION,\n path: 'ocr',\n pages: numPages,\n ocrFallback: !!fallback,\n chars: md.length,\n charsPerPage: perPage,\n sparse: perPage < SPARSE_CHARS_PER_PAGE,\n headings: heads.length,\n junkHeadings: heads.filter((h) => !/[A-Za-z]{3}/.test(h)).length,\n paragraphs: (md.match(/^(?![#>-])\\S.*$/gm) || []).length,\n // Exclude BOTH \"[floats]\" and \"[review: ...]\" lines — a review marker is\n // a labeled unstructured block, not a genuine callout.\n callouts: (md.match(/^> (?!\\[floats\\]|\\[review:)/gm) || []).length,\n floats: (md.match(/^> \\[floats\\]/gm) || []).length,\n danglingLong: perPageStats.reduce((sum, r) => sum + r.danglingLong, 0),\n danglingShort: perPageStats.reduce((sum, r) => sum + r.danglingShort, 0),\n reviewBlocks: perPageStats.reduce((sum, r) => sum + r.reviewBlocks, 0),\n lowConfidenceWords: perPageStats.reduce((sum, r) => sum + (r.lowConfidenceWords ?? 0), 0),\n pageStats: perPageStats,\n };\n}\n"],"names":["countPageMarkers","isPageMarkerLine","splitByPageMarker","SPARSE_CHARS_PER_PAGE","AUTO_OCR_CHARS_PER_PAGE","OUTPUT_VERSION","TERMINAL_PUNCT","NON_PROSE_LINE","REVIEW_LINE","DANGLING_LONG_MIN","LOW_CONFIDENCE_THRESHOLD","LOW_CONFIDENCE_SAMPLE_CAP","lowConfidenceByPage","words","byPage","Map","w","confidence","undefined","entry","get","page","measured","low","lowWords","push","text","set","result","sample","slice","sort","a","b","map","danglingCounts","body","long","short","raw","split","line","trim","test","length","measurePage","heads","match","dangling","chars","headings","junkHeadings","filter","h","floats","paragraphs","danglingLong","danglingShort","reviewBlocks","pageStats","md","hybridPages","rows","row","largeImages","includes","buildReport","numPages","statsMd","stats","bodyH","headingSizes","perPage","Math","round","perPageStats","outputVersion","path","pages","bodyFontHeight","charsPerPage","sparse","listItems","joinedPairs","callouts","closedHyphens","reduce","sum","r","lowConfidenceWords","buildOcrReport","fallback","lowConf","lowConfidenceSample","ocrFallback"],"mappings":"AAAA,0EA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O,cAAcL,MAAMM,MAAM,CAAC,CAACC,IAAM,CAAC,cAAcV,IAAI,CAACU,IAAIT,MAAM;QAChEW,YAAY,AAACK,CAAAA,GAAGb,KAAK,CAAC,wBAAwB,EAAE,AAAD,EAAGH,MAAM;QACxD,yEAAyE;QACzE,uDAAuD;QACvDwC,UAAU,AAACxB,CAAAA,GAAGb,KAAK,CAAC,oCAAoC,EAAE,AAAD,EAAGH,MAAM;QAClEU,QAAQ,AAACM,CAAAA,GAAGb,KAAK,CAAC,sBAAsB,EAAE,AAAD,EAAGH,MAAM;QAClDY,cAAcmB,aAAaW,MAAM,CAAC,CAACC,KAAKC,IAAMD,MAAMC,EAAEhC,YAAY,EAAE;QACpEC,eAAekB,aAAaW,MAAM,CAAC,CAACC,KAAKC,IAAMD,MAAMC,EAAE/B,aAAa,EAAE;QACtEC,cAAciB,aAAaW,MAAM,CAAC,CAACC,KAAKC,IAAMD,MAAMC,EAAE9B,YAAY,EAAE;QACpE+B,oBAAoBd,aAAaW,MAAM,CAAC,CAACC,KAAKC;gBAAaA;mBAAPD,QAAOC,wBAAAA,EAAEC,kBAAkB,cAApBD,mCAAAA,wBAAwB;WAAI;QACvF7B,WAAWgB;IACb;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function resolvePackagePath(specifier: string): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Resolves an installed package file's on-disk path for both build targets:
|
|
2
|
+
// ESM's import.meta.resolve returns a file:// URL, the CJS build's rewritten require.resolve a plain path — normalizes both to a plain path.
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
export function resolvePackagePath(specifier) {
|
|
5
|
+
const resolved = import.meta.resolve(specifier);
|
|
6
|
+
return resolved.startsWith('file://') ? fileURLToPath(resolved) : resolved;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/resolve.ts"],"sourcesContent":["// Resolves an installed package file's on-disk path for both build targets:\n// ESM's import.meta.resolve returns a file:// URL, the CJS build's rewritten require.resolve a plain path — normalizes both to a plain path.\nimport { fileURLToPath } from 'node:url';\n\nexport function resolvePackagePath(specifier: string): string {\n const resolved = import.meta.resolve(specifier);\n return resolved.startsWith('file://') ? fileURLToPath(resolved) : resolved;\n}\n"],"names":["fileURLToPath","resolvePackagePath","specifier","resolved","resolve","startsWith"],"mappings":"AAAA,4EAA4E;AAC5E,6IAA6I;AAC7I,SAASA,aAAa,QAAQ,WAAW;AAEzC,OAAO,SAASC,mBAAmBC,SAAiB;IAClD,MAAMC,WAAW,YAAYC,OAAO,CAACF;IACrC,OAAOC,SAASE,UAAU,CAAC,aAAaL,cAAcG,YAAYA;AACpE"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
export interface Glyph {
|
|
2
|
+
s: string;
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
w: number;
|
|
6
|
+
h: number;
|
|
7
|
+
}
|
|
8
|
+
export interface CollectedPage {
|
|
9
|
+
page: number;
|
|
10
|
+
glyphs: Glyph[];
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
textChars: number;
|
|
14
|
+
largeImage: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface CollectResult {
|
|
17
|
+
numPages: number;
|
|
18
|
+
pages: CollectedPage[];
|
|
19
|
+
heightChars: Map<number, number>;
|
|
20
|
+
}
|
|
21
|
+
export interface Line {
|
|
22
|
+
text: string;
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
h: number;
|
|
26
|
+
col: number;
|
|
27
|
+
}
|
|
28
|
+
export interface PageLines {
|
|
29
|
+
page: number;
|
|
30
|
+
lines: Line[];
|
|
31
|
+
}
|
|
32
|
+
export interface BuildMarkdownOptions {
|
|
33
|
+
pageMarkers?: boolean;
|
|
34
|
+
}
|
|
35
|
+
export interface MarkdownStats {
|
|
36
|
+
headings: number;
|
|
37
|
+
listItems: number;
|
|
38
|
+
paragraphs: number;
|
|
39
|
+
joinedPairs: number;
|
|
40
|
+
reflowed: number;
|
|
41
|
+
callouts: number;
|
|
42
|
+
closedHyphens: number;
|
|
43
|
+
}
|
|
44
|
+
export interface MarkdownResult {
|
|
45
|
+
md: string;
|
|
46
|
+
stats: MarkdownStats;
|
|
47
|
+
bodyH: number;
|
|
48
|
+
headingSizes: number[];
|
|
49
|
+
}
|
|
50
|
+
/** One row of the per-page QA breakdown ("### pN" blocks) — used to decide
|
|
51
|
+
* which pages are worth rendering and re-checking (README's "Design contract"). */
|
|
52
|
+
export interface PageStatsRow {
|
|
53
|
+
page: number;
|
|
54
|
+
chars: number;
|
|
55
|
+
headings: number;
|
|
56
|
+
junkHeadings: number;
|
|
57
|
+
floats: number;
|
|
58
|
+
paragraphs: number;
|
|
59
|
+
danglingLong: number;
|
|
60
|
+
danglingShort: number;
|
|
61
|
+
/** count of `> [review: ...]` lines — blocks the geometry could not order
|
|
62
|
+
* and labeled instead of guessing (README's "Design contract"). */
|
|
63
|
+
reviewBlocks: number;
|
|
64
|
+
/** present only on the text path's rows; omitted (not `false`) on the OCR
|
|
65
|
+
* path, which has no per-page hybrid-image signal. */
|
|
66
|
+
largeImages?: boolean;
|
|
67
|
+
/** count of words below `report.ts`'s LOW_CONFIDENCE_THRESHOLD; omitted (not
|
|
68
|
+
* 0) when the page had no confidence data at all, so "measured 0" and "nothing to measure" stay distinct. */
|
|
69
|
+
lowConfidenceWords?: number;
|
|
70
|
+
/** the suspect tokens behind `lowConfidenceWords`, worst-confidence first
|
|
71
|
+
* and capped (`report.ts`'s LOW_CONFIDENCE_SAMPLE_CAP) so a chart-heavy page can't bloat the JSON. */
|
|
72
|
+
lowConfidenceSample?: string[];
|
|
73
|
+
}
|
|
74
|
+
export interface BuildReportInput {
|
|
75
|
+
numPages: number;
|
|
76
|
+
md: string;
|
|
77
|
+
/** identical to `md` but always carries "### pN" markers, so pageStats is
|
|
78
|
+
* measured independent of the caller's `--page-markers` choice; same string as `md` when markers were requested. */
|
|
79
|
+
statsMd: string;
|
|
80
|
+
stats: MarkdownStats;
|
|
81
|
+
bodyH: number;
|
|
82
|
+
headingSizes: number[];
|
|
83
|
+
hybridPages?: number[];
|
|
84
|
+
}
|
|
85
|
+
/** One recognized word's page/text/confidence — `report.ts`'s low-confidence
|
|
86
|
+
* detector input; a structural subset of OcrWordInput kept here so `types.ts` doesn't import the library-layer module that depends on it. */
|
|
87
|
+
export interface WordConfidenceInput {
|
|
88
|
+
page: number;
|
|
89
|
+
text: string;
|
|
90
|
+
confidence?: number;
|
|
91
|
+
}
|
|
92
|
+
export interface BuildOcrReportInput {
|
|
93
|
+
md: string;
|
|
94
|
+
fallback?: boolean;
|
|
95
|
+
/** every recognized word, for `report.ts`'s low-confidence detector.
|
|
96
|
+
* Defaults to [] (a pre-confidence --words-json replay), reporting the detector absent, never wrong. */
|
|
97
|
+
words?: WordConfidenceInput[];
|
|
98
|
+
}
|
|
99
|
+
/** Per-document and per-page QA metrics returned by every extraction entry
|
|
100
|
+
* point, discriminated by `path`; OCR-only and text-only fields are grouped below. */
|
|
101
|
+
export interface Report {
|
|
102
|
+
/** manually-bumped integer (not the npm package version) — see
|
|
103
|
+
* `report.ts`'s OUTPUT_VERSION for the bump rule; present on every Report so a frozen baseline's staleness is self-announcing. */
|
|
104
|
+
outputVersion: number;
|
|
105
|
+
path: 'text' | 'ocr';
|
|
106
|
+
pages: number;
|
|
107
|
+
chars: number;
|
|
108
|
+
charsPerPage: number;
|
|
109
|
+
sparse: boolean;
|
|
110
|
+
headings: number;
|
|
111
|
+
junkHeadings: number;
|
|
112
|
+
paragraphs: number;
|
|
113
|
+
callouts: number;
|
|
114
|
+
danglingLong: number;
|
|
115
|
+
danglingShort: number;
|
|
116
|
+
/** total `> [review: ...]` blocks across all pages (sum of pageStats'
|
|
117
|
+
* reviewBlocks); always 0 on the text path — only the OCR path emits review markers. */
|
|
118
|
+
reviewBlocks: number;
|
|
119
|
+
/** document-level sum of pageStats' `lowConfidenceWords` — see that
|
|
120
|
+
* field for why 0 can mean "measured 0" or "nothing to measure". */
|
|
121
|
+
lowConfidenceWords: number;
|
|
122
|
+
pageStats: PageStatsRow[];
|
|
123
|
+
floats?: number;
|
|
124
|
+
ocrFallback?: boolean;
|
|
125
|
+
bodyFontHeight?: number;
|
|
126
|
+
headingSizes?: number[];
|
|
127
|
+
listItems?: number;
|
|
128
|
+
joinedPairs?: number;
|
|
129
|
+
closedHyphens?: number;
|
|
130
|
+
hybridPages?: number[];
|
|
131
|
+
}
|
|
132
|
+
/** One recognized/collected word, page-relative; `box` is always present.
|
|
133
|
+
* See `AnalysisPage.words` for the coordinate contract and per-path granularity difference. */
|
|
134
|
+
export interface AnalysisWord {
|
|
135
|
+
text: string;
|
|
136
|
+
box: {
|
|
137
|
+
x: number;
|
|
138
|
+
y: number;
|
|
139
|
+
w: number;
|
|
140
|
+
h: number;
|
|
141
|
+
};
|
|
142
|
+
confidence?: number;
|
|
143
|
+
}
|
|
144
|
+
/** One structural block; `wordIndexes` links it to its page's `words` list (blocks -> words only).
|
|
145
|
+
* `text` is bare content for `'heading'` (level separate), else the LITERAL markdown line (decoration included, round-trippable) — a pull-quote is `'paragraph'`, not a 5th type. */
|
|
146
|
+
export interface AnalysisBlock {
|
|
147
|
+
type: 'heading' | 'paragraph' | 'float' | 'review';
|
|
148
|
+
text: string;
|
|
149
|
+
/** Heading level (integer 1-6); REQUIRED when `type` is `'heading'`,
|
|
150
|
+
* FORBIDDEN otherwise — toMarkdown()/toText() validate this fail-fast (throws PdfToMdError/'ANALYSIS_INPUT'), never silently ignored. */
|
|
151
|
+
level?: number;
|
|
152
|
+
wordIndexes: number[];
|
|
153
|
+
}
|
|
154
|
+
/** One page's flat words plus its structural blocks. */
|
|
155
|
+
export interface AnalysisPage {
|
|
156
|
+
page: number;
|
|
157
|
+
/** Flat, page-relative word list; `AnalysisBlock.wordIndexes` indexes into it. `box` is always normalized [0,1], origin bottom-left, y up, regardless of path.
|
|
158
|
+
* OCR-path is genuinely per-word; TEXT-path is per visual LINE (`box.w` always 0, `box.y` the line's baseline) — coarser but honest, not faked. */
|
|
159
|
+
words: AnalysisWord[];
|
|
160
|
+
blocks: AnalysisBlock[];
|
|
161
|
+
}
|
|
162
|
+
/** analyze()'s output: plain, JSON-serializable per-page words/blocks plus
|
|
163
|
+
* the same QA `report` the other entry points return. */
|
|
164
|
+
export interface Analysis {
|
|
165
|
+
/** manually-bumped integer — see `Report.outputVersion` / `report.ts`'s OUTPUT_VERSION. */
|
|
166
|
+
outputVersion: number;
|
|
167
|
+
/** the same QA report extractText/extractOcr/pdfToMarkdown return, always
|
|
168
|
+
* measured off real markdown (OCR: the shared core's output; TEXT: buildMarkdown's reflowed output, not this coarser IR) — never stale relative to the IR. */
|
|
169
|
+
report: Report;
|
|
170
|
+
pages: AnalysisPage[];
|
|
171
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Shared shapes threaded through the collect -> lines -> markdown -> report
|
|
2
|
+
// pipeline; kept in one place since every downstream stage uses at least one.
|
|
3
|
+
// A positioned glyph run from the PDF's text layer: `s` may be more than one
|
|
4
|
+
// character; `y` is flipped so ascending sorts top-down; `h` is font height rounded to 0.1pt.
|
|
5
|
+
/** analyze()'s output: plain, JSON-serializable per-page words/blocks plus
|
|
6
|
+
* the same QA `report` the other entry points return. */ export { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/types.ts"],"sourcesContent":["// Shared shapes threaded through the collect -> lines -> markdown -> report\n// pipeline; kept in one place since every downstream stage uses at least one.\n\n// A positioned glyph run from the PDF's text layer: `s` may be more than one\n// character; `y` is flipped so ascending sorts top-down; `h` is font height rounded to 0.1pt.\nexport interface Glyph {\n s: string;\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n// One page's worth of collected glyphs plus the geometry collect.ts needs\n// downstream (lines.ts's two-column detection, report.ts's hybrid-page flag).\nexport interface CollectedPage {\n page: number;\n glyphs: Glyph[];\n width: number;\n height: number;\n textChars: number;\n largeImage: boolean;\n}\n\n// collect.ts's return value: selected pages plus the char-weighted\n// font-height histogram, built ONLY from those pages (matters for a `--pages` re-extraction — see collect.ts).\nexport interface CollectResult {\n numPages: number;\n pages: CollectedPage[];\n heightChars: Map<number, number>;\n}\n\n// A visual line assembled from glyphs (lines.ts's `toLines`): `h` is the\n// char-weighted dominant height (not max); `col` is 0 (left/only) or 1 (right, de-braided page).\nexport interface Line {\n text: string;\n x: number;\n y: number;\n h: number;\n col: number;\n}\n\n// One page's lines, as src/extract.ts assembles them for buildMarkdown.\nexport interface PageLines {\n page: number;\n lines: Line[];\n}\n\nexport interface BuildMarkdownOptions {\n pageMarkers?: boolean;\n}\n\n// Running counters src/markdown.ts accumulates while building the output,\n// surfaced in the QA report (src/report.ts's buildReport).\nexport interface MarkdownStats {\n headings: number;\n listItems: number;\n paragraphs: number;\n joinedPairs: number;\n reflowed: number;\n callouts: number;\n closedHyphens: number;\n}\n\nexport interface MarkdownResult {\n md: string;\n stats: MarkdownStats;\n bodyH: number;\n headingSizes: number[];\n}\n\n/** One row of the per-page QA breakdown (\"### pN\" blocks) — used to decide\n * which pages are worth rendering and re-checking (README's \"Design contract\"). */\nexport interface PageStatsRow {\n page: number;\n chars: number;\n headings: number;\n junkHeadings: number;\n floats: number;\n paragraphs: number;\n danglingLong: number;\n danglingShort: number;\n /** count of `> [review: ...]` lines — blocks the geometry could not order\n * and labeled instead of guessing (README's \"Design contract\"). */\n reviewBlocks: number;\n /** present only on the text path's rows; omitted (not `false`) on the OCR\n * path, which has no per-page hybrid-image signal. */\n largeImages?: boolean;\n /** count of words below `report.ts`'s LOW_CONFIDENCE_THRESHOLD; omitted (not\n * 0) when the page had no confidence data at all, so \"measured 0\" and \"nothing to measure\" stay distinct. */\n lowConfidenceWords?: number;\n /** the suspect tokens behind `lowConfidenceWords`, worst-confidence first\n * and capped (`report.ts`'s LOW_CONFIDENCE_SAMPLE_CAP) so a chart-heavy page can't bloat the JSON. */\n lowConfidenceSample?: string[];\n}\n\n// buildReport's params (src/report.ts) — everything the text (default) path\n// hands over after running collect -> lines -> markdown.\nexport interface BuildReportInput {\n numPages: number;\n md: string;\n /** identical to `md` but always carries \"### pN\" markers, so pageStats is\n * measured independent of the caller's `--page-markers` choice; same string as `md` when markers were requested. */\n statsMd: string;\n stats: MarkdownStats;\n bodyH: number;\n headingSizes: number[];\n hybridPages?: number[];\n}\n\n/** One recognized word's page/text/confidence — `report.ts`'s low-confidence\n * detector input; a structural subset of OcrWordInput kept here so `types.ts` doesn't import the library-layer module that depends on it. */\nexport interface WordConfidenceInput {\n page: number;\n text: string;\n confidence?: number;\n}\n\nexport interface BuildOcrReportInput {\n md: string;\n fallback?: boolean;\n /** every recognized word, for `report.ts`'s low-confidence detector.\n * Defaults to [] (a pre-confidence --words-json replay), reporting the detector absent, never wrong. */\n words?: WordConfidenceInput[];\n}\n\n/** Per-document and per-page QA metrics returned by every extraction entry\n * point, discriminated by `path`; OCR-only and text-only fields are grouped below. */\nexport interface Report {\n /** manually-bumped integer (not the npm package version) — see\n * `report.ts`'s OUTPUT_VERSION for the bump rule; present on every Report so a frozen baseline's staleness is self-announcing. */\n outputVersion: number;\n path: 'text' | 'ocr';\n pages: number;\n chars: number;\n charsPerPage: number;\n sparse: boolean;\n headings: number;\n junkHeadings: number;\n paragraphs: number;\n callouts: number;\n danglingLong: number;\n danglingShort: number;\n /** total `> [review: ...]` blocks across all pages (sum of pageStats'\n * reviewBlocks); always 0 on the text path — only the OCR path emits review markers. */\n reviewBlocks: number;\n /** document-level sum of pageStats' `lowConfidenceWords` — see that\n * field for why 0 can mean \"measured 0\" or \"nothing to measure\". */\n lowConfidenceWords: number;\n pageStats: PageStatsRow[];\n\n // --- OCR-only ---\n floats?: number;\n ocrFallback?: boolean;\n\n // --- text-only ---\n bodyFontHeight?: number;\n headingSizes?: number[];\n listItems?: number;\n joinedPairs?: number;\n closedHyphens?: number;\n hybridPages?: number[];\n}\n\n// --- Analysis: the raw-mode IR (analyze() produces it; toMarkdown()/toText()\n// in render-analysis.ts render it — see toMarkdown's doc comment for the fail-fast validation contract) ---\n\n/** One recognized/collected word, page-relative; `box` is always present.\n * See `AnalysisPage.words` for the coordinate contract and per-path granularity difference. */\nexport interface AnalysisWord {\n text: string;\n box: { x: number; y: number; w: number; h: number };\n confidence?: number;\n}\n\n/** One structural block; `wordIndexes` links it to its page's `words` list (blocks -> words only).\n * `text` is bare content for `'heading'` (level separate), else the LITERAL markdown line (decoration included, round-trippable) — a pull-quote is `'paragraph'`, not a 5th type. */\nexport interface AnalysisBlock {\n type: 'heading' | 'paragraph' | 'float' | 'review';\n text: string;\n /** Heading level (integer 1-6); REQUIRED when `type` is `'heading'`,\n * FORBIDDEN otherwise — toMarkdown()/toText() validate this fail-fast (throws PdfToMdError/'ANALYSIS_INPUT'), never silently ignored. */\n level?: number;\n wordIndexes: number[];\n}\n\n/** One page's flat words plus its structural blocks. */\nexport interface AnalysisPage {\n page: number;\n /** Flat, page-relative word list; `AnalysisBlock.wordIndexes` indexes into it. `box` is always normalized [0,1], origin bottom-left, y up, regardless of path.\n * OCR-path is genuinely per-word; TEXT-path is per visual LINE (`box.w` always 0, `box.y` the line's baseline) — coarser but honest, not faked. */\n words: AnalysisWord[];\n blocks: AnalysisBlock[];\n}\n\n/** analyze()'s output: plain, JSON-serializable per-page words/blocks plus\n * the same QA `report` the other entry points return. */\nexport interface Analysis {\n /** manually-bumped integer — see `Report.outputVersion` / `report.ts`'s OUTPUT_VERSION. */\n outputVersion: number;\n /** the same QA report extractText/extractOcr/pdfToMarkdown return, always\n * measured off real markdown (OCR: the shared core's output; TEXT: buildMarkdown's reflowed output, not this coarser IR) — never stale relative to the IR. */\n report: Report;\n pages: AnalysisPage[];\n}\n"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,8EAA8E;AAE9E,6EAA6E;AAC7E,8FAA8F;AA+L9F;uDACuD,GACvD,WAOC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { OcrWordInput } from './extract.js';
|
|
2
|
+
/** Validates the one option every entry point accepts before touching the
|
|
3
|
+
* PDF. Type-only import from `extract.ts` keeps this cycle-free. */
|
|
4
|
+
export declare function validatePages(pages: unknown): void;
|
|
5
|
+
/** Validates analyze()'s non-string `input` branch (`{ words }`) before
|
|
6
|
+
* `.words` is read, so a plain-JS mistake like `analyze(null)` throws PdfToMdError/WORDS_INPUT instead of a raw TypeError. */
|
|
7
|
+
export declare function validateWordsInput(input: unknown): OcrWordInput[];
|
|
8
|
+
/** Runtime shape guard for `buildOcrAnalysisPages`'s input: a plain-JS
|
|
9
|
+
* caller bypasses the TS union, so this turns a raw TypeError into PdfToMdError before any property is read off it. */
|
|
10
|
+
export declare function validateOcrAnalysisInput(input: unknown): {
|
|
11
|
+
pdfPath: string;
|
|
12
|
+
} | {
|
|
13
|
+
words: OcrWordInput[];
|
|
14
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// validate.ts — input-shape validation shared by analyze.ts and extract.ts,
|
|
2
|
+
// kept out of both to dodge their value-level import cycle (extract.ts imports analyze.ts's buildOcrAnalysisPages; both need this).
|
|
3
|
+
import { PdfToMdError } from './errors.js';
|
|
4
|
+
/** Validates the one option every entry point accepts before touching the
|
|
5
|
+
* PDF. Type-only import from `extract.ts` keeps this cycle-free. */ export function validatePages(pages) {
|
|
6
|
+
if (pages === undefined) return;
|
|
7
|
+
const isRecord = typeof pages === 'object' && pages !== null;
|
|
8
|
+
const first = isRecord ? pages.first : undefined;
|
|
9
|
+
const last = isRecord ? pages.last : undefined;
|
|
10
|
+
const firstOk = typeof first === 'number' && Number.isFinite(first) && first >= 1;
|
|
11
|
+
const lastOk = last === undefined || typeof last === 'number' && Number.isFinite(last) && last >= first;
|
|
12
|
+
if (!isRecord || !firstOk || !lastOk) {
|
|
13
|
+
throw new PdfToMdError('PAGE_RANGE', `pages option must be { first: number, last?: number } with first >= 1 and last >= first (got ${JSON.stringify(pages)})`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Validates analyze()'s non-string `input` branch (`{ words }`) before
|
|
17
|
+
* `.words` is read, so a plain-JS mistake like `analyze(null)` throws PdfToMdError/WORDS_INPUT instead of a raw TypeError. */ export function validateWordsInput(input) {
|
|
18
|
+
const isRecord = typeof input === 'object' && input !== null;
|
|
19
|
+
const words = isRecord ? input.words : undefined;
|
|
20
|
+
if (!Array.isArray(words)) {
|
|
21
|
+
throw new PdfToMdError('WORDS_INPUT', `analyze() input must be a pdf path (string) or { words: OcrWordInput[] } (got ${isRecord ? JSON.stringify(input) : String(input)})`);
|
|
22
|
+
}
|
|
23
|
+
return words;
|
|
24
|
+
}
|
|
25
|
+
/** Runtime shape guard for `buildOcrAnalysisPages`'s input: a plain-JS
|
|
26
|
+
* caller bypasses the TS union, so this turns a raw TypeError into PdfToMdError before any property is read off it. */ export function validateOcrAnalysisInput(input) {
|
|
27
|
+
if (typeof input === 'string') {
|
|
28
|
+
throw new PdfToMdError('PDF_OPEN', `extractOcr/analyze input must be { pdfPath: string } or { words: OcrWordInput[] }, not a bare path string (got ${JSON.stringify(input)}) — did you mean { pdfPath: ${JSON.stringify(input)} }?`);
|
|
29
|
+
}
|
|
30
|
+
const isRecord = typeof input === 'object' && input !== null;
|
|
31
|
+
const rawPdfPath = isRecord ? input.pdfPath : undefined;
|
|
32
|
+
const rawWords = isRecord ? input.words : undefined;
|
|
33
|
+
if (!isRecord || rawPdfPath === undefined && rawWords === undefined) {
|
|
34
|
+
throw new PdfToMdError('WORDS_INPUT', `extractOcr/analyze input must be { pdfPath: string } or { words: OcrWordInput[] } (got ${isRecord ? JSON.stringify(input) : String(input)})`);
|
|
35
|
+
}
|
|
36
|
+
if (rawWords !== undefined && !Array.isArray(rawWords)) {
|
|
37
|
+
throw new PdfToMdError('WORDS_INPUT', `extractOcr/analyze { words } input must be an array (got ${JSON.stringify(rawWords)})`);
|
|
38
|
+
}
|
|
39
|
+
if (rawPdfPath !== undefined && typeof rawPdfPath !== 'string') {
|
|
40
|
+
throw new PdfToMdError('PDF_OPEN', `extractOcr/analyze { pdfPath } input must be a string (got ${JSON.stringify(rawPdfPath)})`);
|
|
41
|
+
}
|
|
42
|
+
if (rawWords !== undefined) {
|
|
43
|
+
for (const w of rawWords){
|
|
44
|
+
const rec = w;
|
|
45
|
+
if (typeof rec.page !== 'number' || typeof rec.text !== 'string' || typeof rec.x !== 'number' || typeof rec.y !== 'number' || typeof rec.w !== 'number' || typeof rec.h !== 'number') {
|
|
46
|
+
throw new PdfToMdError('WORDS_INPUT', `malformed word entry (need {page, text, x, y, w, h}): ${JSON.stringify(w)}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
words: rawWords
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
pdfPath: rawPdfPath
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/validate.ts"],"sourcesContent":["// validate.ts — input-shape validation shared by analyze.ts and extract.ts,\n// kept out of both to dodge their value-level import cycle (extract.ts imports analyze.ts's buildOcrAnalysisPages; both need this).\nimport { PdfToMdError } from './errors.ts';\nimport type { OcrWordInput } from './extract.ts';\n\n/** Validates the one option every entry point accepts before touching the\n * PDF. Type-only import from `extract.ts` keeps this cycle-free. */\nexport function validatePages(pages: unknown): void {\n if (pages === undefined) return;\n const isRecord = typeof pages === 'object' && pages !== null;\n const first = isRecord ? (pages as Record<string, unknown>).first : undefined;\n const last = isRecord ? (pages as Record<string, unknown>).last : undefined;\n const firstOk = typeof first === 'number' && Number.isFinite(first) && first >= 1;\n const lastOk = last === undefined || (typeof last === 'number' && Number.isFinite(last) && last >= (first as number));\n if (!isRecord || !firstOk || !lastOk) {\n throw new PdfToMdError('PAGE_RANGE', `pages option must be { first: number, last?: number } with first >= 1 and last >= first (got ${JSON.stringify(pages)})`);\n }\n}\n\n/** Validates analyze()'s non-string `input` branch (`{ words }`) before\n * `.words` is read, so a plain-JS mistake like `analyze(null)` throws PdfToMdError/WORDS_INPUT instead of a raw TypeError. */\nexport function validateWordsInput(input: unknown): OcrWordInput[] {\n const isRecord = typeof input === 'object' && input !== null;\n const words = isRecord ? (input as Record<string, unknown>).words : undefined;\n if (!Array.isArray(words)) {\n throw new PdfToMdError('WORDS_INPUT', `analyze() input must be a pdf path (string) or { words: OcrWordInput[] } (got ${isRecord ? JSON.stringify(input) : String(input)})`);\n }\n return words as OcrWordInput[];\n}\n\n/** Runtime shape guard for `buildOcrAnalysisPages`'s input: a plain-JS\n * caller bypasses the TS union, so this turns a raw TypeError into PdfToMdError before any property is read off it. */\nexport function validateOcrAnalysisInput(input: unknown): { pdfPath: string } | { words: OcrWordInput[] } {\n if (typeof input === 'string') {\n throw new PdfToMdError('PDF_OPEN', `extractOcr/analyze input must be { pdfPath: string } or { words: OcrWordInput[] }, not a bare path string (got ${JSON.stringify(input)}) — did you mean { pdfPath: ${JSON.stringify(input)} }?`);\n }\n const isRecord = typeof input === 'object' && input !== null;\n const rawPdfPath = isRecord ? (input as Record<string, unknown>).pdfPath : undefined;\n const rawWords = isRecord ? (input as Record<string, unknown>).words : undefined;\n if (!isRecord || (rawPdfPath === undefined && rawWords === undefined)) {\n throw new PdfToMdError('WORDS_INPUT', `extractOcr/analyze input must be { pdfPath: string } or { words: OcrWordInput[] } (got ${isRecord ? JSON.stringify(input) : String(input)})`);\n }\n if (rawWords !== undefined && !Array.isArray(rawWords)) {\n throw new PdfToMdError('WORDS_INPUT', `extractOcr/analyze { words } input must be an array (got ${JSON.stringify(rawWords)})`);\n }\n if (rawPdfPath !== undefined && typeof rawPdfPath !== 'string') {\n throw new PdfToMdError('PDF_OPEN', `extractOcr/analyze { pdfPath } input must be a string (got ${JSON.stringify(rawPdfPath)})`);\n }\n if (rawWords !== undefined) {\n for (const w of rawWords as unknown[]) {\n const rec = w as Record<string, unknown>;\n if (typeof rec.page !== 'number' || typeof rec.text !== 'string' || typeof rec.x !== 'number' || typeof rec.y !== 'number' || typeof rec.w !== 'number' || typeof rec.h !== 'number') {\n throw new PdfToMdError('WORDS_INPUT', `malformed word entry (need {page, text, x, y, w, h}): ${JSON.stringify(w)}`);\n }\n }\n return { words: rawWords as OcrWordInput[] };\n }\n return { pdfPath: rawPdfPath as string };\n}\n"],"names":["PdfToMdError","validatePages","pages","undefined","isRecord","first","last","firstOk","Number","isFinite","lastOk","JSON","stringify","validateWordsInput","input","words","Array","isArray","String","validateOcrAnalysisInput","rawPdfPath","pdfPath","rawWords","w","rec","page","text","x","y","h"],"mappings":"AAAA,4EAA4E;AAC5E,oIAAoI;AACpI,SAASA,YAAY,QAAQ,cAAc;AAG3C;kEACkE,GAClE,OAAO,SAASC,cAAcC,KAAc;IAC1C,IAAIA,UAAUC,WAAW;IACzB,MAAMC,WAAW,OAAOF,UAAU,YAAYA,UAAU;IACxD,MAAMG,QAAQD,WAAW,AAACF,MAAkCG,KAAK,GAAGF;IACpE,MAAMG,OAAOF,WAAW,AAACF,MAAkCI,IAAI,GAAGH;IAClE,MAAMI,UAAU,OAAOF,UAAU,YAAYG,OAAOC,QAAQ,CAACJ,UAAUA,SAAS;IAChF,MAAMK,SAASJ,SAASH,aAAc,OAAOG,SAAS,YAAYE,OAAOC,QAAQ,CAACH,SAASA,QAASD;IACpG,IAAI,CAACD,YAAY,CAACG,WAAW,CAACG,QAAQ;QACpC,MAAM,IAAIV,aAAa,cAAc,CAAC,6FAA6F,EAAEW,KAAKC,SAAS,CAACV,OAAO,CAAC,CAAC;IAC/J;AACF;AAEA;4HAC4H,GAC5H,OAAO,SAASW,mBAAmBC,KAAc;IAC/C,MAAMV,WAAW,OAAOU,UAAU,YAAYA,UAAU;IACxD,MAAMC,QAAQX,WAAW,AAACU,MAAkCC,KAAK,GAAGZ;IACpE,IAAI,CAACa,MAAMC,OAAO,CAACF,QAAQ;QACzB,M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OC,IAAIK,CAAC,KAAK,UAAU;gBACpL,MAAM,IAAI7B,aAAa,eAAe,CAAC,sDAAsD,EAAEW,KAAKC,SAAS,CAACW,IAAI;YACpH;QACF;QACA,OAAO;YAAER,OAAOO;QAA2B;IAC7C;IACA,OAAO;QAAED,SAASD;IAAqB;AACzC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effortlessmotion/pdf-to-md",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Extract PDFs to structured markdown: text layer via pdf.js, OCR fallback for image-only pages, pure npm and cross-platform",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pdf",
|
|
7
|
+
"markdown",
|
|
8
|
+
"ocr",
|
|
9
|
+
"tesseract",
|
|
10
|
+
"extract",
|
|
11
|
+
"convert"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/kmalakoff/pdf-to-md",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+ssh://git@github.com/kmalakoff/pdf-to-md.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Kevin Malakoff <kmalakoff@gmail.com> (https://github.com/kmalakoff)",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/esm/index.js",
|
|
24
|
+
"require": "./dist/cjs/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"main": "dist/cjs/index.js",
|
|
29
|
+
"source": "src/index.ts",
|
|
30
|
+
"types": "dist/cjs/index.d.ts",
|
|
31
|
+
"bin": {
|
|
32
|
+
"pdf-to-md": "bin/cli.js"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"skills"
|
|
37
|
+
],
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsds build",
|
|
40
|
+
"format": "tsds format",
|
|
41
|
+
"prepublishOnly": "tsds validate",
|
|
42
|
+
"test": "tsds test:node --no-timeouts",
|
|
43
|
+
"test:engines": "nvu engines tsds test:node --no-timeouts",
|
|
44
|
+
"version": "tsds version"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@napi-rs/canvas": "^1.0.6",
|
|
48
|
+
"@tesseract.js-data/eng": "^1.0.0",
|
|
49
|
+
"pdfjs-dist": "^6.2.108",
|
|
50
|
+
"tesseract.js": "^7.0.0",
|
|
51
|
+
"word-list": "^4.1.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/mocha": "^10.0.10",
|
|
55
|
+
"@types/node": "^26.2.0",
|
|
56
|
+
"node-version-use": "^2.5.0",
|
|
57
|
+
"pdf-lib": "^1.17.1",
|
|
58
|
+
"ts-dev-stack": "^1.22.3",
|
|
59
|
+
"tsds-config": "^1.0.5"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=22.13.0"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
}
|
|
67
|
+
}
|