@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/engines/tesseract.ts"],"sourcesContent":["// engines/tesseract.ts — THE OCR engine: tesseract.js on every platform.\n//\n// Seam contract: words come back in normalized 0-1 page coordinates, origin\n// bottom-left, y increasing upward — the y-flip happens here, and nowhere else.\n//\n// Pure WASM, in-process, no native dependency: no crash containment needed.\nimport { mkdirSync } from 'node:fs';\nimport os from 'node:os';\nimport path from 'node:path';\nimport type { Page as TesseractPage } from 'tesseract.js';\nimport { createWorker, PSM } from 'tesseract.js';\nimport type { PageWord, Word } from '../geometry.ts';\nimport { foldTypographic } from '../lines.ts';\nimport { awaitPdfOpen, openPdfForRender } from '../pdf-open.ts';\nimport { renderLoadedPage } from '../raster.ts';\nimport { resolvePackagePath } from '../resolve.ts';\n\n// 288 dpi: the render resolution the engine's word-overlap numbers were\n// measured at (test/fixtures/ocr-engine-poc/) — the known-good operating point.\nexport const OCR_RENDER_DPI = 288;\n\n// Chart pages starve PSM.AUTO (unrecognized text can't be marked for review).\n// Prose-fraction retry trigger, measured (sparse-retry.test.ts): chart 0.442 vs prose 0.767-0.938, cut 0.6.\nexport const SPARSE_RETRY_PROSE_FRACTION = 0.6;\n\n/** Share of `words` whose text contains a run of 3+ letters (`src/emit.ts`'s isUnstructured canary, applied per-page instead of per-block). */\nfunction proseFraction(words: { text: string }[]): number {\n if (words.length === 0) return 0;\n return words.filter((w) => /[A-Za-z]{3}/.test(w.text)).length / words.length;\n}\n\n/** Does this page's PSM.AUTO pass look chart-like enough to re-recognize\n * with PSM.SPARSE_TEXT? A page with 0 words never retries (nothing to take a prose fraction of). */\nexport function shouldRetrySparse(words: { text: string }[]): boolean {\n return words.length > 0 && proseFraction(words) < SPARSE_RETRY_PROSE_FRACTION;\n}\n\n// The locally installed traineddata (npm package @tesseract.js-data/eng),\n// resolved dynamically so depcheck won't flag it \"unused\" — it's a genuine runtime dependency, listed in .depcheckrc's `ignores`.\nconst TESSDATA_DIR = path.join(path.dirname(resolvePackagePath('@tesseract.js-data/eng/package.json')), '4.0.0');\n\n/** Per-page recognition progress, delivered once per page as it finishes.\n * Engine chatter goes through `onWarning`/`warnings` instead — this is timing/count data only. */\nexport type OcrProgressEvent = { type: 'page'; page: number; words: number; ms: number };\n\nexport interface RecognizeResult {\n words: PageWord[];\n /** pages (1-based) where the engine returned zero words */\n emptyPages: number[];\n /** wall-clock ms per page (render + recognize), keyed by page number */\n msPerPage: Map<number, number>;\n}\n\n/** tesseract.js's nested blocks/paragraphs/lines/words -> this repo's flat Word contract (normalize + y-flip). Same shape regardless of which PSM produced `page`, so both the AUTO pass and the SPARSE_TEXT retry share it. */\nfunction wordsFromPage(page: TesseractPage, width: number, height: number): Word[] {\n const pageWords: Word[] = [];\n for (const block of page.blocks ?? []) {\n for (const para of block.paragraphs ?? []) {\n for (const line of para.lines ?? []) {\n for (const w of line.words ?? []) {\n const { x0, y0, x1, y1 } = w.bbox;\n pageWords.push({\n // Same typographic folding as the text path (src/lines.ts) — both\n // paths must agree on how a printed apostrophe is spelled.\n text: foldTypographic(w.text),\n x: x0 / width,\n y: (height - y1) / height, // y-flip: top-left px -> bottom-left normalized\n w: (x1 - x0) / width,\n h: (y1 - y0) / height,\n // Plumbed through unmodified — tesseract.js's own 0-100\n // per-word confidence. See src/report.ts's LOW_CONFIDENCE_THRESHOLD.\n confidence: w.confidence,\n });\n }\n }\n }\n }\n return pageWords;\n}\n\n// tesseract.js-core's WASM build writes native diagnostics straight to this\n// process's stdout/stderr, with no override to silence them at the source.\n//\n// Suppression must span the whole run: worker-thread stdio arrives\n// asynchronously, on later event-loop turns; the synchronous carve-out below (`withRealStdio`) is the only safe exit.\nlet suppressDepth = 0;\nlet savedStdoutWrite: typeof process.stdout.write | null = null;\nlet savedStderrWrite: typeof process.stderr.write | null = null;\nasync function withSuppressedStdio<T>(fn: () => Promise<T>): Promise<T> {\n if (suppressDepth === 0) {\n savedStdoutWrite = process.stdout.write.bind(process.stdout);\n savedStderrWrite = process.stderr.write.bind(process.stderr);\n process.stdout.write = (() => true) as typeof process.stdout.write;\n process.stderr.write = (() => true) as typeof process.stderr.write;\n }\n suppressDepth++;\n try {\n return await fn();\n } finally {\n suppressDepth--;\n if (suppressDepth === 0) {\n if (savedStdoutWrite) process.stdout.write = savedStdoutWrite;\n if (savedStderrWrite) process.stderr.write = savedStderrWrite;\n savedStdoutWrite = null;\n savedStderrWrite = null;\n }\n }\n}\n\n/** Pierces an active `withSuppressedStdio` region SYNCHRONOUSLY to let this\n * module's own onProgress/onWarning calls through. No-op outside any suppressed region. */\nfunction withRealStdio<T>(fn: () => T): T {\n if (suppressDepth === 0) return fn();\n const realOut = savedStdoutWrite;\n const realErr = savedStderrWrite;\n if (realOut) process.stdout.write = realOut;\n if (realErr) process.stderr.write = realErr;\n try {\n return fn();\n } finally {\n process.stdout.write = (() => true) as typeof process.stdout.write;\n process.stderr.write = (() => true) as typeof process.stderr.write;\n }\n}\n\n/** Recognize words on pages [first..last] of a PDF (renders at `dpi`,\n * default OCR_RENDER_DPI). Progress via `onProgress`; diagnostics via `onWarning` — this module never writes to stdout/stderr itself. */\nexport async function recognizePdf(pdfPath: string, first: number, last: number, { dpi = OCR_RENDER_DPI, onProgress, onWarning }: { dpi?: number; onProgress?: (e: OcrProgressEvent) => void; onWarning?: (message: string) => void } = {}): Promise<RecognizeResult> {\n // tesseract.js writes its decompressed traineddata cache here by default,\n // dropping a model file into the user's cwd — redirect it under tmpdir() instead.\n const cachePath = path.join(os.tmpdir(), 'pdf-to-md-engine');\n mkdirSync(cachePath, { recursive: true });\n\n const words: PageWord[] = [];\n const emptyPages: number[] = [];\n const msPerPage = new Map<number, number>();\n\n // ONE continuous suppressed region for the whole run; onProgress/onWarning\n // below go through `withRealStdio`, the only sanctioned way out of it.\n await withSuppressedStdio(async () => {\n // OEM 1 (LSTM only, tesseract.js's own default): measured 99.0% recall\n // on the hard benchmark pages (2026-08-16).\n const worker = await createWorker('eng', 1, {\n langPath: TESSDATA_DIR,\n cachePath,\n });\n try {\n // NOT optional: tesseract.js's own default PSM (SINGLE_BLOCK) measured\n // 3-8 percentage points worse than AUTO on two-column pages.\n await worker.setParameters({ tessedit_pageseg_mode: PSM.AUTO });\n\n const loadingTask = openPdfForRender(pdfPath);\n try {\n const pdf = await awaitPdfOpen(loadingTask, pdfPath);\n for (let pn = first; pn <= Math.min(last, pdf.numPages); pn++) {\n const t0 = Date.now();\n const { png, width, height } = await renderLoadedPage(pdf, pn, dpi);\n const { data } = await worker.recognize(png, {}, { blocks: true });\n let pageWords: Word[] = wordsFromPage(data, width, height);\n\n // Adaptive second pass: re-recognizes the SAME rendered PNG, so\n // this costs one extra recognize() call only on chart-like pages.\n if (shouldRetrySparse(pageWords)) {\n const autoFraction = proseFraction(pageWords);\n const autoWords = pageWords;\n // setParameters on the shared worker is reversible, so one\n // in-process worker can alternate PSM per page instead of paying for a second worker.\n await worker.setParameters({ tessedit_pageseg_mode: PSM.SPARSE_TEXT });\n const sparse = await worker.recognize(png, {}, { blocks: true });\n const sparseWords = wordsFromPage(sparse.data, width, height);\n await worker.setParameters({ tessedit_pageseg_mode: PSM.AUTO });\n\n if (sparseWords.length > autoWords.length) {\n pageWords = sparseWords;\n const autoCount = autoWords.length;\n const sparseCount = sparseWords.length;\n withRealStdio(() => onWarning?.(`p${pn}: chart-like page (prose fraction ${autoFraction.toFixed(2)}) — retried with sparse-text segmentation, ${autoCount} -> ${sparseCount} words`));\n }\n }\n\n const ms = Date.now() - t0;\n for (const w of pageWords) words.push({ page: pn, ...w });\n if (pageWords.length === 0) emptyPages.push(pn);\n msPerPage.set(pn, ms);\n const wordCount = pageWords.length;\n withRealStdio(() => onProgress?.({ type: 'page', page: pn, words: wordCount, ms }));\n }\n } finally {\n await loadingTask.destroy();\n }\n } finally {\n await worker.terminate();\n }\n });\n\n return { words, emptyPages, msPerPage };\n}\n"],"names":["OCR_RENDER_DPI","SPARSE_RETRY_PROSE_FRACTION","recognizePdf","shouldRetrySparse","proseFraction","words","length","filter","w","test","text","TESSDATA_DIR","path","join","dirname","resolvePackagePath","wordsFromPage","page","width","height","pageWords","blocks","block","paragraphs","para","lines","line","bbox","x0","y0","x1","y1","push","foldTypographic","x","y","h","confidence","suppressDepth","savedStdoutWrite","savedStderrWrite","withSuppressedStdio","fn","process","stdout","write","bind","stderr","withRealStdio","realOut","realErr","pdfPath","first","last","dpi","onProgress","onWarning","cachePath","emptyPages","msPerPage","os","tmpdir","mkdirSync","recursive","Map","worker","loadingTask","pdf","pn","createWorker","langPath","setParameters","tessedit_pageseg_mode","PSM","AUTO","openPdfForRender","t0","png","data","autoFraction","autoWords","sparse","sparseWords","autoCount","sparseCount","ms","wordCount","Date","now","renderLoadedPage","recognize","SPARSE_TEXT","toFixed","set","type","awaitPdfOpen","Math","min","numPages","destroy","terminate"],"mappings":"AAAA,yEAAyE;AACzE,EAAE;AACF,4EAA4E;AAC5E,gFAAgF;AAChF,EAAE;AACF,4EAA4E;;;;;;;;;;;;QAc/DA;eAAAA;;QAIAC;eAAAA;;QAwGSC;eAAAA;;QA9FNC;eAAAA;;;sBA3BU;6DACX;+DACE;yBAEiB;uBAEF;yBACe;wBACd;yBACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAI5B,IAAMH,iBAAiB;AAIvB,IAAMC,8BAA8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
|
4
|
+
/** input missing/unreadable/not a PDF */
|
|
5
|
+
'PDF_OPEN'
|
|
6
|
+
/** malformed page selection (validated before any work) */
|
|
7
|
+
| 'PAGE_RANGE'
|
|
8
|
+
/** rendering an opened, in-range page to PNG failed */
|
|
9
|
+
| 'PAGE_RENDER'
|
|
10
|
+
/** the OCR engine crashed twice on one page (message names it) */
|
|
11
|
+
| 'OCR_PAGE_FAILED'
|
|
12
|
+
/** malformed word-dump input (--words-json / extractOcr words) */
|
|
13
|
+
| 'WORDS_INPUT'
|
|
14
|
+
/** malformed Analysis handed to toMarkdown/toText (e.g. an out-of-range
|
|
15
|
+
* wordIndexes entry) — validated fail-fast, whole object checked before any output. */
|
|
16
|
+
| 'ANALYSIS_INPUT';
|
|
17
|
+
/** The error every entry point throws — never a bare `Error`. */
|
|
18
|
+
export declare class PdfToMdError extends Error {
|
|
19
|
+
code: PdfToMdErrorCode;
|
|
20
|
+
constructor(code: PdfToMdErrorCode, message: string);
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The closed set of error codes every `PdfToMdError` carries — switch on
|
|
2
|
+
* `code` instead of parsing `message`. */
|
|
3
|
+
export type PdfToMdErrorCode =
|
|
4
|
+
/** input missing/unreadable/not a PDF */
|
|
5
|
+
'PDF_OPEN'
|
|
6
|
+
/** malformed page selection (validated before any work) */
|
|
7
|
+
| 'PAGE_RANGE'
|
|
8
|
+
/** rendering an opened, in-range page to PNG failed */
|
|
9
|
+
| 'PAGE_RENDER'
|
|
10
|
+
/** the OCR engine crashed twice on one page (message names it) */
|
|
11
|
+
| 'OCR_PAGE_FAILED'
|
|
12
|
+
/** malformed word-dump input (--words-json / extractOcr words) */
|
|
13
|
+
| 'WORDS_INPUT'
|
|
14
|
+
/** malformed Analysis handed to toMarkdown/toText (e.g. an out-of-range
|
|
15
|
+
* wordIndexes entry) — validated fail-fast, whole object checked before any output. */
|
|
16
|
+
| 'ANALYSIS_INPUT';
|
|
17
|
+
/** The error every entry point throws — never a bare `Error`. */
|
|
18
|
+
export declare class PdfToMdError extends Error {
|
|
19
|
+
code: PdfToMdErrorCode;
|
|
20
|
+
constructor(code: PdfToMdErrorCode, message: string);
|
|
21
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Library code throws; only src/cli.ts prints and exits.
|
|
2
|
+
/** The closed set of error codes every `PdfToMdError` carries — switch on
|
|
3
|
+
* `code` instead of parsing `message`. */ "use strict";
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "PdfToMdError", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function() {
|
|
10
|
+
return PdfToMdError;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
function _assert_this_initialized(self) {
|
|
14
|
+
if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
15
|
+
return self;
|
|
16
|
+
}
|
|
17
|
+
function _call_super(_this, derived, args) {
|
|
18
|
+
derived = _get_prototype_of(derived);
|
|
19
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
20
|
+
}
|
|
21
|
+
function _class_call_check(instance, Constructor) {
|
|
22
|
+
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
|
|
23
|
+
}
|
|
24
|
+
function _construct(Parent, args, Class) {
|
|
25
|
+
if (_is_native_reflect_construct()) _construct = Reflect.construct;
|
|
26
|
+
else {
|
|
27
|
+
_construct = function construct(Parent, args, Class) {
|
|
28
|
+
var a = [
|
|
29
|
+
null
|
|
30
|
+
];
|
|
31
|
+
a.push.apply(a, args);
|
|
32
|
+
var Constructor = Function.bind.apply(Parent, a);
|
|
33
|
+
var instance = new Constructor();
|
|
34
|
+
if (Class) _set_prototype_of(instance, Class.prototype);
|
|
35
|
+
return instance;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return _construct.apply(null, arguments);
|
|
39
|
+
}
|
|
40
|
+
function _get_prototype_of(o) {
|
|
41
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
42
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
43
|
+
};
|
|
44
|
+
return _get_prototype_of(o);
|
|
45
|
+
}
|
|
46
|
+
function _inherits(subClass, superClass) {
|
|
47
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
48
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
49
|
+
}
|
|
50
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
51
|
+
constructor: {
|
|
52
|
+
value: subClass,
|
|
53
|
+
writable: true,
|
|
54
|
+
configurable: true
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
58
|
+
}
|
|
59
|
+
function _is_native_function(fn) {
|
|
60
|
+
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
61
|
+
}
|
|
62
|
+
function _is_native_reflect_construct() {
|
|
63
|
+
try {
|
|
64
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
65
|
+
} catch (_) {}
|
|
66
|
+
return (_is_native_reflect_construct = function() {
|
|
67
|
+
return !!result;
|
|
68
|
+
})();
|
|
69
|
+
}
|
|
70
|
+
function _possible_constructor_return(self, call) {
|
|
71
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) return call;
|
|
72
|
+
return _assert_this_initialized(self);
|
|
73
|
+
}
|
|
74
|
+
function _set_prototype_of(o, p) {
|
|
75
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
76
|
+
o.__proto__ = p;
|
|
77
|
+
return o;
|
|
78
|
+
};
|
|
79
|
+
return _set_prototype_of(o, p);
|
|
80
|
+
}
|
|
81
|
+
function _type_of(obj) {
|
|
82
|
+
"@swc/helpers - typeof";
|
|
83
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
84
|
+
}
|
|
85
|
+
function _wrap_native_super(Class) {
|
|
86
|
+
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
87
|
+
_wrap_native_super = function(Class) {
|
|
88
|
+
if (Class === null || !_is_native_function(Class)) return Class;
|
|
89
|
+
if (typeof Class !== "function") throw new TypeError("Super expression must either be null or a function");
|
|
90
|
+
if (typeof _cache !== "undefined") {
|
|
91
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
92
|
+
_cache.set(Class, Wrapper);
|
|
93
|
+
}
|
|
94
|
+
function Wrapper() {
|
|
95
|
+
return _construct(Class, arguments, _get_prototype_of(this).constructor);
|
|
96
|
+
}
|
|
97
|
+
Wrapper.prototype = Object.create(Class.prototype, {
|
|
98
|
+
constructor: {
|
|
99
|
+
value: Wrapper,
|
|
100
|
+
enumerable: false,
|
|
101
|
+
writable: true,
|
|
102
|
+
configurable: true
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return _set_prototype_of(Wrapper, Class);
|
|
106
|
+
};
|
|
107
|
+
return _wrap_native_super(Class);
|
|
108
|
+
}
|
|
109
|
+
var PdfToMdError = /*#__PURE__*/ function(Error1) {
|
|
110
|
+
"use strict";
|
|
111
|
+
_inherits(PdfToMdError, Error1);
|
|
112
|
+
function PdfToMdError(code, message) {
|
|
113
|
+
_class_call_check(this, PdfToMdError);
|
|
114
|
+
var _this;
|
|
115
|
+
_this = _call_super(this, PdfToMdError, [
|
|
116
|
+
message
|
|
117
|
+
]);
|
|
118
|
+
_this.name = 'PdfToMdError';
|
|
119
|
+
_this.code = code;
|
|
120
|
+
return _this;
|
|
121
|
+
}
|
|
122
|
+
return PdfToMdError;
|
|
123
|
+
}(_wrap_native_super(Error));
|
|
124
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/errors.ts"],"sourcesContent":["// Library code throws; only src/cli.ts prints and exits.\n\n/** The closed set of error codes every `PdfToMdError` carries — switch on\n * `code` instead of parsing `message`. */\nexport type PdfToMdErrorCode =\n /** input missing/unreadable/not a PDF */\n | 'PDF_OPEN'\n /** malformed page selection (validated before any work) */\n | 'PAGE_RANGE'\n /** rendering an opened, in-range page to PNG failed */\n | 'PAGE_RENDER'\n /** the OCR engine crashed twice on one page (message names it) */\n | 'OCR_PAGE_FAILED'\n /** malformed word-dump input (--words-json / extractOcr words) */\n | 'WORDS_INPUT'\n /** malformed Analysis handed to toMarkdown/toText (e.g. an out-of-range\n * wordIndexes entry) — validated fail-fast, whole object checked before any output. */\n | 'ANALYSIS_INPUT';\n\n/** The error every entry point throws — never a bare `Error`. */\nexport class PdfToMdError extends Error {\n code: PdfToMdErrorCode;\n\n constructor(code: PdfToMdErrorCode, message: string) {\n super(message);\n this.name = 'PdfToMdError';\n this.code = code;\n }\n}\n"],"names":["PdfToMdError","code","message","name","Error"],"mappings":"AAAA,yDAAyD;AAEzD;wCACwC;;;;+BAiB3BA;;;eAAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAN,IAAA,AAAMA,6BAAN;;cAAMA;aAAAA,aAGCC,IAAsB,EAAEC,OAAe;gCAHxCF;;gBAIT,kBAJSA;YAIHE;;QACN,MAAKC,IAAI,GAAG;QACZ,MAAKF,IAAI,GAAGA;;;WANHD;qBAAqBI"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { OcrProgressEvent } from './engines/tesseract.js';
|
|
2
|
+
import type { PageWord, Tuning } from './geometry.js';
|
|
3
|
+
import type { Report } from './types.js';
|
|
4
|
+
export type { OcrProgressEvent } from './engines/tesseract.js';
|
|
5
|
+
/** One recognized word with its page — the OCR replay input contract (same
|
|
6
|
+
* shape as `geometry.ts`'s `PageWord`, re-exported under this module's public name). */
|
|
7
|
+
export type OcrWordInput = PageWord;
|
|
8
|
+
/** A 1-based, inclusive page range — the shape every entry point's `pages`
|
|
9
|
+
* option takes (the CLI's own `--pages N[-M]` parses into this). */
|
|
10
|
+
export interface PageSelection {
|
|
11
|
+
first: number;
|
|
12
|
+
/** inclusive; defaults to `first` */
|
|
13
|
+
last?: number;
|
|
14
|
+
}
|
|
15
|
+
/** extractText/extractOcr/pdfToMarkdown's common return shape. */
|
|
16
|
+
export interface ExtractResult {
|
|
17
|
+
markdown: string;
|
|
18
|
+
/** per-document and per-page QA metrics — see `Report` (`src/types.ts`) */
|
|
19
|
+
report: Report;
|
|
20
|
+
/** every notice also delivered incrementally via `onWarning`, collected
|
|
21
|
+
* here in full (hybrid pages, auto-OCR-fallback, empty OCR pages, sparse-retry) */
|
|
22
|
+
warnings: string[];
|
|
23
|
+
}
|
|
24
|
+
interface CommonOptions {
|
|
25
|
+
/** convert only this page range (1-based, inclusive); defaults to the
|
|
26
|
+
* whole document */
|
|
27
|
+
pages?: PageSelection;
|
|
28
|
+
/** delivered as each warning happens (also returned, in full, as
|
|
29
|
+
* `warnings` on the resolved result) */
|
|
30
|
+
onWarning?: (message: string) => void;
|
|
31
|
+
}
|
|
32
|
+
/** Options for {@link extractText}. */
|
|
33
|
+
export interface ExtractTextOptions extends CommonOptions {
|
|
34
|
+
/** emit a `### pN` marker before each page's content (output formatting
|
|
35
|
+
* only — `report.pageStats` is always populated regardless) */
|
|
36
|
+
pageMarkers?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/** Options for {@link extractOcr}. */
|
|
39
|
+
export interface ExtractOcrOptions extends CommonOptions {
|
|
40
|
+
/** override any subset of the OCR-path geometry defaults — see
|
|
41
|
+
* DEFAULT_TUNING (`src/geometry.ts`) for each knob's measured default */
|
|
42
|
+
tuning?: Partial<Tuning>;
|
|
43
|
+
/** page-render resolution for engine recognition (default 288, measured;
|
|
44
|
+
* >~400 measured WORSE on ordinary pages — not a "bigger is safer" knob) */
|
|
45
|
+
dpi?: number;
|
|
46
|
+
/** marks report.ocrFallback — set by pdfToMarkdown's auto-fallback */
|
|
47
|
+
fallback?: boolean;
|
|
48
|
+
/** one event per page as recognition finishes (timing/count only — engine
|
|
49
|
+
* chatter and the sparse-retry notice arrive via `onWarning` instead) */
|
|
50
|
+
onProgress?: (e: OcrProgressEvent) => void;
|
|
51
|
+
/** every recognized word entering the geometry, delivered once after
|
|
52
|
+
* recognition — the severity-1 audit instrument (--debug-words) */
|
|
53
|
+
onWords?: (words: OcrWordInput[]) => void;
|
|
54
|
+
}
|
|
55
|
+
/** Options for {@link pdfToMarkdown}. */
|
|
56
|
+
export interface PdfToMarkdownOptions extends ExtractTextOptions, Omit<ExtractOcrOptions, 'fallback'> {
|
|
57
|
+
/** force the OCR path even when a text layer exists */
|
|
58
|
+
ocr?: boolean;
|
|
59
|
+
/** never fall back to OCR, even for image-only pages */
|
|
60
|
+
noOcr?: boolean;
|
|
61
|
+
}
|
|
62
|
+
export declare function extractText(pdfPath: string, opts?: ExtractTextOptions): Promise<ExtractResult>;
|
|
63
|
+
/**
|
|
64
|
+
* OCR extraction. Input is either a PDF (pages render at `dpi` and the
|
|
65
|
+
* engine recognizes them, imported lazily) or an already-recognized word dump (the offline replay input — see `--words-json`).
|
|
66
|
+
*
|
|
67
|
+
* @param input `{ pdfPath }` to recognize, or `{ words }` to replay a dump
|
|
68
|
+
* @param opts see {@link ExtractOcrOptions}
|
|
69
|
+
*/
|
|
70
|
+
export declare function extractOcr(input: {
|
|
71
|
+
pdfPath: string;
|
|
72
|
+
} | {
|
|
73
|
+
words: OcrWordInput[];
|
|
74
|
+
}, opts?: ExtractOcrOptions): Promise<ExtractResult>;
|
|
75
|
+
/** pdfToMarkdown's auto-OCR fallback predicate, shared with the CLI's
|
|
76
|
+
* --format=raw|txt path so the two call sites can never drift apart. Module-internal: not re-exported by `src/index.ts`. */
|
|
77
|
+
export declare function needsOcrFallback(charsPerPage: number): boolean;
|
|
78
|
+
/** The exact stderr/warnings notice printed when needsOcrFallback fires —
|
|
79
|
+
* one copy of the string for both call sites (asserted verbatim by the suite). */
|
|
80
|
+
export declare function ocrFallbackNotice(charsPerPage: number): string;
|
|
81
|
+
/**
|
|
82
|
+
* The headline API: extract a PDF to markdown, text layer first, falling
|
|
83
|
+
* back to OCR when the text layer is empty enough the pages are certainly images of text (< `AUTO_OCR_CHARS_PER_PAGE` chars/page) — the same decision the CLI makes.
|
|
84
|
+
*
|
|
85
|
+
* @param pdfPath path to the PDF file
|
|
86
|
+
* @param opts see {@link PdfToMarkdownOptions}
|
|
87
|
+
*/
|
|
88
|
+
export declare function pdfToMarkdown(pdfPath: string, opts?: PdfToMarkdownOptions): Promise<ExtractResult>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { OcrProgressEvent } from './engines/tesseract.js';
|
|
2
|
+
import type { PageWord, Tuning } from './geometry.js';
|
|
3
|
+
import type { Report } from './types.js';
|
|
4
|
+
export type { OcrProgressEvent } from './engines/tesseract.js';
|
|
5
|
+
/** One recognized word with its page — the OCR replay input contract (same
|
|
6
|
+
* shape as `geometry.ts`'s `PageWord`, re-exported under this module's public name). */
|
|
7
|
+
export type OcrWordInput = PageWord;
|
|
8
|
+
/** A 1-based, inclusive page range — the shape every entry point's `pages`
|
|
9
|
+
* option takes (the CLI's own `--pages N[-M]` parses into this). */
|
|
10
|
+
export interface PageSelection {
|
|
11
|
+
first: number;
|
|
12
|
+
/** inclusive; defaults to `first` */
|
|
13
|
+
last?: number;
|
|
14
|
+
}
|
|
15
|
+
/** extractText/extractOcr/pdfToMarkdown's common return shape. */
|
|
16
|
+
export interface ExtractResult {
|
|
17
|
+
markdown: string;
|
|
18
|
+
/** per-document and per-page QA metrics — see `Report` (`src/types.ts`) */
|
|
19
|
+
report: Report;
|
|
20
|
+
/** every notice also delivered incrementally via `onWarning`, collected
|
|
21
|
+
* here in full (hybrid pages, auto-OCR-fallback, empty OCR pages, sparse-retry) */
|
|
22
|
+
warnings: string[];
|
|
23
|
+
}
|
|
24
|
+
interface CommonOptions {
|
|
25
|
+
/** convert only this page range (1-based, inclusive); defaults to the
|
|
26
|
+
* whole document */
|
|
27
|
+
pages?: PageSelection;
|
|
28
|
+
/** delivered as each warning happens (also returned, in full, as
|
|
29
|
+
* `warnings` on the resolved result) */
|
|
30
|
+
onWarning?: (message: string) => void;
|
|
31
|
+
}
|
|
32
|
+
/** Options for {@link extractText}. */
|
|
33
|
+
export interface ExtractTextOptions extends CommonOptions {
|
|
34
|
+
/** emit a `### pN` marker before each page's content (output formatting
|
|
35
|
+
* only — `report.pageStats` is always populated regardless) */
|
|
36
|
+
pageMarkers?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/** Options for {@link extractOcr}. */
|
|
39
|
+
export interface ExtractOcrOptions extends CommonOptions {
|
|
40
|
+
/** override any subset of the OCR-path geometry defaults — see
|
|
41
|
+
* DEFAULT_TUNING (`src/geometry.ts`) for each knob's measured default */
|
|
42
|
+
tuning?: Partial<Tuning>;
|
|
43
|
+
/** page-render resolution for engine recognition (default 288, measured;
|
|
44
|
+
* >~400 measured WORSE on ordinary pages — not a "bigger is safer" knob) */
|
|
45
|
+
dpi?: number;
|
|
46
|
+
/** marks report.ocrFallback — set by pdfToMarkdown's auto-fallback */
|
|
47
|
+
fallback?: boolean;
|
|
48
|
+
/** one event per page as recognition finishes (timing/count only — engine
|
|
49
|
+
* chatter and the sparse-retry notice arrive via `onWarning` instead) */
|
|
50
|
+
onProgress?: (e: OcrProgressEvent) => void;
|
|
51
|
+
/** every recognized word entering the geometry, delivered once after
|
|
52
|
+
* recognition — the severity-1 audit instrument (--debug-words) */
|
|
53
|
+
onWords?: (words: OcrWordInput[]) => void;
|
|
54
|
+
}
|
|
55
|
+
/** Options for {@link pdfToMarkdown}. */
|
|
56
|
+
export interface PdfToMarkdownOptions extends ExtractTextOptions, Omit<ExtractOcrOptions, 'fallback'> {
|
|
57
|
+
/** force the OCR path even when a text layer exists */
|
|
58
|
+
ocr?: boolean;
|
|
59
|
+
/** never fall back to OCR, even for image-only pages */
|
|
60
|
+
noOcr?: boolean;
|
|
61
|
+
}
|
|
62
|
+
export declare function extractText(pdfPath: string, opts?: ExtractTextOptions): Promise<ExtractResult>;
|
|
63
|
+
/**
|
|
64
|
+
* OCR extraction. Input is either a PDF (pages render at `dpi` and the
|
|
65
|
+
* engine recognizes them, imported lazily) or an already-recognized word dump (the offline replay input — see `--words-json`).
|
|
66
|
+
*
|
|
67
|
+
* @param input `{ pdfPath }` to recognize, or `{ words }` to replay a dump
|
|
68
|
+
* @param opts see {@link ExtractOcrOptions}
|
|
69
|
+
*/
|
|
70
|
+
export declare function extractOcr(input: {
|
|
71
|
+
pdfPath: string;
|
|
72
|
+
} | {
|
|
73
|
+
words: OcrWordInput[];
|
|
74
|
+
}, opts?: ExtractOcrOptions): Promise<ExtractResult>;
|
|
75
|
+
/** pdfToMarkdown's auto-OCR fallback predicate, shared with the CLI's
|
|
76
|
+
* --format=raw|txt path so the two call sites can never drift apart. Module-internal: not re-exported by `src/index.ts`. */
|
|
77
|
+
export declare function needsOcrFallback(charsPerPage: number): boolean;
|
|
78
|
+
/** The exact stderr/warnings notice printed when needsOcrFallback fires —
|
|
79
|
+
* one copy of the string for both call sites (asserted verbatim by the suite). */
|
|
80
|
+
export declare function ocrFallbackNotice(charsPerPage: number): string;
|
|
81
|
+
/**
|
|
82
|
+
* The headline API: extract a PDF to markdown, text layer first, falling
|
|
83
|
+
* back to OCR when the text layer is empty enough the pages are certainly images of text (< `AUTO_OCR_CHARS_PER_PAGE` chars/page) — the same decision the CLI makes.
|
|
84
|
+
*
|
|
85
|
+
* @param pdfPath path to the PDF file
|
|
86
|
+
* @param opts see {@link PdfToMarkdownOptions}
|
|
87
|
+
*/
|
|
88
|
+
export declare function pdfToMarkdown(pdfPath: string, opts?: PdfToMarkdownOptions): Promise<ExtractResult>;
|