@mdgate/text 0.6.7 → 0.6.9
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 +130 -11
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,28 +1,147 @@
|
|
|
1
1
|
# @mdgate/text
|
|
2
2
|
|
|
3
|
-
Convert
|
|
4
|
-
in Node, Edge, and browsers. No native addons.
|
|
3
|
+
**Convert text, Markdown, and source files in TypeScript.**
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
`.py`, `.go`, `.rs`, and similar). Markdown is passed through; source is wrapped in a fenced code
|
|
8
|
-
block.
|
|
5
|
+
[`@mdgate/text`](https://github.com/mdgate/converters/tree/main/packages/text) reads `.txt`, `.md`, and common source-code files directly in JavaScript and returns GitHub-Flavored Markdown, without Python, native addons, or an extra toolchain.
|
|
9
6
|
|
|
10
|
-
|
|
7
|
+
Works in **Node.js, Cloudflare Workers, Edge runtimes, and browsers**.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @mdgate/text
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { toMarkdown } from '@mdgate/text';
|
|
15
|
+
|
|
16
|
+
const markdown = await toMarkdown(bytes, {
|
|
17
|
+
path: 'notes.txt',
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`bytes` is a `Uint8Array`, so the file can come from a file upload, object storage, a git checkout, a browser file picker, or anywhere else your application gets bytes.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Why [`@mdgate/text`](https://github.com/mdgate/converters/tree/main/packages/text)
|
|
26
|
+
|
|
27
|
+
Plain text and source files still need to join the same reader as DOCX and PDF. This converter is that path.
|
|
28
|
+
|
|
29
|
+
* **Pure TypeScript**
|
|
30
|
+
* **Local conversion**
|
|
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
|
+
* **Uses a path hint, because these formats have no reliable file signature**
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## What it extracts
|
|
41
|
+
|
|
42
|
+
* `.txt` / `.text`: paragraphs
|
|
43
|
+
* `.md` / `.markdown` / `.mdx`: passed through as Markdown
|
|
44
|
+
* source extensions (`.js`, `.ts`, `.py`, `.go`, `.rs`, and similar): wrapped in a fenced code block
|
|
45
|
+
|
|
46
|
+
The path selects the treatment. It is never used to read a file from disk.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Node.js
|
|
11
51
|
|
|
12
52
|
```ts
|
|
53
|
+
import { readFile } from 'node:fs/promises';
|
|
13
54
|
import { toMarkdown } from '@mdgate/text';
|
|
14
55
|
|
|
15
|
-
const
|
|
56
|
+
const bytes = new Uint8Array(await readFile('notes.txt'));
|
|
57
|
+
const markdown = await toMarkdown(bytes, {
|
|
58
|
+
path: 'notes.txt',
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
console.log(markdown);
|
|
16
62
|
```
|
|
17
63
|
|
|
18
|
-
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Browser
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { toMarkdown } from '@mdgate/text';
|
|
70
|
+
|
|
71
|
+
const file = input.files![0];
|
|
72
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
73
|
+
|
|
74
|
+
const markdown = await toMarkdown(bytes, {
|
|
75
|
+
path: file.name,
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Cloudflare Workers and Edge runtimes
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { toMarkdown } from '@mdgate/text';
|
|
85
|
+
|
|
86
|
+
export default {
|
|
87
|
+
async fetch(request: Request) {
|
|
88
|
+
const bytes = new Uint8Array(await request.arrayBuffer());
|
|
89
|
+
const markdown = await toMarkdown(bytes, {
|
|
90
|
+
path: request.headers.get('x-filename') ?? 'upload.txt',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return new Response(markdown, {
|
|
94
|
+
headers: {
|
|
95
|
+
'content-type': 'text/markdown; charset=utf-8',
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## Compose it with other file readers
|
|
105
|
+
|
|
106
|
+
[`@mdgate/text`](https://github.com/mdgate/converters/tree/main/packages/text) implements the converter interface from [`@mdgate/core`](https://github.com/mdgate/converters/tree/main/packages/core).
|
|
19
107
|
|
|
20
108
|
```ts
|
|
21
109
|
import { create } from '@mdgate/core';
|
|
22
110
|
import { text } from '@mdgate/text';
|
|
111
|
+
import { html } from '@mdgate/html';
|
|
23
112
|
|
|
24
|
-
const
|
|
113
|
+
const read = create([
|
|
114
|
+
text(),
|
|
115
|
+
html(),
|
|
116
|
+
]);
|
|
25
117
|
```
|
|
26
118
|
|
|
27
|
-
|
|
28
|
-
|
|
119
|
+
The application still uses one reading interface while each format remains independently installable.
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## Need more than text?
|
|
124
|
+
|
|
125
|
+
If your application needs to read many different file types, use the complete converter set:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
npm install @mdgate/converters
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { toMarkdown } from '@mdgate/converters';
|
|
133
|
+
|
|
134
|
+
const markdown = await toMarkdown(bytes, {
|
|
135
|
+
path: filename,
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
[`@mdgate/text`](https://github.com/mdgate/converters/tree/main/packages/text) is one of the single-format packages in the open-source [`mdgate/converters`](https://github.com/mdgate/converters) project.
|
|
140
|
+
|
|
141
|
+
For AI agents, the same converter architecture can be used to extend `read_file` from text files to real-world document formats.
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgate/text",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.9",
|
|
4
4
|
"description": "mdgate plain text, markdown, and source 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,9 +28,9 @@
|
|
|
28
28
|
"prepublishOnly": "bun run build"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@mdgate/core": "0.6.
|
|
32
|
-
"@mdgate/document": "0.6.
|
|
33
|
-
"@mdgate/utils": "0.6.
|
|
31
|
+
"@mdgate/core": "0.6.9",
|
|
32
|
+
"@mdgate/document": "0.6.9",
|
|
33
|
+
"@mdgate/utils": "0.6.9"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|