@kklab/json2markdown 0.0.26 → 0.0.27
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 +23 -11
- 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
1
|
var h = Object.defineProperty;
|
|
2
2
|
var l = (i, r, t) => r in i ? h(i, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[r] = t;
|
|
3
|
-
var
|
|
3
|
+
var a = (i, r, t) => (l(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
|
+
}, v = (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 = v(r);
|
|
100
|
+
return this.data = t ?? r, this;
|
|
89
101
|
}
|
|
90
102
|
getElements() {
|
|
91
103
|
return this.elements;
|
|
@@ -95,7 +107,7 @@ ${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)
|
|
@@ -157,9 +169,9 @@ ${t.value}
|
|
|
157
169
|
}), this;
|
|
158
170
|
}
|
|
159
171
|
formatValue(r) {
|
|
160
|
-
return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) :
|
|
172
|
+
return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) : String(r).trim();
|
|
161
173
|
}
|
|
162
174
|
}
|
|
163
175
|
export {
|
|
164
|
-
|
|
176
|
+
u as Converter
|
|
165
177
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(n,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(n=typeof globalThis<"u"?globalThis:n||self,e(n.JSON2Markdown={}))})(this,function(n){"use strict";var l=Object.defineProperty;var v=(n,e,o)=>e in n?l(n,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[e]=o;var a=(n,e,o)=>(v(n,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 s=i=>i.replaceAll(`
|
|
13
|
+
`," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(r.indent)}- ${s(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 s=i=>i.replaceAll("|","\\|").replaceAll(`
|
|
21
|
+
`,"<br>");return`| ${r.values.map(s).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(s=>typeof s=="object"&&s!==null&&Object.keys(s).length>0)){const[s]=t;this.pushElement({tag:e.TR,values:Object.keys(s)});const i=Object.keys(s);return t.forEach(u=>{this.pushElement({tag:e.TD,values:i.map(f=>this.formatValue(u[f]))})}),this}for(const s of t){if(Array.isArray(s)){this.convertFromArray(s,r+1);continue}this.pushElement({tag:e.LI,value:this.formatValue(s),indent:r})}return this}convertFromObject(t,r=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[s,i]of Object.entries(t))if(this.pushElement({tag:e.HEADING,value:s,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()}}n.Converter=c,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|