@kklab/json2markdown 0.0.22 → 0.0.24
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 +36 -23
- package/dist/index.umd.js +14 -14
- package/dist/utils/escapeHTML.d.ts +2 -0
- package/dist/utils/index.d.ts +3 -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
|
-
var l = (
|
|
3
|
-
var
|
|
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 c = (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,25 @@ const e = {
|
|
|
13
13
|
PRE: "pre",
|
|
14
14
|
TD: "td",
|
|
15
15
|
TR: "tr"
|
|
16
|
+
}, f = (i) => i.replaceAll("<", "<").replaceAll(">", ">"), p = (i) => {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(i);
|
|
19
|
+
} catch {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
16
22
|
};
|
|
17
|
-
class
|
|
23
|
+
class u {
|
|
18
24
|
constructor(r) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
c(this, "data");
|
|
26
|
+
c(this, "elements", []);
|
|
27
|
+
c(this, "onConvert", (r) => r);
|
|
22
28
|
this.data = typeof r == "string" ? JSON.parse(r) : r;
|
|
23
29
|
}
|
|
24
30
|
/**
|
|
25
31
|
* Converts the provided data into Markdown format.
|
|
26
32
|
*/
|
|
27
33
|
static toMarkdown(r, t) {
|
|
28
|
-
return new
|
|
34
|
+
return new u(r).toMarkdown(t);
|
|
29
35
|
}
|
|
30
36
|
/**
|
|
31
37
|
* Converts the provided data into Markdown format.
|
|
@@ -56,10 +62,12 @@ class a {
|
|
|
56
62
|
return `
|
|
57
63
|
|
|
58
64
|
`;
|
|
59
|
-
case e.LI:
|
|
60
|
-
|
|
61
|
-
`, " ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)", "g"), (
|
|
65
|
+
case e.LI: {
|
|
66
|
+
const s = (n) => n.replaceAll(`
|
|
67
|
+
`, " ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)", "g"), (o) => o.trim());
|
|
68
|
+
return `${" ".repeat(t.indent)}- ${s(t.value)}
|
|
62
69
|
`;
|
|
70
|
+
}
|
|
63
71
|
case e.P:
|
|
64
72
|
return `${t.value}
|
|
65
73
|
|
|
@@ -70,10 +78,15 @@ ${t.value}
|
|
|
70
78
|
\`\`\`
|
|
71
79
|
|
|
72
80
|
`;
|
|
73
|
-
case e.TD:
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
case e.TD: {
|
|
82
|
+
const s = (n) => {
|
|
83
|
+
const o = p(n);
|
|
84
|
+
return typeof o == "object" ? `<pre><code>${f(JSON.stringify(o, null, 2))}</code></pre>` : n.replaceAll(`
|
|
85
|
+
`, "<br>").replaceAll("|", "\\|");
|
|
86
|
+
};
|
|
87
|
+
return `| ${t.values.map(s).join(" | ")} |
|
|
76
88
|
`;
|
|
89
|
+
}
|
|
77
90
|
case e.TR:
|
|
78
91
|
return `| ${t.values.join(" | ")} |
|
|
79
92
|
| ${t.values.map(() => "---").join(" | ")} |
|
|
@@ -102,11 +115,11 @@ ${t.value}
|
|
|
102
115
|
tag: e.TR,
|
|
103
116
|
values: Object.keys(s)
|
|
104
117
|
});
|
|
105
|
-
const
|
|
106
|
-
return r.forEach((
|
|
118
|
+
const n = Object.keys(s);
|
|
119
|
+
return r.forEach((o) => {
|
|
107
120
|
this.pushElement({
|
|
108
121
|
tag: e.TD,
|
|
109
|
-
values:
|
|
122
|
+
values: n.map((a) => this.formatValue(o[a]))
|
|
110
123
|
});
|
|
111
124
|
}), this;
|
|
112
125
|
}
|
|
@@ -126,23 +139,23 @@ ${t.value}
|
|
|
126
139
|
convertFromObject(r, t = 0) {
|
|
127
140
|
if (r == null)
|
|
128
141
|
return this.convertFromPrimitive(r), this;
|
|
129
|
-
for (const [s,
|
|
142
|
+
for (const [s, n] of Object.entries(r))
|
|
130
143
|
if (this.pushElement({
|
|
131
144
|
tag: e.HEADING,
|
|
132
145
|
value: s,
|
|
133
146
|
level: t + 1
|
|
134
147
|
})) {
|
|
135
|
-
if (Array.isArray(
|
|
136
|
-
this.convertFromArray(
|
|
148
|
+
if (Array.isArray(n)) {
|
|
149
|
+
this.convertFromArray(n), this.pushElement({
|
|
137
150
|
tag: e.BR
|
|
138
151
|
});
|
|
139
152
|
continue;
|
|
140
153
|
}
|
|
141
|
-
if (typeof
|
|
142
|
-
this.convertFromObject(
|
|
154
|
+
if (typeof n == "object") {
|
|
155
|
+
this.convertFromObject(n, t + 1);
|
|
143
156
|
continue;
|
|
144
157
|
}
|
|
145
|
-
this.convertFromPrimitive(
|
|
158
|
+
this.convertFromPrimitive(n);
|
|
146
159
|
}
|
|
147
160
|
return this;
|
|
148
161
|
}
|
|
@@ -157,5 +170,5 @@ ${t.value}
|
|
|
157
170
|
}
|
|
158
171
|
}
|
|
159
172
|
export {
|
|
160
|
-
|
|
173
|
+
u as Converter
|
|
161
174
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(s,t){typeof exports=="object"&&typeof module<"u"?t(exports):typeof define=="function"&&define.amd?define(["exports"],t):(s=typeof globalThis<"u"?globalThis:s||self,t(s.JSON2Markdown={}))})(this,function(s){"use strict";var p=Object.defineProperty;var v=(s,t,o)=>t in s?p(s,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):s[t]=o;var c=(s,t,o)=>(v(s,typeof t!="symbol"?t+"":t,o),o);const t={A:"a",BLOCKQUOTE:"blockquote",BR:"br",HEADING:"heading",HR:"hr",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"},o=h=>h.replaceAll("<","<").replaceAll(">",">"),l=h=>{try{return JSON.parse(h)}catch{return}};class a{constructor(r){c(this,"data");c(this,"elements",[]);c(this,"onConvert",r=>r);this.data=typeof r=="string"?JSON.parse(r):r}static toMarkdown(r,e){return new a(r).toMarkdown(e)}toMarkdown(r){return this.convert(r),this.elements.map(e=>{switch(e.tag){case t.A:return`[${e.value}](${e.href})
|
|
2
2
|
|
|
3
|
-
`;case
|
|
3
|
+
`;case t.BLOCKQUOTE:return`> ${e.value}
|
|
4
4
|
|
|
5
|
-
`;case
|
|
6
|
-
`;case
|
|
5
|
+
`;case t.BR:return`
|
|
6
|
+
`;case t.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,e.level)))} ${e.value}
|
|
7
7
|
|
|
8
|
-
`;case
|
|
8
|
+
`;case t.HR:return`---
|
|
9
9
|
|
|
10
|
-
`;case
|
|
10
|
+
`;case t.IMG:return`
|
|
11
11
|
|
|
12
|
-
`;case
|
|
13
|
-
`," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),
|
|
14
|
-
|
|
12
|
+
`;case t.LI:{const n=i=>i.replaceAll(`
|
|
13
|
+
`," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(e.indent)}- ${n(e.value)}
|
|
14
|
+
`}case t.P:return`${e.value}
|
|
15
15
|
|
|
16
|
-
`;case
|
|
16
|
+
`;case t.PRE:return`\`\`\`
|
|
17
17
|
${e.value}
|
|
18
18
|
\`\`\`
|
|
19
19
|
|
|
20
|
-
`;case
|
|
21
|
-
`,"<br>").replaceAll("|","\\|")).join(" | ")} |
|
|
22
|
-
|
|
20
|
+
`;case t.TD:{const n=i=>{const u=l(i);return typeof u=="object"?`<pre><code>${o(JSON.stringify(u,null,2))}</code></pre>`:i.replaceAll(`
|
|
21
|
+
`,"<br>").replaceAll("|","\\|")};return`| ${e.values.map(n).join(" | ")} |
|
|
22
|
+
`}case t.TR:return`| ${e.values.join(" | ")} |
|
|
23
23
|
| ${e.values.map(()=>"---").join(" | ")} |
|
|
24
|
-
`;default:return""}}).join("")}getElements(){return this.elements}pushElement(
|
|
24
|
+
`;default:return""}}).join("")}getElements(){return this.elements}pushElement(r){const e=this.onConvert(r);return e?(this.elements.push(e),!0):!1}convert(r){return r&&(this.onConvert=r),Array.isArray(this.data)&&this.convertFromArray(this.data),this.convertFromObject(this.data),this}convertFromArray(r,e=0){if(r==null)return this.convertFromPrimitive(r),this;if(r.length>0&&r.every(n=>typeof n=="object"&&n!==null&&Object.keys(n).length>0)){const[n]=r;this.pushElement({tag:t.TR,values:Object.keys(n)});const i=Object.keys(n);return r.forEach(u=>{this.pushElement({tag:t.TD,values:i.map(f=>this.formatValue(u[f]))})}),this}for(const n of r){if(Array.isArray(n)){this.convertFromArray(n,e+1);continue}this.pushElement({tag:t.LI,value:this.formatValue(n),indent:e})}return this}convertFromObject(r,e=0){if(r==null)return this.convertFromPrimitive(r),this;for(const[n,i]of Object.entries(r))if(this.pushElement({tag:t.HEADING,value:n,level:e+1})){if(Array.isArray(i)){this.convertFromArray(i),this.pushElement({tag:t.BR});continue}if(typeof i=="object"){this.convertFromObject(i,e+1);continue}this.convertFromPrimitive(i)}return this}convertFromPrimitive(r){return this.pushElement({tag:t.P,value:this.formatValue(r)}),this}formatValue(r){return r==null?"":Array.isArray(r)?r.map(e=>this.formatValue(e)).join(", "):typeof r=="object"?JSON.stringify(r):typeof r=="string"?r.trim():String(r)}}s.Converter=a,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|