@kklab/json2markdown 0.0.19 → 0.0.20
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 +11 -9
- package/dist/index.umd.js +4 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var o = (n, r, t) => (
|
|
1
|
+
var c = Object.defineProperty;
|
|
2
|
+
var f = (n, r, t) => r in n ? c(n, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[r] = t;
|
|
3
|
+
var o = (n, r, t) => (f(n, typeof r != "symbol" ? r + "" : r, t), t);
|
|
4
4
|
const e = {
|
|
5
5
|
A: "a",
|
|
6
6
|
BLOCKQUOTE: "blockquote",
|
|
@@ -14,7 +14,7 @@ const e = {
|
|
|
14
14
|
TD: "td",
|
|
15
15
|
TR: "tr"
|
|
16
16
|
};
|
|
17
|
-
class
|
|
17
|
+
class h {
|
|
18
18
|
constructor(r) {
|
|
19
19
|
o(this, "data");
|
|
20
20
|
o(this, "elements", []);
|
|
@@ -25,7 +25,7 @@ class a {
|
|
|
25
25
|
* Converts the provided data into Markdown format.
|
|
26
26
|
*/
|
|
27
27
|
static toMarkdown(r, t) {
|
|
28
|
-
return new
|
|
28
|
+
return new h(r).toMarkdown(t);
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* Converts the provided data into Markdown format.
|
|
@@ -97,13 +97,15 @@ ${t.value}
|
|
|
97
97
|
return this.convertFromPrimitive(r), this;
|
|
98
98
|
if (r.length > 0 && r.every((s) => typeof s == "object" && s !== null && Object.keys(s).length > 0)) {
|
|
99
99
|
const [s] = r;
|
|
100
|
-
|
|
100
|
+
this.pushElement({
|
|
101
101
|
tag: e.TR,
|
|
102
102
|
values: Object.keys(s)
|
|
103
|
-
})
|
|
103
|
+
});
|
|
104
|
+
const i = Object.keys(s);
|
|
105
|
+
return r.forEach((u) => {
|
|
104
106
|
this.pushElement({
|
|
105
107
|
tag: e.TD,
|
|
106
|
-
values:
|
|
108
|
+
values: i.map((a) => this.formatValue(u[a]))
|
|
107
109
|
});
|
|
108
110
|
}), this;
|
|
109
111
|
}
|
|
@@ -154,5 +156,5 @@ ${t.value}
|
|
|
154
156
|
}
|
|
155
157
|
}
|
|
156
158
|
export {
|
|
157
|
-
|
|
159
|
+
h as Converter
|
|
158
160
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(i,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(i=typeof globalThis<"u"?globalThis:i||self,r(i.JSON2Markdown={}))})(this,function(i){"use strict";var a=Object.defineProperty;var f=(i,r,s)=>r in i?a(i,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[r]=s;var u=(i,r,s)=>(f(i,typeof r!="symbol"?r+"":r,s),s);const r={A:"a",BLOCKQUOTE:"blockquote",BR:"br",HEADING:"heading",HR:"hr",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"};class s{constructor(t){u(this,"data");u(this,"elements",[]);u(this,"onConvert",t=>t);this.data=typeof t=="string"?JSON.parse(t):t}static toMarkdown(t,e){return new s(t).toMarkdown(e)}toMarkdown(t){return this.convert(t),this.elements.map(e=>{switch(e.tag){case r.A:return`[${e.value}](${e.href})
|
|
2
2
|
|
|
3
3
|
`;case r.BLOCKQUOTE:return`> ${e.value}
|
|
4
4
|
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
${e.value}
|
|
17
17
|
\`\`\`
|
|
18
18
|
|
|
19
|
-
`;case r.TD:return`| ${e.values.map(
|
|
20
|
-
`,"<br>"):
|
|
19
|
+
`;case r.TD:return`| ${e.values.map(n=>typeof n=="string"?n.replaceAll(`
|
|
20
|
+
`,"<br>"):n).join(" | ")} |
|
|
21
21
|
`;case r.TR:return`| ${e.values.join(" | ")} |
|
|
22
22
|
| ${e.values.map(()=>"---").join(" | ")} |
|
|
23
|
-
`;default:return""}}).join("")}getElements(){return this.elements}pushElement(t){const e=this.onConvert(t);return e?(this.elements.push(e),!0):!1}convert(t){return t&&(this.onConvert=t),Array.isArray(this.data)&&this.convertFromArray(this.data),this.convertFromObject(this.data),this}convertFromArray(t,e=0){if(t==null)return this.convertFromPrimitive(t),this;if(t.length>0&&t.every(
|
|
23
|
+
`;default:return""}}).join("")}getElements(){return this.elements}pushElement(t){const e=this.onConvert(t);return e?(this.elements.push(e),!0):!1}convert(t){return t&&(this.onConvert=t),Array.isArray(this.data)&&this.convertFromArray(this.data),this.convertFromObject(this.data),this}convertFromArray(t,e=0){if(t==null)return this.convertFromPrimitive(t),this;if(t.length>0&&t.every(n=>typeof n=="object"&&n!==null&&Object.keys(n).length>0)){const[n]=t;this.pushElement({tag:r.TR,values:Object.keys(n)});const o=Object.keys(n);return t.forEach(c=>{this.pushElement({tag:r.TD,values:o.map(h=>this.formatValue(c[h]))})}),this}for(const n of t){if(Array.isArray(n)){this.convertFromArray(n,e+1);continue}this.pushElement({tag:r.LI,value:this.formatValue(n),indent:e})}return this}convertFromObject(t,e=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[n,o]of Object.entries(t))if(this.pushElement({tag:r.HEADING,value:n,level:e+1})){if(Array.isArray(o)){this.convertFromArray(o),this.pushElement({tag:r.BR});continue}if(typeof o=="object"){this.convertFromObject(o,e+1);continue}this.convertFromPrimitive(o)}return this}convertFromPrimitive(t){return this.pushElement({tag:r.P,value:this.formatValue(t)}),this}formatValue(t){return t==null?"":Array.isArray(t)?t.map(e=>this.formatValue(e)).join(", "):typeof t=="object"?JSON.stringify(t):typeof t=="string"?t.trim():t}}i.Converter=s,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|