paper-drift 0.0.0-stage → 0.1.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.
- package/LICENSE +190 -0
- package/README.md +206 -2
- package/bin/cli.js +227 -0
- package/package.json +50 -4
- package/skills/paper-drift/SKILL.md +100 -0
- package/src/converter.js +203 -0
- package/src/index.d.ts +145 -0
- package/src/index.js +50 -0
- package/src/markdown/converter.js +254 -0
- package/src/markdown/index.js +10 -0
- package/src/math/index.js +26 -0
- package/src/math/restorer.js +221 -0
- package/src/math/symbols.js +153 -0
- package/src/skill/installer.js +95 -0
- package/src/spatial/geometry.js +110 -0
- package/src/spatial/reading-order.js +179 -0
- package/src/tables/index.js +11 -0
- package/src/tables/reconstructor.js +199 -0
- package/src/utils/html-template.js +118 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Sooriya
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,207 @@
|
|
|
1
|
-
#
|
|
1
|
+
# paper-drift
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/Sooriya04/Paper-Drift/actions/workflows/ci.yml)
|
|
4
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
[](https://webassembly.org/)
|
|
7
|
+
|
|
8
|
+
> **Zero-Python, zero-Chromium WebAssembly document parser, HTML renderer, and Markdown engine with GFM tables and math.**
|
|
9
|
+
|
|
10
|
+
Convert complex academic research papers and enterprise PDFs directly into clean, structured **Markdown** or standalone **HTML** with pixel-perfect visual fidelity—without Puppeteer, Python, Docker, or external servers. Slashes AI Agent token ingestion costs by 90%+.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## ⚡ Why paper-drift?
|
|
15
|
+
|
|
16
|
+
Most PDF conversion pipelines in JavaScript suffer from two major flaws:
|
|
17
|
+
1. **The Heavyweight Trap**: Requiring Headless Chromium (Puppeteer / Playwright) or Python microservices (`unstructured`, `poppler`, `nougat`) that need 500MB+ Docker images, multi-second cold boots, and high cloud costs.
|
|
18
|
+
2. **The "Text Soup" Trap**: Spatially naive parsers that dump unordered lines, break multi-column layouts, and butcher tabular grids.
|
|
19
|
+
|
|
20
|
+
**`paper-drift` solves this natively:**
|
|
21
|
+
* **Zero-Python & Zero-Chromium**: Runs compiled WebAssembly directly in any V8 runtime (Node.js, Bun, or modern browsers). Under 20 KB package footprint.
|
|
22
|
+
* **Direct Markdown Engine**: Converts directly into GitHub Flavored Markdown (GFM) with auto-detected `#`, `##` headings, list markers, and clean math.
|
|
23
|
+
* **Geometric Table Reconstruction**: Discovers column gutters and slices cells into standard pipe tables (`| col1 | col2 |`) with clean universal math exponents (`O(n² · d)`, `10¹⁹`).
|
|
24
|
+
* **Multi-Column Spatial Ordering**: Resolves 2-column papers via Recursive XY-Cut without column interweaving.
|
|
25
|
+
* **Dual-Layer HTML Mode**: High-DPI background raster rendering paired with a 1:1 selectable, searchable text layer.
|
|
26
|
+
* **100% Private & Instant**: Ingests pages in under 50ms locally. Zero data leaves your machine.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 🚀 Quick Start (CLI)
|
|
31
|
+
|
|
32
|
+
### Run with `npx` (Zero installation required)
|
|
33
|
+
```bash
|
|
34
|
+
# Convert to Markdown (for Claude, LLMs, or AI Agents)
|
|
35
|
+
npx paper-drift paper.pdf output.md --format md
|
|
36
|
+
|
|
37
|
+
# Convert to Standalone HTML (for human viewing)
|
|
38
|
+
npx paper-drift paper.pdf output.html
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Install Globally
|
|
42
|
+
```bash
|
|
43
|
+
npm install -g paper-drift
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Common Commands
|
|
47
|
+
```bash
|
|
48
|
+
# Convert to clean Markdown
|
|
49
|
+
paper-drift research-paper.pdf paper.md --format md
|
|
50
|
+
|
|
51
|
+
# Convert specific pages to Markdown
|
|
52
|
+
paper-drift paper.pdf output.md --format md --pages 1-5,8
|
|
53
|
+
|
|
54
|
+
# Convert to pixel-perfect HTML at 2.0x Retina scale
|
|
55
|
+
paper-drift report.pdf output.html --scale 2.0
|
|
56
|
+
|
|
57
|
+
# Text-only HTML mode (skips background raster)
|
|
58
|
+
paper-drift book.pdf book.html --no-bg
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### CLI Options
|
|
62
|
+
|
|
63
|
+
| Flag | Type | Default | Description |
|
|
64
|
+
| :--- | :--- | :--- | :--- |
|
|
65
|
+
| `<input.pdf>` | `string` | *(required)* | Path to the source PDF file |
|
|
66
|
+
| `[output]` | `string` | `<name>.<ext>` | Destination output file path |
|
|
67
|
+
| `--format <fmt>` | `string` | `html` | Output format: `markdown` (`md`) \| `html` \| `text` \| `json` |
|
|
68
|
+
| `--pages <range>` | `string` | `"all"` | Target page range (e.g. `1-10`, `2,4,6`) |
|
|
69
|
+
| `--scale <num>` | `number` | `2.0` | DPI multiplier for background raster rendering in HTML mode |
|
|
70
|
+
| `--no-bg` | `boolean` | `false` | Disables background rasterization (text-only HTML) |
|
|
71
|
+
| `-v, --version` | `flag` | — | Display current version |
|
|
72
|
+
| `-h, --help` | `flag` | — | Display CLI help menu |
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## 💻 Programmatic API (Node.js / Bun)
|
|
77
|
+
|
|
78
|
+
Install in your project:
|
|
79
|
+
```bash
|
|
80
|
+
npm install paper-drift
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Direct PDF to Markdown
|
|
84
|
+
```javascript
|
|
85
|
+
import { convertPdfToMarkdown } from 'paper-drift';
|
|
86
|
+
|
|
87
|
+
const result = await convertPdfToMarkdown('research-paper.pdf', {
|
|
88
|
+
pageRange: '1-10'
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
console.log(`Converted ${result.pagesProcessed} pages in ${result.durationMs}ms`);
|
|
92
|
+
console.log(result.markdown);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### PDF to Standalone HTML
|
|
96
|
+
```javascript
|
|
97
|
+
import { convertPdfToHtml } from 'paper-drift';
|
|
98
|
+
import fs from 'fs';
|
|
99
|
+
|
|
100
|
+
const result = await convertPdfToHtml('document.pdf', {
|
|
101
|
+
scale: 2.0,
|
|
102
|
+
withBg: true
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
fs.writeFileSync('output.html', result.html);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Buffer Input Support
|
|
109
|
+
Both functions accept raw `Buffer` or `Uint8Array` inputs:
|
|
110
|
+
```javascript
|
|
111
|
+
import { convertPdfToMarkdown } from 'paper-drift';
|
|
112
|
+
|
|
113
|
+
const pdfBuffer = await fetch('https://example.com/paper.pdf').then(r => r.arrayBuffer());
|
|
114
|
+
const { markdown } = await convertPdfToMarkdown(Buffer.from(pdfBuffer));
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 💸 AI Agent Token Savings
|
|
120
|
+
|
|
121
|
+
When feeding 10-page academic papers into LLMs (Claude, GPT-4, Cursor, Antigravity):
|
|
122
|
+
|
|
123
|
+
| Mode | Tokens Consumed | Cost / Ingestion | Reading Accuracy |
|
|
124
|
+
| :--- | :--- | :--- | :--- |
|
|
125
|
+
| **Multimodal Vision (Images)** | ~16,000 – 20,000 tokens | ~$0.15 – $0.40 | Heavy hallucinations on dense math |
|
|
126
|
+
| **Naive OCR / PDF Text Dump** | ~8,000 tokens | ~$0.08 | Tables destroyed into loose lines |
|
|
127
|
+
| **`paper-drift` Markdown** | **~1,200 – 1,800 tokens** | **~$0.01** | **Pixel-accurate reading order, GFM tables, math** |
|
|
128
|
+
|
|
129
|
+
> **Net Savings:** Over **90% token reduction** and **12x faster ingestion**.
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 🤖 Turnkey AI Agent Skill (Claude, Cursor, Antigravity)
|
|
134
|
+
|
|
135
|
+
`paper-drift` is built from the ground up as a native **AI Agent Skill** to eliminate the **"Multimodal PDF Token Tax"**.
|
|
136
|
+
|
|
137
|
+
When an AI coding or research agent ingests raw PDFs as images, it burns ~1,500 to 2,000 vision tokens per page (crowding context windows and costing $0.50–$2.00 per query). By equipping your agent with the `paper-drift` skill, the agent pre-processes PDFs locally via WebAssembly into clean Markdown, GFM tables, and LaTeX equations in **< 400ms**—cutting token consumption by **90%+**.
|
|
138
|
+
|
|
139
|
+
### 1-Click Skill Installation
|
|
140
|
+
|
|
141
|
+
Install the canonical [`SKILL.md`](skills/paper-drift/SKILL.md) into any workspace with a single command:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
# 1. Default: Auto-install to current workspace (.agent/skills/paper-drift/SKILL.md)
|
|
145
|
+
npx paper-drift skill
|
|
146
|
+
|
|
147
|
+
# 2. Target Claude Code workspace (.claude/skills/paper-drift/SKILL.md)
|
|
148
|
+
npx paper-drift skill --claude
|
|
149
|
+
|
|
150
|
+
# 3. Target Cursor workspace rules (.cursor/rules/paper-drift.md)
|
|
151
|
+
npx paper-drift skill --cursor
|
|
152
|
+
|
|
153
|
+
# 4. Install globally for all projects (~/.gemini/... & ~/.claude/...)
|
|
154
|
+
npx paper-drift skill --global
|
|
155
|
+
|
|
156
|
+
# 5. Output raw SKILL.md to stdout (for piping or custom paths)
|
|
157
|
+
npx paper-drift skill cat
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### How the Agent Uses It
|
|
161
|
+
|
|
162
|
+
Once installed, your agent automatically activates the skill whenever you provide a PDF:
|
|
163
|
+
|
|
164
|
+
```text
|
|
165
|
+
User: "Summarize the experimental results from attention-is-all-you-need.pdf"
|
|
166
|
+
|
|
167
|
+
Agent (Internal):
|
|
168
|
+
1. Detects paper-drift skill
|
|
169
|
+
2. Executes: npx -y paper-drift attention-is-all-you-need.pdf --format md --pages 6-8
|
|
170
|
+
3. Receives clean GFM Table 1 and BLEU score comparisons in < 250ms
|
|
171
|
+
4. Answers your query using 1,200 text tokens instead of 20,000 vision tokens!
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
- 📖 **Skill Specification:** [`skills/paper-drift/SKILL.md`](skills/paper-drift/SKILL.md)
|
|
175
|
+
- 🌐 **Interactive Documentation:** [https://sooriya04.github.io/Paper-Drift/#skill](https://sooriya04.github.io/Paper-Drift/#skill)
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## 🌐 Interactive Web Playground
|
|
180
|
+
|
|
181
|
+
`paper-drift` includes a local WebAssembly playground with live GFM table and KaTeX formula preview:
|
|
182
|
+
|
|
183
|
+
1. Start the local server:
|
|
184
|
+
```bash
|
|
185
|
+
npm start
|
|
186
|
+
```
|
|
187
|
+
2. Open your browser at **http://localhost:3000** and drag-and-drop any PDF.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## 🧪 Testing & Benchmarks
|
|
192
|
+
|
|
193
|
+
Run the complete test suite:
|
|
194
|
+
```bash
|
|
195
|
+
npm test
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Benchmark throughput and layout fidelity across landmark ArXiv papers:
|
|
199
|
+
```bash
|
|
200
|
+
npm run benchmark
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## 📄 License
|
|
206
|
+
|
|
207
|
+
Apache-2.0 License. See [LICENSE](LICENSE) for details.
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @file bin/cli.js
|
|
4
|
+
* @description Command-line interface executable for paper-drift.
|
|
5
|
+
* Enables zero-Chromium PDF to high-fidelity HTML, Markdown, and natural spatial text conversion straight from terminal.
|
|
6
|
+
* Part of paper-drift - https://github.com/Sooriya04/Paper-Drift
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import fs from 'fs';
|
|
10
|
+
import path from 'path';
|
|
11
|
+
import { fileURLToPath } from 'url';
|
|
12
|
+
import { convertPdfToHtml, convertPdfToMarkdown } from '../src/index.js';
|
|
13
|
+
import { installSkill, getSkillContent } from '../src/skill/installer.js';
|
|
14
|
+
|
|
15
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
16
|
+
const __dirname = path.dirname(__filename);
|
|
17
|
+
|
|
18
|
+
// Read package metadata for version flag
|
|
19
|
+
const packageJson = JSON.parse(
|
|
20
|
+
fs.readFileSync(path.join(__dirname, '../package.json'), 'utf-8')
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
function printHelp() {
|
|
24
|
+
console.log(`
|
|
25
|
+
\x1b[1m\x1b[36mpaper-drift\x1b[0m v${packageJson.version}
|
|
26
|
+
High-fidelity, zero-Chromium PDF parser powered by WebAssembly & spatial layout decomposition.
|
|
27
|
+
|
|
28
|
+
\x1b[1mUSAGE:\x1b[0m
|
|
29
|
+
$ paper-drift <input.pdf> [output] [options]
|
|
30
|
+
|
|
31
|
+
\x1b[1mARGUMENTS:\x1b[0m
|
|
32
|
+
input.pdf Path to the PDF file to convert
|
|
33
|
+
output (Optional) Destination file path (default: <input>.<ext>)
|
|
34
|
+
|
|
35
|
+
\x1b[1mOPTIONS:\x1b[0m
|
|
36
|
+
--format <fmt> Output format: markdown (md) | html | text | json (default: html)
|
|
37
|
+
--scale <num> DPI scale for background raster rendering (default: 2.0)
|
|
38
|
+
--no-bg Text-only mode; disables background raster rendering
|
|
39
|
+
--pages <range> Target pages to convert (e.g. "1-5", "2,4,6", default: "all")
|
|
40
|
+
-v, --version Show version number
|
|
41
|
+
-h, --help Display this help message
|
|
42
|
+
|
|
43
|
+
\x1b[1mEXAMPLES:\x1b[0m
|
|
44
|
+
$ paper-drift document.pdf
|
|
45
|
+
$ paper-drift paper.pdf output.md --format md
|
|
46
|
+
$ paper-drift paper.pdf output.txt --format text
|
|
47
|
+
$ paper-drift paper.pdf output.html --scale 2.5
|
|
48
|
+
$ paper-drift manual.pdf manual.html --pages 1-10 --no-bg
|
|
49
|
+
|
|
50
|
+
\x1b[1mAGENT SKILL:\x1b[0m
|
|
51
|
+
$ paper-drift skill Install skill into agent workspace (.agent/skills)
|
|
52
|
+
$ paper-drift skill --claude Install skill into Claude workspace (.claude/skills)
|
|
53
|
+
$ paper-drift skill --cursor Install skill into Cursor rules (.cursor/rules)
|
|
54
|
+
$ paper-drift skill --global Install skill globally to user home directory
|
|
55
|
+
$ paper-drift skill cat Output raw SKILL.md to stdout
|
|
56
|
+
`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function run() {
|
|
60
|
+
const args = process.argv.slice(2);
|
|
61
|
+
|
|
62
|
+
// Handle agent skill subcommands
|
|
63
|
+
if (args[0] === 'skill' || args.includes('--install-skill')) {
|
|
64
|
+
if (args.includes('cat') || args.includes('print')) {
|
|
65
|
+
process.stdout.write(getSkillContent());
|
|
66
|
+
process.exit(0);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const isGlobal = args.includes('--global');
|
|
70
|
+
const isClaude = args.includes('--claude');
|
|
71
|
+
const isCursor = args.includes('--cursor');
|
|
72
|
+
const isAgent = args.includes('--agent') || args.includes('--antigravity');
|
|
73
|
+
let dest = null;
|
|
74
|
+
const destIdx = args.indexOf('--dest');
|
|
75
|
+
if (destIdx !== -1 && args[destIdx + 1]) {
|
|
76
|
+
dest = args[destIdx + 1];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const installed = installSkill({
|
|
81
|
+
global: isGlobal,
|
|
82
|
+
claude: isClaude,
|
|
83
|
+
cursor: isCursor,
|
|
84
|
+
agent: isAgent,
|
|
85
|
+
dest
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
console.log('\n\x1b[1m\x1b[36mpaper-drift\x1b[0m \x1b[32mAgent Skill Installed Successfully!\x1b[0m\n');
|
|
89
|
+
for (const p of installed) {
|
|
90
|
+
console.log(` \x1b[32m✔\x1b[0m ${path.relative(process.cwd(), p) || p}`);
|
|
91
|
+
}
|
|
92
|
+
console.log('\n\x1b[90mYour AI coding agent can now parse PDFs with zero multimodal token tax.\x1b[0m');
|
|
93
|
+
console.log('\x1b[90mAsk your agent: "Read this PDF" or "Extract Table 1 from paper.pdf"\x1b[0m\n');
|
|
94
|
+
process.exit(0);
|
|
95
|
+
} catch (err) {
|
|
96
|
+
console.error(`\x1b[31mError installing skill:\x1b[0m ${err.message}`);
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (args.length === 0 || args.includes('-h') || args.includes('--help')) {
|
|
102
|
+
printHelp();
|
|
103
|
+
process.exit(0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (args.includes('-v') || args.includes('--version')) {
|
|
107
|
+
console.log(`paper-drift v${packageJson.version}`);
|
|
108
|
+
process.exit(0);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Parse positional and flag arguments
|
|
112
|
+
let inputFile = null;
|
|
113
|
+
let outputFile = null;
|
|
114
|
+
let format = 'html';
|
|
115
|
+
let scale = 2.0;
|
|
116
|
+
let withBg = true;
|
|
117
|
+
let pageRange = 'all';
|
|
118
|
+
|
|
119
|
+
for (let i = 0; i < args.length; i++) {
|
|
120
|
+
const arg = args[i];
|
|
121
|
+
if (arg === '--format') {
|
|
122
|
+
format = (args[++i] || 'html').toLowerCase();
|
|
123
|
+
} else if (arg === '--scale') {
|
|
124
|
+
scale = parseFloat(args[++i]) || 2.0;
|
|
125
|
+
} else if (arg === '--no-bg') {
|
|
126
|
+
withBg = false;
|
|
127
|
+
} else if (arg === '--pages') {
|
|
128
|
+
pageRange = args[++i] || 'all';
|
|
129
|
+
} else if (!arg.startsWith('-')) {
|
|
130
|
+
if (!inputFile) {
|
|
131
|
+
inputFile = arg;
|
|
132
|
+
} else if (!outputFile) {
|
|
133
|
+
outputFile = arg;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!inputFile) {
|
|
139
|
+
console.error('\x1b[31mError: Missing required argument <input.pdf>\x1b[0m');
|
|
140
|
+
printHelp();
|
|
141
|
+
process.exit(1);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const resolvedInput = path.resolve(process.cwd(), inputFile);
|
|
145
|
+
if (!fs.existsSync(resolvedInput)) {
|
|
146
|
+
console.error(`\x1b[31mError: Input file does not exist: ${resolvedInput}\x1b[0m`);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const validFormats = ['html', 'md', 'markdown', 'text', 'json'];
|
|
151
|
+
if (!validFormats.includes(format)) {
|
|
152
|
+
console.error(`\x1b[31mError: Unsupported format "${format}". Supported: markdown (md) | html | text | json\x1b[0m`);
|
|
153
|
+
process.exit(1);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const isMarkdown = format === 'md' || format === 'markdown';
|
|
157
|
+
const ext = isMarkdown ? 'md' : (format === 'text' ? 'txt' : (format === 'json' ? 'json' : 'html'));
|
|
158
|
+
if (!outputFile) {
|
|
159
|
+
const parsedPath = path.parse(resolvedInput);
|
|
160
|
+
outputFile = path.join(process.cwd(), `${parsedPath.name}.${ext}`);
|
|
161
|
+
} else {
|
|
162
|
+
outputFile = path.resolve(process.cwd(), outputFile);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
console.log(`\x1b[36m⚡ paper-drift\x1b[0m v${packageJson.version}`);
|
|
166
|
+
console.log(`📄 Input: ${resolvedInput}`);
|
|
167
|
+
console.log(`🎯 Output: ${outputFile} (Format: ${format.toUpperCase()})`);
|
|
168
|
+
console.log(`⚙️ Config: Scale=${scale}x, Background=${withBg ? 'lossless' : 'disabled'}, Pages=${pageRange}`);
|
|
169
|
+
console.log('🔄 Running WebAssembly parser with spatial reading order decomposition...');
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
let outputContent = '';
|
|
173
|
+
let durationMs = 0;
|
|
174
|
+
let pagesProcessed = 0;
|
|
175
|
+
let totalPages = 0;
|
|
176
|
+
let columnsOrTablesInfo = '';
|
|
177
|
+
|
|
178
|
+
if (isMarkdown) {
|
|
179
|
+
const result = await convertPdfToMarkdown(resolvedInput, {
|
|
180
|
+
pageRange,
|
|
181
|
+
onProgress: ({ current, total, pageNumber }) => {
|
|
182
|
+
process.stdout.write(` ⏳ Processing page ${pageNumber} (${current}/${total})...\r`);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
outputContent = result.markdown;
|
|
186
|
+
durationMs = result.durationMs;
|
|
187
|
+
pagesProcessed = result.pagesProcessed;
|
|
188
|
+
totalPages = result.totalPages;
|
|
189
|
+
columnsOrTablesInfo = result.pages.map(p => `p${p.pageNumber}:${p.tablesCount}tables`).join(', ');
|
|
190
|
+
} else {
|
|
191
|
+
const result = await convertPdfToHtml(resolvedInput, {
|
|
192
|
+
scale,
|
|
193
|
+
withBg,
|
|
194
|
+
pageRange,
|
|
195
|
+
format,
|
|
196
|
+
onProgress: ({ current, total, pageNumber }) => {
|
|
197
|
+
process.stdout.write(` ⏳ Processing page ${pageNumber} (${current}/${total})...\r`);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
durationMs = result.durationMs;
|
|
201
|
+
pagesProcessed = result.pagesProcessed;
|
|
202
|
+
totalPages = result.totalPages;
|
|
203
|
+
columnsOrTablesInfo = result.pages.map(p => `p${p.pageNumber}:${p.columnsDetected}col`).join(', ');
|
|
204
|
+
|
|
205
|
+
if (format === 'text') {
|
|
206
|
+
outputContent = result.text;
|
|
207
|
+
} else if (format === 'json') {
|
|
208
|
+
outputContent = JSON.stringify(result.pages, null, 2);
|
|
209
|
+
} else {
|
|
210
|
+
outputContent = result.html;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
fs.writeFileSync(outputFile, outputContent, 'utf-8');
|
|
215
|
+
const stats = fs.statSync(outputFile);
|
|
216
|
+
const sizeKb = (stats.size / 1024).toFixed(1);
|
|
217
|
+
|
|
218
|
+
console.log(`\n\x1b[32m✔ Conversion complete in ${durationMs}ms!\x1b[0m`);
|
|
219
|
+
console.log(`📊 Converted ${pagesProcessed} of ${totalPages} pages (${sizeKb} KB) [${columnsOrTablesInfo}]`);
|
|
220
|
+
console.log(`✨ Open your file: \x1b[4mfile://${outputFile}\x1b[0m\n`);
|
|
221
|
+
} catch (err) {
|
|
222
|
+
console.error(`\n\x1b[31m❌ Conversion failed:\x1b[0m ${err.message}`);
|
|
223
|
+
process.exit(1);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
run();
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paper-drift",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Zero-Python, zero-Chromium WebAssembly document parser, HTML renderer, and Markdown engine with GFM tables and math",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"types": "src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"import": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"bin",
|
|
17
|
+
"skills",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"bin": {
|
|
22
|
+
"paper-drift": "./bin/cli.js"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/Sooriya04/Paper-Drift.git"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"start": "node playground/server.js",
|
|
30
|
+
"test": "node test/cli.test.js && node test/spatial.test.js && node test/math.test.js && node test/tables.test.js && node test/markdown.test.js",
|
|
31
|
+
"convert": "node bin/cli.js test/fixtures/sample.pdf output.html",
|
|
32
|
+
"generate:sample": "node test/scripts/generate-sample.js",
|
|
33
|
+
"benchmark": "node test/scripts/benchmark-papers.js run --limit 3 --pages 1-2",
|
|
34
|
+
"benchmark:20": "node test/scripts/benchmark-papers.js run --limit 20 --pages 1-2",
|
|
35
|
+
"benchmark:download": "node test/scripts/benchmark-papers.js download 20",
|
|
36
|
+
"benchmark:list": "node test/scripts/benchmark-papers.js list"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"pdf",
|
|
40
|
+
"webassembly",
|
|
41
|
+
"wasm",
|
|
42
|
+
"pdf-to-html",
|
|
43
|
+
"rag",
|
|
44
|
+
"mupdf",
|
|
45
|
+
"document-parser"
|
|
46
|
+
],
|
|
47
|
+
"author": "Sooriya",
|
|
48
|
+
"license": "Apache-2.0",
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"mupdf": "^1.28.1"
|
|
51
|
+
}
|
|
52
|
+
}
|