markit-ai 0.5.0 → 0.5.2
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/dist/converters/pdf/grid.js +144 -3
- package/dist/converters/pdf/index.js +21 -1
- package/dist/markit.d.ts +15 -0
- package/dist/markit.js +90 -7
- package/package.json +1 -1
|
@@ -258,6 +258,122 @@ function expandSubRowsByYClusters(originalRows, cols, cells, cellBoxes) {
|
|
|
258
258
|
return originalRows + addedRows;
|
|
259
259
|
}
|
|
260
260
|
// ---------------------------------------------------------------------------
|
|
261
|
+
// Cross-column text box splitting
|
|
262
|
+
// ---------------------------------------------------------------------------
|
|
263
|
+
/**
|
|
264
|
+
* Find which column a horizontal position falls into.
|
|
265
|
+
* Returns -1 if outside the grid.
|
|
266
|
+
*/
|
|
267
|
+
function findCol(x, xLines) {
|
|
268
|
+
for (let i = 0; i < xLines.length - 1; i++) {
|
|
269
|
+
if (x >= xLines[i] && x <= xLines[i + 1])
|
|
270
|
+
return i;
|
|
271
|
+
}
|
|
272
|
+
return -1;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* When a text box spans across one or more vertical column boundaries,
|
|
276
|
+
* split it into multiple virtual text boxes — one per column — with the
|
|
277
|
+
* text divided proportionally by width.
|
|
278
|
+
*
|
|
279
|
+
* We split at word boundaries closest to the proportional split point
|
|
280
|
+
* so we don't chop words in half.
|
|
281
|
+
*/
|
|
282
|
+
function splitCrossColumnBoxes(textBoxes, xLines) {
|
|
283
|
+
const result = [];
|
|
284
|
+
const MARGIN = 5; // allow small overlap before considering it cross-column
|
|
285
|
+
for (const tb of textBoxes) {
|
|
286
|
+
const leftCol = findCol(tb.bounds.left + MARGIN, xLines);
|
|
287
|
+
const rightCol = findCol(tb.bounds.right - MARGIN, xLines);
|
|
288
|
+
// Not spanning columns, or outside grid — keep as-is
|
|
289
|
+
if (leftCol < 0 || rightCol < 0 || leftCol === rightCol) {
|
|
290
|
+
result.push(tb);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
// Text box spans from leftCol to rightCol — split it
|
|
294
|
+
const totalWidth = tb.bounds.right - tb.bounds.left;
|
|
295
|
+
if (totalWidth <= 0) {
|
|
296
|
+
result.push(tb);
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const words = tb.text.split(/\s+/);
|
|
300
|
+
if (words.length <= 1) {
|
|
301
|
+
// Single word spanning columns — just assign to whichever col has more overlap
|
|
302
|
+
result.push(tb);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
// For each column boundary crossing, find the best word-boundary split
|
|
306
|
+
let remainingWords = [...words];
|
|
307
|
+
let currentLeft = tb.bounds.left;
|
|
308
|
+
for (let col = leftCol; col <= rightCol && remainingWords.length > 0; col++) {
|
|
309
|
+
const colRight = col < xLines.length - 1 ? xLines[col + 1] : tb.bounds.right;
|
|
310
|
+
const segmentRight = Math.min(colRight, tb.bounds.right);
|
|
311
|
+
if (col === rightCol) {
|
|
312
|
+
// Last column — take all remaining words
|
|
313
|
+
result.push({
|
|
314
|
+
...tb,
|
|
315
|
+
id: `${tb.id}-split${col}`,
|
|
316
|
+
text: remainingWords.join(" "),
|
|
317
|
+
bounds: {
|
|
318
|
+
...tb.bounds,
|
|
319
|
+
left: currentLeft,
|
|
320
|
+
right: tb.bounds.right,
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
remainingWords = [];
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
// Find how many words fit in this column segment proportionally
|
|
327
|
+
const segmentWidth = segmentRight - currentLeft;
|
|
328
|
+
const fractionOfTotal = segmentWidth / totalWidth;
|
|
329
|
+
const approxChars = Math.round(fractionOfTotal * tb.text.length);
|
|
330
|
+
// Walk words to find the split closest to the proportional point
|
|
331
|
+
let charCount = 0;
|
|
332
|
+
let splitIdx = 0;
|
|
333
|
+
for (let w = 0; w < remainingWords.length; w++) {
|
|
334
|
+
const nextCount = charCount + remainingWords[w].length + (w > 0 ? 1 : 0);
|
|
335
|
+
if (nextCount > approxChars && splitIdx > 0)
|
|
336
|
+
break;
|
|
337
|
+
charCount = nextCount;
|
|
338
|
+
splitIdx = w + 1;
|
|
339
|
+
}
|
|
340
|
+
if (splitIdx === 0)
|
|
341
|
+
splitIdx = 1; // take at least one word
|
|
342
|
+
if (splitIdx >= remainingWords.length) {
|
|
343
|
+
// All remaining words fit here
|
|
344
|
+
result.push({
|
|
345
|
+
...tb,
|
|
346
|
+
id: `${tb.id}-split${col}`,
|
|
347
|
+
text: remainingWords.join(" "),
|
|
348
|
+
bounds: {
|
|
349
|
+
...tb.bounds,
|
|
350
|
+
left: currentLeft,
|
|
351
|
+
right: segmentRight,
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
remainingWords = [];
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
const partWords = remainingWords.slice(0, splitIdx);
|
|
358
|
+
result.push({
|
|
359
|
+
...tb,
|
|
360
|
+
id: `${tb.id}-split${col}`,
|
|
361
|
+
text: partWords.join(" "),
|
|
362
|
+
bounds: {
|
|
363
|
+
...tb.bounds,
|
|
364
|
+
left: currentLeft,
|
|
365
|
+
right: segmentRight,
|
|
366
|
+
},
|
|
367
|
+
});
|
|
368
|
+
remainingWords = remainingWords.slice(splitIdx);
|
|
369
|
+
currentLeft = segmentRight;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return result;
|
|
375
|
+
}
|
|
376
|
+
// ---------------------------------------------------------------------------
|
|
261
377
|
// Full grid table (H + V lines)
|
|
262
378
|
// ---------------------------------------------------------------------------
|
|
263
379
|
function buildCells(rows, cols) {
|
|
@@ -278,11 +394,26 @@ function buildTableGrid(pageNumber, yLines, xLines, filteredSegments, textBoxes)
|
|
|
278
394
|
const yMax = yLines[0];
|
|
279
395
|
const xMin = xLines[0];
|
|
280
396
|
const xMax = xLines[xLines.length - 1];
|
|
281
|
-
//
|
|
397
|
+
// Split text boxes that span multiple columns before placement
|
|
398
|
+
const splitBoxes = splitCrossColumnBoxes(textBoxes, xLines);
|
|
399
|
+
// Track which split piece IDs get placed in cells, so we can consume
|
|
400
|
+
// the original (unsplit) text box IDs too.
|
|
401
|
+
const placedSplitIds = new Set();
|
|
402
|
+
// Look for header text boxes just above the grid.
|
|
403
|
+
// Use the ORIGINAL (unsplit) text boxes for header detection so that
|
|
404
|
+
// wide paragraph text isn't falsely split into column-sized header chunks.
|
|
405
|
+
// Reject boxes wider than 1.5 columns — those are paragraph text, not headers.
|
|
406
|
+
const avgColWidth = (xMax - xMin) / cols;
|
|
407
|
+
const maxHeaderBoxWidth = avgColWidth * 1.5;
|
|
282
408
|
const headerBoxes = textBoxes.filter((tb) => {
|
|
283
409
|
const cy = (tb.bounds.top + tb.bounds.bottom) / 2;
|
|
284
410
|
const cx = (tb.bounds.left + tb.bounds.right) / 2;
|
|
285
|
-
|
|
411
|
+
const boxWidth = tb.bounds.right - tb.bounds.left;
|
|
412
|
+
return (cy > yMax &&
|
|
413
|
+
cy <= yMax + 20 &&
|
|
414
|
+
cx >= xMin &&
|
|
415
|
+
cx <= xMax &&
|
|
416
|
+
boxWidth <= maxHeaderBoxWidth);
|
|
286
417
|
});
|
|
287
418
|
if (headerBoxes.length > 0) {
|
|
288
419
|
rows += 1;
|
|
@@ -308,7 +439,7 @@ function buildTableGrid(pageNumber, yLines, xLines, filteredSegments, textBoxes)
|
|
|
308
439
|
}
|
|
309
440
|
}
|
|
310
441
|
const cellBoxes = new Map();
|
|
311
|
-
for (const tb of
|
|
442
|
+
for (const tb of splitBoxes) {
|
|
312
443
|
const cx = (tb.bounds.left + tb.bounds.right) / 2;
|
|
313
444
|
const cy = (tb.bounds.top + tb.bounds.bottom) / 2;
|
|
314
445
|
if (cy < yMin || cy > yMax || cx < xMin || cx > xMax)
|
|
@@ -338,6 +469,8 @@ function buildTableGrid(pageNumber, yLines, xLines, filteredSegments, textBoxes)
|
|
|
338
469
|
cellBoxes.set(cell, []);
|
|
339
470
|
cellBoxes.get(cell)?.push(tb);
|
|
340
471
|
consumedIds.push(tb.id);
|
|
472
|
+
if (tb.id.includes("-split"))
|
|
473
|
+
placedSplitIds.add(tb.id);
|
|
341
474
|
}
|
|
342
475
|
rows = expandSubRowsByYClusters(rows, cols, cells, cellBoxes);
|
|
343
476
|
// Merge text boxes within each cell into cell text
|
|
@@ -369,6 +502,14 @@ function buildTableGrid(pageNumber, yLines, xLines, filteredSegments, textBoxes)
|
|
|
369
502
|
topY: yLines[0],
|
|
370
503
|
isBorderless: false,
|
|
371
504
|
});
|
|
505
|
+
// Also consume the original (unsplit) text box IDs when any of their
|
|
506
|
+
// split pieces were placed in a cell.
|
|
507
|
+
for (const splitId of placedSplitIds) {
|
|
508
|
+
const origId = splitId.replace(/-split\d+$/, "");
|
|
509
|
+
if (!consumedIds.includes(origId)) {
|
|
510
|
+
consumedIds.push(origId);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
372
513
|
return { grid, consumedIds };
|
|
373
514
|
}
|
|
374
515
|
// ---------------------------------------------------------------------------
|
|
@@ -80,8 +80,28 @@ export class PdfConverter {
|
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
// Detect column layout
|
|
83
|
+
// Detect column layout.
|
|
84
|
+
// If the page has vertical segments (tables), suppress column detection
|
|
85
|
+
// when one detected column is very narrow — that's a table's first column,
|
|
86
|
+
// not a page layout column.
|
|
84
87
|
const layout = detectColumns(page.textBoxes);
|
|
88
|
+
if (layout.columnCount > 1 &&
|
|
89
|
+
page.segments.some((s) => Math.abs(s.x1 - s.x2) <= 0.8)) {
|
|
90
|
+
const pageXMin = Math.min(...page.textBoxes.map((tb) => tb.bounds.left));
|
|
91
|
+
const pageXMax = Math.max(...page.textBoxes.map((tb) => tb.bounds.right));
|
|
92
|
+
const pageWidth = pageXMax - pageXMin;
|
|
93
|
+
const minColFraction = 0.3;
|
|
94
|
+
const tooNarrow = layout.columns.some((col) => {
|
|
95
|
+
const colXMin = Math.min(...col.map((tb) => tb.bounds.left));
|
|
96
|
+
const colXMax = Math.max(...col.map((tb) => tb.bounds.right));
|
|
97
|
+
return (colXMax - colXMin) / pageWidth < minColFraction;
|
|
98
|
+
});
|
|
99
|
+
if (tooNarrow) {
|
|
100
|
+
layout.columnCount = 1;
|
|
101
|
+
layout.columns = [page.textBoxes];
|
|
102
|
+
layout.boundaries = [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
85
105
|
if (layout.columnCount === 1) {
|
|
86
106
|
// Single column — process normally
|
|
87
107
|
const md = processColumn(page.pageNumber, page.textBoxes, page.segments, imageBlocks);
|
package/dist/markit.d.ts
CHANGED
|
@@ -12,8 +12,23 @@ export declare class Markit {
|
|
|
12
12
|
* Convert a URL to markdown.
|
|
13
13
|
*/
|
|
14
14
|
convertUrl(url: string): Promise<ConversionResult>;
|
|
15
|
+
/**
|
|
16
|
+
* Inspect an HTML response for a discoverable markdown source URL.
|
|
17
|
+
* If found, fetch and convert the raw markdown instead.
|
|
18
|
+
*/
|
|
19
|
+
private tryMarkdownSource;
|
|
15
20
|
/**
|
|
16
21
|
* Convert a buffer with stream info to markdown.
|
|
17
22
|
*/
|
|
18
23
|
convert(input: Buffer, streamInfo: StreamInfo): Promise<ConversionResult>;
|
|
19
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Try to discover a raw markdown source URL from an HTML response.
|
|
27
|
+
* Checks multiple patterns:
|
|
28
|
+
* 1. <link rel="alternate" type="text/markdown" href="..."> tag
|
|
29
|
+
* 2. VitePress markers → append .md to the URL
|
|
30
|
+
* 3. llms.txt convention → try url.md or url.html.md
|
|
31
|
+
*
|
|
32
|
+
* @internal Exported for testing.
|
|
33
|
+
*/
|
|
34
|
+
export declare function discoverMarkdownSource(html: string, url: string, ext: string): string | null;
|
package/dist/markit.js
CHANGED
|
@@ -19,6 +19,7 @@ import { XlsxConverter } from "./converters/xlsx.js";
|
|
|
19
19
|
import { XmlConverter } from "./converters/xml.js";
|
|
20
20
|
import { YamlConverter } from "./converters/yaml.js";
|
|
21
21
|
import { ZipConverter } from "./converters/zip.js";
|
|
22
|
+
const USER_AGENT = "markit/0.1.0";
|
|
22
23
|
export class Markit {
|
|
23
24
|
converters = [];
|
|
24
25
|
options;
|
|
@@ -89,25 +90,72 @@ export class Markit {
|
|
|
89
90
|
const response = await fetch(url, {
|
|
90
91
|
headers: {
|
|
91
92
|
Accept: "text/markdown, text/html;q=0.9, text/plain;q=0.8, */*;q=0.1",
|
|
92
|
-
"User-Agent":
|
|
93
|
+
"User-Agent": USER_AGENT,
|
|
93
94
|
},
|
|
94
95
|
});
|
|
95
96
|
if (!response.ok) {
|
|
96
97
|
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
|
97
98
|
}
|
|
98
99
|
const contentType = response.headers.get("content-type") || "";
|
|
99
|
-
const
|
|
100
|
-
// Derive extension from URL path
|
|
100
|
+
const mimetype = contentType.split(";")[0].trim();
|
|
101
101
|
const urlPath = new URL(url).pathname;
|
|
102
102
|
const ext = extname(urlPath).toLowerCase();
|
|
103
|
+
// Content negotiation worked — server returned markdown directly
|
|
104
|
+
if (mimetype === "text/markdown") {
|
|
105
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
106
|
+
return this.convert(buffer, {
|
|
107
|
+
url,
|
|
108
|
+
mimetype: "text/markdown",
|
|
109
|
+
extension: ".md",
|
|
110
|
+
filename: basename(urlPath) || undefined,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
103
113
|
const buffer = Buffer.from(await response.arrayBuffer());
|
|
104
|
-
|
|
114
|
+
// For HTML responses, try to discover a raw markdown source.
|
|
115
|
+
// Patterns: <link rel="alternate">, VitePress .md files, llms.txt convention.
|
|
116
|
+
if (mimetype === "text/html") {
|
|
117
|
+
const result = await this.tryMarkdownSource(buffer, url, ext);
|
|
118
|
+
if (result)
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
return this.convert(buffer, {
|
|
105
122
|
url,
|
|
106
|
-
mimetype
|
|
123
|
+
mimetype,
|
|
107
124
|
extension: ext || undefined,
|
|
108
125
|
filename: basename(urlPath) || undefined,
|
|
109
|
-
};
|
|
110
|
-
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Inspect an HTML response for a discoverable markdown source URL.
|
|
130
|
+
* If found, fetch and convert the raw markdown instead.
|
|
131
|
+
*/
|
|
132
|
+
async tryMarkdownSource(htmlBuffer, url, ext) {
|
|
133
|
+
const html = htmlBuffer.toString("utf-8", 0, Math.min(htmlBuffer.length, 50_000));
|
|
134
|
+
const mdSourceUrl = discoverMarkdownSource(html, url, ext);
|
|
135
|
+
if (!mdSourceUrl)
|
|
136
|
+
return null;
|
|
137
|
+
try {
|
|
138
|
+
const response = await fetch(mdSourceUrl, {
|
|
139
|
+
headers: { "User-Agent": USER_AGENT },
|
|
140
|
+
});
|
|
141
|
+
if (!response.ok)
|
|
142
|
+
return null;
|
|
143
|
+
const ct = (response.headers.get("content-type") || "")
|
|
144
|
+
.split(";")[0]
|
|
145
|
+
.trim();
|
|
146
|
+
if (!ct.includes("markdown") && !ct.includes("text/plain"))
|
|
147
|
+
return null;
|
|
148
|
+
const mdBuffer = Buffer.from(await response.arrayBuffer());
|
|
149
|
+
return this.convert(mdBuffer, {
|
|
150
|
+
url: mdSourceUrl,
|
|
151
|
+
mimetype: "text/markdown",
|
|
152
|
+
extension: ".md",
|
|
153
|
+
filename: basename(new URL(mdSourceUrl).pathname),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
111
159
|
}
|
|
112
160
|
/**
|
|
113
161
|
* Convert a buffer with stream info to markdown.
|
|
@@ -136,3 +184,38 @@ export class Markit {
|
|
|
136
184
|
throw new Error(`Unsupported format: ${streamInfo.extension || streamInfo.mimetype || "unknown"}`);
|
|
137
185
|
}
|
|
138
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Try to discover a raw markdown source URL from an HTML response.
|
|
189
|
+
* Checks multiple patterns:
|
|
190
|
+
* 1. <link rel="alternate" type="text/markdown" href="..."> tag
|
|
191
|
+
* 2. VitePress markers → append .md to the URL
|
|
192
|
+
* 3. llms.txt convention → try url.md or url.html.md
|
|
193
|
+
*
|
|
194
|
+
* @internal Exported for testing.
|
|
195
|
+
*/
|
|
196
|
+
export function discoverMarkdownSource(html, url, ext) {
|
|
197
|
+
// 1. Look for <link rel="alternate" type="text/markdown" href="...">
|
|
198
|
+
const linkMatch = html.match(/<link[^>]+rel=["']alternate["'][^>]+type=["']text\/markdown["'][^>]+href=["']([^"']+)["']/i) ??
|
|
199
|
+
html.match(/<link[^>]+type=["']text\/markdown["'][^>]+rel=["']alternate["'][^>]+href=["']([^"']+)["']/i);
|
|
200
|
+
if (linkMatch?.[1]) {
|
|
201
|
+
try {
|
|
202
|
+
return new URL(linkMatch[1], url).href;
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
/* ignore malformed URLs */
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
// 2. VitePress detection — serves .md alongside HTML
|
|
209
|
+
const isVitePress = html.includes("__VP_HASH_MAP__") ||
|
|
210
|
+
html.includes("VPContent") ||
|
|
211
|
+
html.includes("vitepress");
|
|
212
|
+
// 3. llms.txt convention: try url.md for extensionless URLs
|
|
213
|
+
const hasLlmsTxt = html.includes("llms.txt");
|
|
214
|
+
if (!ext && (isVitePress || hasLlmsTxt)) {
|
|
215
|
+
return appendMdExtension(url);
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
function appendMdExtension(url) {
|
|
220
|
+
return url.endsWith("/") ? `${url.slice(0, -1)}.md` : `${url}.md`;
|
|
221
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "markit-ai",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Convert anything to markdown. PDF, DOCX, PPTX, XLSX, HTML, EPUB, Jupyter, RSS, images, audio, URLs, and more. Pluggable converters, built-in LLM providers for image description and audio transcription. Works as a CLI and as a library.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|