@mdgate/data 0.5.2 → 0.5.4

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 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};
1
+ import{ConvertError as h}from"@mdgate/core";import{documentToMarkdown as g}from"@mdgate/document";import{fileExtension as v}from"@mdgate/utils";import{ConvertError as s}from"@mdgate/core";var w=[208,207,17,224,161,177,26,225];function $(n){let u=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)u=3;while(u<n.length){let r=n[u];if(r!==9&&r!==10&&r!==13&&r!==32)break;u+=1}return u}function J(n){let u=$(n);return d(n,"%PDF-",u)}function e(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}function L(n){if(n.length<w.length)return!1;for(let u=0;u<w.length;u+=1)if(n[u]!==w[u])return!1;return!0}function O(n){let u=$(n);return d(n,"{\\rtf",u)}function I(n){return J(n)||e(n)||L(n)||O(n)}function K(n){let u=$(n);if(u>=n.length)return!1;let r=n[u];if(r===123)return y(n,u+1);if(r===91)return b(n,u+1);return!1}function z(n,u){while(u<n.length){let r=n[u];if(r!==9&&r!==10&&r!==13&&r!==32)break;u+=1}return u}function y(n,u){let r=z(n,u);if(r>=n.length)return!0;let o=n[r];return o===34||o===125}function b(n,u){let r=z(n,u);if(r>=n.length)return!0;let o=n[r];if(o===93||o===123||o===91||o===34)return!0;if(o===116)return d(n,"true",r);if(o===102)return d(n,"false",r);if(o===110)return d(n,"null",r);if(o===45||o>=48&&o<=57)return nn(n,r);return!1}function nn(n,u){let r=u;if(n[r]===45)r+=1;if(r>=n.length||n[r]<48||n[r]>57)return!1;if(n[r]===48)r+=1;else while(r<n.length&&n[r]>=48&&n[r]<=57)r+=1;if(n[r]===46){if(r+=1,r>=n.length||n[r]<48||n[r]>57)return!1;while(r<n.length&&n[r]>=48&&n[r]<=57)r+=1}let o=n[r];if(o===101||o===69){if(r+=1,n[r]===43||n[r]===45)r+=1;if(r>=n.length||n[r]<48||n[r]>57)return!1;while(r<n.length&&n[r]>=48&&n[r]<=57)r+=1}if(r=z(n,r),r>=n.length)return!0;let c=n[r];return c===44||c===93}function R(n){let u=$(n);if(d(n,"<?xml",u))return!0;if(u>=n.length||n[u]!==60)return!1;let r=n[u+1];return r!==void 0&&rn(r)}function Z(n){if(n.length>=2&&n[0]===255&&n[1]===254)return C(new TextDecoder("utf-16le").decode(n));if(n.length>=2&&n[0]===254&&n[1]===255)return C(new TextDecoder("utf-16be").decode(n));let u=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(u)}catch{throw s.malformed("invalid utf-8")}}function G(n){let u=n.split(/[/\\]/).pop()??"",r=u.lastIndexOf("."),o=r<=0?u:u.slice(0,r);return o.length>0?o:void 0}function C(n){return n.charCodeAt(0)===65279?n.slice(1):n}function d(n,u,r){if(r+u.length>n.length)return!1;for(let o=0;o<u.length;o+=1)if(n[r+o]!==u.charCodeAt(o))return!1;return!0}function rn(n){return n>=48&&n<=57||n>=65&&n<=90||n>=97&&n<=122||n===45||n===95||n===58}import{ConvertError as F}from"@mdgate/core";import{cleanText as ln}from"@mdgate/utils";import{emptyDocument as P,heading as un,plain as _,tableFromRows as on}from"@mdgate/document";import{cleanText as cn}from"@mdgate/utils";function p(n,u){let r=P();return r.blocks.push({type:"codeBlock",lang:n,text:u}),r}function i(n,u){let r=P();if(n!==void 0&&n.length>0)r.blocks.push(un(1,[_(cn(n))]));return u(r),r}function B(n){return n.map((u)=>({blocks:[{type:"paragraph",inlines:[_(u)]}],checked:void 0,markerLabel:void 0}))}function M(n){return{blocks:[{type:"paragraph",inlines:[_(n)]}],colSpan:1,rowSpan:1}}function N(n,u){let r=[n.map(M),...u.map((o)=>n.map((c,l)=>M(o[l]??"")))];return{type:"table",table:on(r,1,"data")}}var fn=24,T=50,W=12;function Q(n,u){let r;try{r=JSON.parse(n)}catch(c){throw F.malformed(c instanceof Error?c.message:"invalid json")}return mn(r,u)??p("json",JSON.stringify(r,null,2))}function U(n,u){let r=dn(n);if(r.length===0)throw F.malformed("empty jsonl");if(r.length<=T&&r.every(Y)){let c=E(r);if(c!==void 0&&c.length>0&&c.length<=W)return i(u,(l)=>{l.blocks.push(N(c,r.map((f)=>c.map((m)=>q(f[m])))))})}let o=i(u,()=>{return});for(let c=0;c<r.length;c+=1)o.blocks.push({type:"codeBlock",lang:"json",text:JSON.stringify(r[c],null,2)});return o}function x(n,u){try{return Q(n,u)}catch(r){if(gn(n))return U(n,u);throw r}}function dn(n){let u=n.split(`
2
+ `),r=[];for(let o=0;o<u.length;o+=1){let c=u[o];if(c.endsWith("\r"))c=c.slice(0,-1);let l=c.trim();if(l.length===0)continue;try{r.push(JSON.parse(l))}catch(f){throw F.malformed(f instanceof Error?`jsonl line ${o+1}: ${f.message}`:`jsonl line ${o+1}`)}}return r}function gn(n){let u=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(u+=1,u>=2)return!0}return!1}function mn(n,u){if(Y(n)){let r=Object.keys(n);if(r.length===0||r.length>fn)return;return i(u,(o)=>{o.blocks.push({type:"list",list:{marker:"bullet",start:1,items:B(r.map((c)=>`${c}: ${q(n[c])}`))}})})}if(Array.isArray(n)&&n.length>0&&n.length<=T){if(!n.every(Y))return;let r=E(n);if(r===void 0||r.length===0||r.length>W)return;return i(u,(o)=>{o.blocks.push(N(r,n.map((c)=>r.map((l)=>q(c[l])))))})}return}function E(n){let u=Object.keys(n[0]),r=new Set(u);for(let o=1;o<n.length;o+=1){let c=Object.keys(n[o]);if(c.length!==r.size)return;for(let l of c)if(!r.has(l))return}return u}function Y(n){if(n===null||typeof n!=="object"||Array.isArray(n))return!1;for(let u of Object.values(n))if(!pn(u))return!1;return!0}function pn(n){return n===null||typeof n==="string"||typeof n==="number"||typeof n==="boolean"}function q(n){if(n===null||n===void 0)return"";if(typeof n==="string")return ln(n);if(typeof n==="number"&&Number.isFinite(n))return String(n);if(typeof n==="boolean")return n?"true":"false";return""}function A(n){return p("xml",hn(n))}function t(n){return p("yaml",n.trimEnd())}function hn(n){let u=n.trim();if(u.length===0)return"";let r=Dn(u),o=[],c=0;for(let l=0;l<r.length;l+=1){let f=r[l],m=V(f);if(m==="text"){let D=f.trim();if(D.length===0)continue;let S=r[l+1],k=r[l-1];if(S!==void 0&&k!==void 0&&V(k)==="open"&&V(S)==="close"&&!D.includes(`
4
+ `)){let j=o.pop();o.push(`${j??`${a(c)}${k}`}${D}${S}`),l+=1,c=Math.max(0,c-1);continue}for(let j of D.split(/\r?\n/)){let X=j.trim();if(X.length>0)o.push(`${a(c)}${X}`)}continue}if(m==="close"){c=Math.max(0,c-1),o.push(`${a(c)}${f}`);continue}if(o.push(`${a(c)}${f}`),m==="open")c+=1}return o.join(`
5
+ `)}function Dn(n){let u=[],r=0;while(r<n.length){if(n.charCodeAt(r)!==60){let l=n.indexOf("<",r),f=l<0?n.length:l;u.push(n.slice(r,f)),r=f;continue}if(n.startsWith("<!--",r)){let l=n.indexOf("-->",r+4),f=l<0?n.length:l+3;u.push(n.slice(r,f)),r=f;continue}if(n.startsWith("<![CDATA[",r)){let l=n.indexOf("]]>",r+9),f=l<0?n.length:l+3;u.push(n.slice(r,f)),r=f;continue}let o=n.indexOf(">",r+1),c=o<0?n.length:o+1;u.push(n.slice(r,c)),r=c}return u}function V(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 a(n){return" ".repeat(n)}var $n=new Set(["json","jsonl"]),an=new Set(["xml","yaml","yml"]);function H(){return{id:"data",sniff(n,u){if(I(n))return 0;let r=u?.path!==void 0?v(u.path):void 0;if(r!==void 0&&$n.has(r))return 2;if(!R(n)&&K(n))return 2;if(r!==void 0&&an.has(r))return 1;return 0},convert(n,u){Sn(n);let r=u?.path!==void 0?v(u.path):void 0,o=u?.path!==void 0?G(u.path):void 0,c=Z(n);if(r==="jsonl")return{markdown:g(U(c,o))};if(r==="json")return{markdown:g(Q(c,o))};if(r==="xml")return{markdown:g(A(c))};if(r==="yaml"||r==="yml")return{markdown:g(t(c))};if(K(n))return{markdown:g(x(c,o))};if(R(n))return{markdown:g(A(c))};throw h.unsupported("data")}}}function Sn(n){if(J(n))throw h.unsupported("pdf");if(L(n))throw h.unsupported("ole");if(e(n))throw h.unsupported("zip");if(O(n))throw h.unsupported("rtf")}import{create as kn}from"@mdgate/core";var jn=kn([H()]);function wn(n,u){return jn(n,u)}export{wn as toMarkdown,H as data};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/data",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
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.2",
26
- "@mdgate/document": "0.5.2",
27
- "@mdgate/utils": "0.5.2"
25
+ "@mdgate/core": "0.5.4",
26
+ "@mdgate/document": "0.5.4",
27
+ "@mdgate/utils": "0.5.4"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "public"