@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 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/markdown.ts"],"sourcesContent":["// Pass 3: lines -> markdown. Structure is measured, not guessed — heading\n// levels come from the font-height histogram, hyphen-closes only against a dictionary (see the inline comments below for each rule).\nimport { getDict } from './dict.ts';\nimport { pageMarkerLine } from './page-marker.ts';\nimport type { BuildMarkdownOptions, Line, MarkdownResult, MarkdownStats, PageLines } from './types.ts';\n\nconst GLYPH = /^[•◦▪▫●○·‣⁃]$/;\nconst BULLETS = /^(?:[•◦▪▫●○·‣⁃]\\s*)+/;\n\nexport function buildMarkdown(pageLines: PageLines[], heightChars: Map<number, number>, opts: BuildMarkdownOptions = {}): MarkdownResult {\n // body height = char-weighted mode; headings are the rarer, larger sizes.\n // The 1.35 multiplier keeps emphasized body text out of the outline.\n const bodyH = [...heightChars.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? 0;\n const headingSizes = [...heightChars.keys()].filter((h) => h > bodyH * 1.35).sort((a, b) => b - a);\n // with page markers on, content headings start below the page ### level;\n // sizes beyond the ###### budget share level 6 rather than being dropped\n const offset = opts.pageMarkers ? 3 : 0;\n const levelOf = new Map(headingSizes.slice(0, 6).map((h, i) => [h, Math.min(6, i + 1 + offset)]));\n\n const out: string[] = [];\n const stats: MarkdownStats = {\n headings: 0,\n listItems: 0,\n paragraphs: 0,\n joinedPairs: 0,\n reflowed: 0,\n callouts: 0,\n closedHyphens: 0,\n };\n let para: string[] = [];\n\n // a label line ending in ':' whose next line is a bare number belongs on one line\n function attachValue(label: string, lines: Line[], get: () => number, set: (n: number) => void): string {\n const j = get() + 1;\n if (/[::]\\s*$/.test(label) && j < lines.length && /^[0-9][\\d.,]*%?$/.test(lines[j].text)) {\n set(j);\n stats.joinedPairs++;\n return `${label} ${lines[j].text}`;\n }\n return label;\n }\n const flush = () => {\n if (!para.length) return;\n out.push(para.join(' ').replace(/\\s+([,.;:%])/g, '$1'));\n out.push('');\n stats.paragraphs++;\n para = [];\n };\n // append a wrapped line to the open paragraph, resolving a line-final hyphen\n const appendToPara = (text: string) => {\n const prev = para.at(-1);\n if (prev && /\\w-$/.test(prev)) {\n // a bare \" -\" is punctuation, not a wrapped word\n const head = prev.replace(/-$/, '').split(/\\s/).pop() as string;\n const tail = (text.split(/\\s/)[0] ?? '').replace(/[^A-Za-z-]/g, '').split('-')[0];\n const close = getDict().has((head + tail).toLowerCase());\n if (close) stats.closedHyphens++;\n para[para.length - 1] = prev.replace(/-$/, close ? '' : '-') + text;\n } else para.push(text);\n };\n\n for (const { page, lines } of pageLines) {\n if (opts.pageMarkers) {\n flush();\n out.push(pageMarkerLine(page));\n out.push('');\n }\n if (!lines.length) continue;\n const indentBase = Math.min(...lines.map((l) => l.x));\n\n for (let i = 0; i < lines.length; i++) {\n let { text, h, x } = lines[i];\n const prevLine = i > 0 ? lines[i - 1] : null;\n // a column change or a vertical jump wider than a blank line ends the paragraph\n if (para.length && prevLine && (lines[i].col !== prevLine.col || lines[i].y - prevLine.y > h * 2.2)) flush();\n\n // A large-font line opens a run: gather the consecutive same-size lines\n // below it (a title wraps across lines). A short merged run is one heading; a long one is a callout/pull-quote in a bigger face — prose, not structure — and becomes a blockquote.\n if (levelOf.has(h) && !BULLETS.test(text)) {\n const run = [text];\n while (i + 1 < lines.length && lines[i + 1].h === h && lines[i + 1].col === lines[i].col && lines[i + 1].y - lines[i].y < h * 2.2 && !BULLETS.test(lines[i + 1].text)) run.push(lines[++i].text);\n // join wrapped title lines, resolving line-final hyphens like body text\n const merged = run.reduce((acc, part) => (/\\w-$/.test(acc) ? acc.replace(/-$/, getDict().has(((acc.replace(/-$/, '').split(/\\s/).pop() as string) + (part.split(/\\s/)[0] ?? '').replace(/[^A-Za-z-]/g, '').split('-')[0]).toLowerCase()) ? '' : '-') + part : `${acc} ${part}`)).replace(/\\s+([,.;:%])/g, '$1');\n flush();\n if (merged.length > 150) {\n out.push(`> ${merged}`);\n out.push('');\n stats.callouts++;\n } else {\n out.push(`${'#'.repeat(levelOf.get(h) as number)} ${merged}`);\n out.push('');\n stats.headings++;\n }\n continue;\n }\n\n // Lines can carry several stacked glyphs (\"• ▪\") from nested source lists.\n const stripped = text.replace(BULLETS, '').trim();\n const wasBullet = stripped !== text;\n if (wasBullet) text = stripped;\n\n // bullet with nothing left on the line: attach the following line's text\n if ((!text || GLYPH.test(text)) && i + 1 < lines.length) {\n flush();\n const depth = Math.max(0, Math.round((x - indentBase) / 18));\n let label = lines[++i].text.replace(BULLETS, '').trim();\n while (!label && i + 1 < lines.length)\n // stacked glyphs\n label = lines[++i].text.replace(BULLETS, '').trim();\n label = attachValue(\n label,\n lines,\n () => i,\n (n) => {\n i = n;\n }\n );\n out.push(`${' '.repeat(depth)}- ${label}`);\n stats.listItems++;\n continue;\n }\n // glyph(s) followed by text on the same line\n if (wasBullet) {\n flush();\n const depth = Math.max(0, Math.round((x - indentBase) / 18));\n out.push(\n `${' '.repeat(depth)}- ${attachValue(\n text,\n lines,\n () => i,\n (n) => {\n i = n;\n }\n )}`\n );\n stats.listItems++;\n continue;\n }\n // `label:` on its own line followed by a bare value -> keep together\n if (/[::]\\s*$/.test(text) && i + 1 < lines.length && /^[0-9][\\d.,]*%?$/.test(lines[i + 1].text)) {\n flush();\n out.push(`- ${text} ${lines[++i].text}`);\n stats.joinedPairs++;\n continue;\n }\n // otherwise body text: re-flow wrapped lines into a paragraph\n appendToPara(text);\n if (/[.!?:]$/.test(text)) flush();\n else stats.reflowed++;\n }\n flush();\n }\n flush();\n\n const md = `${out\n .join('\\n')\n .replace(/\\n{3,}/g, '\\n\\n')\n .trim()}\\n`;\n return { md, stats, bodyH, headingSizes };\n}\n"],"names":["getDict","pageMarkerLine","GLYPH","BULLETS","buildMarkdown","pageLines","heightChars","opts","bodyH","entries","sort","a","b","headingSizes","keys","filter","h","offset","pageMarkers","levelOf","Map","slice","map","i","Math","min","out","stats","headings","listItems","paragraphs","joinedPairs","reflowed","callouts","closedHyphens","para","attachValue","label","lines","get","set","j","test","length","text","flush","push","join","replace","appendToPara","prev","at","head","split","pop","tail","close","has","toLowerCase","page","indentBase","l","x","prevLine","col","y","run","merged","reduce","acc","part","repeat","stripped","trim","wasBullet","depth","max","round","n","md"],"mappings":"AAAA,0EAA0E;AAC1E,qIAAqI;AACrI,SAASA,OAAO,QAAQ,YAAY;AACpC,SAASC,cAAc,QAAQ,mBAAmB;AAGlD,MAAMC,QAAQ;AACd,MAAMC,UAAU;AAEhB,OAAO,SAASC,cAAcC,SAAsB,EAAEC,WAAgC,EAAEC,OAA6B,CAAC,CAAC;;QAGvG;IAFd,0EAA0E;IAC1E,qEAAqE;IACrE,MAAMC,iBAAQ,SAAA;WAAIF,YAAYG,OAAO;KAAG,CAACC,IAAI,CAAC,CAACC,GAAGC,IAAMA,CAAC,CAAC,EAAE,GAAGD,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,cAAzD,6BAAA,MAA2D,CAAC,EAAE,uCAAI;IAChF,MAAME,eAAe;WAAIP,YAAYQ,IAAI;KAAG,CAACC,MAAM,CAAC,CAACC,IAAMA,IAAIR,QAAQ,MAAME,IAAI,CAAC,CAACC,GAAGC,IAAMA,IAAID;IAChG,yEAAyE;IACzE,yEAAyE;IACzE,MAAMM,SAASV,KAAKW,WAAW,GAAG,IAAI;IACtC,MAAMC,UAAU,IAAIC,IAAIP,aAAaQ,KAAK,CAAC,GAAG,GAAGC,GAAG,CAAC,CAACN,GAAGO,IAAM;YAACP;YAAGQ,KAAKC,GAAG,CAAC,GAAGF,IAAI,IAAIN;SAAQ;IAE/F,MAAMS,MAAgB,EAAE;IACxB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EAAE;gBACvCjB,MAAMI,WAAW;gBACjB;YACF;YACA,8DAA8D;YAC9DkB,aAAaL;YACb,IAAI,UAAUF,IAAI,CAACE,OAAOC;iBACrBlB,MAAMK,QAAQ;QACrB;QACAa;IACF;IACAA;IAEA,MAAMkC,KAAK,GAAGrD,IACXqB,IAAI,CAAC,MACLC,OAAO,CAAC,WAAW,QACnByB,IAAI,GAAG,EAAE,CAAC;IACb,OAAO;QAAEM;QAAIpD;QAAOnB;QAAOK;IAAa;AAC1C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The marker heading for page N, with no surrounding whitespace/newlines
|
|
2
|
+
* (callers own their own blank-line formatting). */
|
|
3
|
+
export declare function pageMarkerLine(page: number): string;
|
|
4
|
+
/** Is this line (already split, no trailing newline) a page-marker heading? */
|
|
5
|
+
export declare function isPageMarkerLine(line: string): boolean;
|
|
6
|
+
/** Split markdown into per-marker body slices, in document order — one entry
|
|
7
|
+
* PER OCCURRENCE (a duplicate "### pN" yields two entries; no slice is ever dropped). */
|
|
8
|
+
export declare function splitByPageMarker(md: string): {
|
|
9
|
+
page: number;
|
|
10
|
+
body: string;
|
|
11
|
+
}[];
|
|
12
|
+
/** Per-page body text, duplicate-marker slices CONCATENATED — a word anywhere
|
|
13
|
+
* on page N counts as on page N (the auditor's membership semantics). */
|
|
14
|
+
export declare function bodyByPage(md: string): Map<number, string>;
|
|
15
|
+
/** Count of "### pN" marker lines in `md`. */
|
|
16
|
+
export declare function countPageMarkers(md: string): number;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// page-marker.ts — the single "### pN" convention: every producer/parser of
|
|
2
|
+
// the page-marker heading imports from here so the regex can't drift (report.ts once had a `\s+` variant).
|
|
3
|
+
/** The marker heading for page N, with no surrounding whitespace/newlines
|
|
4
|
+
* (callers own their own blank-line formatting). */ export function pageMarkerLine(page) {
|
|
5
|
+
return `### p${page}`;
|
|
6
|
+
}
|
|
7
|
+
// Single space, matching pageMarkerLine's own output exactly.
|
|
8
|
+
const MARKER_RE = /^### p(\d+)$/;
|
|
9
|
+
/** Is this line (already split, no trailing newline) a page-marker heading? */ export function isPageMarkerLine(line) {
|
|
10
|
+
return MARKER_RE.test(line);
|
|
11
|
+
}
|
|
12
|
+
/** Split markdown into per-marker body slices, in document order — one entry
|
|
13
|
+
* PER OCCURRENCE (a duplicate "### pN" yields two entries; no slice is ever dropped). */ export function splitByPageMarker(md) {
|
|
14
|
+
const re = /^### p(\d+)$/gm;
|
|
15
|
+
const markers = [];
|
|
16
|
+
for(let m = re.exec(md); m !== null; m = re.exec(md))markers.push({
|
|
17
|
+
page: Number(m[1]),
|
|
18
|
+
index: m.index,
|
|
19
|
+
end: m.index + m[0].length
|
|
20
|
+
});
|
|
21
|
+
const out = [];
|
|
22
|
+
for(let i = 0; i < markers.length; i++){
|
|
23
|
+
const end = i + 1 < markers.length ? markers[i + 1].index : md.length;
|
|
24
|
+
out.push({
|
|
25
|
+
page: markers[i].page,
|
|
26
|
+
body: md.slice(markers[i].end, end)
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
/** Per-page body text, duplicate-marker slices CONCATENATED — a word anywhere
|
|
32
|
+
* on page N counts as on page N (the auditor's membership semantics). */ export function bodyByPage(md) {
|
|
33
|
+
var _out_get;
|
|
34
|
+
const out = new Map();
|
|
35
|
+
for (const { page, body } of splitByPageMarker(md))out.set(page, ((_out_get = out.get(page)) !== null && _out_get !== void 0 ? _out_get : '') + body);
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
/** Count of "### pN" marker lines in `md`. */ export function countPageMarkers(md) {
|
|
39
|
+
return (md.match(/^### p\d+$/gm) || []).length;
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/page-marker.ts"],"sourcesContent":["// page-marker.ts — the single \"### pN\" convention: every producer/parser of\n// the page-marker heading imports from here so the regex can't drift (report.ts once had a `\\s+` variant).\n\n/** The marker heading for page N, with no surrounding whitespace/newlines\n * (callers own their own blank-line formatting). */\nexport function pageMarkerLine(page: number): string {\n return `### p${page}`;\n}\n\n// Single space, matching pageMarkerLine's own output exactly.\nconst MARKER_RE = /^### p(\\d+)$/;\n\n/** Is this line (already split, no trailing newline) a page-marker heading? */\nexport function isPageMarkerLine(line: string): boolean {\n return MARKER_RE.test(line);\n}\n\n/** Split markdown into per-marker body slices, in document order — one entry\n * PER OCCURRENCE (a duplicate \"### pN\" yields two entries; no slice is ever dropped). */\nexport function splitByPageMarker(md: string): { page: number; body: string }[] {\n const re = /^### p(\\d+)$/gm;\n const markers: { page: number; index: number; end: number }[] = [];\n for (let m = re.exec(md); m !== null; m = re.exec(md)) markers.push({ page: Number(m[1]), index: m.index, end: m.index + m[0].length });\n const out: { page: number; body: string }[] = [];\n for (let i = 0; i < markers.length; i++) {\n const end = i + 1 < markers.length ? markers[i + 1].index : md.length;\n out.push({ page: markers[i].page, body: md.slice(markers[i].end, end) });\n }\n return out;\n}\n\n/** Per-page body text, duplicate-marker slices CONCATENATED — a word anywhere\n * on page N counts as on page N (the auditor's membership semantics). */\nexport function bodyByPage(md: string): Map<number, string> {\n const out = new Map<number, string>();\n for (const { page, body } of splitByPageMarker(md)) out.set(page, (out.get(page) ?? '') + body);\n return out;\n}\n\n/** Count of \"### pN\" marker lines in `md`. */\nexport function countPageMarkers(md: string): number {\n return (md.match(/^### p\\d+$/gm) || []).length;\n}\n"],"names":["pageMarkerLine","page","MARKER_RE","isPageMarkerLine","line","test","splitByPageMarker","md","re","markers","m","exec","push","Number","index","end","length","out","i","body","slice","bodyByPage","Map","set","get","countPageMarkers","match"],"mappings":"AAAA,4EAA4E;AAC5E,2GAA2G;AAE3G;kDACkD,GAClD,OAAO,SAASA,eAAeC,IAAY;IACzC,OAAO,CAAC,KAAK,EAAEA,MAAM;AACvB;AA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E,AAAD,EAAGV,MAAM;AAChD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';
|
|
2
|
+
/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction
|
|
3
|
+
* only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */
|
|
4
|
+
export declare function openDocument(src: string): Promise<PDFDocumentProxy>;
|
|
5
|
+
/**
|
|
6
|
+
* Open a PDF for rendering (raster.ts/tesseract.ts's seam): cMaps + standard
|
|
7
|
+
* fonts wired up, url-based. Callers must `await` the returned task's `.promise` (via `awaitPdfOpen`) and call `.destroy()` when done.
|
|
8
|
+
*/
|
|
9
|
+
export declare function openPdfForRender(pdfPath: string): PDFDocumentLoadingTask;
|
|
10
|
+
/** Shared error wrapping for `openPdfForRender`'s task: a raw pdfjs open
|
|
11
|
+
* failure becomes PdfToMdError/PDF_OPEN, original message preserved. */
|
|
12
|
+
export declare function awaitPdfOpen(loadingTask: PDFDocumentLoadingTask, pdfPath: string): Promise<PDFDocumentProxy>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two
|
|
2
|
+
// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).
|
|
3
|
+
// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.
|
|
4
|
+
import { readFileSync } from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { getDocument, VerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';
|
|
7
|
+
import { PdfToMdError } from './errors.js';
|
|
8
|
+
import { resolvePackagePath } from './resolve.js';
|
|
9
|
+
// `verbosity: VerbosityLevel.ERRORS` silences pdfjs's own console.warn calls
|
|
10
|
+
// — the library's documented promise is it never writes to stdout/stderr on its own.
|
|
11
|
+
/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction
|
|
12
|
+
* only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */ export async function openDocument(src) {
|
|
13
|
+
let data;
|
|
14
|
+
try {
|
|
15
|
+
data = new Uint8Array(readFileSync(src));
|
|
16
|
+
} catch (err) {
|
|
17
|
+
throw new PdfToMdError('PDF_OPEN', `cannot read ${src}: ${err.message}`);
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
return await getDocument({
|
|
21
|
+
data,
|
|
22
|
+
useSystemFonts: true,
|
|
23
|
+
verbosity: VerbosityLevel.ERRORS
|
|
24
|
+
}).promise;
|
|
25
|
+
} catch (err) {
|
|
26
|
+
throw new PdfToMdError('PDF_OPEN', `cannot open ${src} as a PDF: ${err.message}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded
|
|
30
|
+
// standard fonts resolve instead of falling back to substitute glyphs.
|
|
31
|
+
//
|
|
32
|
+
// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`
|
|
33
|
+
// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.
|
|
34
|
+
const PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));
|
|
35
|
+
const CMAP_URL = path.join(PDFJS_DIST_ROOT, 'cmaps') + path.sep;
|
|
36
|
+
const STANDARD_FONT_DATA_URL = path.join(PDFJS_DIST_ROOT, 'standard_fonts') + path.sep;
|
|
37
|
+
// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal
|
|
38
|
+
// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.
|
|
39
|
+
const WASM_URL = path.join(PDFJS_DIST_ROOT, 'wasm') + path.sep;
|
|
40
|
+
/**
|
|
41
|
+
* Open a PDF for rendering (raster.ts/tesseract.ts's seam): cMaps + standard
|
|
42
|
+
* fonts wired up, url-based. Callers must `await` the returned task's `.promise` (via `awaitPdfOpen`) and call `.destroy()` when done.
|
|
43
|
+
*/ export function openPdfForRender(pdfPath) {
|
|
44
|
+
// No `worker` option: this pdfjs-dist Node build needs no real Worker thread wired up to run.
|
|
45
|
+
return getDocument({
|
|
46
|
+
url: pdfPath,
|
|
47
|
+
cMapUrl: CMAP_URL,
|
|
48
|
+
cMapPacked: true,
|
|
49
|
+
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
|
50
|
+
wasmUrl: WASM_URL,
|
|
51
|
+
verbosity: VerbosityLevel.ERRORS
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/** Shared error wrapping for `openPdfForRender`'s task: a raw pdfjs open
|
|
55
|
+
* failure becomes PdfToMdError/PDF_OPEN, original message preserved. */ export async function awaitPdfOpen(loadingTask, pdfPath) {
|
|
56
|
+
try {
|
|
57
|
+
return await loadingTask.promise;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
throw new PdfToMdError('PDF_OPEN', `could not open PDF: ${pdfPath} (${err.message})`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/pdf-open.ts"],"sourcesContent":["// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two\n// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).\n// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { getDocument, VerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { resolvePackagePath } from './resolve.ts';\n\n// `verbosity: VerbosityLevel.ERRORS` silences pdfjs's own console.warn calls\n// — the library's documented promise is it never writes to stdout/stderr on its own.\n\n/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction\n * only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */\nexport async function openDocument(src: string): Promise<PDFDocumentProxy> {\n let data: Uint8Array;\n try {\n data = new Uint8Array(readFileSync(src));\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot read ${src}: ${(err as Error).message}`);\n }\n try {\n return await getDocument({\n data,\n useSystemFonts: true,\n verbosity: VerbosityLevel.ERRORS,\n }).promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot open ${src} as a PDF: ${(err as Error).message}`);\n }\n}\n\n// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded\n// standard fonts resolve instead of falling back to substitute glyphs.\n//\n// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`\n// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.\nconst PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));\nconst CMAP_URL = path.join(PDFJS_DIST_ROOT, 'cmaps') + path.sep;\nconst STANDARD_FONT_DATA_URL = path.join(PDFJS_DIST_ROOT, 'standard_fonts') + path.sep;\n// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal\n// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.\nconst WASM_URL = path.join(PDFJS_DIST_ROOT, 'wasm') + path.sep;\n\n/**\n * Open a PDF for rendering (raster.ts/tesseract.ts's seam): cMaps + standard\n * fonts wired up, url-based. Callers must `await` the returned task's `.promise` (via `awaitPdfOpen`) and call `.destroy()` when done.\n */\nexport function openPdfForRender(pdfPath: string): PDFDocumentLoadingTask {\n // No `worker` option: this pdfjs-dist Node build needs no real Worker thread wired up to run.\n return getDocument({\n url: pdfPath,\n cMapUrl: CMAP_URL,\n cMapPacked: true,\n standardFontDataUrl: STANDARD_FONT_DATA_URL,\n wasmUrl: WASM_URL,\n verbosity: VerbosityLevel.ERRORS,\n });\n}\n\n/** Shared error wrapping for `openPdfForRender`'s task: a raw pdfjs open\n * failure becomes PdfToMdError/PDF_OPEN, original message preserved. */\nexport async function awaitPdfOpen(loadingTask: PDFDocumentLoadingTask, pdfPath: string): Promise<PDFDocumentProxy> {\n try {\n return await loadingTask.promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `could not open PDF: ${pdfPath} (${(err as Error).message})`);\n }\n}\n"],"names":["readFileSync","path","getDocument","VerbosityLevel","PdfToMdError","resolvePackagePath","openDocument","src","data","Uint8Array","err","message","useSystemFonts","verbosity","ERRORS","promise","PDFJS_DIST_ROOT","dirname","CMAP_URL","join","sep","STANDARD_FONT_DATA_URL","WASM_URL","openPdfForRender","pdfPath","url","cMapUrl","cMapPacked","standardFontDataUrl","wasmUrl","awaitPdfOpen","loadingTask"],"mappings":"AAAA,yEAAyE;AACzE,sIAAsI;AACtI,uFAAuF;AACvF,SAASA,YAAY,QAAQ,UAAU;AACvC,OAAOC,UAAU,YAAY;AAE7B,SAASC,WAAW,EAAEC,cAAc,QAAQ,kCAAkC;AAC9E,SAASC,YAAY,QAAQ,cAAc;AAC3C,SAASC,kBAAkB,QAAQ,eAAe;AAElD,6EAA6E;AAC7E,qFAAqF;AAErF;gHACgH,GAChH,OAAO,eAAeC,aAAaC,GAAW;IAC5C,IAAIC;IACJ,IAAI;QACFA,OAAO,IAAIC,WAAWT,aAAaO;IACrC,EAAE,OAAOG,KAAK;QACZ,MAAM,IAAIN,aAAa,YAAY,CAAC,YAAY,EAAEG,IAAI,EAAE,EAAE,AAACG,IAAcC,OAAO,EAAE;IACpF;IACA,IAAI;QACF,OAAO,MAAMT,YAAY;YACvBM;YACAI,gBAAgB;YAChBC,WAAWV,eAAeW,MAAM;QAClC,GAAGC,OAAO;IACZ,EAAE,OAAOL,KAAK;QACZ,MAAM,IAAIN,aAAa,YAAY,CAAC,YAAY,EAAEG,IAAI,WAAW,EAAE,AAACG,IAAcC,OAAO,EAAE;IAC7F;AACF;AAEA,wEAAwE;AACxE,uEAAuE;AACvE,EAAE;AACF,4EAA4E;AAC5E,qIAAqI;AACrI,MAAMK,kBAAkBf,KAAKgB,OAAO,CAACZ,mBAAmB;AACxD,MAAMa,WAAWjB,KAAKkB,IAAI,CAACH,iBAAiB,WAAWf,KAAKmB,GAAG;AAC/D,MAAMC,yBAAyBpB,KAAKkB,IAAI,CAACH,iBAAiB,oBAAoBf,KAAKmB,GAAG;AACtF,+EAA+E;AAC/E,8FAA8F;AAC9F,MAAME,WAAWrB,KAAKkB,IAAI,CAACH,iBAAiB,UAAUf,KAAKmB,GAAG;AAE9D;;;CAGC,GACD,OAAO,SAASG,iBAAiBC,OAAe;IAC9C,8FAA8F;IAC9F,OAAOtB,YAAY;QACjBuB,KAAKD;QACLE,SAASR;QACTS,YAAY;QACZC,qBAAqBP;QACrBQ,SAASP;QACTT,WAAWV,eAAeW,MAAM;IAClC;AACF;AAEA;sEACsE,GACtE,OAAO,eAAegB,aAAaC,WAAmC,EAAEP,OAAe;IACrF,IAAI;QACF,OAAO,MAAMO,YAAYhB,OAAO;IAClC,EAAE,OAAOL,KAAK;QACZ,MAAM,IAAIN,aAAa,YAAY,CAAC,oBAAoB,EAAEoB,QAAQ,EAAE,EAAE,AAACd,IAAcC,OAAO,CAAC,CAAC,CAAC;IACjG;AACF"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';
|
|
2
|
+
export { openPdfForRender } from './pdf-open.js';
|
|
3
|
+
/** One rendered page, from renderPageToPNG (or the `pdf-to-md render` CLI
|
|
4
|
+
* command it backs). */
|
|
5
|
+
export interface RenderPageResult {
|
|
6
|
+
/** the rendered page, PNG-encoded */
|
|
7
|
+
png: Buffer;
|
|
8
|
+
/** pixel width at the requested dpi */
|
|
9
|
+
width: number;
|
|
10
|
+
/** pixel height at the requested dpi */
|
|
11
|
+
height: number;
|
|
12
|
+
/** the page's own /Rotate entry, baked into `png`/`width`/`height` already
|
|
13
|
+
* — informational, not something a caller needs to apply again */
|
|
14
|
+
rotation: number;
|
|
15
|
+
}
|
|
16
|
+
/** Render one already-open document's page to a PNG buffer at the given DPI. */
|
|
17
|
+
export declare function renderLoadedPage(doc: PDFDocumentProxy, pageNum: number, dpi: number): Promise<RenderPageResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Render one page of a PDF to a PNG buffer at the given DPI. Opens and
|
|
20
|
+
* closes the document itself — for more than one page from the same PDF, use `openPdfForRender` + `renderLoadedPage` instead.
|
|
21
|
+
*
|
|
22
|
+
* @param pdfPath path to the PDF file
|
|
23
|
+
* @param pageNum 1-based page number
|
|
24
|
+
* @param dpi PDF user space is 72 units/inch, so scale = dpi / 72
|
|
25
|
+
*/
|
|
26
|
+
export declare function renderPageToPNG(pdfPath: string, pageNum: number, dpi: number): Promise<RenderPageResult>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Renders PDF pages to PNG with pdfjs-dist + @napi-rs/canvas. Shared by the
|
|
2
|
+
// `render` CLI command (human/vision-model page verification) and src/engines/tesseract.ts (OCR page images).
|
|
3
|
+
import { createCanvas } from '@napi-rs/canvas';
|
|
4
|
+
import { PdfToMdError } from './errors.js';
|
|
5
|
+
import { awaitPdfOpen, openPdfForRender } from './pdf-open.js';
|
|
6
|
+
// No CanvasFactory shim needed: pdfjs-dist detects `@napi-rs/canvas` itself
|
|
7
|
+
// and uses it internally (patterns, SMasks, Type3 glyphs); we only hand it a `canvasContext` for the page itself, created directly below.
|
|
8
|
+
export { openPdfForRender } from './pdf-open.js';
|
|
9
|
+
/** Render one already-open document's page to a PNG buffer at the given DPI. */ export async function renderLoadedPage(doc, pageNum, dpi) {
|
|
10
|
+
if (!(pageNum >= 1) || pageNum > doc.numPages) {
|
|
11
|
+
throw new PdfToMdError('PAGE_RANGE', `page ${pageNum} is outside this document (1-${doc.numPages})`);
|
|
12
|
+
}
|
|
13
|
+
const page = await doc.getPage(pageNum);
|
|
14
|
+
// getViewport({scale}) bakes in the page's own /Rotate entry AND its
|
|
15
|
+
// mediaBox origin automatically — no manual origin translate needed on top of it.
|
|
16
|
+
const scale = dpi / 72;
|
|
17
|
+
const viewport = page.getViewport({
|
|
18
|
+
scale
|
|
19
|
+
});
|
|
20
|
+
const width = Math.round(viewport.width);
|
|
21
|
+
const height = Math.round(viewport.height);
|
|
22
|
+
if (width <= 0 || height <= 0) throw new PdfToMdError('PAGE_RENDER', `page ${pageNum} has non-positive dimensions ${width}x${height}`);
|
|
23
|
+
const canvas = createCanvas(width, height);
|
|
24
|
+
const context = canvas.getContext('2d');
|
|
25
|
+
// Paint white first: an unpainted @napi-rs/canvas context is transparent,
|
|
26
|
+
// which would encode as black/checkered rather than a white page.
|
|
27
|
+
context.fillStyle = 'white';
|
|
28
|
+
context.fillRect(0, 0, width, height);
|
|
29
|
+
// pdfjs's RenderParameters types canvas/context against DOM types;
|
|
30
|
+
// @napi-rs/canvas's are API-compatible at runtime but not that DOM type — the interop boundary the "no any" rule allows a cast for.
|
|
31
|
+
const renderTask = page.render({
|
|
32
|
+
canvas: canvas,
|
|
33
|
+
canvasContext: context,
|
|
34
|
+
viewport
|
|
35
|
+
});
|
|
36
|
+
await renderTask.promise;
|
|
37
|
+
const png = await canvas.encode('png');
|
|
38
|
+
return {
|
|
39
|
+
png,
|
|
40
|
+
width,
|
|
41
|
+
height,
|
|
42
|
+
rotation: viewport.rotation
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Render one page of a PDF to a PNG buffer at the given DPI. Opens and
|
|
47
|
+
* closes the document itself — for more than one page from the same PDF, use `openPdfForRender` + `renderLoadedPage` instead.
|
|
48
|
+
*
|
|
49
|
+
* @param pdfPath path to the PDF file
|
|
50
|
+
* @param pageNum 1-based page number
|
|
51
|
+
* @param dpi PDF user space is 72 units/inch, so scale = dpi / 72
|
|
52
|
+
*/ export async function renderPageToPNG(pdfPath, pageNum, dpi) {
|
|
53
|
+
const loadingTask = openPdfForRender(pdfPath);
|
|
54
|
+
try {
|
|
55
|
+
const doc = await awaitPdfOpen(loadingTask, pdfPath);
|
|
56
|
+
return await renderLoadedPage(doc, pageNum, dpi);
|
|
57
|
+
} finally{
|
|
58
|
+
await loadingTask.destroy();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/raster.ts"],"sourcesContent":["// Renders PDF pages to PNG with pdfjs-dist + @napi-rs/canvas. Shared by the\n// `render` CLI command (human/vision-model page verification) and src/engines/tesseract.ts (OCR page images).\n\nimport { createCanvas } from '@napi-rs/canvas';\nimport type { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { awaitPdfOpen, openPdfForRender } from './pdf-open.ts';\n\n// pdfjs-dist doesn't export RenderParameters from the package root; derive\n// it from PDFPageProxy['render'] rather than hand-declaring a shape that could drift.\ntype RenderParams = Parameters<PDFPageProxy['render']>[0];\n\n// No CanvasFactory shim needed: pdfjs-dist detects `@napi-rs/canvas` itself\n// and uses it internally (patterns, SMasks, Type3 glyphs); we only hand it a `canvasContext` for the page itself, created directly below.\n\nexport { openPdfForRender } from './pdf-open.ts';\n\n/** One rendered page, from renderPageToPNG (or the `pdf-to-md render` CLI\n * command it backs). */\nexport interface RenderPageResult {\n /** the rendered page, PNG-encoded */\n png: Buffer;\n /** pixel width at the requested dpi */\n width: number;\n /** pixel height at the requested dpi */\n height: number;\n /** the page's own /Rotate entry, baked into `png`/`width`/`height` already\n * — informational, not something a caller needs to apply again */\n rotation: number;\n}\n\n/** Render one already-open document's page to a PNG buffer at the given DPI. */\nexport async function renderLoadedPage(doc: PDFDocumentProxy, pageNum: number, dpi: number): Promise<RenderPageResult> {\n if (!(pageNum >= 1) || pageNum > doc.numPages) {\n throw new PdfToMdError('PAGE_RANGE', `page ${pageNum} is outside this document (1-${doc.numPages})`);\n }\n const page = await doc.getPage(pageNum);\n\n // getViewport({scale}) bakes in the page's own /Rotate entry AND its\n // mediaBox origin automatically — no manual origin translate needed on top of it.\n const scale = dpi / 72;\n const viewport = page.getViewport({ scale });\n const width = Math.round(viewport.width);\n const height = Math.round(viewport.height);\n if (width <= 0 || height <= 0) throw new PdfToMdError('PAGE_RENDER', `page ${pageNum} has non-positive dimensions ${width}x${height}`);\n\n const canvas = createCanvas(width, height);\n const context = canvas.getContext('2d');\n\n // Paint white first: an unpainted @napi-rs/canvas context is transparent,\n // which would encode as black/checkered rather than a white page.\n context.fillStyle = 'white';\n context.fillRect(0, 0, width, height);\n\n // pdfjs's RenderParameters types canvas/context against DOM types;\n // @napi-rs/canvas's are API-compatible at runtime but not that DOM type — the interop boundary the \"no any\" rule allows a cast for.\n const renderTask = page.render({\n canvas: canvas as unknown as RenderParams['canvas'],\n canvasContext: context as unknown as RenderParams['canvasContext'],\n viewport,\n });\n await renderTask.promise;\n\n const png = await canvas.encode('png');\n return { png, width, height, rotation: viewport.rotation };\n}\n\n/**\n * Render one page of a PDF to a PNG buffer at the given DPI. Opens and\n * closes the document itself — for more than one page from the same PDF, use `openPdfForRender` + `renderLoadedPage` instead.\n *\n * @param pdfPath path to the PDF file\n * @param pageNum 1-based page number\n * @param dpi PDF user space is 72 units/inch, so scale = dpi / 72\n */\nexport async function renderPageToPNG(pdfPath: string, pageNum: number, dpi: number): Promise<RenderPageResult> {\n const loadingTask = openPdfForRender(pdfPath);\n try {\n const doc = await awaitPdfOpen(loadingTask, pdfPath);\n return await renderLoadedPage(doc, pageNum, dpi);\n } finally {\n await loadingTask.destroy();\n }\n}\n"],"names":["createCanvas","PdfToMdError","awaitPdfOpen","openPdfForRender","renderLoadedPage","doc","pageNum","dpi","numPages","page","getPage","scale","viewport","getViewport","width","Math","round","height","canvas","context","getContext","fillStyle","fillRect","renderTask","render","canvasContext","promise","png","encode","rotation","renderPageToPNG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AAM,MAAMT,OAAOU,MAAM,CAAC;IAChC,OAAO;QAAED;QAAKb;QAAOG;QAAQY,UAAUjB,SAASiB,QAAQ;IAAC;AAC3D;AAEA;;;;;;;CAOC,GACD,OAAO,eAAeC,gBAAgBC,OAAe,EAAEzB,OAAe,EAAEC,GAAW;IACjF,MAAMyB,cAAc7B,iBAAiB4B;IACrC,IAAI;QACF,MAAM1B,MAAM,MAAMH,aAAa8B,aAAaD;QAC5C,OAAO,MAAM3B,iBAAiBC,KAAKC,SAASC;IAC9C,SAAU;QACR,MAAMyB,YAAYC,OAAO;IAC3B;AACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Analysis, AnalysisPage } from './types.js';
|
|
2
|
+
/** Unvalidated core, reused by `src/analyze.ts`. `forcePageMarkers` is the
|
|
3
|
+
* OCR path's always-on "### pN"; `pageMarkers` is the text path's opt-in flag, ignored when `forcePageMarkers` is set. */
|
|
4
|
+
export declare function renderPagesToMarkdown(pages: AnalysisPage[], opts: {
|
|
5
|
+
forcePageMarkers: boolean;
|
|
6
|
+
pageMarkers: boolean;
|
|
7
|
+
}): string;
|
|
8
|
+
export declare function renderPagesToText(pages: AnalysisPage[]): string;
|
|
9
|
+
/**
|
|
10
|
+
* Render an Analysis to markdown. PURE and fail-fast over any well-formed Analysis (including hand-built/spliced ones) — throws `PdfToMdError`/`'ANALYSIS_INPUT'` before any output on a malformed one.
|
|
11
|
+
* OCR-origin output is byte-identical to extractOcr's own markdown (shared core); text-origin is NOT byte-identical to extractText's (see `AnalysisPage.words`, `src/types.ts`).
|
|
12
|
+
*
|
|
13
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
14
|
+
* @param opts.pageMarkers text-origin only; an OCR-origin Analysis always carries markers
|
|
15
|
+
*/
|
|
16
|
+
export declare function toMarkdown(analysis: Analysis, opts?: {
|
|
17
|
+
pageMarkers?: boolean;
|
|
18
|
+
}): string;
|
|
19
|
+
/**
|
|
20
|
+
* Render an Analysis to reading-order plain text: headings drop their `#`;
|
|
21
|
+
* float/review content is INCLUDED with only its markdown decoration stripped, so no content is lost silently. Same fail-fast validation as toMarkdown.
|
|
22
|
+
*
|
|
23
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
24
|
+
*/
|
|
25
|
+
export declare function toText(analysis: Analysis): string;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Pure renderers over the Analysis IR: toMarkdown()/toText() are the public,
|
|
2
|
+
// validated entry points; renderPagesToMarkdown/renderPagesToText below are the unvalidated core, reused (unvalidated) by src/analyze.ts.
|
|
3
|
+
import { PdfToMdError } from './errors.js';
|
|
4
|
+
import { pageMarkerLine } from './page-marker.js';
|
|
5
|
+
const BLOCK_TYPES = new Set([
|
|
6
|
+
'heading',
|
|
7
|
+
'paragraph',
|
|
8
|
+
'float',
|
|
9
|
+
'review'
|
|
10
|
+
]);
|
|
11
|
+
// Whole-object, fail-fast validation: every page/block/wordIndexes entry is
|
|
12
|
+
// checked BEFORE toMarkdown/toText render a single character — a malformed Analysis must never produce partial output, only a clean throw.
|
|
13
|
+
function validateAnalysis(analysis) {
|
|
14
|
+
const fail = (msg)=>{
|
|
15
|
+
throw new PdfToMdError('ANALYSIS_INPUT', msg);
|
|
16
|
+
};
|
|
17
|
+
if (typeof analysis !== 'object' || analysis === null) fail('Analysis must be an object');
|
|
18
|
+
const a = analysis;
|
|
19
|
+
if (typeof a.report !== 'object' || a.report === null) fail('Analysis.report must be an object');
|
|
20
|
+
const path = a.report.path;
|
|
21
|
+
if (path !== 'text' && path !== 'ocr') fail(`Analysis.report.path must be "text" or "ocr" (got ${JSON.stringify(path)})`);
|
|
22
|
+
if (!Array.isArray(a.pages)) fail('Analysis.pages must be an array');
|
|
23
|
+
for (const [pi, page] of a.pages.entries()){
|
|
24
|
+
if (typeof page !== 'object' || page === null) fail(`Analysis.pages[${pi}] must be an object`);
|
|
25
|
+
const p = page;
|
|
26
|
+
if (typeof p.page !== 'number') fail(`Analysis.pages[${pi}].page must be a number`);
|
|
27
|
+
if (!Array.isArray(p.words)) fail(`Analysis.pages[${pi}].words must be an array`);
|
|
28
|
+
for (const [wi, w] of p.words.entries()){
|
|
29
|
+
if (typeof w !== 'object' || w === null || typeof w.text !== 'string') {
|
|
30
|
+
fail(`Analysis.pages[${pi}].words[${wi}] must be an object with a string 'text'`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const wordCount = p.words.length;
|
|
34
|
+
if (!Array.isArray(p.blocks)) fail(`Analysis.pages[${pi}].blocks must be an array`);
|
|
35
|
+
for (const [bi, b] of p.blocks.entries()){
|
|
36
|
+
if (typeof b !== 'object' || b === null) fail(`Analysis.pages[${pi}].blocks[${bi}] must be an object`);
|
|
37
|
+
const block = b;
|
|
38
|
+
if (typeof block.type !== 'string' || !BLOCK_TYPES.has(block.type)) {
|
|
39
|
+
fail(`Analysis.pages[${pi}].blocks[${bi}].type must be one of heading|paragraph|float|review (got ${JSON.stringify(block.type)})`);
|
|
40
|
+
}
|
|
41
|
+
if (typeof block.text !== 'string') fail(`Analysis.pages[${pi}].blocks[${bi}].text must be a string`);
|
|
42
|
+
// `level` contract (see AnalysisBlock, src/types.ts): required 1-6 on a
|
|
43
|
+
// heading, forbidden otherwise — rejected, not ignored, since it likely means `type` was mistyped.
|
|
44
|
+
if (block.type === 'heading') {
|
|
45
|
+
if (!Number.isInteger(block.level) || block.level < 1 || block.level > 6) {
|
|
46
|
+
fail(`Analysis.pages[${pi}].blocks[${bi}].level must be an integer 1-6 on a heading block (got ${JSON.stringify(block.level)})`);
|
|
47
|
+
}
|
|
48
|
+
} else if (block.level !== undefined) {
|
|
49
|
+
fail(`Analysis.pages[${pi}].blocks[${bi}].level is only allowed on heading blocks (got a ${JSON.stringify(block.type)} block with level ${JSON.stringify(block.level)})`);
|
|
50
|
+
}
|
|
51
|
+
if (!Array.isArray(block.wordIndexes)) fail(`Analysis.pages[${pi}].blocks[${bi}].wordIndexes must be an array`);
|
|
52
|
+
for (const idx of block.wordIndexes){
|
|
53
|
+
if (typeof idx !== 'number' || !Number.isInteger(idx) || idx < 0 || idx >= wordCount) {
|
|
54
|
+
fail(`Analysis.pages[${pi}].blocks[${bi}].wordIndexes has an out-of-range index (${JSON.stringify(idx)}; page ${pi} has ${wordCount} words)`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// See AnalysisBlock's doc comment (src/types.ts) for the `text` contract this relies on.
|
|
61
|
+
function renderBlockLiteral(block) {
|
|
62
|
+
// A heading's `level` is guaranteed present (1-6): validateAnalysis
|
|
63
|
+
// enforces it, and src/analyze.ts (the only unvalidated caller) always sets it.
|
|
64
|
+
if (block.type === 'heading') return `${'#'.repeat(block.level)} ${block.text}`;
|
|
65
|
+
return block.text;
|
|
66
|
+
}
|
|
67
|
+
/** Unvalidated core, reused by `src/analyze.ts`. `forcePageMarkers` is the
|
|
68
|
+
* OCR path's always-on "### pN"; `pageMarkers` is the text path's opt-in flag, ignored when `forcePageMarkers` is set. */ export function renderPagesToMarkdown(pages, opts) {
|
|
69
|
+
let md = '';
|
|
70
|
+
for (const page of pages){
|
|
71
|
+
if (opts.forcePageMarkers) md += `\n${pageMarkerLine(page.page)}\n\n`;
|
|
72
|
+
else if (opts.pageMarkers) md += `${pageMarkerLine(page.page)}\n\n`;
|
|
73
|
+
for (const block of page.blocks)md += `${renderBlockLiteral(block)}\n\n`;
|
|
74
|
+
}
|
|
75
|
+
return md;
|
|
76
|
+
}
|
|
77
|
+
// Strips a block's markdown decoration for plain-text rendering (the inverse
|
|
78
|
+
// of AnalysisBlock's literal-text convention); headings need no stripping.
|
|
79
|
+
function bareBlockText(block) {
|
|
80
|
+
if (block.type === 'heading') return block.text;
|
|
81
|
+
if (block.type === 'review') return block.text.replace(/^> \[review: [^\]]*\] /, '');
|
|
82
|
+
if (block.type === 'float') return block.text.replace(/^> \[floats\] /, '');
|
|
83
|
+
return block.text.replace(/^> /, '');
|
|
84
|
+
}
|
|
85
|
+
export function renderPagesToText(pages) {
|
|
86
|
+
const out = [];
|
|
87
|
+
for (const page of pages){
|
|
88
|
+
for (const block of page.blocks)out.push(bareBlockText(block));
|
|
89
|
+
}
|
|
90
|
+
return out.length ? `${out.join('\n\n').trim()}\n` : '';
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Render an Analysis to markdown. PURE and fail-fast over any well-formed Analysis (including hand-built/spliced ones) — throws `PdfToMdError`/`'ANALYSIS_INPUT'` before any output on a malformed one.
|
|
94
|
+
* OCR-origin output is byte-identical to extractOcr's own markdown (shared core); text-origin is NOT byte-identical to extractText's (see `AnalysisPage.words`, `src/types.ts`).
|
|
95
|
+
*
|
|
96
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
97
|
+
* @param opts.pageMarkers text-origin only; an OCR-origin Analysis always carries markers
|
|
98
|
+
*/ export function toMarkdown(analysis, opts = {}) {
|
|
99
|
+
validateAnalysis(analysis);
|
|
100
|
+
const isOcr = analysis.report.path === 'ocr';
|
|
101
|
+
const md = renderPagesToMarkdown(analysis.pages, {
|
|
102
|
+
forcePageMarkers: isOcr,
|
|
103
|
+
pageMarkers: !!opts.pageMarkers
|
|
104
|
+
});
|
|
105
|
+
return isOcr ? md : `${md.trim()}\n`;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Render an Analysis to reading-order plain text: headings drop their `#`;
|
|
109
|
+
* float/review content is INCLUDED with only its markdown decoration stripped, so no content is lost silently. Same fail-fast validation as toMarkdown.
|
|
110
|
+
*
|
|
111
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
112
|
+
*/ export function toText(analysis) {
|
|
113
|
+
validateAnalysis(analysis);
|
|
114
|
+
return renderPagesToText(analysis.pages);
|
|
115
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/render-analysis.ts"],"sourcesContent":["// Pure renderers over the Analysis IR: toMarkdown()/toText() are the public,\n// validated entry points; renderPagesToMarkdown/renderPagesToText below are the unvalidated core, reused (unvalidated) by src/analyze.ts.\nimport { PdfToMdError } from './errors.ts';\nimport { pageMarkerLine } from './page-marker.ts';\nimport type { Analysis, AnalysisBlock, AnalysisPage } from './types.ts';\n\nconst BLOCK_TYPES = new Set(['heading', 'paragraph', 'float', 'review']);\n\n// Whole-object, fail-fast validation: every page/block/wordIndexes entry is\n// checked BEFORE toMarkdown/toText render a single character — a malformed Analysis must never produce partial output, only a clean throw.\nfunction validateAnalysis(analysis: unknown): asserts analysis is Analysis {\n const fail = (msg: string): never => {\n throw new PdfToMdError('ANALYSIS_INPUT', msg);\n };\n if (typeof analysis !== 'object' || analysis === null) fail('Analysis must be an object');\n const a = analysis as Record<string, unknown>;\n if (typeof a.report !== 'object' || a.report === null) fail('Analysis.report must be an object');\n const path = (a.report as Record<string, unknown>).path;\n if (path !== 'text' && path !== 'ocr') fail(`Analysis.report.path must be \"text\" or \"ocr\" (got ${JSON.stringify(path)})`);\n if (!Array.isArray(a.pages)) fail('Analysis.pages must be an array');\n\n for (const [pi, page] of (a.pages as unknown[]).entries()) {\n if (typeof page !== 'object' || page === null) fail(`Analysis.pages[${pi}] must be an object`);\n const p = page as Record<string, unknown>;\n if (typeof p.page !== 'number') fail(`Analysis.pages[${pi}].page must be a number`);\n if (!Array.isArray(p.words)) fail(`Analysis.pages[${pi}].words must be an array`);\n for (const [wi, w] of (p.words as unknown[]).entries()) {\n if (typeof w !== 'object' || w === null || typeof (w as Record<string, unknown>).text !== 'string') {\n fail(`Analysis.pages[${pi}].words[${wi}] must be an object with a string 'text'`);\n }\n }\n const wordCount = (p.words as unknown[]).length;\n\n if (!Array.isArray(p.blocks)) fail(`Analysis.pages[${pi}].blocks must be an array`);\n for (const [bi, b] of (p.blocks as unknown[]).entries()) {\n if (typeof b !== 'object' || b === null) fail(`Analysis.pages[${pi}].blocks[${bi}] must be an object`);\n const block = b as Record<string, unknown>;\n if (typeof block.type !== 'string' || !BLOCK_TYPES.has(block.type)) {\n fail(`Analysis.pages[${pi}].blocks[${bi}].type must be one of heading|paragraph|float|review (got ${JSON.stringify(block.type)})`);\n }\n if (typeof block.text !== 'string') fail(`Analysis.pages[${pi}].blocks[${bi}].text must be a string`);\n // `level` contract (see AnalysisBlock, src/types.ts): required 1-6 on a\n // heading, forbidden otherwise — rejected, not ignored, since it likely means `type` was mistyped.\n if (block.type === 'heading') {\n if (!Number.isInteger(block.level) || (block.level as number) < 1 || (block.level as number) > 6) {\n fail(`Analysis.pages[${pi}].blocks[${bi}].level must be an integer 1-6 on a heading block (got ${JSON.stringify(block.level)})`);\n }\n } else if (block.level !== undefined) {\n fail(`Analysis.pages[${pi}].blocks[${bi}].level is only allowed on heading blocks (got a ${JSON.stringify(block.type)} block with level ${JSON.stringify(block.level)})`);\n }\n if (!Array.isArray(block.wordIndexes)) fail(`Analysis.pages[${pi}].blocks[${bi}].wordIndexes must be an array`);\n for (const idx of block.wordIndexes as unknown[]) {\n if (typeof idx !== 'number' || !Number.isInteger(idx) || idx < 0 || idx >= wordCount) {\n fail(`Analysis.pages[${pi}].blocks[${bi}].wordIndexes has an out-of-range index (${JSON.stringify(idx)}; page ${pi} has ${wordCount} words)`);\n }\n }\n }\n }\n}\n\n// See AnalysisBlock's doc comment (src/types.ts) for the `text` contract this relies on.\nfunction renderBlockLiteral(block: AnalysisBlock): string {\n // A heading's `level` is guaranteed present (1-6): validateAnalysis\n // enforces it, and src/analyze.ts (the only unvalidated caller) always sets it.\n if (block.type === 'heading') return `${'#'.repeat(block.level as number)} ${block.text}`;\n return block.text;\n}\n\n/** Unvalidated core, reused by `src/analyze.ts`. `forcePageMarkers` is the\n * OCR path's always-on \"### pN\"; `pageMarkers` is the text path's opt-in flag, ignored when `forcePageMarkers` is set. */\nexport function renderPagesToMarkdown(pages: AnalysisPage[], opts: { forcePageMarkers: boolean; pageMarkers: boolean }): string {\n let md = '';\n for (const page of pages) {\n if (opts.forcePageMarkers) md += `\\n${pageMarkerLine(page.page)}\\n\\n`;\n else if (opts.pageMarkers) md += `${pageMarkerLine(page.page)}\\n\\n`;\n for (const block of page.blocks) md += `${renderBlockLiteral(block)}\\n\\n`;\n }\n return md;\n}\n\n// Strips a block's markdown decoration for plain-text rendering (the inverse\n// of AnalysisBlock's literal-text convention); headings need no stripping.\nfunction bareBlockText(block: AnalysisBlock): string {\n if (block.type === 'heading') return block.text;\n if (block.type === 'review') return block.text.replace(/^> \\[review: [^\\]]*\\] /, '');\n if (block.type === 'float') return block.text.replace(/^> \\[floats\\] /, '');\n return block.text.replace(/^> /, '');\n}\n\nexport function renderPagesToText(pages: AnalysisPage[]): string {\n const out: string[] = [];\n for (const page of pages) {\n for (const block of page.blocks) out.push(bareBlockText(block));\n }\n return out.length ? `${out.join('\\n\\n').trim()}\\n` : '';\n}\n\n/**\n * Render an Analysis to markdown. PURE and fail-fast over any well-formed Analysis (including hand-built/spliced ones) — throws `PdfToMdError`/`'ANALYSIS_INPUT'` before any output on a malformed one.\n * OCR-origin output is byte-identical to extractOcr's own markdown (shared core); text-origin is NOT byte-identical to extractText's (see `AnalysisPage.words`, `src/types.ts`).\n *\n * @param analysis the Analysis to render (from analyze(), or hand-built/spliced)\n * @param opts.pageMarkers text-origin only; an OCR-origin Analysis always carries markers\n */\nexport function toMarkdown(analysis: Analysis, opts: { pageMarkers?: boolean } = {}): string {\n validateAnalysis(analysis);\n const isOcr = analysis.report.path === 'ocr';\n const md = renderPagesToMarkdown(analysis.pages, { forcePageMarkers: isOcr, pageMarkers: !!opts.pageMarkers });\n return isOcr ? md : `${md.trim()}\\n`;\n}\n\n/**\n * Render an Analysis to reading-order plain text: headings drop their `#`;\n * float/review content is INCLUDED with only its markdown decoration stripped, so no content is lost silently. Same fail-fast validation as toMarkdown.\n *\n * @param analysis the Analysis to render (from analyze(), or hand-built/spliced)\n */\nexport function toText(analysis: Analysis): string {\n validateAnalysis(analysis);\n return renderPagesToText(analysis.pages);\n}\n"],"names":["PdfToMdError","pageMarkerLine","BLOCK_TYPES","Set","validateAnalysis","analysis","fail","msg","a","report","path","JSON","stringify","Array","isArray","pages","pi","page","entries","p","words","wi","w","text","wordCount","length","blocks","bi","b","block","type","has","Number","isInteger","level","undefined","wordIndexes","idx","renderBlockLiteral","repeat","renderPagesToMarkdown","opts","md","forcePageMarkers","pageMarkers","bareBlockText","replace","renderPagesToText","out","push","join","trim","toMarkdown","isOcr","toText"],"mappings":"AAAA,6EAA6E;AAC7E,0IAA0I;AAC1I,SAASA,YAAY,QAAQ,cAAc;AAC3C,SAASC,cAAc,QAAQ,mBAAmB;AAGlD,MAAMC,cAAc,IAAIC,IAAI;IAAC;IAAW;IAAa;IAAS;CAAS;AAEvE,4EAA4E;AAC5E,2IAA2I;AAC3I,SAASC,iBAAiBC,QAAiB;IACzC,MAAMC,OAAO,CAACC;QACZ,MAAM,IAAIP,aAAa,kBAAkBO;IAC3C;IACA,IAAI,OAAOF,aAAa,YAAYA,aAAa,MAAMC,KAAK;IAC5D,MAAME,IAAIH;IACV,IAAI,OAAOG,EAAEC,MAAM,KAAK,YAAYD,EAAEC,MAAM,KAAK,MAAMH,KAAK;IAC5D,MAAMI,OAAO,AAACF,EAAEC,MAAM,CAA6BC,IAAI;IACvD,IAAIA,SAAS,UAAUA,SAAS,OAAOJ,KAAK,CAAC,kDAAkD,EAAEK,KAAKC,SAAS,CAACF,MAAM,CAAC,CAAC;IACxH,IAAI,CAACG,MAAMC,OAAO,CAACN,EAAEO,KAAK,GAAGT,KAAK;IAElC,KAAK,MAAM,CAACU,IAAIC,KAAK,IAAI,AAACT,EAAEO,KAAK,CAAeG,OAAO,GAAI;QACzD,IAAI,OAAOD,SAAS,YAAYA,SAAS,MAAMX,KAAK,CAAC,eAAe,EAAEU,GAAG,mBAAmB,CAAC;QAC7F,MAAMG,IAAIF;QACV,IAAI,OAAOE,EAAEF,IAAI,KAAK,UAAUX,KAAK,CAAC,eAAe,EAA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,EAAE,CAAC;AACtC;AAEA;;;;;CAKC,GACD,OAAO,SAASG,OAAOjD,QAAkB;IACvCD,iBAAiBC;IACjB,OAAO0C,kBAAkB1C,SAASU,KAAK;AACzC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BuildOcrReportInput, BuildReportInput, Report } from './types.js';
|
|
2
|
+
export declare const SPARSE_CHARS_PER_PAGE = 200;
|
|
3
|
+
export declare const AUTO_OCR_CHARS_PER_PAGE = 20;
|
|
4
|
+
/** A manually-bumped integer, deliberately not the npm package version —
|
|
5
|
+
* bump whenever a change can alter output for identical input. See `CHANGELOG.md` for history. */
|
|
6
|
+
export declare const OUTPUT_VERSION = 1;
|
|
7
|
+
/** Below this per-word confidence (0-100, tesseract.js's `confidence`), a
|
|
8
|
+
* word counts as low-confidence. Measured: junk max 43 vs prose min 76, cut 60, 2026-08-17. */
|
|
9
|
+
export declare const LOW_CONFIDENCE_THRESHOLD = 60;
|
|
10
|
+
export declare const LOW_CONFIDENCE_SAMPLE_CAP = 12;
|
|
11
|
+
export declare function buildReport({ numPages, md, statsMd, stats, bodyH, headingSizes, hybridPages }: BuildReportInput): Report;
|
|
12
|
+
export declare function buildOcrReport({ md, fallback, words }: BuildOcrReportInput): Report;
|