@mdgate/data 0.4.1 → 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.
- package/dist/index.js +5 -5
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import{ConvertError as
|
|
2
|
-
`),r=[];for(let
|
|
3
|
-
`)){let
|
|
4
|
-
`)){let k=
|
|
5
|
-
`)}function
|
|
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.
|
|
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.
|
|
26
|
-
"@mdgate/document": "0.
|
|
27
|
-
"@mdgate/utils": "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"
|