@bendyline/squisq-formats 1.2.3 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/dist/chunk-33YRFXZZ.js +1187 -0
  2. package/dist/chunk-33YRFXZZ.js.map +1 -0
  3. package/dist/{chunk-EUQQYBZZ.js → chunk-ERZ627GR.js} +4 -4
  4. package/dist/chunk-ERZ627GR.js.map +1 -0
  5. package/dist/{chunk-MEZF76JA.js → chunk-UDS45KUJ.js} +68 -25
  6. package/dist/chunk-UDS45KUJ.js.map +1 -0
  7. package/dist/{chunk-NGWHV77G.js → chunk-VN2KEOYB.js} +7 -6
  8. package/dist/chunk-VN2KEOYB.js.map +1 -0
  9. package/dist/container/index.d.ts +7 -5
  10. package/dist/container/index.js +39 -70
  11. package/dist/container/index.js.map +1 -1
  12. package/dist/docx/index.d.ts +128 -15
  13. package/dist/docx/index.js +17 -22
  14. package/dist/docx/index.js.map +1 -1
  15. package/dist/epub/index.d.ts +66 -11
  16. package/dist/epub/index.js +10 -18
  17. package/dist/epub/index.js.map +1 -1
  18. package/dist/html/index.d.ts +278 -9
  19. package/dist/html/index.js +26 -137
  20. package/dist/html/index.js.map +1 -1
  21. package/dist/index.d.ts +9 -38
  22. package/dist/index.js +62 -37
  23. package/dist/index.js.map +1 -1
  24. package/dist/ooxml/index.d.ts +353 -14
  25. package/dist/ooxml/index.js +116 -22
  26. package/dist/ooxml/index.js.map +1 -1
  27. package/dist/pdf/index.d.ts +143 -17
  28. package/dist/pdf/index.js +16 -25
  29. package/dist/pdf/index.js.map +1 -1
  30. package/dist/pptx/index.d.ts +83 -8
  31. package/dist/pptx/index.js +14 -34
  32. package/dist/pptx/index.js.map +1 -1
  33. package/dist/xlsx/index.d.ts +12 -9
  34. package/dist/xlsx/index.js +12 -44
  35. package/dist/xlsx/index.js.map +1 -1
  36. package/package.json +3 -2
  37. package/src/__tests__/html.test.ts +82 -4
  38. package/src/__tests__/pdfExport.test.ts +1 -1
  39. package/src/__tests__/plainHtml.test.ts +372 -0
  40. package/src/__tests__/plainHtmlBundle.test.ts +235 -0
  41. package/src/docx/export.ts +163 -22
  42. package/src/epub/export.ts +3 -3
  43. package/src/html/docsHtmlBundle.ts +369 -0
  44. package/src/html/htmlTemplate.ts +40 -0
  45. package/src/html/index.ts +32 -3
  46. package/src/html/plainHtml.ts +736 -0
  47. package/src/html/plainHtmlBundle.ts +419 -0
  48. package/src/pptx/export.ts +10 -9
  49. package/dist/__tests__/container.test.d.ts +0 -8
  50. package/dist/__tests__/container.test.d.ts.map +0 -1
  51. package/dist/__tests__/container.test.js +0 -147
  52. package/dist/__tests__/container.test.js.map +0 -1
  53. package/dist/__tests__/docxExport.test.d.ts +0 -8
  54. package/dist/__tests__/docxExport.test.d.ts.map +0 -1
  55. package/dist/__tests__/docxExport.test.js +0 -383
  56. package/dist/__tests__/docxExport.test.js.map +0 -1
  57. package/dist/__tests__/docxImport.test.d.ts +0 -8
  58. package/dist/__tests__/docxImport.test.d.ts.map +0 -1
  59. package/dist/__tests__/docxImport.test.js +0 -309
  60. package/dist/__tests__/docxImport.test.js.map +0 -1
  61. package/dist/__tests__/epub.test.d.ts +0 -8
  62. package/dist/__tests__/epub.test.d.ts.map +0 -1
  63. package/dist/__tests__/epub.test.js +0 -472
  64. package/dist/__tests__/epub.test.js.map +0 -1
  65. package/dist/__tests__/html.test.d.ts +0 -8
  66. package/dist/__tests__/html.test.d.ts.map +0 -1
  67. package/dist/__tests__/html.test.js +0 -243
  68. package/dist/__tests__/html.test.js.map +0 -1
  69. package/dist/__tests__/ooxml.test.d.ts +0 -5
  70. package/dist/__tests__/ooxml.test.d.ts.map +0 -1
  71. package/dist/__tests__/ooxml.test.js +0 -152
  72. package/dist/__tests__/ooxml.test.js.map +0 -1
  73. package/dist/__tests__/pdfExport.test.d.ts +0 -8
  74. package/dist/__tests__/pdfExport.test.d.ts.map +0 -1
  75. package/dist/__tests__/pdfExport.test.js +0 -275
  76. package/dist/__tests__/pdfExport.test.js.map +0 -1
  77. package/dist/__tests__/pdfImport.test.d.ts +0 -11
  78. package/dist/__tests__/pdfImport.test.d.ts.map +0 -1
  79. package/dist/__tests__/pdfImport.test.js +0 -254
  80. package/dist/__tests__/pdfImport.test.js.map +0 -1
  81. package/dist/__tests__/roundTrip.test.d.ts +0 -7
  82. package/dist/__tests__/roundTrip.test.d.ts.map +0 -1
  83. package/dist/__tests__/roundTrip.test.js +0 -173
  84. package/dist/__tests__/roundTrip.test.js.map +0 -1
  85. package/dist/chunk-EUQQYBZZ.js.map +0 -1
  86. package/dist/chunk-MEZF76JA.js.map +0 -1
  87. package/dist/chunk-NGWHV77G.js.map +0 -1
  88. package/dist/chunk-UM5V2XZG.js +0 -242
  89. package/dist/chunk-UM5V2XZG.js.map +0 -1
  90. package/dist/container/index.d.ts.map +0 -1
  91. package/dist/docx/export.d.ts +0 -69
  92. package/dist/docx/export.d.ts.map +0 -1
  93. package/dist/docx/export.js +0 -854
  94. package/dist/docx/export.js.map +0 -1
  95. package/dist/docx/import.d.ts +0 -65
  96. package/dist/docx/import.d.ts.map +0 -1
  97. package/dist/docx/import.js +0 -779
  98. package/dist/docx/import.js.map +0 -1
  99. package/dist/docx/index.d.ts.map +0 -1
  100. package/dist/docx/styles.d.ts +0 -68
  101. package/dist/docx/styles.d.ts.map +0 -1
  102. package/dist/docx/styles.js +0 -125
  103. package/dist/docx/styles.js.map +0 -1
  104. package/dist/epub/export.d.ts +0 -72
  105. package/dist/epub/export.d.ts.map +0 -1
  106. package/dist/epub/export.js +0 -698
  107. package/dist/epub/export.js.map +0 -1
  108. package/dist/epub/index.d.ts.map +0 -1
  109. package/dist/html/htmlTemplate.d.ts +0 -70
  110. package/dist/html/htmlTemplate.d.ts.map +0 -1
  111. package/dist/html/htmlTemplate.js +0 -239
  112. package/dist/html/htmlTemplate.js.map +0 -1
  113. package/dist/html/imageUtils.d.ts +0 -29
  114. package/dist/html/imageUtils.d.ts.map +0 -1
  115. package/dist/html/imageUtils.js +0 -63
  116. package/dist/html/imageUtils.js.map +0 -1
  117. package/dist/html/index.d.ts.map +0 -1
  118. package/dist/index.d.ts.map +0 -1
  119. package/dist/ooxml/index.d.ts.map +0 -1
  120. package/dist/ooxml/namespaces.d.ts +0 -77
  121. package/dist/ooxml/namespaces.d.ts.map +0 -1
  122. package/dist/ooxml/namespaces.js +0 -104
  123. package/dist/ooxml/namespaces.js.map +0 -1
  124. package/dist/ooxml/reader.d.ts +0 -52
  125. package/dist/ooxml/reader.d.ts.map +0 -1
  126. package/dist/ooxml/reader.js +0 -190
  127. package/dist/ooxml/reader.js.map +0 -1
  128. package/dist/ooxml/types.d.ts +0 -82
  129. package/dist/ooxml/types.d.ts.map +0 -1
  130. package/dist/ooxml/types.js +0 -9
  131. package/dist/ooxml/types.js.map +0 -1
  132. package/dist/ooxml/writer.d.ts +0 -71
  133. package/dist/ooxml/writer.d.ts.map +0 -1
  134. package/dist/ooxml/writer.js +0 -200
  135. package/dist/ooxml/writer.js.map +0 -1
  136. package/dist/ooxml/xmlUtils.d.ts +0 -76
  137. package/dist/ooxml/xmlUtils.d.ts.map +0 -1
  138. package/dist/ooxml/xmlUtils.js +0 -108
  139. package/dist/ooxml/xmlUtils.js.map +0 -1
  140. package/dist/pdf/export.d.ts +0 -56
  141. package/dist/pdf/export.d.ts.map +0 -1
  142. package/dist/pdf/export.js +0 -768
  143. package/dist/pdf/export.js.map +0 -1
  144. package/dist/pdf/import.d.ts +0 -94
  145. package/dist/pdf/import.d.ts.map +0 -1
  146. package/dist/pdf/import.js +0 -796
  147. package/dist/pdf/import.js.map +0 -1
  148. package/dist/pdf/index.d.ts.map +0 -1
  149. package/dist/pdf/styles.d.ts +0 -142
  150. package/dist/pdf/styles.d.ts.map +0 -1
  151. package/dist/pdf/styles.js +0 -145
  152. package/dist/pdf/styles.js.map +0 -1
  153. package/dist/pptx/export.d.ts +0 -75
  154. package/dist/pptx/export.d.ts.map +0 -1
  155. package/dist/pptx/export.js +0 -722
  156. package/dist/pptx/export.js.map +0 -1
  157. package/dist/pptx/index.d.ts.map +0 -1
  158. package/dist/pptx/styles.d.ts +0 -54
  159. package/dist/pptx/styles.d.ts.map +0 -1
  160. package/dist/pptx/styles.js +0 -73
  161. package/dist/pptx/styles.js.map +0 -1
  162. package/dist/pptx/templates.d.ts +0 -24
  163. package/dist/pptx/templates.d.ts.map +0 -1
  164. package/dist/pptx/templates.js +0 -158
  165. package/dist/pptx/templates.js.map +0 -1
  166. package/dist/xlsx/index.d.ts.map +0 -1
