@mdgate/data 0.5.0 → 0.5.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.
Files changed (2) hide show
  1. package/dist/index.js +5 -5
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,5 +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};
1
+ import{ConvertError as p}from"@mdgate/core";import{documentToMarkdown as f}from"@mdgate/document";import{fileExtension as E}from"@mdgate/utils";import{ConvertError as v}from"@mdgate/core";var $=[208,207,17,224,161,177,26,225];function m(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 S(n){let c=m(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 w(n){if(n.length<$.length)return!1;for(let c=0;c<$.length;c+=1)if(n[c]!==$[c])return!1;return!0}function O(n){let c=m(n);return A(n,"{\\rtf",c)}function H(n){return S(n)||j(n)||w(n)||O(n)}function L(n){let c=m(n);if(c>=n.length)return!1;let r=n[c];return r===123||r===91}function J(n){let c=m(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&&s(r)}function X(n){if(n.length>=2&&n[0]===255&&n[1]===254)return W(new TextDecoder("utf-16le").decode(n));if(n.length>=2&&n[0]===254&&n[1]===255)return W(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 v.malformed("invalid utf-8")}}function C(n){let c=n.split(/[/\\]/).pop()??"",r=c.lastIndexOf("."),u=r<=0?c:c.slice(0,r);return u.length>0?u:void 0}function W(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 u=0;u<c.length;u+=1)if(n[r+u]!==c.charCodeAt(u))return!1;return!0}function s(n){return n>=48&&n<=57||n>=65&&n<=90||n>=97&&n<=122||n===45||n===95||n===58}import{ConvertError as _}from"@mdgate/core";import{cleanText as rn}from"@mdgate/utils";import{emptyDocument as Z,heading as y,plain as K,tableFromRows as b}from"@mdgate/document";import{cleanText as nn}from"@mdgate/utils";function t(n,c){let r=Z();return r.blocks.push({type:"codeBlock",lang:n,text:c}),r}function d(n,c){let r=Z();if(n!==void 0&&n.length>0)r.blocks.push(y(1,[K(nn(n))]));return c(r),r}function G(n){return n.map((c)=>({blocks:[{type:"paragraph",inlines:[K(c)]}],checked:void 0,markerLabel:void 0}))}function I(n){return{blocks:[{type:"paragraph",inlines:[K(n)]}],colSpan:1,rowSpan:1}}function z(n,c){let r=[n.map(I),...c.map((u)=>n.map((o,i)=>I(u[i]??"")))];return{type:"table",table:b(r,1,"data")}}var cn=24,M=50,P=12;function Y(n,c){let r;try{r=JSON.parse(n)}catch(o){throw _.malformed(o instanceof Error?o.message:"invalid json")}return en(r,c)??t("json",JSON.stringify(r,null,2))}function q(n,c){let r=on(n);if(r.length===0)throw _.malformed("empty jsonl");if(r.length<=M&&r.every(N)){let o=B(r);if(o!==void 0&&o.length>0&&o.length<=P)return d(c,(i)=>{i.blocks.push(z(o,r.map((e)=>o.map((l)=>R(e[l])))))})}let u=d(c,()=>{return});for(let o=0;o<r.length;o+=1)u.blocks.push({type:"codeBlock",lang:"json",text:JSON.stringify(r[o],null,2)});return u}function x(n,c){try{return Y(n,c)}catch(r){if(un(n))return q(n,c);throw r}}function on(n){let c=n.split(`
2
+ `),r=[];for(let u=0;u<c.length;u+=1){let o=c[u];if(o.endsWith("\r"))o=o.slice(0,-1);let i=o.trim();if(i.length===0)continue;try{r.push(JSON.parse(i))}catch(e){throw _.malformed(e instanceof Error?`jsonl line ${u+1}: ${e.message}`:`jsonl line ${u+1}`)}}return r}function un(n){let c=0;for(let r of n.split(`
3
+ `)){let u=(r.endsWith("\r")?r.slice(0,-1):r).trim();if(u.length===0)continue;try{JSON.parse(u)}catch{return!1}if(c+=1,c>=2)return!0}return!1}function en(n,c){if(N(n)){let r=Object.keys(n);if(r.length===0||r.length>cn)return;return d(c,(u)=>{u.blocks.push({type:"list",list:{marker:"bullet",start:1,items:G(r.map((o)=>`${o}: ${R(n[o])}`))}})})}if(Array.isArray(n)&&n.length>0&&n.length<=M){if(!n.every(N))return;let r=B(n);if(r===void 0||r.length===0||r.length>P)return;return d(c,(u)=>{u.blocks.push(z(r,n.map((o)=>r.map((i)=>R(o[i])))))})}return}function B(n){let c=Object.keys(n[0]),r=new Set(c);for(let u=1;u<n.length;u+=1){let o=Object.keys(n[u]);if(o.length!==r.size)return;for(let i of o)if(!r.has(i))return}return c}function N(n){if(n===null||typeof n!=="object"||Array.isArray(n))return!1;for(let c of Object.values(n))if(!fn(c))return!1;return!0}function fn(n){return n===null||typeof n==="string"||typeof n==="number"||typeof n==="boolean"}function R(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 t("xml",ln(n))}function T(n){return t("yaml",n.trimEnd())}function ln(n){let c=n.trim();if(c.length===0)return"";let r=tn(c),u=[],o=0;for(let i=0;i<r.length;i+=1){let e=r[i],l=F(e);if(l==="text"){let g=e.trim();if(g.length===0)continue;let a=r[i+1],D=r[i-1];if(a!==void 0&&D!==void 0&&F(D)==="open"&&F(a)==="close"&&!g.includes(`
4
+ `)){let k=u.pop();u.push(`${k??`${h(o)}${D}`}${g}${a}`),i+=1,o=Math.max(0,o-1);continue}for(let k of g.split(/\r?\n/)){let V=k.trim();if(V.length>0)u.push(`${h(o)}${V}`)}continue}if(l==="close"){o=Math.max(0,o-1),u.push(`${h(o)}${e}`);continue}if(u.push(`${h(o)}${e}`),l==="open")o+=1}return u.join(`
5
+ `)}function tn(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 u=n.indexOf(">",r+1),o=u<0?n.length:u+1;c.push(n.slice(r,o)),r=o}return c}function F(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 dn=new Set(["json","jsonl"]),pn=new Set(["xml","yaml","yml"]);function U(){return{id:"data",sniff(n,c){if(H(n))return 0;let r=c?.path!==void 0?E(c.path):void 0;if(r!==void 0&&dn.has(r))return 2;if(!J(n)&&L(n))return 2;if(r!==void 0&&pn.has(r))return 1;return 0},convert(n,c){gn(n);let r=c?.path!==void 0?E(c.path):void 0,u=c?.path!==void 0?C(c.path):void 0,o=X(n);if(r==="jsonl")return{markdown:f(q(o,u))};if(r==="json")return{markdown:f(Y(o,u))};if(r==="xml")return{markdown:f(Q(o))};if(r==="yaml"||r==="yml")return{markdown:f(T(o))};if(L(n))return{markdown:f(x(o,u))};if(J(n))return{markdown:f(Q(o))};throw p.unsupported("data")}}}function gn(n){if(S(n))throw p.unsupported("pdf");if(w(n))throw p.unsupported("ole");if(j(n))throw p.unsupported("zip");if(O(n))throw p.unsupported("rtf")}import{create as mn}from"@mdgate/core";var hn=mn([U()]);function an(n,c){return hn(n,c)}export{an as toMarkdown,U as data};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/data",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "mdgate JSON, JSONL, XML, and YAML converter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,9 +22,9 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "dependencies": {
25
- "@mdgate/core": "0.5.0",
26
- "@mdgate/document": "0.5.0",
27
- "@mdgate/utils": "0.5.0"
25
+ "@mdgate/core": "0.5.1",
26
+ "@mdgate/document": "0.5.1",
27
+ "@mdgate/utils": "0.5.1"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "public"