-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbun-basic.ts
More file actions
44 lines (38 loc) · 1.77 KB
/
Copy pathbun-basic.ts
File metadata and controls
44 lines (38 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// docling.rs from Bun, in TypeScript. Run with:
//
// bun run examples/bun-basic.ts
//
// Bun implements N-API, so the exact same native addon loads — no rebuild. This
// example leans on the TypeScript types and shows async conversion and the
// streaming async generator. It also runs unchanged under `tsx`/`ts-node` on Node.
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import {
convertFileAsync,
streamFileMarkdown,
DocumentConverter,
type ConvertResult,
} from 'docling.rs'
const here = dirname(fileURLToPath(import.meta.url))
const html = join(here, 'inputs', 'sample.html')
// 1. Async conversion — the CPU-bound work runs off the event loop, so this
// scales to PDF/image without blocking. Fully typed: `res` is ConvertResult.
const res: ConvertResult = await convertFileAsync(html, { to: 'markdown' })
console.log(`converted "${res.inputName}" (${res.format}) → ${res.status}`)
console.log(res.content)
// 2. Streaming Markdown: chunks arrive in document order as conversion
// progresses. For PDF, pages stream out as each finishes — output starts
// before the whole document is done. Concatenating the chunks equals the
// buffered `content`.
console.log('--- streamed ---')
let streamed = ''
for await (const chunk of streamFileMarkdown(html)) {
process.stdout.write(chunk)
streamed += chunk
}
console.log(`\n(streamed ${streamed.length} bytes)`)
// 3. Embedded images: pictures become inline base64 data URIs, so the Markdown
// is self-contained. (This HTML has none; the option is a no-op here, but
// the same call handles DOCX/PPTX/PDF figures.)
const embedded = new DocumentConverter().convertFile(html, { imageMode: 'embedded' })
console.log('embedded-image Markdown length:', embedded.content.length)