@mdgate/text 0.1.0

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 ADDED
@@ -0,0 +1,28 @@
1
+ # @mdgate/text
2
+
3
+ Convert plain text, Markdown, and source files to Markdown. Outputs GitHub-Flavored Markdown. Works
4
+ in Node, Edge, and browsers. No native addons.
5
+
6
+ Handles: `.txt`, `.text`, `.md`, `.markdown`, `.mdx`, and common source extensions (`.js`, `.ts`,
7
+ `.py`, `.go`, `.rs`, and similar). Markdown is passed through; source is wrapped in a fenced code
8
+ block.
9
+
10
+ ## Usage
11
+
12
+ ```ts
13
+ import { toMarkdown } from '@mdgate/text';
14
+
15
+ const markdown = await toMarkdown(bytes, { path: 'notes.txt' });
16
+ ```
17
+
18
+ Compose with other converters:
19
+
20
+ ```ts
21
+ import { create } from '@mdgate/core';
22
+ import { text } from '@mdgate/text';
23
+
24
+ const convert = create([text()]);
25
+ ```
26
+
27
+ Part of [mdgate converters](https://github.com/mdgate/converters); install
28
+ `@mdgate/converters` for every format at once.
@@ -0,0 +1,2 @@
1
+ export { text } from './text.js';
2
+ export { toMarkdown } from './to-markdown.js';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import{ConvertError as Q}from"@mdgate/core";import{documentToMarkdown as L}from"@mdgate/document";import{fileExtension as I}from"@mdgate/utils";import{emptyDocument as $,plain as P}from"@mdgate/document";import{trim as k}from"@mdgate/utils";var N="€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ";function z(q){return C(O(q))}function j(q){let G=$(),H=q.replace(/\r\n/g,`
2
+ `).replace(/\r/g,`
3
+ `);for(let J of H.split(/\n(?:[ \t]*\n)+/)){let Z=k(J);if(Z.length===0)continue;G.blocks.push({type:"paragraph",inlines:[P(Z)]})}return G}function K(q,G){let H=$();return H.blocks.push({type:"codeBlock",lang:G,text:q}),H}function O(q){if(q.length>=2&&q[0]===255&&q[1]===254)return new TextDecoder("utf-16le").decode(q);if(q.length>=2&&q[0]===254&&q[1]===255)return new TextDecoder("utf-16be").decode(q);let G=q.length>=3&&q[0]===239&&q[1]===187&&q[2]===191?q.subarray(3):q;try{return new TextDecoder("utf-8",{fatal:!0}).decode(G)}catch{return w(G)}}function w(q){let G="";for(let H=0;H<q.length;H+=1){let J=q[H];if(J<128||J>=160)G+=String.fromCharCode(J);else G+=N[J-128]}return G}function C(q){return q.charCodeAt(0)===65279?q.slice(1):q}var A=new Set(["txt","text"]),B=new Set(["md","markdown","mdx"]),D=new Set(["js","jsx","ts","tsx","py","rb","go","rs","java","kt","c","h","cc","cpp","cs","swift","sh","bash","zsh","sql","css","scss","less","graphql"]),R=new Set([...A,...B,...D]);function Y(){return{id:"text",sniff(q,G){let H=G?.path!==void 0?I(G.path):void 0;if(H==="txt"&&M(q)&&X(q.subarray(3)))return 2;if(H!==void 0&&R.has(H))return 1;return 0},convert(q,G){F(q);let H=G?.path!==void 0?I(G.path):void 0;if(H===void 0||!R.has(H))throw Q.unsupported(H??"text");let J=z(q);if(B.has(H))return{markdown:J.endsWith(`
4
+ `)?J:`${J}
5
+ `};if(A.has(H))return{markdown:L(j(J))};return{markdown:L(K(J,H))}}}}function F(q){let G=q.length>=3&&q[0]===239&&q[1]===187&&q[2]===191?q.subarray(3):q;if(V(G,[37,80,68,70,45]))throw Q.unsupported("pdf");if(V(G,[208,207,17,224,161,177,26,225]))throw Q.unsupported("ole");if(V(G,[80,75,3,4]))throw Q.unsupported("zip")}function M(q){return q.length>=3&&q[0]===239&&q[1]===187&&q[2]===191}function X(q){try{let G=new TextDecoder("utf-8",{fatal:!0}).decode(q);for(let H=0;H<G.length;H+=1){let J=G.charCodeAt(H);if(J===9||J===10||J===13)continue;if(J<32||J===127)return!1}return!0}catch{return!1}}function V(q,G){if(q.length<G.length)return!1;for(let H=0;H<G.length;H+=1)if(q[H]!==G[H])return!1;return!0}import{create as f}from"@mdgate/core";var U=f([Y()]);function W(q,G){return U(q,G)}export{W as toMarkdown,Y as text};
package/dist/text.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Converter } from '@mdgate/core';
2
+ export declare function text(): Converter;
@@ -0,0 +1,6 @@
1
+ import { type ConvertHint } from '@mdgate/core';
2
+ /**
3
+ * Convert a single file's bytes to Markdown with the text converter.
4
+ * `hint.path` is only a sniff hint; it is never read from disk.
5
+ */
6
+ export declare function toMarkdown(bytes: Uint8Array, hint?: ConvertHint): Promise<string>;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@mdgate/text",
3
+ "version": "0.1.0",
4
+ "description": "mdgate plain text, markdown, and source converter",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "files": [
17
+ "dist/**/*.js",
18
+ "dist/**/*.d.ts"
19
+ ],
20
+ "scripts": {
21
+ "build": "bun ../../scripts/build-package.ts",
22
+ "prepublishOnly": "bun run build"
23
+ },
24
+ "dependencies": {
25
+ "@mdgate/core": "0.1.3",
26
+ "@mdgate/document": "0.1.0",
27
+ "@mdgate/utils": "0.1.2"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "engines": {
33
+ "node": ">=20"
34
+ },
35
+ "keywords": [
36
+ "mdgate",
37
+ "markdown",
38
+ "text"
39
+ ]
40
+ }