@kakemono/wasm 0.0.0-stage → 0.22.3

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/README.md CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # @kakemono/wasm (experimental)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [kakemono](https://github.com/iamcaominhtien/kakemono) in WebAssembly: convert PDF papers, Word
4
+ documents, Excel workbooks, EPUB books and web pages to Markdown, HTML, Word, LaTeX, Typst, EPUB,
5
+ editable JSON and chunks for RAG, in the browser, in a worker or at the edge, without a server.
6
+
7
+ ```js
8
+ import init, { Document, addFont } from '@kakemono/wasm'
9
+
10
+ await init() // in browsers (Node needs no init)
11
+ const doc = Document.fromBytes(new Uint8Array(await file.arrayBuffer()))
12
+ const { text, files } = doc.toMarkdown() // files: [{ path: 'images/…', data: Uint8Array }]
13
+ const chunks = doc.toChunks({ maxTokens: 512 })
14
+ ```
15
+
16
+ How it differs from `@kakemono/node`:
17
+
18
+ - Synchronous and single-threaded: run it in a Web Worker to keep a page responsive. A call cannot
19
+ be stopped once started (no `signal` or `timeout`): to stop one, terminate its Worker.
20
+ - Documents come from bytes only: no files, no network (fetch them yourself).
21
+ - No installed fonts. Laying out a Word document or a workbook as a PDF (`toPdf()`) needs one:
22
+ give it with `addFont(bytes)` first, or `toPdf()` throws a `DocumentError`. Text in SVG pictures
23
+ drawn as PNG uses the fonts given so too.
24
+ - Tokens of chunks are estimated (no tokenizers).
25
+ - It needs WebAssembly SIMD: every browser since 2023 (Safari 16.4, Chrome 91, Firefox 89), Node
26
+ 16.4+, Deno, Cloudflare Workers. It runs about 1.25 times slower than `@kakemono/node` on one
27
+ thread (8 test papers: 8.4 s against 6.8 s).
28
+ - The module is about 19 MB (7 MB compressed). A panic of kakemono aborts it with a
29
+ `RuntimeError` (its message is logged to `console.error`): load the module again after one.
30
+
31
+ Errors are `Error`s named by their kind: `InvalidArgument` (a wrong option), `DocumentError`,
32
+ `InternalError`.
33
+
34
+ Build: `./build.sh` (needs the `wasm32-unknown-unknown` target and `wasm-bindgen-cli` of the
35
+ version in `Cargo.toml`); test: `npm test`.
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@kakemono/wasm",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.22.3",
4
+ "description": "kakemono in WebAssembly: PDF papers, Word documents, Excel workbooks, EPUB books and web pages to Markdown, HTML, Word, EPUB and chunks for RAG, in the browser (experimental)",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/iamcaominhtien/kakemono.git",
9
+ "directory": "bindings/wasm"
10
+ },
11
+ "keywords": [
12
+ "pdf",
13
+ "docx",
14
+ "xlsx",
15
+ "epub",
16
+ "markdown",
17
+ "wasm",
18
+ "webassembly",
19
+ "browser",
20
+ "rag"
21
+ ],
22
+ "type": "module",
23
+ "exports": {
24
+ ".": {
25
+ "node": {
26
+ "import": {
27
+ "types": "./pkg/node/index.d.mts",
28
+ "default": "./pkg/node/index.mjs"
29
+ },
30
+ "require": {
31
+ "types": "./pkg/node/kakemono_wasm.d.ts",
32
+ "default": "./pkg/node/kakemono_wasm.js"
33
+ }
34
+ },
35
+ "types": "./pkg/web/kakemono_wasm.d.ts",
36
+ "default": "./pkg/web/kakemono_wasm.js"
37
+ }
38
+ },
39
+ "types": "./pkg/web/kakemono_wasm.d.ts",
40
+ "files": [
41
+ "pkg/node",
42
+ "pkg/web",
43
+ "README.md"
44
+ ],
45
+ "scripts": {
46
+ "build": "./build.sh",
47
+ "test": "node --test test/*.test.mjs"
48
+ }
49
+ }
@@ -0,0 +1,171 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /** Options of reading and analysing a document, kept for every output. */
5
+ export interface OpenOptions {
6
+ /** The name recorded as the document's source (nothing by default). */
7
+ sourceName?: string
8
+ /** Rebuild tables as real tables when their grid is clear (default true). */
9
+ tables?: boolean
10
+ /** How hard PNG pictures are compressed; the pixels are the same. */
11
+ pngCompression?: 'fast' | 'balanced' | 'high'
12
+ /** An edited copy of the output of `toJson()` (its text, or the parsed object): every output shows its text. */
13
+ content?: string | object
14
+ /** The header rows of every table of an Excel workbook, given instead of found. */
15
+ headerRows?: number
16
+ }
17
+ type Equations = 'mathml' | 'image'
18
+ /** Options of the HTML output. */
19
+ export interface HtmlOptions {
20
+ mode?: 'hard' | 'hard-95' | 'medium' | 'friendly'
21
+ equations?: Equations
22
+ /** Pixels per PDF point of the pictures, above 0.2 and at most 8 (default 3). */
23
+ scale?: number
24
+ pageImage?: 'svg' | 'svg-only' | 'png'
25
+ embedFonts?: boolean
26
+ /** Pictures as data URIs in the page (default true); otherwise as `files`. */
27
+ inlineImages?: boolean
28
+ imageDir?: string
29
+ }
30
+ /** Options of the Markdown output. */
31
+ export interface MarkdownOptions {
32
+ mode?: 'medium' | 'friendly'
33
+ equations?: Equations
34
+ scale?: number
35
+ /** The folder of the picture files, relative (default 'images'). */
36
+ imageDir?: string
37
+ inlineImages?: boolean
38
+ }
39
+ /** Options of the editable JSON output. */
40
+ export interface JsonOptions { equations?: Equations; scale?: number; imageDir?: string; inlineImages?: boolean }
41
+ /** Options of the LaTeX output. */
42
+ export interface LatexOptions { equations?: Equations; scale?: number; imageDir?: string }
43
+ /** Options of the Typst output. */
44
+ export interface TypstOptions { equations?: Equations; scale?: number; imageDir?: string; layout?: 'auto' | 'article' | 'book' }
45
+ /** Options of the Word output: with `imageDir`, its pictures are files too. */
46
+ export interface DocxOptions { equations?: Equations; scale?: number; imageDir?: string }
47
+ /** Options of the EPUB output. */
48
+ export interface EpubOptions {
49
+ equations?: Equations
50
+ scale?: number
51
+ maxMathWidthEm?: number
52
+ cover?: boolean
53
+ /** The book's date, `CCYY-MM-DDThh:mm:ssZ` (default: a fixed date). */
54
+ modified?: string
55
+ }
56
+ /** Options of the chunks for retrieval (tokens are estimated). */
57
+ export interface ChunkOptions { maxTokens?: number }
58
+ /** A picture file of an output, at a path relative to the output's folder. */
59
+ export interface OutputFile { path: string; data: Uint8Array }
60
+ /** A text output and its picture files. */
61
+ export interface TextOutput { text: string; files: OutputFile[] }
62
+ /** A binary output (Word document, EPUB book, PDF) and its picture files. */
63
+ export interface BinaryOutput { data: Uint8Array; files: OutputFile[] }
64
+ /** A chunk for retrieval. */
65
+ export interface Chunk {
66
+ id: number
67
+ /** The text to embed: the section path, a blank line, then the text. */
68
+ text: string
69
+ tokens: number
70
+ title?: string
71
+ section: string[]
72
+ blocks: number[]
73
+ /** The pages it comes from, from 0 (`[0]` for documents without pages). */
74
+ pages: number[]
75
+ }
76
+
77
+
78
+
79
+ /**
80
+ * A PDF, Word document, Excel workbook, EPUB book or web page, read and
81
+ * analysed once: every output written from it shares that analysis.
82
+ */
83
+ export class Document {
84
+ private constructor();
85
+ free(): void;
86
+ [Symbol.dispose](): void;
87
+ /**
88
+ * The structure of the document (its blocks in reading order), as the
89
+ * command line's `--json` writes it.
90
+ */
91
+ analyze(): any;
92
+ /**
93
+ * Reads and analyses the bytes of a document (a PDF, a Word document,
94
+ * an Excel workbook, an EPUB book or a web page).
95
+ */
96
+ static fromBytes(data: Uint8Array | number[], opts: OpenOptions): Document;
97
+ /**
98
+ * Chunks for retrieval (RAG), cut at sentence ends and headed by their
99
+ * section.
100
+ */
101
+ toChunks(opts: ChunkOptions): Chunk[];
102
+ /**
103
+ * A Word document (`.docx`).
104
+ */
105
+ toDocx(opts: DocxOptions): BinaryOutput;
106
+ /**
107
+ * An EPUB 3 book.
108
+ */
109
+ toEpub(opts: EpubOptions): BinaryOutput;
110
+ /**
111
+ * An HTML page; its pictures are inline unless `inlineImages` is false.
112
+ */
113
+ toHtml(opts: HtmlOptions): TextOutput;
114
+ /**
115
+ * The editable JSON and its picture files.
116
+ */
117
+ toJson(opts: JsonOptions): TextOutput;
118
+ /**
119
+ * LaTeX source and its picture files.
120
+ */
121
+ toLatex(opts: LatexOptions): TextOutput;
122
+ /**
123
+ * Markdown and its picture files.
124
+ */
125
+ toMarkdown(opts: MarkdownOptions): TextOutput;
126
+ /**
127
+ * A Word document, workbook or EPUB book laid out in pages, as a PDF.
128
+ */
129
+ toPdf(): BinaryOutput;
130
+ /**
131
+ * Typst source and its picture files.
132
+ */
133
+ toTypst(opts: TypstOptions): TextOutput;
134
+ /**
135
+ * The input format: `'pdf'`, `'docx'`, `'doc'`, `'xlsx'`, `'epub'` or
136
+ * `'html'`.
137
+ */
138
+ readonly format: string;
139
+ /**
140
+ * The number of pages of a PDF (0 for the formats that have none).
141
+ */
142
+ readonly pageCount: number;
143
+ /**
144
+ * The title of the document, when it has one.
145
+ */
146
+ readonly title: string | undefined;
147
+ /**
148
+ * The warnings of applying `content`: blocks that kept their own text.
149
+ */
150
+ readonly warnings: string[];
151
+ }
152
+
153
+ /**
154
+ * Adds a TrueType or OpenType font (or a collection) to those documents
155
+ * are laid out and SVG text drawn with: WebAssembly has no installed
156
+ * fonts, and laying out a Word document or a workbook as a PDF needs one.
157
+ * Returns its number of faces.
158
+ */
159
+ export function addFont(data: Uint8Array | number[]): number;
160
+
161
+ /**
162
+ * Logs a panic's message to `console.error` before the module aborts.
163
+ */
164
+ export function start(): void;
165
+
166
+ /**
167
+ * The version of kakemono.
168
+ */
169
+ export function version(): string;
170
+ export default function init(): Promise<void>
171
+ export function initSync(): void
@@ -0,0 +1,4 @@
1
+ import wasm from './kakemono_wasm.js'
2
+ export const { Document, addFont, version } = wasm
3
+ export default async function init() {}
4
+ export function initSync() {}
@@ -0,0 +1,169 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /** Options of reading and analysing a document, kept for every output. */
5
+ export interface OpenOptions {
6
+ /** The name recorded as the document's source (nothing by default). */
7
+ sourceName?: string
8
+ /** Rebuild tables as real tables when their grid is clear (default true). */
9
+ tables?: boolean
10
+ /** How hard PNG pictures are compressed; the pixels are the same. */
11
+ pngCompression?: 'fast' | 'balanced' | 'high'
12
+ /** An edited copy of the output of `toJson()` (its text, or the parsed object): every output shows its text. */
13
+ content?: string | object
14
+ /** The header rows of every table of an Excel workbook, given instead of found. */
15
+ headerRows?: number
16
+ }
17
+ type Equations = 'mathml' | 'image'
18
+ /** Options of the HTML output. */
19
+ export interface HtmlOptions {
20
+ mode?: 'hard' | 'hard-95' | 'medium' | 'friendly'
21
+ equations?: Equations
22
+ /** Pixels per PDF point of the pictures, above 0.2 and at most 8 (default 3). */
23
+ scale?: number
24
+ pageImage?: 'svg' | 'svg-only' | 'png'
25
+ embedFonts?: boolean
26
+ /** Pictures as data URIs in the page (default true); otherwise as `files`. */
27
+ inlineImages?: boolean
28
+ imageDir?: string
29
+ }
30
+ /** Options of the Markdown output. */
31
+ export interface MarkdownOptions {
32
+ mode?: 'medium' | 'friendly'
33
+ equations?: Equations
34
+ scale?: number
35
+ /** The folder of the picture files, relative (default 'images'). */
36
+ imageDir?: string
37
+ inlineImages?: boolean
38
+ }
39
+ /** Options of the editable JSON output. */
40
+ export interface JsonOptions { equations?: Equations; scale?: number; imageDir?: string; inlineImages?: boolean }
41
+ /** Options of the LaTeX output. */
42
+ export interface LatexOptions { equations?: Equations; scale?: number; imageDir?: string }
43
+ /** Options of the Typst output. */
44
+ export interface TypstOptions { equations?: Equations; scale?: number; imageDir?: string; layout?: 'auto' | 'article' | 'book' }
45
+ /** Options of the Word output: with `imageDir`, its pictures are files too. */
46
+ export interface DocxOptions { equations?: Equations; scale?: number; imageDir?: string }
47
+ /** Options of the EPUB output. */
48
+ export interface EpubOptions {
49
+ equations?: Equations
50
+ scale?: number
51
+ maxMathWidthEm?: number
52
+ cover?: boolean
53
+ /** The book's date, `CCYY-MM-DDThh:mm:ssZ` (default: a fixed date). */
54
+ modified?: string
55
+ }
56
+ /** Options of the chunks for retrieval (tokens are estimated). */
57
+ export interface ChunkOptions { maxTokens?: number }
58
+ /** A picture file of an output, at a path relative to the output's folder. */
59
+ export interface OutputFile { path: string; data: Uint8Array }
60
+ /** A text output and its picture files. */
61
+ export interface TextOutput { text: string; files: OutputFile[] }
62
+ /** A binary output (Word document, EPUB book, PDF) and its picture files. */
63
+ export interface BinaryOutput { data: Uint8Array; files: OutputFile[] }
64
+ /** A chunk for retrieval. */
65
+ export interface Chunk {
66
+ id: number
67
+ /** The text to embed: the section path, a blank line, then the text. */
68
+ text: string
69
+ tokens: number
70
+ title?: string
71
+ section: string[]
72
+ blocks: number[]
73
+ /** The pages it comes from, from 0 (`[0]` for documents without pages). */
74
+ pages: number[]
75
+ }
76
+
77
+
78
+
79
+ /**
80
+ * A PDF, Word document, Excel workbook, EPUB book or web page, read and
81
+ * analysed once: every output written from it shares that analysis.
82
+ */
83
+ export class Document {
84
+ private constructor();
85
+ free(): void;
86
+ [Symbol.dispose](): void;
87
+ /**
88
+ * The structure of the document (its blocks in reading order), as the
89
+ * command line's `--json` writes it.
90
+ */
91
+ analyze(): any;
92
+ /**
93
+ * Reads and analyses the bytes of a document (a PDF, a Word document,
94
+ * an Excel workbook, an EPUB book or a web page).
95
+ */
96
+ static fromBytes(data: Uint8Array | number[], opts: OpenOptions): Document;
97
+ /**
98
+ * Chunks for retrieval (RAG), cut at sentence ends and headed by their
99
+ * section.
100
+ */
101
+ toChunks(opts: ChunkOptions): Chunk[];
102
+ /**
103
+ * A Word document (`.docx`).
104
+ */
105
+ toDocx(opts: DocxOptions): BinaryOutput;
106
+ /**
107
+ * An EPUB 3 book.
108
+ */
109
+ toEpub(opts: EpubOptions): BinaryOutput;
110
+ /**
111
+ * An HTML page; its pictures are inline unless `inlineImages` is false.
112
+ */
113
+ toHtml(opts: HtmlOptions): TextOutput;
114
+ /**
115
+ * The editable JSON and its picture files.
116
+ */
117
+ toJson(opts: JsonOptions): TextOutput;
118
+ /**
119
+ * LaTeX source and its picture files.
120
+ */
121
+ toLatex(opts: LatexOptions): TextOutput;
122
+ /**
123
+ * Markdown and its picture files.
124
+ */
125
+ toMarkdown(opts: MarkdownOptions): TextOutput;
126
+ /**
127
+ * A Word document, workbook or EPUB book laid out in pages, as a PDF.
128
+ */
129
+ toPdf(): BinaryOutput;
130
+ /**
131
+ * Typst source and its picture files.
132
+ */
133
+ toTypst(opts: TypstOptions): TextOutput;
134
+ /**
135
+ * The input format: `'pdf'`, `'docx'`, `'doc'`, `'xlsx'`, `'epub'` or
136
+ * `'html'`.
137
+ */
138
+ readonly format: string;
139
+ /**
140
+ * The number of pages of a PDF (0 for the formats that have none).
141
+ */
142
+ readonly pageCount: number;
143
+ /**
144
+ * The title of the document, when it has one.
145
+ */
146
+ readonly title: string | undefined;
147
+ /**
148
+ * The warnings of applying `content`: blocks that kept their own text.
149
+ */
150
+ readonly warnings: string[];
151
+ }
152
+
153
+ /**
154
+ * Adds a TrueType or OpenType font (or a collection) to those documents
155
+ * are laid out and SVG text drawn with: WebAssembly has no installed
156
+ * fonts, and laying out a Word document or a workbook as a PDF needs one.
157
+ * Returns its number of faces.
158
+ */
159
+ export function addFont(data: Uint8Array | number[]): number;
160
+
161
+ /**
162
+ * Logs a panic's message to `console.error` before the module aborts.
163
+ */
164
+ export function start(): void;
165
+
166
+ /**
167
+ * The version of kakemono.
168
+ */
169
+ export function version(): string;