@bendyline/squisq-formats 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/dist/chunk-532L4D5D.js +21 -0
- package/dist/chunk-532L4D5D.js.map +1 -0
- package/dist/chunk-67KIJHV2.js +21 -0
- package/dist/chunk-67KIJHV2.js.map +1 -0
- package/dist/chunk-KAK4V57E.js +387 -0
- package/dist/chunk-KAK4V57E.js.map +1 -0
- package/dist/chunk-MQHCXI56.js +1035 -0
- package/dist/chunk-MQHCXI56.js.map +1 -0
- package/dist/chunk-TBPD5PCU.js +1136 -0
- package/dist/chunk-TBPD5PCU.js.map +1 -0
- package/dist/chunk-ULLIPBEJ.js +271 -0
- package/dist/chunk-ULLIPBEJ.js.map +1 -0
- package/dist/docx/index.d.ts +108 -0
- package/dist/docx/index.js +14 -0
- package/dist/docx/index.js.map +1 -0
- package/dist/html/index.d.ts +166 -0
- package/dist/html/index.js +17 -0
- package/dist/html/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/ooxml/index.d.ts +351 -0
- package/dist/ooxml/index.js +99 -0
- package/dist/ooxml/index.js.map +1 -0
- package/dist/pdf/index.d.ts +130 -0
- package/dist/pdf/index.js +15 -0
- package/dist/pdf/index.js.map +1 -0
- package/dist/pptx/index.d.ts +58 -0
- package/dist/pptx/index.js +13 -0
- package/dist/pptx/index.js.map +1 -0
- package/dist/xlsx/index.d.ts +58 -0
- package/dist/xlsx/index.js +13 -0
- package/dist/xlsx/index.js.map +1 -0
- package/package.json +85 -0
- package/src/__tests__/docxExport.test.ts +457 -0
- package/src/__tests__/docxImport.test.ts +410 -0
- package/src/__tests__/html.test.ts +295 -0
- package/src/__tests__/ooxml.test.ts +197 -0
- package/src/__tests__/pdfExport.test.ts +322 -0
- package/src/__tests__/pdfImport.test.ts +290 -0
- package/src/__tests__/roundTrip.test.ts +201 -0
- package/src/docx/export.ts +978 -0
- package/src/docx/import.ts +909 -0
- package/src/docx/index.ts +26 -0
- package/src/docx/styles.ts +145 -0
- package/src/html/htmlTemplate.ts +307 -0
- package/src/html/imageUtils.ts +66 -0
- package/src/html/index.ts +168 -0
- package/src/index.ts +51 -0
- package/src/ooxml/index.ts +87 -0
- package/src/ooxml/namespaces.ts +160 -0
- package/src/ooxml/reader.ts +218 -0
- package/src/ooxml/types.ts +104 -0
- package/src/ooxml/writer.ts +321 -0
- package/src/ooxml/xmlUtils.ts +123 -0
- package/src/pdf/export.ts +1029 -0
- package/src/pdf/import.ts +835 -0
- package/src/pdf/index.ts +29 -0
- package/src/pdf/styles.ts +180 -0
- package/src/pptx/index.ts +78 -0
- package/src/xlsx/index.ts +78 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for PDF export: markdownDocToPdf, docToPdf.
|
|
3
|
+
*
|
|
4
|
+
* Exports various MarkdownDocuments to PDF, then re-opens the
|
|
5
|
+
* resulting ArrayBuffers with pdf-lib to verify structure/metadata.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect } from 'vitest';
|
|
9
|
+
import { PDFDocument } from 'pdf-lib';
|
|
10
|
+
import type {
|
|
11
|
+
MarkdownDocument,
|
|
12
|
+
MarkdownHeading,
|
|
13
|
+
MarkdownParagraph,
|
|
14
|
+
MarkdownList,
|
|
15
|
+
MarkdownListItem,
|
|
16
|
+
MarkdownTable,
|
|
17
|
+
MarkdownTableRow,
|
|
18
|
+
MarkdownTableCell,
|
|
19
|
+
MarkdownCodeBlock,
|
|
20
|
+
MarkdownBlockquote,
|
|
21
|
+
MarkdownThematicBreak,
|
|
22
|
+
MarkdownText,
|
|
23
|
+
MarkdownStrong,
|
|
24
|
+
MarkdownEmphasis,
|
|
25
|
+
MarkdownInlineCode,
|
|
26
|
+
MarkdownLink,
|
|
27
|
+
} from '@bendyline/squisq/markdown';
|
|
28
|
+
|
|
29
|
+
import { markdownDocToPdf, docToPdf } from '../pdf/export';
|
|
30
|
+
import type { Doc } from '@bendyline/squisq/schemas';
|
|
31
|
+
|
|
32
|
+
// ============================================
|
|
33
|
+
// Helpers
|
|
34
|
+
// ============================================
|
|
35
|
+
|
|
36
|
+
async function exportAndLoad(
|
|
37
|
+
doc: MarkdownDocument,
|
|
38
|
+
options?: Parameters<typeof markdownDocToPdf>[1],
|
|
39
|
+
) {
|
|
40
|
+
const buffer = await markdownDocToPdf(doc, options);
|
|
41
|
+
expect(buffer).toBeInstanceOf(ArrayBuffer);
|
|
42
|
+
expect(buffer.byteLength).toBeGreaterThan(0);
|
|
43
|
+
return PDFDocument.load(buffer);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function text(value: string): MarkdownText {
|
|
47
|
+
return { type: 'text', value };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ============================================
|
|
51
|
+
// Basic Structure
|
|
52
|
+
// ============================================
|
|
53
|
+
|
|
54
|
+
describe('markdownDocToPdf', () => {
|
|
55
|
+
it('produces a valid PDF for an empty document', async () => {
|
|
56
|
+
const doc: MarkdownDocument = { type: 'document', children: [] };
|
|
57
|
+
const pdf = await exportAndLoad(doc);
|
|
58
|
+
expect(pdf.getPageCount()).toBeGreaterThanOrEqual(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('sets title and author metadata', async () => {
|
|
62
|
+
const doc: MarkdownDocument = { type: 'document', children: [] };
|
|
63
|
+
const pdf = await exportAndLoad(doc, { title: 'Test Title', author: 'Test Author' });
|
|
64
|
+
expect(pdf.getTitle()).toBe('Test Title');
|
|
65
|
+
expect(pdf.getAuthor()).toBe('Test Author');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('renders a single paragraph', async () => {
|
|
69
|
+
const doc: MarkdownDocument = {
|
|
70
|
+
type: 'document',
|
|
71
|
+
children: [
|
|
72
|
+
{
|
|
73
|
+
type: 'paragraph',
|
|
74
|
+
children: [text('Hello, World!')],
|
|
75
|
+
} as MarkdownParagraph,
|
|
76
|
+
],
|
|
77
|
+
};
|
|
78
|
+
const pdf = await exportAndLoad(doc);
|
|
79
|
+
expect(pdf.getPageCount()).toBe(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('renders headings at various depths', async () => {
|
|
83
|
+
const children: MarkdownHeading[] = [1, 2, 3, 4, 5, 6].map((d) => ({
|
|
84
|
+
type: 'heading',
|
|
85
|
+
depth: d as 1 | 2 | 3 | 4 | 5 | 6,
|
|
86
|
+
children: [text(`Heading Level ${d}`)],
|
|
87
|
+
}));
|
|
88
|
+
const doc: MarkdownDocument = { type: 'document', children };
|
|
89
|
+
const pdf = await exportAndLoad(doc);
|
|
90
|
+
expect(pdf.getPageCount()).toBeGreaterThanOrEqual(1);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('renders inline formatting (bold, italic, code)', async () => {
|
|
94
|
+
const doc: MarkdownDocument = {
|
|
95
|
+
type: 'document',
|
|
96
|
+
children: [
|
|
97
|
+
{
|
|
98
|
+
type: 'paragraph',
|
|
99
|
+
children: [
|
|
100
|
+
text('Normal '),
|
|
101
|
+
{ type: 'strong', children: [text('bold')] } as MarkdownStrong,
|
|
102
|
+
text(' '),
|
|
103
|
+
{ type: 'emphasis', children: [text('italic')] } as MarkdownEmphasis,
|
|
104
|
+
text(' '),
|
|
105
|
+
{ type: 'inlineCode', value: 'code' } as MarkdownInlineCode,
|
|
106
|
+
],
|
|
107
|
+
} as MarkdownParagraph,
|
|
108
|
+
],
|
|
109
|
+
};
|
|
110
|
+
// Should not throw
|
|
111
|
+
const pdf = await exportAndLoad(doc);
|
|
112
|
+
expect(pdf.getPageCount()).toBe(1);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('renders a link', async () => {
|
|
116
|
+
const doc: MarkdownDocument = {
|
|
117
|
+
type: 'document',
|
|
118
|
+
children: [
|
|
119
|
+
{
|
|
120
|
+
type: 'paragraph',
|
|
121
|
+
children: [
|
|
122
|
+
text('See '),
|
|
123
|
+
{
|
|
124
|
+
type: 'link',
|
|
125
|
+
url: 'https://example.com',
|
|
126
|
+
children: [text('Example')],
|
|
127
|
+
} as MarkdownLink,
|
|
128
|
+
],
|
|
129
|
+
} as MarkdownParagraph,
|
|
130
|
+
],
|
|
131
|
+
};
|
|
132
|
+
const pdf = await exportAndLoad(doc);
|
|
133
|
+
expect(pdf.getPageCount()).toBe(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('renders unordered and ordered lists', async () => {
|
|
137
|
+
const unordered: MarkdownList = {
|
|
138
|
+
type: 'list',
|
|
139
|
+
ordered: false,
|
|
140
|
+
children: [
|
|
141
|
+
{
|
|
142
|
+
type: 'listItem',
|
|
143
|
+
children: [{ type: 'paragraph', children: [text('Apple')] } as MarkdownParagraph],
|
|
144
|
+
} as MarkdownListItem,
|
|
145
|
+
{
|
|
146
|
+
type: 'listItem',
|
|
147
|
+
children: [{ type: 'paragraph', children: [text('Banana')] } as MarkdownParagraph],
|
|
148
|
+
} as MarkdownListItem,
|
|
149
|
+
],
|
|
150
|
+
};
|
|
151
|
+
const ordered: MarkdownList = {
|
|
152
|
+
type: 'list',
|
|
153
|
+
ordered: true,
|
|
154
|
+
children: [
|
|
155
|
+
{
|
|
156
|
+
type: 'listItem',
|
|
157
|
+
children: [{ type: 'paragraph', children: [text('First')] } as MarkdownParagraph],
|
|
158
|
+
} as MarkdownListItem,
|
|
159
|
+
{
|
|
160
|
+
type: 'listItem',
|
|
161
|
+
children: [{ type: 'paragraph', children: [text('Second')] } as MarkdownParagraph],
|
|
162
|
+
} as MarkdownListItem,
|
|
163
|
+
],
|
|
164
|
+
};
|
|
165
|
+
const doc: MarkdownDocument = { type: 'document', children: [unordered, ordered] };
|
|
166
|
+
const pdf = await exportAndLoad(doc);
|
|
167
|
+
expect(pdf.getPageCount()).toBeGreaterThanOrEqual(1);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('renders a code block', async () => {
|
|
171
|
+
const doc: MarkdownDocument = {
|
|
172
|
+
type: 'document',
|
|
173
|
+
children: [
|
|
174
|
+
{
|
|
175
|
+
type: 'code',
|
|
176
|
+
value: 'function hello() {\n return "world";\n}',
|
|
177
|
+
} as MarkdownCodeBlock,
|
|
178
|
+
],
|
|
179
|
+
};
|
|
180
|
+
const pdf = await exportAndLoad(doc);
|
|
181
|
+
expect(pdf.getPageCount()).toBe(1);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('renders a blockquote', async () => {
|
|
185
|
+
const doc: MarkdownDocument = {
|
|
186
|
+
type: 'document',
|
|
187
|
+
children: [
|
|
188
|
+
{
|
|
189
|
+
type: 'blockquote',
|
|
190
|
+
children: [{ type: 'paragraph', children: [text('Quoted text')] } as MarkdownParagraph],
|
|
191
|
+
} as MarkdownBlockquote,
|
|
192
|
+
],
|
|
193
|
+
};
|
|
194
|
+
const pdf = await exportAndLoad(doc);
|
|
195
|
+
expect(pdf.getPageCount()).toBe(1);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('renders a table', async () => {
|
|
199
|
+
const row = (cells: string[], isHeader: boolean): MarkdownTableRow => ({
|
|
200
|
+
type: 'tableRow',
|
|
201
|
+
children: cells.map(
|
|
202
|
+
(c) =>
|
|
203
|
+
({
|
|
204
|
+
type: 'tableCell',
|
|
205
|
+
isHeader,
|
|
206
|
+
children: [text(c)],
|
|
207
|
+
}) as MarkdownTableCell,
|
|
208
|
+
),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const doc: MarkdownDocument = {
|
|
212
|
+
type: 'document',
|
|
213
|
+
children: [
|
|
214
|
+
{
|
|
215
|
+
type: 'table',
|
|
216
|
+
children: [
|
|
217
|
+
row(['Name', 'Age', 'City'], true),
|
|
218
|
+
row(['Alice', '30', 'NYC'], false),
|
|
219
|
+
row(['Bob', '25', 'LA'], false),
|
|
220
|
+
],
|
|
221
|
+
} as MarkdownTable,
|
|
222
|
+
],
|
|
223
|
+
};
|
|
224
|
+
const pdf = await exportAndLoad(doc);
|
|
225
|
+
expect(pdf.getPageCount()).toBeGreaterThanOrEqual(1);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('renders a thematic break', async () => {
|
|
229
|
+
const doc: MarkdownDocument = {
|
|
230
|
+
type: 'document',
|
|
231
|
+
children: [
|
|
232
|
+
{ type: 'paragraph', children: [text('Before')] } as MarkdownParagraph,
|
|
233
|
+
{ type: 'thematicBreak' } as MarkdownThematicBreak,
|
|
234
|
+
{ type: 'paragraph', children: [text('After')] } as MarkdownParagraph,
|
|
235
|
+
],
|
|
236
|
+
};
|
|
237
|
+
const pdf = await exportAndLoad(doc);
|
|
238
|
+
expect(pdf.getPageCount()).toBe(1);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('uses A4 page size when specified', async () => {
|
|
242
|
+
const doc: MarkdownDocument = {
|
|
243
|
+
type: 'document',
|
|
244
|
+
children: [{ type: 'paragraph', children: [text('A4 page')] } as MarkdownParagraph],
|
|
245
|
+
};
|
|
246
|
+
const pdf = await exportAndLoad(doc, { pageSize: 'a4' });
|
|
247
|
+
const page = pdf.getPage(0);
|
|
248
|
+
const { width, height } = page.getSize();
|
|
249
|
+
// A4 is approximately 595 × 842
|
|
250
|
+
expect(width).toBeCloseTo(595.28, 0);
|
|
251
|
+
expect(height).toBeCloseTo(841.89, 0);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('paginates long documents', async () => {
|
|
255
|
+
const children: MarkdownParagraph[] = [];
|
|
256
|
+
// Each paragraph is a few lines — 100 paragraphs should overflow a page
|
|
257
|
+
for (let i = 0; i < 100; i++) {
|
|
258
|
+
children.push({
|
|
259
|
+
type: 'paragraph',
|
|
260
|
+
children: [text(`Paragraph number ${i + 1}. This is some body text to fill the page.`)],
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
const doc: MarkdownDocument = { type: 'document', children };
|
|
264
|
+
const pdf = await exportAndLoad(doc);
|
|
265
|
+
expect(pdf.getPageCount()).toBeGreaterThan(1);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('handles nested lists', async () => {
|
|
269
|
+
const inner: MarkdownList = {
|
|
270
|
+
type: 'list',
|
|
271
|
+
ordered: false,
|
|
272
|
+
children: [
|
|
273
|
+
{
|
|
274
|
+
type: 'listItem',
|
|
275
|
+
children: [{ type: 'paragraph', children: [text('Nested item')] } as MarkdownParagraph],
|
|
276
|
+
} as MarkdownListItem,
|
|
277
|
+
],
|
|
278
|
+
};
|
|
279
|
+
const outer: MarkdownList = {
|
|
280
|
+
type: 'list',
|
|
281
|
+
ordered: false,
|
|
282
|
+
children: [
|
|
283
|
+
{
|
|
284
|
+
type: 'listItem',
|
|
285
|
+
children: [
|
|
286
|
+
{ type: 'paragraph', children: [text('Outer item')] } as MarkdownParagraph,
|
|
287
|
+
inner,
|
|
288
|
+
],
|
|
289
|
+
} as MarkdownListItem,
|
|
290
|
+
],
|
|
291
|
+
};
|
|
292
|
+
const doc: MarkdownDocument = { type: 'document', children: [outer] };
|
|
293
|
+
const pdf = await exportAndLoad(doc);
|
|
294
|
+
expect(pdf.getPageCount()).toBeGreaterThanOrEqual(1);
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// ============================================
|
|
299
|
+
// docToPdf convenience wrapper
|
|
300
|
+
// ============================================
|
|
301
|
+
|
|
302
|
+
describe('docToPdf', () => {
|
|
303
|
+
it('converts a Doc object to PDF', async () => {
|
|
304
|
+
const doc: Doc = {
|
|
305
|
+
articleId: 'test-article',
|
|
306
|
+
duration: 3,
|
|
307
|
+
audio: { segments: [] },
|
|
308
|
+
blocks: [
|
|
309
|
+
{
|
|
310
|
+
id: 'block-1',
|
|
311
|
+
startTime: 0,
|
|
312
|
+
duration: 3,
|
|
313
|
+
audioSegment: 0,
|
|
314
|
+
template: 'titleBlock',
|
|
315
|
+
},
|
|
316
|
+
],
|
|
317
|
+
};
|
|
318
|
+
const buffer = await docToPdf(doc);
|
|
319
|
+
expect(buffer).toBeInstanceOf(ArrayBuffer);
|
|
320
|
+
expect(buffer.byteLength).toBeGreaterThan(0);
|
|
321
|
+
});
|
|
322
|
+
});
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for PDF import: pdfToMarkdownDoc, pdfToDoc.
|
|
3
|
+
*
|
|
4
|
+
* Builds PDFs with pdf-lib (adding known text), then imports them
|
|
5
|
+
* via pdfToMarkdownDoc and inspects the resulting MarkdownDocument.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: pdfjs-dist text extraction quality depends on the environment.
|
|
8
|
+
* Standard fonts embedded by pdf-lib should produce readable text items.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from 'vitest';
|
|
12
|
+
import { PDFDocument, StandardFonts, rgb } from 'pdf-lib';
|
|
13
|
+
|
|
14
|
+
import { pdfToMarkdownDoc, pdfToDoc } from '../pdf/import';
|
|
15
|
+
import type {
|
|
16
|
+
MarkdownHeading,
|
|
17
|
+
MarkdownBlockNode,
|
|
18
|
+
MarkdownCodeBlock,
|
|
19
|
+
} from '@bendyline/squisq/markdown';
|
|
20
|
+
|
|
21
|
+
/** Structural shape shared by all markdown node types, for recursive walkers. */
|
|
22
|
+
type TreeNode = { type: string; value?: string; children?: TreeNode[] };
|
|
23
|
+
|
|
24
|
+
// ============================================
|
|
25
|
+
// Helpers
|
|
26
|
+
// ============================================
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create a simple PDF buffer with text drawn at specified sizes/positions.
|
|
30
|
+
*/
|
|
31
|
+
async function buildSimplePdf(
|
|
32
|
+
lines: Array<{ text: string; x: number; y: number; fontSize: number; fontName?: string }>,
|
|
33
|
+
): Promise<Uint8Array> {
|
|
34
|
+
const doc = await PDFDocument.create();
|
|
35
|
+
const page = doc.addPage([612, 792]); // Letter
|
|
36
|
+
|
|
37
|
+
const regular = await doc.embedFont(StandardFonts.Helvetica);
|
|
38
|
+
const bold = await doc.embedFont(StandardFonts.HelveticaBold);
|
|
39
|
+
const italic = await doc.embedFont(StandardFonts.HelveticaOblique);
|
|
40
|
+
const mono = await doc.embedFont(StandardFonts.Courier);
|
|
41
|
+
|
|
42
|
+
for (const line of lines) {
|
|
43
|
+
let font = regular;
|
|
44
|
+
const fName = line.fontName?.toLowerCase() ?? '';
|
|
45
|
+
if (fName.includes('bold')) font = bold;
|
|
46
|
+
else if (fName.includes('italic') || fName.includes('oblique')) font = italic;
|
|
47
|
+
else if (fName.includes('courier') || fName.includes('mono')) font = mono;
|
|
48
|
+
|
|
49
|
+
page.drawText(line.text, {
|
|
50
|
+
x: line.x,
|
|
51
|
+
y: line.y,
|
|
52
|
+
size: line.fontSize,
|
|
53
|
+
font,
|
|
54
|
+
color: rgb(0, 0, 0),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return doc.save();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function flatText(node: MarkdownBlockNode): string {
|
|
62
|
+
const parts: string[] = [];
|
|
63
|
+
function walk(n: TreeNode) {
|
|
64
|
+
if (n.type === 'text' && n.value) parts.push(n.value);
|
|
65
|
+
if (n.value && n.type === 'code') parts.push(n.value);
|
|
66
|
+
if (n.value && n.type === 'inlineCode') parts.push(n.value);
|
|
67
|
+
if (n.children) for (const c of n.children) walk(c);
|
|
68
|
+
}
|
|
69
|
+
walk(node);
|
|
70
|
+
return parts.join('');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ============================================
|
|
74
|
+
// Basic Import
|
|
75
|
+
// ============================================
|
|
76
|
+
|
|
77
|
+
describe('pdfToMarkdownDoc', () => {
|
|
78
|
+
it('returns an empty document for a blank PDF', async () => {
|
|
79
|
+
const doc = await PDFDocument.create();
|
|
80
|
+
doc.addPage([612, 792]);
|
|
81
|
+
const buffer = await doc.save();
|
|
82
|
+
|
|
83
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
84
|
+
expect(md.type).toBe('document');
|
|
85
|
+
expect(md.children.length).toBe(0);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('imports a single paragraph', async () => {
|
|
89
|
+
const buffer = await buildSimplePdf([{ text: 'Hello World', x: 72, y: 700, fontSize: 11 }]);
|
|
90
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
91
|
+
expect(md.children.length).toBeGreaterThanOrEqual(1);
|
|
92
|
+
|
|
93
|
+
// The first block should contain "Hello World"
|
|
94
|
+
const allText = md.children.map(flatText).join(' ');
|
|
95
|
+
expect(allText).toContain('Hello');
|
|
96
|
+
expect(allText).toContain('World');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('detects a heading by font size', async () => {
|
|
100
|
+
const buffer = await buildSimplePdf([
|
|
101
|
+
{ text: 'Big Title', x: 72, y: 700, fontSize: 24 },
|
|
102
|
+
{ text: 'Body text here.', x: 72, y: 660, fontSize: 11 },
|
|
103
|
+
]);
|
|
104
|
+
// Provide explicit bodyFontSize — with only 2 lines auto-detection
|
|
105
|
+
// picks the first seen size (24) which masks the heading.
|
|
106
|
+
const md = await pdfToMarkdownDoc(buffer, { bodyFontSize: 11 });
|
|
107
|
+
|
|
108
|
+
// Find heading block
|
|
109
|
+
const headings = md.children.filter((c) => c.type === 'heading');
|
|
110
|
+
expect(headings.length).toBeGreaterThanOrEqual(1);
|
|
111
|
+
if (headings.length > 0) {
|
|
112
|
+
const h = headings[0] as MarkdownHeading;
|
|
113
|
+
expect(h.depth).toBeLessThanOrEqual(2);
|
|
114
|
+
expect(flatText(h)).toContain('Title');
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('renders bold text as plain text (standard font limitation)', async () => {
|
|
119
|
+
// pdfjs-dist returns opaque font IDs ("g_d0_f2") for standard PDF fonts,
|
|
120
|
+
// not the actual font name. Bold/italic detection only works with
|
|
121
|
+
// embedded fonts whose names contain "Bold"/"Italic" — a known heuristic
|
|
122
|
+
// limitation. Here we verify the text is still extracted.
|
|
123
|
+
const buffer = await buildSimplePdf([
|
|
124
|
+
{ text: 'Bold text', x: 72, y: 700, fontSize: 11, fontName: 'Helvetica-Bold' },
|
|
125
|
+
]);
|
|
126
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
127
|
+
expect(md.children.length).toBeGreaterThanOrEqual(1);
|
|
128
|
+
const allText = md.children.map(flatText).join(' ');
|
|
129
|
+
expect(allText).toContain('Bold text');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('renders italic text as plain text (standard font limitation)', async () => {
|
|
133
|
+
// Same as bold — standard PDF font names are not exposed by pdfjs-dist.
|
|
134
|
+
const buffer = await buildSimplePdf([
|
|
135
|
+
{ text: 'Italic text', x: 72, y: 700, fontSize: 11, fontName: 'Helvetica-Oblique' },
|
|
136
|
+
]);
|
|
137
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
138
|
+
expect(md.children.length).toBeGreaterThanOrEqual(1);
|
|
139
|
+
const allText = md.children.map(flatText).join(' ');
|
|
140
|
+
expect(allText).toContain('Italic text');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('detects monospace text as inline code', async () => {
|
|
144
|
+
const buffer = await buildSimplePdf([
|
|
145
|
+
{ text: 'const x = 1;', x: 72, y: 700, fontSize: 10, fontName: 'Courier' },
|
|
146
|
+
]);
|
|
147
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
148
|
+
expect(md.children.length).toBeGreaterThanOrEqual(1);
|
|
149
|
+
|
|
150
|
+
// Monospace lines should become code blocks or inline code
|
|
151
|
+
function hasCode(node: TreeNode): boolean {
|
|
152
|
+
if (node.type === 'code' || node.type === 'inlineCode') return true;
|
|
153
|
+
if (node.children) return node.children.some(hasCode);
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
expect(md.children.some(hasCode)).toBe(true);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('detects consecutive monospace lines as a code block', async () => {
|
|
160
|
+
const buffer = await buildSimplePdf([
|
|
161
|
+
{ text: 'function hello() {', x: 72, y: 700, fontSize: 10, fontName: 'Courier' },
|
|
162
|
+
{ text: ' return 42;', x: 72, y: 686, fontSize: 10, fontName: 'Courier' },
|
|
163
|
+
{ text: '}', x: 72, y: 672, fontSize: 10, fontName: 'Courier' },
|
|
164
|
+
]);
|
|
165
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
166
|
+
|
|
167
|
+
const codeBlocks = md.children.filter((c) => c.type === 'code');
|
|
168
|
+
expect(codeBlocks.length).toBeGreaterThanOrEqual(1);
|
|
169
|
+
if (codeBlocks.length > 0) {
|
|
170
|
+
const cb = codeBlocks[0] as MarkdownCodeBlock;
|
|
171
|
+
expect(cb.value).toContain('function');
|
|
172
|
+
expect(cb.value).toContain('return');
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('detects bullet list items', async () => {
|
|
177
|
+
const buffer = await buildSimplePdf([
|
|
178
|
+
{ text: '\u2022 First item', x: 72, y: 700, fontSize: 11 },
|
|
179
|
+
{ text: '\u2022 Second item', x: 72, y: 684, fontSize: 11 },
|
|
180
|
+
]);
|
|
181
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
182
|
+
|
|
183
|
+
const lists = md.children.filter((c) => c.type === 'list');
|
|
184
|
+
expect(lists.length).toBeGreaterThanOrEqual(1);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('detects ordered list items', async () => {
|
|
188
|
+
const buffer = await buildSimplePdf([
|
|
189
|
+
{ text: '1. First step', x: 72, y: 700, fontSize: 11 },
|
|
190
|
+
{ text: '2. Second step', x: 72, y: 684, fontSize: 11 },
|
|
191
|
+
]);
|
|
192
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
193
|
+
|
|
194
|
+
const lists = md.children.filter((c) => c.type === 'list');
|
|
195
|
+
expect(lists.length).toBeGreaterThanOrEqual(1);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('detects indented text as blockquote', async () => {
|
|
199
|
+
const buffer = await buildSimplePdf([
|
|
200
|
+
{ text: 'Normal paragraph.', x: 72, y: 720, fontSize: 11 },
|
|
201
|
+
{ text: 'This is a quote.', x: 110, y: 700, fontSize: 11 },
|
|
202
|
+
]);
|
|
203
|
+
const md = await pdfToMarkdownDoc(buffer, { detectBlockquotes: true });
|
|
204
|
+
|
|
205
|
+
const quotes = md.children.filter((c) => c.type === 'blockquote');
|
|
206
|
+
expect(quotes.length).toBeGreaterThanOrEqual(1);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('detects URLs in text as links', async () => {
|
|
210
|
+
const buffer = await buildSimplePdf([
|
|
211
|
+
{ text: 'Visit https://example.com for more.', x: 72, y: 700, fontSize: 11 },
|
|
212
|
+
]);
|
|
213
|
+
const md = await pdfToMarkdownDoc(buffer, { detectLinks: true });
|
|
214
|
+
|
|
215
|
+
function hasLink(node: TreeNode): boolean {
|
|
216
|
+
if (node.type === 'link') return true;
|
|
217
|
+
if (node.children) return node.children.some(hasLink);
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
expect(md.children.some(hasLink)).toBe(true);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('handles multi-page documents', async () => {
|
|
224
|
+
const doc = await PDFDocument.create();
|
|
225
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
226
|
+
|
|
227
|
+
for (let p = 0; p < 3; p++) {
|
|
228
|
+
const page = doc.addPage([612, 792]);
|
|
229
|
+
page.drawText(`Page ${p + 1} content`, {
|
|
230
|
+
x: 72,
|
|
231
|
+
y: 700,
|
|
232
|
+
size: 11,
|
|
233
|
+
font,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
const buffer = await doc.save();
|
|
237
|
+
|
|
238
|
+
const md = await pdfToMarkdownDoc(buffer);
|
|
239
|
+
expect(md.children.length).toBeGreaterThanOrEqual(3);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('respects bodyFontSize option', async () => {
|
|
243
|
+
// If we say body is 14pt, then a 16pt line should still be heading
|
|
244
|
+
// but borderline 13pt would not be
|
|
245
|
+
const buffer = await buildSimplePdf([
|
|
246
|
+
{ text: 'Medium heading', x: 72, y: 700, fontSize: 16 },
|
|
247
|
+
{ text: 'Body text at 14pt', x: 72, y: 670, fontSize: 14 },
|
|
248
|
+
]);
|
|
249
|
+
const md = await pdfToMarkdownDoc(buffer, { bodyFontSize: 14 });
|
|
250
|
+
|
|
251
|
+
const headings = md.children.filter((c) => c.type === 'heading');
|
|
252
|
+
expect(headings.length).toBeGreaterThanOrEqual(1);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('accepts Uint8Array input', async () => {
|
|
256
|
+
const buffer = await buildSimplePdf([{ text: 'Uint8Array test', x: 72, y: 700, fontSize: 11 }]);
|
|
257
|
+
const uint8 = new Uint8Array(buffer);
|
|
258
|
+
const md = await pdfToMarkdownDoc(uint8);
|
|
259
|
+
expect(md.children.length).toBeGreaterThanOrEqual(1);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('can disable table detection', async () => {
|
|
263
|
+
const buffer = await buildSimplePdf([
|
|
264
|
+
{ text: 'Col A', x: 72, y: 700, fontSize: 11 },
|
|
265
|
+
{ text: 'Col B', x: 250, y: 700, fontSize: 11 },
|
|
266
|
+
{ text: 'Val 1', x: 72, y: 684, fontSize: 11 },
|
|
267
|
+
{ text: 'Val 2', x: 250, y: 684, fontSize: 11 },
|
|
268
|
+
]);
|
|
269
|
+
const md = await pdfToMarkdownDoc(buffer, { detectTables: false });
|
|
270
|
+
const tables = md.children.filter((c) => c.type === 'table');
|
|
271
|
+
expect(tables.length).toBe(0);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// ============================================
|
|
276
|
+
// pdfToDoc convenience wrapper
|
|
277
|
+
// ============================================
|
|
278
|
+
|
|
279
|
+
describe('pdfToDoc', () => {
|
|
280
|
+
it('converts PDF to a Doc object', async () => {
|
|
281
|
+
const buffer = await buildSimplePdf([
|
|
282
|
+
{ text: 'Test Title', x: 72, y: 700, fontSize: 24 },
|
|
283
|
+
{ text: 'Some body text.', x: 72, y: 660, fontSize: 11 },
|
|
284
|
+
]);
|
|
285
|
+
const doc = await pdfToDoc(buffer);
|
|
286
|
+
expect(doc).toBeDefined();
|
|
287
|
+
expect(doc.blocks).toBeDefined();
|
|
288
|
+
expect(doc.blocks.length).toBeGreaterThan(0);
|
|
289
|
+
});
|
|
290
|
+
});
|