@mdgate/html 0.6.8 → 0.6.10
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 +146 -8
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
# @mdgate/html
|
|
2
2
|
|
|
3
|
-
Convert HTML
|
|
4
|
-
Edge, and browsers. No native addons.
|
|
3
|
+
**Convert HTML to Markdown in TypeScript.**
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
[`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html) reads `.html`, `.htm`, `.html4`, `.html5`, `.xhtml`, `.mhtml`, and `.mht` files directly in JavaScript and converts them into GitHub-Flavored Markdown, without Python, native addons, WASM, or a browser engine.
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
Works in **Node.js, Cloudflare Workers, Edge runtimes, and browsers**.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @mdgate/html
|
|
11
|
+
```
|
|
9
12
|
|
|
10
13
|
```ts
|
|
11
14
|
import { toMarkdown } from '@mdgate/html';
|
|
@@ -13,14 +16,149 @@ import { toMarkdown } from '@mdgate/html';
|
|
|
13
16
|
const markdown = await toMarkdown(bytes);
|
|
14
17
|
```
|
|
15
18
|
|
|
16
|
-
|
|
19
|
+
`bytes` is a `Uint8Array`, so the page can come from a file upload, a crawl, an HTTP response, a browser file picker, or anywhere else your application gets bytes.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Why [`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html)
|
|
24
|
+
|
|
25
|
+
HTML is the interchange format for saved pages, MHTML archives, and a lot of "export as web page" output from office tools.
|
|
26
|
+
|
|
27
|
+
[`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html) is an HTML reader written for the same runtime as your application:
|
|
28
|
+
|
|
29
|
+
* **Pure TypeScript**
|
|
30
|
+
* **HTML → Markdown locally**
|
|
31
|
+
* **No Python runtime**
|
|
32
|
+
* **No native addons**
|
|
33
|
+
* **No WASM runtime**
|
|
34
|
+
* **Zero third-party runtime dependencies**
|
|
35
|
+
* **Works with raw `Uint8Array` input**
|
|
36
|
+
* **Detects HTML and MHTML from their contents, not only the filename**
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## What it extracts
|
|
41
|
+
|
|
42
|
+
[`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html) parses the markup, walks the tree, and rebuilds a shared document model.
|
|
43
|
+
|
|
44
|
+
The converter handles HTML-specific concerns including:
|
|
45
|
+
|
|
46
|
+
* headings, paragraphs, and block quotes
|
|
47
|
+
* bold, italic, strikethrough, and inline code
|
|
48
|
+
* links and relative URLs
|
|
49
|
+
* ordered, unordered, and nested lists
|
|
50
|
+
* tables, including spanning cells
|
|
51
|
+
* code blocks
|
|
52
|
+
* MHTML / MHT archives (MIME-wrapped HTML)
|
|
53
|
+
* XHTML
|
|
54
|
+
|
|
55
|
+
The output is Markdown that can be searched, indexed, chunked, cached, or passed directly to an AI agent.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Node.js
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { readFile } from 'node:fs/promises';
|
|
63
|
+
import { toMarkdown } from '@mdgate/html';
|
|
64
|
+
|
|
65
|
+
const bytes = new Uint8Array(await readFile('page.html'));
|
|
66
|
+
const markdown = await toMarkdown(bytes);
|
|
67
|
+
|
|
68
|
+
console.log(markdown);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Browser
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { toMarkdown } from '@mdgate/html';
|
|
77
|
+
|
|
78
|
+
const file = input.files![0];
|
|
79
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
80
|
+
|
|
81
|
+
const markdown = await toMarkdown(bytes);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Cloudflare Workers and Edge runtimes
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { toMarkdown } from '@mdgate/html';
|
|
90
|
+
|
|
91
|
+
export default {
|
|
92
|
+
async fetch(request: Request) {
|
|
93
|
+
const bytes = new Uint8Array(await request.arrayBuffer());
|
|
94
|
+
const markdown = await toMarkdown(bytes);
|
|
95
|
+
|
|
96
|
+
return new Response(markdown, {
|
|
97
|
+
headers: {
|
|
98
|
+
'content-type': 'text/markdown; charset=utf-8',
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Format detection
|
|
108
|
+
|
|
109
|
+
You do not need to trust the file extension.
|
|
110
|
+
|
|
111
|
+
[`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html) recognizes HTML and MHTML from their contents.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const markdown = await toMarkdown(bytes);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
A path can still be supplied as a format hint when [`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html) is used through [`@mdgate/converters`](https://github.com/mdgate/converters/tree/main/packages/converters) or a reader composed with [`@mdgate/core`](https://github.com/mdgate/converters/tree/main/packages/core), but the path is never used to read a file from disk.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Compose it with other file readers
|
|
122
|
+
|
|
123
|
+
[`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html) implements the converter interface from [`@mdgate/core`](https://github.com/mdgate/converters/tree/main/packages/core).
|
|
17
124
|
|
|
18
125
|
```ts
|
|
19
126
|
import { create } from '@mdgate/core';
|
|
20
127
|
import { html } from '@mdgate/html';
|
|
128
|
+
import { email } from '@mdgate/email';
|
|
129
|
+
|
|
130
|
+
const read = create([
|
|
131
|
+
html(),
|
|
132
|
+
email(),
|
|
133
|
+
]);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The application still uses one reading interface while each format remains independently installable.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## Need more than HTML?
|
|
141
|
+
|
|
142
|
+
If your application needs to read many different file types, use the complete converter set:
|
|
21
143
|
|
|
22
|
-
|
|
144
|
+
```bash
|
|
145
|
+
npm install @mdgate/converters
|
|
23
146
|
```
|
|
24
147
|
|
|
25
|
-
|
|
26
|
-
|
|
148
|
+
```ts
|
|
149
|
+
import { toMarkdown } from '@mdgate/converters';
|
|
150
|
+
|
|
151
|
+
const markdown = await toMarkdown(bytes, {
|
|
152
|
+
path: filename,
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
[`@mdgate/html`](https://github.com/mdgate/converters/tree/main/packages/html) is one of the single-format packages in the open-source [`mdgate/converters`](https://github.com/mdgate/converters) project.
|
|
157
|
+
|
|
158
|
+
For AI agents, the same converter architecture can be used to extend `read_file` from text files to real-world document formats.
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/html",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.10",
|
|
4
4
|
"description": "mdgate HTML, XHTML, and MHTML converter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"homepage": "https://
|
|
7
|
+
"homepage": "https://convert.mdgate.dev",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "git+https://github.com/mdgate/converters.git",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"prepublishOnly": "bun run build"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@mdgate/containers": "0.6.
|
|
32
|
-
"@mdgate/core": "0.6.
|
|
33
|
-
"@mdgate/document": "0.6.
|
|
34
|
-
"@mdgate/office-common": "0.6.
|
|
35
|
-
"@mdgate/utils": "0.6.
|
|
31
|
+
"@mdgate/containers": "0.6.10",
|
|
32
|
+
"@mdgate/core": "0.6.10",
|
|
33
|
+
"@mdgate/document": "0.6.10",
|
|
34
|
+
"@mdgate/office-common": "0.6.10",
|
|
35
|
+
"@mdgate/utils": "0.6.10"
|
|
36
36
|
},
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|