@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.
Files changed (189) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +157 -0
  3. package/bin/cli.js +9 -0
  4. package/dist/cjs/analyze.d.cts +35 -0
  5. package/dist/cjs/analyze.d.ts +35 -0
  6. package/dist/cjs/analyze.js +620 -0
  7. package/dist/cjs/analyze.js.map +1 -0
  8. package/dist/cjs/audit.d.cts +38 -0
  9. package/dist/cjs/audit.d.ts +38 -0
  10. package/dist/cjs/audit.js +252 -0
  11. package/dist/cjs/audit.js.map +1 -0
  12. package/dist/cjs/cli/audit.d.cts +3 -0
  13. package/dist/cjs/cli/audit.d.ts +3 -0
  14. package/dist/cjs/cli/audit.js +269 -0
  15. package/dist/cjs/cli/audit.js.map +1 -0
  16. package/dist/cjs/cli/extract.d.cts +3 -0
  17. package/dist/cjs/cli/extract.d.ts +3 -0
  18. package/dist/cjs/cli/extract.js +704 -0
  19. package/dist/cjs/cli/extract.js.map +1 -0
  20. package/dist/cjs/cli/index.d.cts +5 -0
  21. package/dist/cjs/cli/index.d.ts +5 -0
  22. package/dist/cjs/cli/index.js +58 -0
  23. package/dist/cjs/cli/index.js.map +1 -0
  24. package/dist/cjs/cli/render.d.cts +3 -0
  25. package/dist/cjs/cli/render.d.ts +3 -0
  26. package/dist/cjs/cli/render.js +361 -0
  27. package/dist/cjs/cli/render.js.map +1 -0
  28. package/dist/cjs/cli/types.d.cts +10 -0
  29. package/dist/cjs/cli/types.d.ts +10 -0
  30. package/dist/cjs/cli/types.js +7 -0
  31. package/dist/cjs/cli/types.js.map +1 -0
  32. package/dist/cjs/cli.d.cts +2 -0
  33. package/dist/cjs/cli.d.ts +2 -0
  34. package/dist/cjs/cli.js +345 -0
  35. package/dist/cjs/cli.js.map +1 -0
  36. package/dist/cjs/collect.d.cts +6 -0
  37. package/dist/cjs/collect.d.ts +6 -0
  38. package/dist/cjs/collect.js +370 -0
  39. package/dist/cjs/collect.js.map +1 -0
  40. package/dist/cjs/dict.d.cts +8 -0
  41. package/dist/cjs/dict.d.ts +8 -0
  42. package/dist/cjs/dict.js +55 -0
  43. package/dist/cjs/dict.js.map +1 -0
  44. package/dist/cjs/emit.d.cts +27 -0
  45. package/dist/cjs/emit.d.ts +27 -0
  46. package/dist/cjs/emit.js +187 -0
  47. package/dist/cjs/emit.js.map +1 -0
  48. package/dist/cjs/engines/tesseract.d.cts +30 -0
  49. package/dist/cjs/engines/tesseract.d.ts +30 -0
  50. package/dist/cjs/engines/tesseract.js +650 -0
  51. package/dist/cjs/engines/tesseract.js.map +1 -0
  52. package/dist/cjs/errors.d.cts +21 -0
  53. package/dist/cjs/errors.d.ts +21 -0
  54. package/dist/cjs/errors.js +124 -0
  55. package/dist/cjs/errors.js.map +1 -0
  56. package/dist/cjs/extract.d.cts +88 -0
  57. package/dist/cjs/extract.d.ts +88 -0
  58. package/dist/cjs/extract.js +436 -0
  59. package/dist/cjs/extract.js.map +1 -0
  60. package/dist/cjs/geometry.d.cts +63 -0
  61. package/dist/cjs/geometry.d.ts +63 -0
  62. package/dist/cjs/geometry.js +521 -0
  63. package/dist/cjs/geometry.js.map +1 -0
  64. package/dist/cjs/index.d.cts +15 -0
  65. package/dist/cjs/index.d.ts +15 -0
  66. package/dist/cjs/index.js +56 -0
  67. package/dist/cjs/index.js.map +1 -0
  68. package/dist/cjs/lines.d.cts +3 -0
  69. package/dist/cjs/lines.d.ts +3 -0
  70. package/dist/cjs/lines.js +181 -0
  71. package/dist/cjs/lines.js.map +1 -0
  72. package/dist/cjs/markdown.d.cts +2 -0
  73. package/dist/cjs/markdown.d.ts +2 -0
  74. package/dist/cjs/markdown.js +222 -0
  75. package/dist/cjs/markdown.js.map +1 -0
  76. package/dist/cjs/package.json +1 -0
  77. package/dist/cjs/page-marker.d.cts +16 -0
  78. package/dist/cjs/page-marker.d.ts +16 -0
  79. package/dist/cjs/page-marker.js +85 -0
  80. package/dist/cjs/page-marker.js.map +1 -0
  81. package/dist/cjs/pdf-open.d.cts +12 -0
  82. package/dist/cjs/pdf-open.d.ts +12 -0
  83. package/dist/cjs/pdf-open.js +257 -0
  84. package/dist/cjs/pdf-open.js.map +1 -0
  85. package/dist/cjs/raster.d.cts +26 -0
  86. package/dist/cjs/raster.d.ts +26 -0
  87. package/dist/cjs/raster.js +262 -0
  88. package/dist/cjs/raster.js.map +1 -0
  89. package/dist/cjs/render-analysis.d.cts +25 -0
  90. package/dist/cjs/render-analysis.d.ts +25 -0
  91. package/dist/cjs/render-analysis.js +319 -0
  92. package/dist/cjs/render-analysis.js.map +1 -0
  93. package/dist/cjs/report.d.cts +12 -0
  94. package/dist/cjs/report.d.ts +12 -0
  95. package/dist/cjs/report.js +392 -0
  96. package/dist/cjs/report.js.map +1 -0
  97. package/dist/cjs/resolve.d.cts +1 -0
  98. package/dist/cjs/resolve.d.ts +1 -0
  99. package/dist/cjs/resolve.js +18 -0
  100. package/dist/cjs/resolve.js.map +1 -0
  101. package/dist/cjs/types.d.cts +171 -0
  102. package/dist/cjs/types.d.ts +171 -0
  103. package/dist/cjs/types.js +9 -0
  104. package/dist/cjs/types.js.map +1 -0
  105. package/dist/cjs/validate.d.cts +14 -0
  106. package/dist/cjs/validate.d.ts +14 -0
  107. package/dist/cjs/validate.js +96 -0
  108. package/dist/cjs/validate.js.map +1 -0
  109. package/dist/esm/analyze.d.ts +35 -0
  110. package/dist/esm/analyze.js +270 -0
  111. package/dist/esm/analyze.js.map +1 -0
  112. package/dist/esm/audit.d.ts +38 -0
  113. package/dist/esm/audit.js +113 -0
  114. package/dist/esm/audit.js.map +1 -0
  115. package/dist/esm/cli/audit.d.ts +3 -0
  116. package/dist/esm/cli/audit.js +20 -0
  117. package/dist/esm/cli/audit.js.map +1 -0
  118. package/dist/esm/cli/extract.d.ts +3 -0
  119. package/dist/esm/cli/extract.js +313 -0
  120. package/dist/esm/cli/extract.js.map +1 -0
  121. package/dist/esm/cli/index.d.ts +5 -0
  122. package/dist/esm/cli/index.js +7 -0
  123. package/dist/esm/cli/index.js.map +1 -0
  124. package/dist/esm/cli/render.d.ts +3 -0
  125. package/dist/esm/cli/render.js +77 -0
  126. package/dist/esm/cli/render.js.map +1 -0
  127. package/dist/esm/cli/types.d.ts +10 -0
  128. package/dist/esm/cli/types.js +3 -0
  129. package/dist/esm/cli/types.js.map +1 -0
  130. package/dist/esm/cli.d.ts +2 -0
  131. package/dist/esm/cli.js +90 -0
  132. package/dist/esm/cli.js.map +1 -0
  133. package/dist/esm/collect.d.ts +6 -0
  134. package/dist/esm/collect.js +80 -0
  135. package/dist/esm/collect.js.map +1 -0
  136. package/dist/esm/dict.d.ts +8 -0
  137. package/dist/esm/dict.js +28 -0
  138. package/dist/esm/dict.js.map +1 -0
  139. package/dist/esm/emit.d.ts +27 -0
  140. package/dist/esm/emit.js +131 -0
  141. package/dist/esm/emit.js.map +1 -0
  142. package/dist/esm/engines/tesseract.d.ts +30 -0
  143. package/dist/esm/engines/tesseract.js +190 -0
  144. package/dist/esm/engines/tesseract.js.map +1 -0
  145. package/dist/esm/errors.d.ts +21 -0
  146. package/dist/esm/errors.js +9 -0
  147. package/dist/esm/errors.js.map +1 -0
  148. package/dist/esm/extract.d.ts +88 -0
  149. package/dist/esm/extract.js +147 -0
  150. package/dist/esm/extract.js.map +1 -0
  151. package/dist/esm/geometry.d.ts +63 -0
  152. package/dist/esm/geometry.js +279 -0
  153. package/dist/esm/geometry.js.map +1 -0
  154. package/dist/esm/index.d.ts +15 -0
  155. package/dist/esm/index.js +10 -0
  156. package/dist/esm/index.js.map +1 -0
  157. package/dist/esm/lines.d.ts +3 -0
  158. package/dist/esm/lines.js +67 -0
  159. package/dist/esm/lines.js.map +1 -0
  160. package/dist/esm/markdown.d.ts +2 -0
  161. package/dist/esm/markdown.js +153 -0
  162. package/dist/esm/markdown.js.map +1 -0
  163. package/dist/esm/package.json +1 -0
  164. package/dist/esm/page-marker.d.ts +16 -0
  165. package/dist/esm/page-marker.js +40 -0
  166. package/dist/esm/page-marker.js.map +1 -0
  167. package/dist/esm/pdf-open.d.ts +12 -0
  168. package/dist/esm/pdf-open.js +61 -0
  169. package/dist/esm/pdf-open.js.map +1 -0
  170. package/dist/esm/raster.d.ts +26 -0
  171. package/dist/esm/raster.js +60 -0
  172. package/dist/esm/raster.js.map +1 -0
  173. package/dist/esm/render-analysis.d.ts +25 -0
  174. package/dist/esm/render-analysis.js +115 -0
  175. package/dist/esm/render-analysis.js.map +1 -0
  176. package/dist/esm/report.d.ts +12 -0
  177. package/dist/esm/report.js +182 -0
  178. package/dist/esm/report.js.map +1 -0
  179. package/dist/esm/resolve.d.ts +1 -0
  180. package/dist/esm/resolve.js +7 -0
  181. package/dist/esm/resolve.js.map +1 -0
  182. package/dist/esm/types.d.ts +171 -0
  183. package/dist/esm/types.js +6 -0
  184. package/dist/esm/types.js.map +1 -0
  185. package/dist/esm/validate.d.ts +14 -0
  186. package/dist/esm/validate.js +56 -0
  187. package/dist/esm/validate.js.map +1 -0
  188. package/package.json +67 -0
  189. package/skills/pdf-to-md/SKILL.md +102 -0
