@kklab/json2markdown 0.0.26 → 0.0.28
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/converter/Converter.d.ts +1 -0
- package/dist/index.js +30 -20
- package/dist/index.umd.js +17 -16
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/safeParseJson.d.ts +2 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var c = Object.defineProperty;
|
|
2
|
+
var h = (i, r, t) => r in i ? c(i, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[r] = t;
|
|
3
|
+
var a = (i, r, t) => (h(i, typeof r != "symbol" ? r + "" : r, t), t);
|
|
4
4
|
const e = {
|
|
5
5
|
A: "a",
|
|
6
6
|
BLOCKQUOTE: "blockquote",
|
|
@@ -13,19 +13,26 @@ const e = {
|
|
|
13
13
|
PRE: "pre",
|
|
14
14
|
TD: "td",
|
|
15
15
|
TR: "tr"
|
|
16
|
+
}, l = (i) => {
|
|
17
|
+
if (typeof i == "string")
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(i);
|
|
20
|
+
} catch {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
16
23
|
};
|
|
17
|
-
class
|
|
24
|
+
class u {
|
|
18
25
|
constructor(r) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
this.
|
|
26
|
+
a(this, "data");
|
|
27
|
+
a(this, "elements", []);
|
|
28
|
+
a(this, "onConvert", (r) => r);
|
|
29
|
+
this.setData(r);
|
|
23
30
|
}
|
|
24
31
|
/**
|
|
25
32
|
* Converts the provided data into Markdown format.
|
|
26
33
|
*/
|
|
27
34
|
static toMarkdown(r, t) {
|
|
28
|
-
return new
|
|
35
|
+
return new u(r).toMarkdown(t);
|
|
29
36
|
}
|
|
30
37
|
/**
|
|
31
38
|
* Converts the provided data into Markdown format.
|
|
@@ -85,7 +92,12 @@ ${t.value}
|
|
|
85
92
|
default:
|
|
86
93
|
return "";
|
|
87
94
|
}
|
|
88
|
-
}).join("")
|
|
95
|
+
}).join("").replace(/\n{2,}$/, `
|
|
96
|
+
`);
|
|
97
|
+
}
|
|
98
|
+
setData(r) {
|
|
99
|
+
const t = l(r);
|
|
100
|
+
return this.data = t ?? r, this;
|
|
89
101
|
}
|
|
90
102
|
getElements() {
|
|
91
103
|
return this.elements;
|
|
@@ -95,22 +107,20 @@ ${t.value}
|
|
|
95
107
|
return t ? (this.elements.push(t), !0) : !1;
|
|
96
108
|
}
|
|
97
109
|
convert(r) {
|
|
98
|
-
return r && (this.onConvert = r), Array.isArray(this.data) ? this.convertFromArray(this.data) : this.convertFromObject(this.data);
|
|
110
|
+
return r && (this.onConvert = r), Array.isArray(this.data) ? this.convertFromArray(this.data) : typeof this.data == "object" ? this.convertFromObject(this.data) : this.convertFromPrimitive(this.data);
|
|
99
111
|
}
|
|
100
112
|
convertFromArray(r, t = 0) {
|
|
101
113
|
if (r == null)
|
|
102
114
|
return this.convertFromPrimitive(r), this;
|
|
103
115
|
if (r.length > 0 && r.every((s) => typeof s == "object" && s !== null && Object.keys(s).length > 0)) {
|
|
104
|
-
const
|
|
105
|
-
this.pushElement({
|
|
116
|
+
const s = [...new Set(r.map(Object.keys).flat())];
|
|
117
|
+
return this.pushElement({
|
|
106
118
|
tag: e.TR,
|
|
107
|
-
values:
|
|
108
|
-
})
|
|
109
|
-
const n = Object.keys(s);
|
|
110
|
-
return r.forEach((o) => {
|
|
119
|
+
values: s
|
|
120
|
+
}), r.forEach((n) => {
|
|
111
121
|
this.pushElement({
|
|
112
122
|
tag: e.TD,
|
|
113
|
-
values:
|
|
123
|
+
values: s.map((o) => this.formatValue(n[o]))
|
|
114
124
|
});
|
|
115
125
|
}), this;
|
|
116
126
|
}
|
|
@@ -157,9 +167,9 @@ ${t.value}
|
|
|
157
167
|
}), this;
|
|
158
168
|
}
|
|
159
169
|
formatValue(r) {
|
|
160
|
-
return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) :
|
|
170
|
+
return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) : String(r).trim();
|
|
161
171
|
}
|
|
162
172
|
}
|
|
163
173
|
export {
|
|
164
|
-
|
|
174
|
+
u as Converter
|
|
165
175
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
(function(s,
|
|
1
|
+
(function(s,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(s=typeof globalThis<"u"?globalThis:s||self,e(s.JSON2Markdown={}))})(this,function(s){"use strict";var f=Object.defineProperty;var l=(s,e,o)=>e in s?f(s,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):s[e]=o;var a=(s,e,o)=>(l(s,typeof e!="symbol"?e+"":e,o),o);const e={A:"a",BLOCKQUOTE:"blockquote",BR:"br",HEADING:"heading",HR:"hr",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"},o=h=>{if(typeof h=="string")try{return JSON.parse(h)}catch{return}};class c{constructor(t){a(this,"data");a(this,"elements",[]);a(this,"onConvert",t=>t);this.setData(t)}static toMarkdown(t,r){return new c(t).toMarkdown(r)}toMarkdown(t){return this.convert(t),this.elements.map(r=>{switch(r.tag){case e.A:return`[${r.value}](${r.href})
|
|
2
2
|
|
|
3
|
-
`;case
|
|
3
|
+
`;case e.BLOCKQUOTE:return`> ${r.value}
|
|
4
4
|
|
|
5
|
-
`;case
|
|
6
|
-
`;case
|
|
5
|
+
`;case e.BR:return`
|
|
6
|
+
`;case e.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,r.level)))} ${r.value}
|
|
7
7
|
|
|
8
|
-
`;case
|
|
8
|
+
`;case e.HR:return`---
|
|
9
9
|
|
|
10
|
-
`;case
|
|
10
|
+
`;case e.IMG:return`
|
|
11
11
|
|
|
12
|
-
`;case
|
|
13
|
-
`," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(
|
|
14
|
-
`}case
|
|
12
|
+
`;case e.LI:{const n=i=>i.replaceAll(`
|
|
13
|
+
`," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(r.indent)}- ${n(r.value)}
|
|
14
|
+
`}case e.P:return`${r.value}
|
|
15
15
|
|
|
16
|
-
`;case
|
|
17
|
-
${
|
|
16
|
+
`;case e.PRE:return`\`\`\`
|
|
17
|
+
${r.value}
|
|
18
18
|
\`\`\`
|
|
19
19
|
|
|
20
|
-
`;case
|
|
21
|
-
`,"<br>");return`| ${
|
|
22
|
-
`}case
|
|
23
|
-
| ${
|
|
24
|
-
`;default:return""}}).join("")
|
|
20
|
+
`;case e.TD:{const n=i=>i.replaceAll("|","\\|").replaceAll(`
|
|
21
|
+
`,"<br>");return`| ${r.values.map(n).join(" | ")} |
|
|
22
|
+
`}case e.TR:return`| ${r.values.join(" | ")} |
|
|
23
|
+
| ${r.values.map(()=>"---").join(" | ")} |
|
|
24
|
+
`;default:return""}}).join("").replace(/\n{2,}$/,`
|
|
25
|
+
`)}setData(t){const r=o(t);return this.data=r??t,this}getElements(){return this.elements}pushElement(t){const r=this.onConvert(t);return r?(this.elements.push(r),!0):!1}convert(t){return t&&(this.onConvert=t),Array.isArray(this.data)?this.convertFromArray(this.data):typeof this.data=="object"?this.convertFromObject(this.data):this.convertFromPrimitive(this.data)}convertFromArray(t,r=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=[...new Set(t.map(Object.keys).flat())];return this.pushElement({tag:e.TR,values:n}),t.forEach(i=>{this.pushElement({tag:e.TD,values:n.map(u=>this.formatValue(i[u]))})}),this}for(const n of t){if(Array.isArray(n)){this.convertFromArray(n,r+1);continue}this.pushElement({tag:e.LI,value:this.formatValue(n),indent:r})}return this}convertFromObject(t,r=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[n,i]of Object.entries(t))if(this.pushElement({tag:e.HEADING,value:n,level:r+1})){if(Array.isArray(i)){this.convertFromArray(i),this.pushElement({tag:e.BR});continue}if(typeof i=="object"){this.convertFromObject(i,r+1);continue}this.convertFromPrimitive(i)}return this}convertFromPrimitive(t){return this.pushElement({tag:e.P,value:this.formatValue(t)}),this}formatValue(t){return t==null?"":Array.isArray(t)?t.map(r=>this.formatValue(r)).join(", "):typeof t=="object"?JSON.stringify(t):String(t).trim()}}s.Converter=c,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|