@mdgate/data 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 +28 -0
- package/dist/data.d.ts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/to-markdown.d.ts +6 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @mdgate/data
|
|
2
|
+
|
|
3
|
+
Convert JSON, JSONL, XML, and YAML files to Markdown. Outputs GitHub-Flavored Markdown. Works in
|
|
4
|
+
Node, Edge, and browsers. No native addons.
|
|
5
|
+
|
|
6
|
+
Handles: `.json`, `.jsonl`, `.xml`, `.yaml`, `.yml`
|
|
7
|
+
|
|
8
|
+
XML is never claimed by content signature — flat ODF and many office parts start with `<?xml`.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { toMarkdown } from '@mdgate/data';
|
|
14
|
+
|
|
15
|
+
const markdown = await toMarkdown(bytes);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Compose with other converters:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { create } from '@mdgate/core';
|
|
22
|
+
import { data } from '@mdgate/data';
|
|
23
|
+
|
|
24
|
+
const convert = create([data()]);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Part of [mdgate converters](https://github.com/mdgate/converters); install
|
|
28
|
+
`@mdgate/converters` for every format at once.
|
package/dist/data.d.ts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import{ConvertError as m}from"@mdgate/core";import{documentToMarkdown as l}from"@mdgate/document";import{fileExtension as T}from"@mdgate/utils";import{ConvertError as s}from"@mdgate/core";var S=[208,207,17,224,161,177,26,225];function g(n){let c=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)c=3;while(c<n.length){let r=n[c];if(r!==9&&r!==10&&r!==13&&r!==32)break;c+=1}return c}function $(n){let c=g(n);return A(n,"%PDF-",c)}function j(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}function L(n){if(n.length<S.length)return!1;for(let c=0;c<S.length;c+=1)if(n[c]!==S[c])return!1;return!0}function O(n){let c=g(n);return A(n,"{\\rtf",c)}function V(n){return $(n)||j(n)||L(n)||O(n)}function w(n){let c=g(n);if(c>=n.length)return!1;let r=n[c];return r===123||r===91}function J(n){let c=g(n);if(A(n,"<?xml",c))return!0;if(c>=n.length||n[c]!==60)return!1;let r=n[c+1];return r!==void 0&&v(r)}function W(n){if(n.length>=2&&n[0]===255&&n[1]===254)return U(new TextDecoder("utf-16le").decode(n));if(n.length>=2&&n[0]===254&&n[1]===255)return U(new TextDecoder("utf-16be").decode(n));let c=n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191?n.subarray(3):n;try{return new TextDecoder("utf-8",{fatal:!0}).decode(c)}catch{throw s.malformed("invalid utf-8")}}function C(n){let c=n.split(/[/\\]/).pop()??"",r=c.lastIndexOf("."),o=r<=0?c:c.slice(0,r);return o.length>0?o:void 0}function U(n){return n.charCodeAt(0)===65279?n.slice(1):n}function A(n,c,r){if(r+c.length>n.length)return!1;for(let o=0;o<c.length;o+=1)if(n[r+o]!==c.charCodeAt(o))return!1;return!0}function v(n){return n>=48&&n<=57||n>=65&&n<=90||n>=97&&n<=122||n===45||n===95||n===58}import{ConvertError as R}from"@mdgate/core";import{cleanText as rn}from"@mdgate/utils";import{emptyDocument as I,heading as y,plain as N,tableFromRows as b}from"@mdgate/document";import{cleanText as nn}from"@mdgate/utils";function d(n,c){let r=I();return r.blocks.push({type:"codeBlock",lang:n,text:c}),r}function p(n,c){let r=I();if(n!==void 0&&n.length>0)r.blocks.push(y(1,[N(nn(n))]));return c(r),r}function Z(n){return n.map((c)=>({blocks:[{type:"paragraph",inlines:[N(c)]}],checked:void 0,markerLabel:void 0}))}function H(n){return{blocks:[{type:"paragraph",inlines:[N(n)]}],colSpan:1,rowSpan:1}}function _(n,c){let r=[n.map(H),...c.map((o)=>n.map((u,i)=>H(o[i]??"")))];return{type:"table",table:b(r,1,"data")}}var cn=24,G=50,P=12,on=200,un=80;function F(n,c){let r;try{r=JSON.parse(n)}catch(u){throw R.malformed(u instanceof Error?u.message:"invalid json")}return ln(r,c)??d("json",JSON.stringify(r,null,2))}function X(n,c){let r=en(n);if(r.length===0)throw R.malformed("empty jsonl");if(r.length<=G&&r.every(K)){let i=B(r);if(i!==void 0&&i.length>0&&i.length<=P)return p(c,(e)=>{e.blocks.push(_(i,r.map((t)=>i.map((f)=>z(t[f])))))})}let o=p(c,()=>{return}),u=Math.min(r.length,un);for(let i=0;i<u;i+=1)o.blocks.push({type:"codeBlock",lang:"json",text:JSON.stringify(r[i],null,2)});return o}function x(n,c){try{return F(n,c)}catch(r){if(fn(n))return X(n,c);throw r}}function en(n){let c=n.split(`
|
|
2
|
+
`),r=[];for(let o=0;o<c.length;o+=1){let u=c[o];if(u.endsWith("\r"))u=u.slice(0,-1);let i=u.trim();if(i.length===0)continue;if(r.length>=on)break;try{r.push(JSON.parse(i))}catch(e){throw R.malformed(e instanceof Error?`jsonl line ${o+1}: ${e.message}`:`jsonl line ${o+1}`)}}return r}function fn(n){let c=0;for(let r of n.split(`
|
|
3
|
+
`)){let o=(r.endsWith("\r")?r.slice(0,-1):r).trim();if(o.length===0)continue;try{JSON.parse(o)}catch{return!1}if(c+=1,c>=2)return!0}return!1}function ln(n,c){if(K(n)){let r=Object.keys(n);if(r.length===0||r.length>cn)return;return p(c,(o)=>{o.blocks.push({type:"list",list:{marker:"bullet",start:1,items:Z(r.map((u)=>`${u}: ${z(n[u])}`))}})})}if(Array.isArray(n)&&n.length>0&&n.length<=G){if(!n.every(K))return;let r=B(n);if(r===void 0||r.length===0||r.length>P)return;return p(c,(o)=>{o.blocks.push(_(r,n.map((u)=>r.map((i)=>z(u[i])))))})}return}function B(n){let c=Object.keys(n[0]),r=new Set(c);for(let o=1;o<n.length;o+=1){let u=Object.keys(n[o]);if(u.length!==r.size)return;for(let i of u)if(!r.has(i))return}return c}function K(n){if(n===null||typeof n!=="object"||Array.isArray(n))return!1;for(let c of Object.values(n))if(!tn(c))return!1;return!0}function tn(n){return n===null||typeof n==="string"||typeof n==="number"||typeof n==="boolean"}function z(n){if(n===null||n===void 0)return"";if(typeof n==="string")return rn(n);if(typeof n==="number"&&Number.isFinite(n))return String(n);if(typeof n==="boolean")return n?"true":"false";return""}function q(n){return d("xml",dn(n))}function E(n){return d("yaml",n.trimEnd())}function dn(n){let c=n.trim();if(c.length===0)return"";let r=pn(c),o=[],u=0;for(let i=0;i<r.length;i+=1){let e=r[i],t=Y(e);if(t==="text"){let f=e.trim();if(f.length===0)continue;let a=r[i+1],D=r[i-1];if(a!==void 0&&D!==void 0&&Y(D)==="open"&&Y(a)==="close"&&!f.includes(`
|
|
4
|
+
`)){let k=o.pop();o.push(`${k??`${h(u)}${D}`}${f}${a}`),i+=1,u=Math.max(0,u-1);continue}for(let k of f.split(/\r?\n/)){let Q=k.trim();if(Q.length>0)o.push(`${h(u)}${Q}`)}continue}if(t==="close"){u=Math.max(0,u-1),o.push(`${h(u)}${e}`);continue}if(o.push(`${h(u)}${e}`),t==="open")u+=1}return o.join(`
|
|
5
|
+
`)}function pn(n){let c=[],r=0;while(r<n.length){if(n.charCodeAt(r)!==60){let i=n.indexOf("<",r),e=i<0?n.length:i;c.push(n.slice(r,e)),r=e;continue}if(n.startsWith("<!--",r)){let i=n.indexOf("-->",r+4),e=i<0?n.length:i+3;c.push(n.slice(r,e)),r=e;continue}if(n.startsWith("<![CDATA[",r)){let i=n.indexOf("]]>",r+9),e=i<0?n.length:i+3;c.push(n.slice(r,e)),r=e;continue}let o=n.indexOf(">",r+1),u=o<0?n.length:o+1;c.push(n.slice(r,u)),r=u}return c}function Y(n){if(n.charCodeAt(0)!==60)return"text";if(n.startsWith("</"))return"close";if(n.startsWith("<?")||n.startsWith("<!"))return"misc";if(n.endsWith("/>"))return"empty";return"open"}function h(n){return" ".repeat(n)}var mn=new Set(["json","jsonl"]),gn=new Set(["xml","yaml","yml"]);function M(){return{id:"data",sniff(n,c){if(V(n))return 0;let r=c?.path!==void 0?T(c.path):void 0;if(r!==void 0&&mn.has(r))return 2;if(!J(n)&&w(n))return 2;if(r!==void 0&&gn.has(r))return 1;return 0},convert(n,c){hn(n);let r=c?.path!==void 0?T(c.path):void 0,o=c?.path!==void 0?C(c.path):void 0,u=W(n);if(r==="jsonl")return{markdown:l(X(u,o))};if(r==="json")return{markdown:l(F(u,o))};if(r==="xml")return{markdown:l(q(u))};if(r==="yaml"||r==="yml")return{markdown:l(E(u))};if(w(n))return{markdown:l(x(u,o))};if(J(n))return{markdown:l(q(u))};throw m.unsupported("data")}}}function hn(n){if($(n))throw m.unsupported("pdf");if(L(n))throw m.unsupported("ole");if(j(n))throw m.unsupported("zip");if(O(n))throw m.unsupported("rtf")}import{create as an}from"@mdgate/core";var Dn=an([M()]);function kn(n,c){return Dn(n,c)}export{kn as toMarkdown,M as data};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ConvertHint } from '@mdgate/core';
|
|
2
|
+
/**
|
|
3
|
+
* Convert a single file's bytes to Markdown with the data 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,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mdgate/data",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "mdgate JSON, JSONL, XML, and YAML 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
|
+
"json",
|
|
39
|
+
"jsonl",
|
|
40
|
+
"xml",
|
|
41
|
+
"yaml"
|
|
42
|
+
]
|
|
43
|
+
}
|