@@ -0,0 +1,436 @@
1
+ // The library layer for PDF -> markdown conversion: extractText/extractOcr/
2
+ // pdfToMarkdown each take plain args, return plain data, and throw PdfToMdError — no process.exit, no console.*, no file writing.
3
+ //
4
+ // Warnings are DATA: every notice is returned in `warnings` verbatim and also
5
+ // delivered incrementally via `onWarning` — the exact strings are load-bearing (the test suite asserts them).
6
+ "use strict";
7
+ Object.defineProperty(exports, "__esModule", {
8
+ value: true
9
+ });
10
+ function _export(target, all) {
11
+ for(var name in all)Object.defineProperty(target, name, {
12
+ enumerable: true,
13
+ get: Object.getOwnPropertyDescriptor(all, name).get
14
+ });
15
+ }
16
+ _export(exports, {
17
+ get extractOcr () {
18
+ return extractOcr;
19
+ },
20
+ get extractText () {
21
+ return extractText;
22
+ },
23
+ get needsOcrFallback () {
24
+ return needsOcrFallback;
25
+ },
26
+ get ocrFallbackNotice () {
27
+ return ocrFallbackNotice;
28
+ },
29
+ get pdfToMarkdown () {
30
+ return pdfToMarkdown;
31
+ }
32
+ });
33
+ var _analyzets = require("./analyze.js");
34
+ var _collectts = require("./collect.js");
35
+ var _dictts = require("./dict.js");
36
+ var _linests = require("./lines.js");
37
+ var _markdownts = require("./markdown.js");
38
+ var _renderanalysists = require("./render-analysis.js");
39
+ var _reportts = require("./report.js");
40
+ var _validatets = require("./validate.js");
41
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
42
+ try {
43
+ var info = gen[key](arg);
44
+ var value = info.value;
45
+ } catch (error) {
46
+ reject(error);
47
+ return;
48
+ }
49
+ if (info.done) resolve(value);
50
+ else Promise.resolve(value).then(_next, _throw);
51
+ }
52
+ function _async_to_generator(fn) {
53
+ return function() {
54
+ var self = this, args = arguments;
55
+ return new Promise(function(resolve, reject) {
56
+ var gen = fn.apply(self, args);
57
+ function _next(value) {
58
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
59
+ }
60
+ function _throw(err) {
61
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
62
+ }
63
+ _next(undefined);
64
+ });
65
+ };
66
+ }
67
+ function _define_property(obj, key, value) {
68
+ if (key in obj) {
69
+ Object.defineProperty(obj, key, {
70
+ value: value,
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true
74
+ });
75
+ } else obj[key] = value;
76
+ return obj;
77
+ }
78
+ function _object_spread(target) {
79
+ for(var i = 1; i < arguments.length; i++){
80
+ var source = arguments[i] != null ? arguments[i] : {};
81
+ var ownKeys = Object.keys(source);
82
+ if (typeof Object.getOwnPropertySymbols === "function") {
83
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
84
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
85
+ }));
86
+ }
87
+ ownKeys.forEach(function(key) {
88
+ _define_property(target, key, source[key]);
89
+ });
90
+ }
91
+ return target;
92
+ }
93
+ function ownKeys(object, enumerableOnly) {
94
+ var keys = Object.keys(object);
95
+ if (Object.getOwnPropertySymbols) {
96
+ var symbols = Object.getOwnPropertySymbols(object);
97
+ if (enumerableOnly) {
98
+ symbols = symbols.filter(function(sym) {
99
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
100
+ });
101
+ }
102
+ keys.push.apply(keys, symbols);
103
+ }
104
+ return keys;
105
+ }
106
+ function _object_spread_props(target, source) {
107
+ source = source != null ? source : {};
108
+ if (Object.getOwnPropertyDescriptors) Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
109
+ else {
110
+ ownKeys(Object(source)).forEach(function(key) {
111
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
112
+ });
113
+ }
114
+ return target;
115
+ }
116
+ function _ts_generator(thisArg, body) {
117
+ var f, y, t, _ = {
118
+ label: 0,
119
+ sent: function() {
120
+ if (t[0] & 1) throw t[1];
121
+ return t[1];
122
+ },
123
+ trys: [],
124
+ ops: []
125
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
126
+ return d(g, "next", {
127
+ value: verb(0)
128
+ }), d(g, "throw", {
129
+ value: verb(1)
130
+ }), d(g, "return", {
131
+ value: verb(2)
132
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
133
+ value: function() {
134
+ return this;
135
+ }
136
+ }), g;
137
+ function verb(n) {
138
+ return function(v) {
139
+ return step([
140
+ n,
141
+ v
142
+ ]);
143
+ };
144
+ }
145
+ function step(op) {
146
+ if (f) throw new TypeError("Generator is already executing.");
147
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
148
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
149
+ if (y = 0, t) op = [
150
+ op[0] & 2,
151
+ t.value
152
+ ];
153
+ switch(op[0]){
154
+ case 0:
155
+ case 1:
156
+ t = op;
157
+ break;
158
+ case 4:
159
+ _.label++;
160
+ return {
161
+ value: op[1],
162
+ done: false
163
+ };
164
+ case 5:
165
+ _.label++;
166
+ y = op[1];
167
+ op = [
168
+ 0
169
+ ];
170
+ continue;
171
+ case 7:
172
+ op = _.ops.pop();
173
+ _.trys.pop();
174
+ continue;
175
+ default:
176
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
177
+ _ = 0;
178
+ continue;
179
+ }
180
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
181
+ _.label = op[1];
182
+ break;
183
+ }
184
+ if (op[0] === 6 && _.label < t[1]) {
185
+ _.label = t[1];
186
+ t = op;
187
+ break;
188
+ }
189
+ if (t && _.label < t[2]) {
190
+ _.label = t[2];
191
+ _.ops.push(op);
192
+ break;
193
+ }
194
+ if (t[2]) _.ops.pop();
195
+ _.trys.pop();
196
+ continue;
197
+ }
198
+ op = body.call(thisArg, _);
199
+ } catch (e) {
200
+ op = [
201
+ 6,
202
+ e
203
+ ];
204
+ y = 0;
205
+ } finally{
206
+ f = t = 0;
207
+ }
208
+ if (op[0] & 5) throw op[1];
209
+ return {
210
+ value: op[0] ? op[1] : void 0,
211
+ done: true
212
+ };
213
+ }
214
+ }
215
+ /**
216
+ * Text-layer extraction. Returns markdown + QA report + warnings (hybrid
217
+ * pages carrying large images whose baked-in text the text layer can never see). Makes no OCR decision — see pdfToMarkdown for the fallback.
218
+ *
219
+ * @param pdfPath path to the PDF file
220
+ * @param opts see {@link ExtractTextOptions}
221
+ */ // One collector per entry point: collects into `warnings`, streams via
222
+ // onWarning, and emits the dictionary-unavailable notice exactly once up front.
223
+ function warnCollector(onWarning) {
224
+ var warnings = [];
225
+ var warn = function warn(m) {
226
+ warnings.push(m);
227
+ onWarning === null || onWarning === void 0 ? void 0 : onWarning(m);
228
+ };
229
+ if (!(0, _dictts.dictAvailable)()) warn((0, _dictts.dictUnavailableWarning)());
230
+ return {
231
+ warnings: warnings,
232
+ warn: warn
233
+ };
234
+ }
235
+ function extractText(_0) {
236
+ return _async_to_generator(function(pdfPath) {
237
+ var opts, _opts_pages_last, _opts_pageMarkers, _warnCollector, warnings, warn, range, _ref, numPages, pages, heightChars, pageLines, pageMarkers, _buildMarkdown, md, stats, bodyH, headingSizes, statsMd, hybridPages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, n, report;
238
+ var _arguments = arguments;
239
+ return _ts_generator(this, function(_state) {
240
+ switch(_state.label){
241
+ case 0:
242
+ opts = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
243
+ (0, _validatets.validatePages)(opts.pages);
244
+ _warnCollector = warnCollector(opts.onWarning), warnings = _warnCollector.warnings, warn = _warnCollector.warn;
245
+ range = opts.pages ? {
246
+ first: opts.pages.first,
247
+ last: (_opts_pages_last = opts.pages.last) !== null && _opts_pages_last !== void 0 ? _opts_pages_last : opts.pages.first
248
+ } : {};
249
+ return [
250
+ 4,
251
+ (0, _collectts.collect)(pdfPath, range)
252
+ ];
253
+ case 1:
254
+ _ref = _state.sent(), numPages = _ref.numPages, pages = _ref.pages, heightChars = _ref.heightChars;
255
+ pageLines = pages.map(function(p) {
256
+ return {
257
+ page: p.page,
258
+ lines: (0, _linests.toLines)(p)
259
+ };
260
+ });
261
+ pageMarkers = (_opts_pageMarkers = opts.pageMarkers) !== null && _opts_pageMarkers !== void 0 ? _opts_pageMarkers : false;
262
+ _buildMarkdown = (0, _markdownts.buildMarkdown)(pageLines, heightChars, {
263
+ pageMarkers: pageMarkers
264
+ }), md = _buildMarkdown.md, stats = _buildMarkdown.stats, bodyH = _buildMarkdown.bodyH, headingSizes = _buildMarkdown.headingSizes;
265
+ // pageStats needs "### pN" markers to segment content; when the caller
266
+ // didn't request them, build a throwaway marked variant (same heightChars) purely to measure stats from, so pageStats is always populated.
267
+ statsMd = pageMarkers ? md : (0, _markdownts.buildMarkdown)(pageLines, heightChars, {
268
+ pageMarkers: true
269
+ }).md;
270
+ // Advisory, not prescriptive: `hasLargeImage` fires on any large image,
271
+ // even one with no baked-in text, where `--ocr` recovers nothing and can
272
+ // actively degrade a correct result (measured: "I" -> "|", stray em-dashes) — so this tells the caller to COMPARE the OCR rerun, never blindly replace.
273
+ hybridPages = pages.filter(function(p) {
274
+ return p.largeImage;
275
+ }).map(function(p) {
276
+ return p.page;
277
+ });
278
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
279
+ try {
280
+ for(_iterator = hybridPages[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
281
+ n = _step.value;
282
+ warn("p".concat(n, ": text layer used but page carries large image(s); if it has baked-in text this output won't have it — compare (don't blindly replace) against --pages ").concat(n, " --ocr, which can itself introduce errors"));
283
+ }
284
+ } catch (err) {
285
+ _didIteratorError = true;
286
+ _iteratorError = err;
287
+ } finally{
288
+ try {
289
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
290
+ _iterator.return();
291
+ }
292
+ } finally{
293
+ if (_didIteratorError) {
294
+ throw _iteratorError;
295
+ }
296
+ }
297
+ }
298
+ report = (0, _reportts.buildReport)({
299
+ numPages: numPages,
300
+ md: md,
301
+ statsMd: statsMd,
302
+ stats: stats,
303
+ bodyH: bodyH,
304
+ headingSizes: headingSizes,
305
+ hybridPages: hybridPages
306
+ });
307
+ return [
308
+ 2,
309
+ {
310
+ markdown: md,
311
+ report: report,
312
+ warnings: warnings
313
+ }
314
+ ];
315
+ }
316
+ });
317
+ }).apply(this, arguments);
318
+ }
319
+ function extractOcr(_0) {
320
+ return _async_to_generator(function(input) {
321
+ var opts, _warnCollector, warnings, warn, _ref, pages, words, md, report;
322
+ var _arguments = arguments;
323
+ return _ts_generator(this, function(_state) {
324
+ switch(_state.label){
325
+ case 0:
326
+ opts = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
327
+ (0, _validatets.validatePages)(opts.pages);
328
+ _warnCollector = warnCollector(opts.onWarning), warnings = _warnCollector.warnings, warn = _warnCollector.warn;
329
+ return [
330
+ 4,
331
+ (0, _analyzets.buildOcrAnalysisPages)(input, opts, warn)
332
+ ];
333
+ case 1:
334
+ _ref = _state.sent(), pages = _ref.pages, words = _ref.words;
335
+ md = (0, _renderanalysists.renderPagesToMarkdown)(pages, {
336
+ forcePageMarkers: true,
337
+ pageMarkers: true
338
+ });
339
+ report = (0, _reportts.buildOcrReport)({
340
+ md: md,
341
+ fallback: opts.fallback,
342
+ // Confidence is on `words` — see report.ts's low-confidence detector for
343
+ // how it handles a confidence-less replay honestly.
344
+ words: words
345
+ });
346
+ return [
347
+ 2,
348
+ {
349
+ markdown: md,
350
+ report: report,
351
+ warnings: warnings
352
+ }
353
+ ];
354
+ }
355
+ });
356
+ }).apply(this, arguments);
357
+ }
358
+ function needsOcrFallback(charsPerPage) {
359
+ return charsPerPage < _reportts.AUTO_OCR_CHARS_PER_PAGE;
360
+ }
361
+ function ocrFallbackNotice(charsPerPage) {
362
+ return "text layer yields ~".concat(charsPerPage, " chars/page (< ").concat(_reportts.AUTO_OCR_CHARS_PER_PAGE, ") — pages are images of text; falling back to OCR (suppress with --no-ocr)");
363
+ }
364
+ function pdfToMarkdown(_0) {
365
+ return _async_to_generator(function(pdfPath) {
366
+ var opts, _opts_onWarning, text, _opts_onWarning1, notice, ocr, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, w;
367
+ var _arguments = arguments;
368
+ return _ts_generator(this, function(_state) {
369
+ switch(_state.label){
370
+ case 0:
371
+ opts = _arguments.length > 1 && _arguments[1] !== void 0 ? _arguments[1] : {};
372
+ if (opts.ocr) return [
373
+ 2,
374
+ extractOcr({
375
+ pdfPath: pdfPath
376
+ }, opts)
377
+ ];
378
+ return [
379
+ 4,
380
+ extractText(pdfPath, _object_spread_props(_object_spread({}, opts), {
381
+ onWarning: undefined
382
+ }))
383
+ ];
384
+ case 1:
385
+ text = _state.sent();
386
+ if (!(needsOcrFallback(text.report.charsPerPage) && !opts.noOcr)) return [
387
+ 3,
388
+ 3
389
+ ];
390
+ notice = ocrFallbackNotice(text.report.charsPerPage);
391
+ (_opts_onWarning1 = opts.onWarning) === null || _opts_onWarning1 === void 0 ? void 0 : _opts_onWarning1.call(opts, notice);
392
+ return [
393
+ 4,
394
+ extractOcr({
395
+ pdfPath: pdfPath
396
+ }, _object_spread_props(_object_spread({}, opts), {
397
+ fallback: true
398
+ }))
399
+ ];
400
+ case 2:
401
+ ocr = _state.sent();
402
+ ocr.warnings.unshift(notice);
403
+ return [
404
+ 2,
405
+ ocr
406
+ ];
407
+ case 3:
408
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
409
+ try {
410
+ for(_iterator = text.warnings[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
411
+ w = _step.value;
412
+ (_opts_onWarning = opts.onWarning) === null || _opts_onWarning === void 0 ? void 0 : _opts_onWarning.call(opts, w);
413
+ }
414
+ } catch (err) {
415
+ _didIteratorError = true;
416
+ _iteratorError = err;
417
+ } finally{
418
+ try {
419
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
420
+ _iterator.return();
421
+ }
422
+ } finally{
423
+ if (_didIteratorError) {
424
+ throw _iteratorError;
425
+ }
426
+ }
427
+ }
428
+ return [
429
+ 2,
430
+ text
431
+ ];
432
+ }
433
+ });
434
+ }).apply(this, arguments);
435
+ }
436
+ /* 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/extract.ts"],"sourcesContent":["// The library layer for PDF -> markdown conversion: extractText/extractOcr/\n// pdfToMarkdown each take plain args, return plain data, and throw PdfToMdError — no process.exit, no console.*, no file writing.\n//\n// Warnings are DATA: every notice is returned in `warnings` verbatim and also\n// delivered incrementally via `onWarning` — the exact strings are load-bearing (the test suite asserts them).\nimport { buildOcrAnalysisPages } from './analyze.ts';\nimport { collect } from './collect.ts';\nimport { dictAvailable, dictUnavailableWarning } from './dict.ts';\nimport type { OcrProgressEvent } from './engines/tesseract.ts';\nimport type { PageWord, Tuning } from './geometry.ts';\nimport { toLines } from './lines.ts';\nimport { buildMarkdown } from './markdown.ts';\nimport { renderPagesToMarkdown } from './render-analysis.ts';\nimport { AUTO_OCR_CHARS_PER_PAGE, buildOcrReport, buildReport } from './report.ts';\nimport type { PageLines, Report } from './types.ts';\nimport { validatePages } from './validate.ts';\n\nexport type { OcrProgressEvent } from './engines/tesseract.ts';\n\n// A plain-JS caller (or `.ts` with `as any`) gets no compile-time check on\n// `pages`, so it's validated once at runtime — see src/validate.ts's `validatePages`.\n\n/** One recognized word with its page — the OCR replay input contract (same\n * shape as `geometry.ts`'s `PageWord`, re-exported under this module's public name). */\nexport type OcrWordInput = PageWord;\n\n/** A 1-based, inclusive page range — the shape every entry point's `pages`\n * option takes (the CLI's own `--pages N[-M]` parses into this). */\nexport interface PageSelection {\n first: number;\n /** inclusive; defaults to `first` */\n last?: number;\n}\n\n/** extractText/extractOcr/pdfToMarkdown's common return shape. */\nexport interface ExtractResult {\n markdown: string;\n /** per-document and per-page QA metrics — see `Report` (`src/types.ts`) */\n report: Report;\n /** every notice also delivered incrementally via `onWarning`, collected\n * here in full (hybrid pages, auto-OCR-fallback, empty OCR pages, sparse-retry) */\n warnings: string[];\n}\n\ninterface CommonOptions {\n /** convert only this page range (1-based, inclusive); defaults to the\n * whole document */\n pages?: PageSelection;\n /** delivered as each warning happens (also returned, in full, as\n * `warnings` on the resolved result) */\n onWarning?: (message: string) => void;\n}\n\n/** Options for {@link extractText}. */\nexport interface ExtractTextOptions extends CommonOptions {\n /** emit a `### pN` marker before each page's content (output formatting\n * only — `report.pageStats` is always populated regardless) */\n pageMarkers?: boolean;\n}\n\n/** Options for {@link extractOcr}. */\nexport interface ExtractOcrOptions extends CommonOptions {\n /** override any subset of the OCR-path geometry defaults — see\n * DEFAULT_TUNING (`src/geometry.ts`) for each knob's measured default */\n tuning?: Partial<Tuning>;\n /** page-render resolution for engine recognition (default 288, measured;\n * >~400 measured WORSE on ordinary pages — not a \"bigger is safer\" knob) */\n dpi?: number;\n /** marks report.ocrFallback — set by pdfToMarkdown's auto-fallback */\n fallback?: boolean;\n /** one event per page as recognition finishes (timing/count only — engine\n * chatter and the sparse-retry notice arrive via `onWarning` instead) */\n onProgress?: (e: OcrProgressEvent) => void;\n /** every recognized word entering the geometry, delivered once after\n * recognition — the severity-1 audit instrument (--debug-words) */\n onWords?: (words: OcrWordInput[]) => void;\n}\n\n/** Options for {@link pdfToMarkdown}. */\nexport interface PdfToMarkdownOptions extends ExtractTextOptions, Omit<ExtractOcrOptions, 'fallback'> {\n /** force the OCR path even when a text layer exists */\n ocr?: boolean;\n /** never fall back to OCR, even for image-only pages */\n noOcr?: boolean;\n}\n\n/**\n * Text-layer extraction. Returns markdown + QA report + warnings (hybrid\n * pages carrying large images whose baked-in text the text layer can never see). Makes no OCR decision — see pdfToMarkdown for the fallback.\n *\n * @param pdfPath path to the PDF file\n * @param opts see {@link ExtractTextOptions}\n */\n// One collector per entry point: collects into `warnings`, streams via\n// onWarning, and emits the dictionary-unavailable notice exactly once up front.\nfunction warnCollector(onWarning?: (message: string) => void): { warnings: string[]; warn: (m: string) => void } {\n const warnings: string[] = [];\n const warn = (m: string) => {\n warnings.push(m);\n onWarning?.(m);\n };\n if (!dictAvailable()) warn(dictUnavailableWarning());\n return { warnings, warn };\n}\n\nexport async function extractText(pdfPath: string, opts: ExtractTextOptions = {}): Promise<ExtractResult> {\n validatePages(opts.pages);\n const { warnings, warn } = warnCollector(opts.onWarning);\n\n const range = opts.pages ? { first: opts.pages.first, last: opts.pages.last ?? opts.pages.first } : {};\n const { numPages, pages, heightChars } = await collect(pdfPath, range);\n const pageLines: PageLines[] = pages.map((p) => ({\n page: p.page,\n lines: toLines(p),\n }));\n const pageMarkers = opts.pageMarkers ?? false;\n const { md, stats, bodyH, headingSizes } = buildMarkdown(pageLines, heightChars, { pageMarkers });\n // pageStats needs \"### pN\" markers to segment content; when the caller\n // didn't request them, build a throwaway marked variant (same heightChars) purely to measure stats from, so pageStats is always populated.\n const statsMd = pageMarkers ? md : buildMarkdown(pageLines, heightChars, { pageMarkers: true }).md;\n\n // Advisory, not prescriptive: `hasLargeImage` fires on any large image,\n // even one with no baked-in text, where `--ocr` recovers nothing and can\n // actively degrade a correct result (measured: \"I\" -> \"|\", stray em-dashes) — so this tells the caller to COMPARE the OCR rerun, never blindly replace.\n const hybridPages = pages.filter((p) => p.largeImage).map((p) => p.page);\n for (const n of hybridPages) {\n warn(`p${n}: text layer used but page carries large image(s); if it has baked-in text this output won't have it — compare (don't blindly replace) against --pages ${n} --ocr, which can itself introduce errors`);\n }\n\n const report = buildReport({\n numPages,\n md,\n statsMd,\n stats,\n bodyH,\n headingSizes,\n hybridPages,\n });\n return { markdown: md, report, warnings };\n}\n\n/**\n * OCR extraction. Input is either a PDF (pages render at `dpi` and the\n * engine recognizes them, imported lazily) or an already-recognized word dump (the offline replay input — see `--words-json`).\n *\n * @param input `{ pdfPath }` to recognize, or `{ words }` to replay a dump\n * @param opts see {@link ExtractOcrOptions}\n */\nexport async function extractOcr(input: { pdfPath: string } | { words: OcrWordInput[] }, opts: ExtractOcrOptions = {}): Promise<ExtractResult> {\n validatePages(opts.pages);\n const { warnings, warn } = warnCollector(opts.onWarning);\n\n // Shared with analyze(pdf, {path:'ocr'}) — see that module's header for\n // why this makes the two byte-identical by construction, not by hand.\n const { pages, words } = await buildOcrAnalysisPages(input, opts, warn);\n const md = renderPagesToMarkdown(pages, { forcePageMarkers: true, pageMarkers: true });\n\n const report = buildOcrReport({\n md,\n fallback: opts.fallback,\n // Confidence is on `words` — see report.ts's low-confidence detector for\n // how it handles a confidence-less replay honestly.\n words,\n });\n return { markdown: md, report, warnings };\n}\n\n/** pdfToMarkdown's auto-OCR fallback predicate, shared with the CLI's\n * --format=raw|txt path so the two call sites can never drift apart. Module-internal: not re-exported by `src/index.ts`. */\nexport function needsOcrFallback(charsPerPage: number): boolean {\n return charsPerPage < AUTO_OCR_CHARS_PER_PAGE;\n}\n\n/** The exact stderr/warnings notice printed when needsOcrFallback fires —\n * one copy of the string for both call sites (asserted verbatim by the suite). */\nexport function ocrFallbackNotice(charsPerPage: number): string {\n return `text layer yields ~${charsPerPage} chars/page (< ${AUTO_OCR_CHARS_PER_PAGE}) — pages are images of text; falling back to OCR (suppress with --no-ocr)`;\n}\n\n/**\n * The headline API: extract a PDF to markdown, text layer first, falling\n * 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.\n *\n * @param pdfPath path to the PDF file\n * @param opts see {@link PdfToMarkdownOptions}\n */\nexport async function pdfToMarkdown(pdfPath: string, opts: PdfToMarkdownOptions = {}): Promise<ExtractResult> {\n if (opts.ocr) return extractOcr({ pdfPath }, opts);\n\n // Warning DELIVERY is deferred on the text pass: if the fallback fires,\n // those hybrid warnings describe output being discarded, so only a kept text result replays them.\n const text = await extractText(pdfPath, { ...opts, onWarning: undefined });\n if (needsOcrFallback(text.report.charsPerPage) && !opts.noOcr) {\n const notice = ocrFallbackNotice(text.report.charsPerPage);\n opts.onWarning?.(notice);\n const ocr = await extractOcr({ pdfPath }, { ...opts, fallback: true });\n ocr.warnings.unshift(notice);\n return ocr;\n }\n for (const w of text.warnings) opts.onWarning?.(w);\n return text;\n}\n"],"names":["extractOcr","extractText","needsOcrFallback","ocrFallbackNotice","pdfToMarkdown","warnCollector","onWarning","warnings","warn","m","push","dictAvailable","dictUnavailableWarning","pdfPath","opts","range","numPages","pages","heightChars","pageLines","pageMarkers","buildMarkdown","md","stats","bodyH","headingSizes","statsMd","hybridPages","n","report","validatePages","first","last","collect","map","p","page","lines","toLines","filter","largeImage","buildReport","markdown","input","words","buildOcrAnalysisPages","renderPagesToMarkdown","forcePageMarkers","buildOcrReport","fallback","charsPerPage","AUTO_OCR_CHARS_PER_PAGE","text","notice","ocr","w","undefined","noOcr","unshift"],"mappings":"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"}
@@ -0,0 +1,63 @@
1
+ export interface Word {
2
+ text: string;
3
+ x: number;
4
+ y: number;
5
+ w: number;
6
+ h: number;
7
+ confidence?: number;
8
+ }
9
+ /** A `Word` with its page number — the shared shape behind both the OCR
10
+ * engine's own output (tesseract.ts) and the replay input contract (extract.ts's OcrWordInput). */
11
+ export interface PageWord extends Word {
12
+ page: number;
13
+ }
14
+ export interface OcrLine {
15
+ text: string;
16
+ x: number;
17
+ width: number;
18
+ midY: number;
19
+ h: number;
20
+ col: number;
21
+ words: Word[];
22
+ }
23
+ export interface PageOCR {
24
+ lines: OcrLine[];
25
+ floats: OcrLine[];
26
+ pitch: number;
27
+ }
28
+ /** Tuning knobs for the OCR-path geometry pass (CLI: `--float-*`/`--para-gap`/
29
+ * `--heading-scale`/`--col-*`/`--line-*`). See DEFAULT_TUNING for defaults. */
30
+ export interface Tuning {
31
+ /** fragments with more words than this are body text, not decoration */
32
+ floatMaxWords: number;
33
+ /** fraction of page width above which a fragment is too wide to be
34
+ * decoration */
35
+ floatMaxWidth: number;
36
+ /** how far a fragment's left edge must sit from its column edge to be
37
+ * decoration */
38
+ floatMargin: number;
39
+ /** a vertical gap this many times the line pitch is a paragraph break */
40
+ paraGap: number;
41
+ /** lines taller than this times body height are headings */
42
+ headingScale: number;
43
+ /** boxes wider than this are excluded from gutter detection */
44
+ colMaxWidth: number;
45
+ /** fractional x of the two-column midpoint */
46
+ colSplit: number;
47
+ /** vertical distance within which two boxes are the same line */
48
+ lineYTol: number;
49
+ /** same-line boxes with a larger height ratio don't merge */
50
+ lineHeightRatio: number;
51
+ }
52
+ /** Measured defaults; derivation in `test/unit/tuning.test.ts`. lineHeightRatio
53
+ * 2.4 = pair-F1 plateau edge (0.924@1.6 -> 0.995@2.4, 9,471 pairs, 2026-08-16). */
54
+ export declare const DEFAULT_TUNING: Tuning;
55
+ export declare const MAX_GUTTER_INK_FRAC = 0.03;
56
+ export declare const MIN_COL_WORDS = 10;
57
+ export declare const MIN_COL_SHARE = 0.2;
58
+ export declare function buildPageOCR(words: Word[], tuning: Tuning): PageOCR;
59
+ export declare function bucket(h: number): number;
60
+ export declare function headingLevels(pages: PageOCR[], tuning: Tuning): {
61
+ bodyH: number;
62
+ levelOf: Map<number, number>;
63
+ };
@@ -0,0 +1,63 @@
1
+ export interface Word {
2
+ text: string;
3
+ x: number;
4
+ y: number;
5
+ w: number;
6
+ h: number;
7
+ confidence?: number;
8
+ }
9
+ /** A `Word` with its page number — the shared shape behind both the OCR
10
+ * engine's own output (tesseract.ts) and the replay input contract (extract.ts's OcrWordInput). */
11
+ export interface PageWord extends Word {
12
+ page: number;
13
+ }
14
+ export interface OcrLine {
15
+ text: string;
16
+ x: number;
17
+ width: number;
18
+ midY: number;
19
+ h: number;
20
+ col: number;
21
+ words: Word[];
22
+ }
23
+ export interface PageOCR {
24
+ lines: OcrLine[];
25
+ floats: OcrLine[];
26
+ pitch: number;
27
+ }
28
+ /** Tuning knobs for the OCR-path geometry pass (CLI: `--float-*`/`--para-gap`/
29
+ * `--heading-scale`/`--col-*`/`--line-*`). See DEFAULT_TUNING for defaults. */
30
+ export interface Tuning {
31
+ /** fragments with more words than this are body text, not decoration */
32
+ floatMaxWords: number;
33
+ /** fraction of page width above which a fragment is too wide to be
34
+ * decoration */
35
+ floatMaxWidth: number;
36
+ /** how far a fragment's left edge must sit from its column edge to be
37
+ * decoration */
38
+ floatMargin: number;
39
+ /** a vertical gap this many times the line pitch is a paragraph break */
40
+ paraGap: number;
41
+ /** lines taller than this times body height are headings */
42
+ headingScale: number;
43
+ /** boxes wider than this are excluded from gutter detection */
44
+ colMaxWidth: number;
45
+ /** fractional x of the two-column midpoint */
46
+ colSplit: number;
47
+ /** vertical distance within which two boxes are the same line */
48
+ lineYTol: number;
49
+ /** same-line boxes with a larger height ratio don't merge */
50
+ lineHeightRatio: number;
51
+ }
52
+ /** Measured defaults; derivation in `test/unit/tuning.test.ts`. lineHeightRatio
53
+ * 2.4 = pair-F1 plateau edge (0.924@1.6 -> 0.995@2.4, 9,471 pairs, 2026-08-16). */
54
+ export declare const DEFAULT_TUNING: Tuning;
55
+ export declare const MAX_GUTTER_INK_FRAC = 0.03;
56
+ export declare const MIN_COL_WORDS = 10;
57
+ export declare const MIN_COL_SHARE = 0.2;
58
+ export declare function buildPageOCR(words: Word[], tuning: Tuning): PageOCR;
59
+ export declare function bucket(h: number): number;
60
+ export declare function headingLevels(pages: PageOCR[], tuning: Tuning): {
61
+ bodyH: number;
62
+ levelOf: Map<number, number>;
63
+ };