@@ -1,796 +0,0 @@
1
- /**
2
- * PDF Import
3
- *
4
- * Parses a PDF file and converts its content into a squisq
5
- * MarkdownDocument (or Doc) using heuristic detection of headings,
6
- * lists, code blocks, tables, blockquotes, and hyperlinks.
7
- *
8
- * Uses pdfjs-dist (Mozilla pdf.js) for text extraction — a battle-tested,
9
- * browser-compatible PDF parser. Since PDFs encode positioned glyphs
10
- * rather than semantic structure, all structure detection is inherently
11
- * heuristic and works best on simply-formatted documents.
12
- *
13
- * @example
14
- * ```ts
15
- * import { pdfToMarkdownDoc } from '@bendyline/squisq-formats/pdf';
16
- *
17
- * const response = await fetch('document.pdf');
18
- * const data = await response.arrayBuffer();
19
- * const doc = await pdfToMarkdownDoc(data);
20
- * ```
21
- */
22
- import { markdownToDoc } from '@bendyline/squisq/doc';
23
- import { MemoryContentContainer } from '@bendyline/squisq/storage';
24
- import { DEFAULT_FONT_SIZE, IMPORT_HEADING_MIN_SIZE, IMPORT_HEADING_SIZE_RANGES, IMPORT_PARAGRAPH_GAP, IMPORT_BULLET_CHARS, IMPORT_ORDERED_PREFIX, IMPORT_COLUMN_TOLERANCE, IMPORT_TABLE_MIN_ROWS, IMPORT_URL_PATTERN, } from './styles.js';
25
- /**
26
- * Convert a PDF file to a MarkdownDocument.
27
- *
28
- * Structure detection is heuristic — results are best-effort.
29
- *
30
- * @param data - The raw PDF file as ArrayBuffer, Uint8Array, or Blob
31
- * @param options - Import options
32
- * @returns A MarkdownDocument representing the detected content
33
- */
34
- export async function pdfToMarkdownDoc(data, options = {}) {
35
- const bytes = data instanceof Blob
36
- ? new Uint8Array(await data.arrayBuffer())
37
- : data instanceof ArrayBuffer
38
- ? new Uint8Array(data)
39
- : data;
40
- const textLines = await extractTextLines(bytes);
41
- if (textLines.length === 0) {
42
- return { type: 'document', children: [] };
43
- }
44
- const bodySize = options.bodyFontSize ?? detectBodyFontSize(textLines);
45
- const blocks = classifyLines(textLines, bodySize, options);
46
- return { type: 'document', children: blocks };
47
- }
48
- /**
49
- * Convert a PDF file to a squisq Doc.
50
- *
51
- * Convenience wrapper: PDF → MarkdownDocument → Doc.
52
- */
53
- export async function pdfToDoc(data, options = {}) {
54
- const markdownDoc = await pdfToMarkdownDoc(data, options);
55
- return markdownToDoc(markdownDoc);
56
- }
57
- /**
58
- * Convert a PDF file to a ContentContainer with markdown + extracted images.
59
- *
60
- * The container will contain:
61
- * - The primary markdown document (index.md)
62
- * - Any embedded images under images/ (e.g., images/image1.png)
63
- *
64
- * Image extraction uses pdfjs-dist's operator list API and requires a browser
65
- * environment (canvas is used to encode pixel data to PNG).
66
- *
67
- * @param data - The raw PDF file as ArrayBuffer, Uint8Array, or Blob
68
- * @param options - Import options
69
- * @returns A ContentContainer with the document and its media
70
- */
71
- export async function pdfToContainer(data, options = {}) {
72
- const bytes = data instanceof Blob
73
- ? new Uint8Array(await data.arrayBuffer())
74
- : data instanceof ArrayBuffer
75
- ? new Uint8Array(data)
76
- : data;
77
- const textLines = await extractTextLines(bytes);
78
- const images = await extractImages(bytes);
79
- const bodySize = options.bodyFontSize ?? detectBodyFontSize(textLines);
80
- // If we have images, insert image references into the block list
81
- let blocks = classifyLines(textLines, bodySize, options);
82
- if (images.length > 0) {
83
- blocks = insertImageBlocks(blocks, images);
84
- }
85
- const markdownDoc = { type: 'document', children: blocks };
86
- const { stringifyMarkdown } = await import('@bendyline/squisq/markdown');
87
- const markdown = stringifyMarkdown(markdownDoc);
88
- const container = new MemoryContentContainer();
89
- await container.writeDocument(markdown);
90
- for (const img of images) {
91
- await container.writeFile(img.path, new Uint8Array(img.data), 'image/png');
92
- }
93
- return container;
94
- }
95
- /**
96
- * Extract embedded images from a PDF using pdfjs-dist operator list API.
97
- * Requires browser canvas for PNG encoding.
98
- */
99
- async function extractImages(data) {
100
- // Canvas is required for PNG encoding — skip in non-browser environments
101
- if (typeof document === 'undefined')
102
- return [];
103
- let pdfjsLib;
104
- try {
105
- pdfjsLib = (await import('pdfjs-dist/legacy/build/pdf.mjs'));
106
- }
107
- catch {
108
- pdfjsLib = (await import('pdfjs-dist'));
109
- }
110
- await applyWorkerConfig(pdfjsLib);
111
- const OPS_paintImageXObject = pdfjsLib.OPS?.paintImageXObject ?? 85;
112
- const loadingTask = pdfjsLib.getDocument({
113
- data,
114
- isEvalSupported: false,
115
- useSystemFonts: true,
116
- });
117
- const pdf = await loadingTask.promise;
118
- const images = [];
119
- let counter = 0;
120
- for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
121
- const page = (await pdf.getPage(pageNum));
122
- if (!page.getOperatorList)
123
- continue;
124
- const opList = await page.getOperatorList();
125
- const seen = new Set();
126
- for (let i = 0; i < opList.fnArray.length; i++) {
127
- if (opList.fnArray[i] !== OPS_paintImageXObject)
128
- continue;
129
- const imgName = opList.argsArray[i]?.[0];
130
- if (!imgName || typeof imgName !== 'string' || seen.has(imgName))
131
- continue;
132
- seen.add(imgName);
133
- try {
134
- const imgData = page.objs?.get(imgName);
135
- if (!imgData?.data || !imgData.width || !imgData.height)
136
- continue;
137
- const pngData = imageDataToPng(imgData);
138
- if (!pngData)
139
- continue;
140
- counter++;
141
- images.push({
142
- path: `images/image${counter}.png`,
143
- data: pngData,
144
- page: pageNum - 1,
145
- y: 0,
146
- });
147
- }
148
- catch {
149
- // Skip images that fail to extract
150
- }
151
- }
152
- }
153
- return images;
154
- }
155
- /** Encode pdfjs image data to PNG using a canvas element. */
156
- function imageDataToPng(img) {
157
- try {
158
- const canvas = document.createElement('canvas');
159
- canvas.width = img.width;
160
- canvas.height = img.height;
161
- const ctx = canvas.getContext('2d');
162
- if (!ctx)
163
- return null;
164
- // pdfjs kind=1 is GRAYSCALE, kind=2 is RGB, kind=3 is RGBA
165
- let imageData;
166
- if (img.kind === 3 || img.data.length === img.width * img.height * 4) {
167
- // RGBA — use directly
168
- imageData = new ImageData(new Uint8ClampedArray(img.data), img.width, img.height);
169
- }
170
- else if (img.kind === 2 || img.data.length === img.width * img.height * 3) {
171
- // RGB — expand to RGBA
172
- const rgba = new Uint8ClampedArray(img.width * img.height * 4);
173
- for (let j = 0, k = 0; j < img.data.length; j += 3, k += 4) {
174
- rgba[k] = img.data[j];
175
- rgba[k + 1] = img.data[j + 1];
176
- rgba[k + 2] = img.data[j + 2];
177
- rgba[k + 3] = 255;
178
- }
179
- imageData = new ImageData(rgba, img.width, img.height);
180
- }
181
- else if (img.kind === 1 || img.data.length === img.width * img.height) {
182
- // Grayscale — expand to RGBA
183
- const rgba = new Uint8ClampedArray(img.width * img.height * 4);
184
- for (let j = 0, k = 0; j < img.data.length; j++, k += 4) {
185
- rgba[k] = img.data[j];
186
- rgba[k + 1] = img.data[j];
187
- rgba[k + 2] = img.data[j];
188
- rgba[k + 3] = 255;
189
- }
190
- imageData = new ImageData(rgba, img.width, img.height);
191
- }
192
- else {
193
- return null;
194
- }
195
- ctx.putImageData(imageData, 0, 0);
196
- // Convert canvas to PNG ArrayBuffer
197
- const dataUrl = canvas.toDataURL('image/png');
198
- const base64 = dataUrl.split(',')[1];
199
- const binaryStr = atob(base64);
200
- const bytes = new Uint8Array(binaryStr.length);
201
- for (let i = 0; i < binaryStr.length; i++) {
202
- bytes[i] = binaryStr.charCodeAt(i);
203
- }
204
- return bytes.buffer;
205
- }
206
- catch {
207
- return null;
208
- }
209
- }
210
- /**
211
- * Insert image reference blocks between text blocks.
212
- * Places each image after the last text block on the same page (or at the end).
213
- */
214
- function insertImageBlocks(blocks, images) {
215
- if (images.length === 0)
216
- return blocks;
217
- // Simple strategy: append all images at the end as paragraphs
218
- const result = [...blocks];
219
- for (const img of images) {
220
- const imgNode = {
221
- type: 'image',
222
- url: img.path,
223
- alt: `Image ${img.path.replace('images/image', '').replace('.png', '')}`,
224
- };
225
- const para = {
226
- type: 'paragraph',
227
- children: [imgNode],
228
- };
229
- result.push(para);
230
- }
231
- return result;
232
- }
233
- // ============================================
234
- // PDF Text Extraction (pdfjs-dist)
235
- // ============================================
236
- /**
237
- * Configure the pdfjs-dist PDF worker source URL.
238
- *
239
- * pdfjs-dist requires a worker for PDF parsing. In the **browser**, bundlers
240
- * (Vite, webpack) typically handle this automatically, or you can point to a
241
- * CDN-hosted worker script. In **Node.js / SSR / test** environments, call
242
- * this with a `file://` URL to the worker module **before** any import call.
243
- *
244
- * @example
245
- * ```ts
246
- * // Browser — CDN
247
- * configurePdfWorker('https://cdn.jsdelivr.net/npm/pdfjs-dist@4/legacy/build/pdf.worker.min.mjs');
248
- *
249
- * // Node / vitest — file URL
250
- * import { pathToFileURL } from 'url';
251
- * configurePdfWorker(pathToFileURL(require.resolve('pdfjs-dist/legacy/build/pdf.worker.mjs')).href);
252
- * ```
253
- */
254
- export function configurePdfWorker(workerSrc) {
255
- _workerSrc = workerSrc;
256
- }
257
- /** Module-level storage for the worker source URL. */
258
- let _workerSrc;
259
- async function applyWorkerConfig(pdfjsLib) {
260
- if (!pdfjsLib.GlobalWorkerOptions)
261
- return;
262
- if (pdfjsLib.GlobalWorkerOptions.workerSrc)
263
- return;
264
- if (_workerSrc) {
265
- pdfjsLib.GlobalWorkerOptions.workerSrc = _workerSrc;
266
- }
267
- // If no workerSrc is set, pdfjs-dist's legacy build will attempt its
268
- // built-in fake-worker fallback. In browsers this usually works; in
269
- // Node.js the caller must have called configurePdfWorker() first.
270
- }
271
- async function extractTextLines(data) {
272
- // Dynamic import — the legacy build bundles a fake-worker fallback
273
- // that avoids a real Web Worker in environments that don't support it.
274
- let pdfjsLib;
275
- try {
276
- pdfjsLib = (await import('pdfjs-dist/legacy/build/pdf.mjs'));
277
- }
278
- catch {
279
- pdfjsLib = (await import('pdfjs-dist'));
280
- }
281
- await applyWorkerConfig(pdfjsLib);
282
- const loadingTask = pdfjsLib.getDocument({
283
- data,
284
- isEvalSupported: false,
285
- useSystemFonts: true,
286
- });
287
- const pdf = await loadingTask.promise;
288
- const allLines = [];
289
- for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
290
- const page = await pdf.getPage(pageNum);
291
- const content = await page.getTextContent();
292
- // Build a fontName → fontFamily lookup from pdfjs styles
293
- const styleMap = content.styles || {};
294
- // Group text items into lines by y-coordinate
295
- const items = [];
296
- for (const item of content.items) {
297
- if (!item.str || item.str.trim().length === 0)
298
- continue;
299
- const transform = item.transform || [1, 0, 0, 1, 0, 0];
300
- const x = transform[4];
301
- const y = transform[5];
302
- const height = Math.abs(transform[3]) || item.height || 12;
303
- const width = item.width || 0;
304
- const fontName = item.fontName || '';
305
- const fontFamily = styleMap[fontName]?.fontFamily || '';
306
- items.push({ str: item.str, x, y, width, height, fontName, fontFamily });
307
- }
308
- // Group into lines (items within 2pt of same y are same line)
309
- const lineMap = new Map();
310
- for (const item of items) {
311
- const roundedY = Math.round(item.y * 2) / 2;
312
- let foundKey;
313
- for (const key of lineMap.keys()) {
314
- if (Math.abs(key - roundedY) < 2) {
315
- foundKey = key;
316
- break;
317
- }
318
- }
319
- if (foundKey !== undefined) {
320
- lineMap.get(foundKey).push(item);
321
- }
322
- else {
323
- lineMap.set(roundedY, [item]);
324
- }
325
- }
326
- // Sort lines top-to-bottom (highest y first), items left-to-right
327
- const sortedKeys = [...lineMap.keys()].sort((a, b) => b - a);
328
- for (const key of sortedKeys) {
329
- const lineItems = lineMap.get(key).sort((a, b) => a.x - b.x);
330
- const fontSizes = lineItems.map((i) => i.height);
331
- const fontSize = mode(fontSizes) || 12;
332
- const fontFamilies = lineItems.map((i) => i.fontFamily);
333
- const fontFamily = modeStr(fontFamilies) || '';
334
- const fontNames = lineItems.map((i) => i.fontName);
335
- const fontName = modeStr(fontNames) || '';
336
- const minX = Math.min(...lineItems.map((i) => i.x));
337
- const text = lineItems.map((i) => i.str).join(' ');
338
- allLines.push({
339
- items: lineItems,
340
- y: key,
341
- page: pageNum - 1,
342
- fontSize,
343
- fontFamily,
344
- fontName,
345
- minX,
346
- text,
347
- });
348
- }
349
- }
350
- return allLines;
351
- }
352
- // ============================================
353
- // Font Size Detection
354
- // ============================================
355
- function detectBodyFontSize(lines) {
356
- const sizes = lines.map((l) => Math.round(l.fontSize * 2) / 2);
357
- return mode(sizes) || DEFAULT_FONT_SIZE;
358
- }
359
- function mode(arr) {
360
- const freq = new Map();
361
- for (const v of arr)
362
- freq.set(v, (freq.get(v) || 0) + 1);
363
- let maxCount = 0;
364
- let maxVal = 0;
365
- for (const [v, c] of freq) {
366
- if (c > maxCount) {
367
- maxCount = c;
368
- maxVal = v;
369
- }
370
- }
371
- return maxVal;
372
- }
373
- function modeStr(arr) {
374
- const freq = new Map();
375
- for (const v of arr)
376
- freq.set(v, (freq.get(v) || 0) + 1);
377
- let maxCount = 0;
378
- let maxVal = '';
379
- for (const [v, c] of freq) {
380
- if (c > maxCount) {
381
- maxCount = c;
382
- maxVal = v;
383
- }
384
- }
385
- return maxVal;
386
- }
387
- // ============================================
388
- // Line Classification → MarkdownBlockNode[]
389
- // ============================================
390
- function classifyLines(lines, bodySize, options) {
391
- const blocks = [];
392
- const detectTables = options.detectTables !== false;
393
- const detectCodeBlocks = options.detectCodeBlocks !== false;
394
- const detectBlockquotes = options.detectBlockquotes !== false;
395
- const _detectLinks = options.detectLinks !== false;
396
- // Determine typical left margin (most common minX)
397
- const leftMargins = lines.map((l) => Math.round(l.minX));
398
- const typicalLeftMargin = mode(leftMargins) || 72;
399
- let i = 0;
400
- while (i < lines.length) {
401
- const line = lines[i];
402
- // --- Heading detection ---
403
- if (line.fontSize >= IMPORT_HEADING_MIN_SIZE && line.fontSize > bodySize + 1) {
404
- const depth = sizeToHeadingDepth(line.fontSize);
405
- blocks.push({
406
- type: 'heading',
407
- depth,
408
- children: buildInlineNodes(line, options),
409
- });
410
- i++;
411
- continue;
412
- }
413
- // --- Code block detection (monospace font runs) ---
414
- if (detectCodeBlocks && isMonospaceLine(line)) {
415
- const codeLines = [];
416
- while (i < lines.length && isMonospaceLine(lines[i])) {
417
- codeLines.push(lines[i].text);
418
- i++;
419
- }
420
- blocks.push({
421
- type: 'code',
422
- value: codeLines.join('\n'),
423
- });
424
- continue;
425
- }
426
- // --- Table detection (column-aligned consecutive lines) ---
427
- if (detectTables && i + 1 < lines.length) {
428
- const tableLines = tryDetectTable(lines, i, typicalLeftMargin);
429
- if (tableLines > 0) {
430
- const table = buildTable(lines.slice(i, i + tableLines), options);
431
- if (table) {
432
- blocks.push(table);
433
- i += tableLines;
434
- continue;
435
- }
436
- }
437
- }
438
- // --- List detection ---
439
- const bulletMatch = tryMatchBullet(line.text);
440
- const orderedMatch = line.text.match(IMPORT_ORDERED_PREFIX);
441
- if (bulletMatch || orderedMatch) {
442
- const listResult = consumeList(lines, i, typicalLeftMargin, bodySize, options);
443
- blocks.push(listResult.list);
444
- i = listResult.nextIndex;
445
- continue;
446
- }
447
- // --- Blockquote detection (indented text) ---
448
- if (detectBlockquotes && line.minX > typicalLeftMargin + 20) {
449
- const quoteLines = [];
450
- while (i < lines.length &&
451
- lines[i].minX > typicalLeftMargin + 20 &&
452
- !isMonospaceLine(lines[i]) &&
453
- lines[i].fontSize <= bodySize + 1) {
454
- quoteLines.push(lines[i]);
455
- i++;
456
- }
457
- const quoteBlocks = quoteLines.map((ql) => ({
458
- type: 'paragraph',
459
- children: buildInlineNodes(ql, options),
460
- }));
461
- blocks.push({
462
- type: 'blockquote',
463
- children: quoteBlocks,
464
- });
465
- continue;
466
- }
467
- // --- Regular paragraph ---
468
- // Merge consecutive body-sized lines on the same page with small y-gaps
469
- const paraLines = [line];
470
- i++;
471
- while (i < lines.length) {
472
- const next = lines[i];
473
- // Same page, same-ish font size, close y (within line-height gap), not bullet/heading
474
- if (next.page === line.page &&
475
- Math.abs(next.fontSize - bodySize) <= 1 &&
476
- !isMonospaceLine(next) &&
477
- next.minX <= typicalLeftMargin + 15 &&
478
- !tryMatchBullet(next.text) &&
479
- !next.text.match(IMPORT_ORDERED_PREFIX)) {
480
- // Check y-gap: lines are sorted top-to-bottom so y decreases
481
- const yGap = paraLines[paraLines.length - 1].y - next.y;
482
- const lineHeight = bodySize * 1.6;
483
- if (yGap > 0 && yGap < lineHeight + IMPORT_PARAGRAPH_GAP) {
484
- paraLines.push(next);
485
- i++;
486
- }
487
- else {
488
- break;
489
- }
490
- }
491
- else {
492
- break;
493
- }
494
- }
495
- // Build paragraph from merged lines
496
- const allInlines = [];
497
- for (let j = 0; j < paraLines.length; j++) {
498
- if (j > 0) {
499
- allInlines.push({ type: 'text', value: ' ' });
500
- }
501
- allInlines.push(...buildInlineNodes(paraLines[j], options));
502
- }
503
- if (allInlines.length > 0) {
504
- blocks.push({
505
- type: 'paragraph',
506
- children: mergeAdjacentText(allInlines),
507
- });
508
- }
509
- }
510
- return blocks;
511
- }
512
- // ============================================
513
- // Heading Depth Mapping
514
- // ============================================
515
- function sizeToHeadingDepth(fontSize) {
516
- for (const range of IMPORT_HEADING_SIZE_RANGES) {
517
- if (fontSize >= range.min)
518
- return range.depth;
519
- }
520
- return 6;
521
- }
522
- // ============================================
523
- // Font Heuristics
524
- // ============================================
525
- /**
526
- * Check if a line is predominantly monospace.
527
- * Uses the resolved fontFamily from pdfjs styles first,
528
- * falls back to fontName pattern matching for embedded fonts.
529
- */
530
- function isMonospaceLine(line) {
531
- return isMonospaceFamily(line.fontFamily) || isMonospaceName(line.fontName);
532
- }
533
- /**
534
- * Check if a text item is monospace.
535
- */
536
- function isMonospaceItem(item) {
537
- return isMonospaceFamily(item.fontFamily) || isMonospaceName(item.fontName);
538
- }
539
- function isMonospaceFamily(fontFamily) {
540
- const lower = fontFamily.toLowerCase();
541
- return lower === 'monospace' || lower.includes('monospace');
542
- }
543
- function isMonospaceName(fontName) {
544
- const lower = fontName.toLowerCase();
545
- return (lower.includes('courier') ||
546
- lower.includes('mono') ||
547
- lower.includes('consolas') ||
548
- lower.includes('menlo') ||
549
- lower.includes('inconsolata') ||
550
- lower.includes('firacode') ||
551
- lower.includes('source code') ||
552
- lower.includes('dejavu sans mono'));
553
- }
554
- function isBoldFont(fontName) {
555
- const lower = fontName.toLowerCase();
556
- return lower.includes('bold') || lower.includes('black') || lower.includes('heavy');
557
- }
558
- function isItalicFont(fontName) {
559
- const lower = fontName.toLowerCase();
560
- return lower.includes('italic') || lower.includes('oblique') || lower.includes('slanted');
561
- }
562
- // ============================================
563
- // Inline Node Construction
564
- // ============================================
565
- function buildInlineNodes(line, options) {
566
- const nodes = [];
567
- const detectLinksOpt = options.detectLinks !== false;
568
- for (const item of line.items) {
569
- const text = item.str;
570
- if (!text || text.trim().length === 0)
571
- continue;
572
- const bold = isBoldFont(item.fontName);
573
- const italic = isItalicFont(item.fontName);
574
- const mono = isMonospaceItem(item);
575
- let inlineNodes;
576
- if (mono) {
577
- inlineNodes = [{ type: 'inlineCode', value: text }];
578
- }
579
- else if (detectLinksOpt) {
580
- inlineNodes = splitTextWithLinks(text);
581
- }
582
- else {
583
- inlineNodes = [{ type: 'text', value: text }];
584
- }
585
- // Wrap in formatting
586
- for (const node of inlineNodes) {
587
- let wrapped = node;
588
- if (italic) {
589
- wrapped = { type: 'emphasis', children: [wrapped] };
590
- }
591
- if (bold) {
592
- wrapped = { type: 'strong', children: [wrapped] };
593
- }
594
- nodes.push(wrapped);
595
- }
596
- }
597
- return nodes;
598
- }
599
- /**
600
- * Split a text string into text nodes and link nodes wherever
601
- * URL patterns are found.
602
- */
603
- function splitTextWithLinks(text) {
604
- const nodes = [];
605
- let lastIndex = 0;
606
- // Reset regex state
607
- IMPORT_URL_PATTERN.lastIndex = 0;
608
- let match;
609
- while ((match = IMPORT_URL_PATTERN.exec(text)) !== null) {
610
- // Text before URL
611
- if (match.index > lastIndex) {
612
- nodes.push({ type: 'text', value: text.slice(lastIndex, match.index) });
613
- }
614
- // URL as link
615
- const url = match[0];
616
- nodes.push({
617
- type: 'link',
618
- url,
619
- children: [{ type: 'text', value: url }],
620
- });
621
- lastIndex = match.index + url.length;
622
- }
623
- // Remaining text
624
- if (lastIndex < text.length) {
625
- nodes.push({ type: 'text', value: text.slice(lastIndex) });
626
- }
627
- return nodes.length > 0 ? nodes : [{ type: 'text', value: text }];
628
- }
629
- // ============================================
630
- // List Detection
631
- // ============================================
632
- function tryMatchBullet(text) {
633
- if (text.length === 0)
634
- return false;
635
- return IMPORT_BULLET_CHARS.has(text[0]) || IMPORT_BULLET_CHARS.has(text.trimStart()[0]);
636
- }
637
- function stripBullet(text) {
638
- const trimmed = text.trimStart();
639
- if (IMPORT_BULLET_CHARS.has(trimmed[0])) {
640
- return trimmed.slice(1).trimStart();
641
- }
642
- return text;
643
- }
644
- function stripOrderedPrefix(text) {
645
- return text.replace(IMPORT_ORDERED_PREFIX, '');
646
- }
647
- function consumeList(lines, startIdx, _typicalLeftMargin, _bodySize, _options) {
648
- const firstLine = lines[startIdx];
649
- const isOrdered = !!firstLine.text.match(IMPORT_ORDERED_PREFIX);
650
- const items = [];
651
- let i = startIdx;
652
- while (i < lines.length) {
653
- const line = lines[i];
654
- const isBullet = tryMatchBullet(line.text);
655
- const isOrd = !!line.text.match(IMPORT_ORDERED_PREFIX);
656
- if (!isBullet && !isOrd)
657
- break;
658
- // All items in one list should be same type
659
- if (isOrdered && !isOrd)
660
- break;
661
- if (!isOrdered && !isBullet)
662
- break;
663
- const cleanText = isOrdered ? stripOrderedPrefix(line.text) : stripBullet(line.text);
664
- const para = {
665
- type: 'paragraph',
666
- children: splitTextWithLinks(cleanText),
667
- };
668
- items.push({
669
- type: 'listItem',
670
- children: [para],
671
- });
672
- i++;
673
- }
674
- return {
675
- list: {
676
- type: 'list',
677
- ordered: isOrdered,
678
- children: items,
679
- },
680
- nextIndex: i,
681
- };
682
- }
683
- // ============================================
684
- // Table Detection
685
- // ============================================
686
- /**
687
- * Look ahead from index `start` and return the number of consecutive
688
- * lines that form an aligned table, or 0 if no table detected.
689
- */
690
- function tryDetectTable(lines, start, _typicalLeftMargin) {
691
- // A table needs multiple items per line (columns) on consecutive lines
692
- // with roughly the same x-alignment pattern.
693
- const firstLine = lines[start];
694
- if (firstLine.items.length < 2)
695
- return 0;
696
- const cols = getColumnPositions(firstLine);
697
- if (cols.length < 2)
698
- return 0;
699
- let count = 1;
700
- for (let i = start + 1; i < lines.length; i++) {
701
- const line = lines[i];
702
- if (line.items.length < 2)
703
- break;
704
- // Check if this line's columns align with the first line's
705
- const lineCols = getColumnPositions(line);
706
- if (lineCols.length !== cols.length)
707
- break;
708
- let aligned = true;
709
- for (let c = 0; c < cols.length; c++) {
710
- if (Math.abs(lineCols[c] - cols[c]) > IMPORT_COLUMN_TOLERANCE) {
711
- aligned = false;
712
- break;
713
- }
714
- }
715
- if (!aligned)
716
- break;
717
- count++;
718
- }
719
- return count >= IMPORT_TABLE_MIN_ROWS ? count : 0;
720
- }
721
- function getColumnPositions(line) {
722
- // Cluster item x-positions
723
- const positions = [];
724
- for (const item of line.items) {
725
- const x = Math.round(item.x);
726
- // Check if this x is close to an existing column
727
- let found = false;
728
- for (const p of positions) {
729
- if (Math.abs(p - x) < IMPORT_COLUMN_TOLERANCE) {
730
- found = true;
731
- break;
732
- }
733
- }
734
- if (!found)
735
- positions.push(x);
736
- }
737
- return positions.sort((a, b) => a - b);
738
- }
739
- function buildTable(lines, _options) {
740
- if (lines.length === 0)
741
- return null;
742
- // Use the first line's column positions as anchors
743
- const cols = getColumnPositions(lines[0]);
744
- if (cols.length < 2)
745
- return null;
746
- const rows = [];
747
- for (let ri = 0; ri < lines.length; ri++) {
748
- const line = lines[ri];
749
- const cells = [];
750
- for (let ci = 0; ci < cols.length; ci++) {
751
- const colLeft = cols[ci] - IMPORT_COLUMN_TOLERANCE;
752
- const colRight = ci + 1 < cols.length ? cols[ci + 1] - IMPORT_COLUMN_TOLERANCE : Infinity;
753
- // Collect items in this column
754
- const cellItems = line.items.filter((item) => item.x >= colLeft && item.x < colRight);
755
- const text = cellItems
756
- .map((i) => i.str)
757
- .join(' ')
758
- .trim();
759
- cells.push({
760
- type: 'tableCell',
761
- isHeader: ri === 0,
762
- children: text.length > 0 ? [{ type: 'text', value: text }] : [],
763
- });
764
- }
765
- rows.push({
766
- type: 'tableRow',
767
- children: cells,
768
- });
769
- }
770
- return {
771
- type: 'table',
772
- children: rows,
773
- };
774
- }
775
- // ============================================
776
- // Text Merging
777
- // ============================================
778
- /**
779
- * Merge adjacent text nodes to reduce fragmentation.
780
- */
781
- function mergeAdjacentText(nodes) {
782
- if (nodes.length <= 1)
783
- return nodes;
784
- const result = [];
785
- for (const node of nodes) {
786
- const prev = result[result.length - 1];
787
- if (prev && prev.type === 'text' && node.type === 'text') {
788
- prev.value += node.value;
789
- }
790
- else {
791
- result.push(node);
792
- }
793
- }
794
- return result;
795
- }
796
- //# sourceMappingURL=import.js.map