@kklab/json2markdown 0.0.28 → 0.0.31
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 +94 -86
- package/dist/index.umd.js +17 -17
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var a = (
|
|
4
|
-
const
|
|
1
|
+
var l = Object.defineProperty;
|
|
2
|
+
var v = (n, t, e) => t in n ? l(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var a = (n, t, e) => (v(n, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
const s = {
|
|
5
5
|
A: "a",
|
|
6
6
|
BLOCKQUOTE: "blockquote",
|
|
7
7
|
BR: "br",
|
|
@@ -13,163 +13,171 @@ const e = {
|
|
|
13
13
|
PRE: "pre",
|
|
14
14
|
TD: "td",
|
|
15
15
|
TR: "tr"
|
|
16
|
-
},
|
|
17
|
-
if (typeof
|
|
16
|
+
}, f = (n) => {
|
|
17
|
+
if (typeof n == "string")
|
|
18
18
|
try {
|
|
19
|
-
return JSON.parse(
|
|
19
|
+
return JSON.parse(n);
|
|
20
20
|
} catch {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
+
}, p = /^(?:-|\d+\.)$/, m = (n) => {
|
|
24
|
+
let t = n.length;
|
|
25
|
+
for (; t > 0 && n[t - 1] === `
|
|
26
|
+
`; )
|
|
27
|
+
t--;
|
|
28
|
+
return n.length - t < 2 ? n : `${n.slice(0, t)}
|
|
29
|
+
`;
|
|
23
30
|
};
|
|
24
|
-
class
|
|
25
|
-
constructor(
|
|
31
|
+
class c {
|
|
32
|
+
constructor(t) {
|
|
26
33
|
a(this, "data");
|
|
27
34
|
a(this, "elements", []);
|
|
28
|
-
a(this, "onConvert", (
|
|
29
|
-
this.setData(
|
|
35
|
+
a(this, "onConvert", (t) => t);
|
|
36
|
+
this.setData(t);
|
|
30
37
|
}
|
|
31
38
|
/**
|
|
32
39
|
* Converts the provided data into Markdown format.
|
|
33
40
|
*/
|
|
34
|
-
static toMarkdown(
|
|
35
|
-
return new
|
|
41
|
+
static toMarkdown(t, e) {
|
|
42
|
+
return new c(t).toMarkdown(e);
|
|
36
43
|
}
|
|
37
44
|
/**
|
|
38
45
|
* Converts the provided data into Markdown format.
|
|
39
46
|
*/
|
|
40
|
-
toMarkdown(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
toMarkdown(t) {
|
|
48
|
+
this.convert(t);
|
|
49
|
+
const e = this.elements.map((r) => {
|
|
50
|
+
switch (r.tag) {
|
|
51
|
+
case s.A:
|
|
52
|
+
return `[${r.value}](${r.href})
|
|
45
53
|
|
|
46
54
|
`;
|
|
47
|
-
case
|
|
48
|
-
return `> ${
|
|
55
|
+
case s.BLOCKQUOTE:
|
|
56
|
+
return `> ${r.value}
|
|
49
57
|
|
|
50
58
|
`;
|
|
51
|
-
case
|
|
59
|
+
case s.BR:
|
|
52
60
|
return `
|
|
53
61
|
`;
|
|
54
|
-
case
|
|
55
|
-
return `${"#".repeat(Math.max(1, Math.min(6,
|
|
62
|
+
case s.HEADING:
|
|
63
|
+
return `${"#".repeat(Math.max(1, Math.min(6, r.level)))} ${r.value}
|
|
56
64
|
|
|
57
65
|
`;
|
|
58
|
-
case
|
|
66
|
+
case s.HR:
|
|
59
67
|
return `---
|
|
60
68
|
|
|
61
69
|
`;
|
|
62
|
-
case
|
|
63
|
-
return `
|
|
64
72
|
|
|
65
73
|
`;
|
|
66
|
-
case
|
|
67
|
-
const
|
|
68
|
-
`, " ").
|
|
69
|
-
return `${" ".repeat(
|
|
74
|
+
case s.LI: {
|
|
75
|
+
const i = (o) => o.replaceAll(`
|
|
76
|
+
`, " ").split(/(\s+)/).filter((y, u, h) => u % 2 === 0 || !p.test(h[u - 1])).join("");
|
|
77
|
+
return `${" ".repeat(r.indent)}- ${i(r.value)}
|
|
70
78
|
`;
|
|
71
79
|
}
|
|
72
|
-
case
|
|
73
|
-
return `${
|
|
80
|
+
case s.P:
|
|
81
|
+
return `${r.value}
|
|
74
82
|
|
|
75
83
|
`;
|
|
76
|
-
case
|
|
84
|
+
case s.PRE:
|
|
77
85
|
return `\`\`\`
|
|
78
|
-
${
|
|
86
|
+
${r.value}
|
|
79
87
|
\`\`\`
|
|
80
88
|
|
|
81
89
|
`;
|
|
82
|
-
case
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
return `| ${t.values.map(s).join(" | ")} |
|
|
90
|
+
case s.TD: {
|
|
91
|
+
const i = (o) => o.replaceAll("|", "\\|").replace(/\r\n|\r|\n/g, "<br>");
|
|
92
|
+
return `| ${r.values.map(i).join(" | ")} |
|
|
86
93
|
`;
|
|
87
94
|
}
|
|
88
|
-
case
|
|
89
|
-
return `| ${
|
|
90
|
-
| ${
|
|
95
|
+
case s.TR:
|
|
96
|
+
return `| ${r.values.join(" | ")} |
|
|
97
|
+
| ${r.values.map(() => "---").join(" | ")} |
|
|
91
98
|
`;
|
|
92
99
|
default:
|
|
93
100
|
return "";
|
|
94
101
|
}
|
|
95
|
-
}).join("")
|
|
96
|
-
|
|
102
|
+
}).join("");
|
|
103
|
+
return m(e);
|
|
97
104
|
}
|
|
98
|
-
setData(
|
|
99
|
-
const
|
|
100
|
-
return this.data =
|
|
105
|
+
setData(t) {
|
|
106
|
+
const e = f(t);
|
|
107
|
+
return this.data = e ?? t, this;
|
|
101
108
|
}
|
|
102
109
|
getElements() {
|
|
103
110
|
return this.elements;
|
|
104
111
|
}
|
|
105
|
-
pushElement(
|
|
106
|
-
const
|
|
107
|
-
return
|
|
112
|
+
pushElement(t) {
|
|
113
|
+
const e = this.onConvert(t);
|
|
114
|
+
return e ? (this.elements.push(e), !0) : !1;
|
|
108
115
|
}
|
|
109
|
-
convert(
|
|
110
|
-
return
|
|
116
|
+
convert(t) {
|
|
117
|
+
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);
|
|
111
118
|
}
|
|
112
|
-
convertFromArray(
|
|
113
|
-
if (
|
|
114
|
-
return this.convertFromPrimitive(
|
|
115
|
-
if (
|
|
116
|
-
const
|
|
119
|
+
convertFromArray(t, e = 0) {
|
|
120
|
+
if (t == null)
|
|
121
|
+
return this.convertFromPrimitive(t), this;
|
|
122
|
+
if (t.length > 0 && t.every((r) => typeof r == "object" && r !== null && Object.keys(r).length > 0)) {
|
|
123
|
+
const r = [...new Set(t.map(Object.keys).flat())];
|
|
117
124
|
return this.pushElement({
|
|
118
|
-
tag:
|
|
119
|
-
values:
|
|
120
|
-
}),
|
|
125
|
+
tag: s.TR,
|
|
126
|
+
values: r
|
|
127
|
+
}), t.forEach((i) => {
|
|
121
128
|
this.pushElement({
|
|
122
|
-
tag:
|
|
123
|
-
|
|
129
|
+
tag: s.TD,
|
|
130
|
+
// A key from another row is read only where the row has it: `constructor` is on every object.
|
|
131
|
+
values: r.map((o) => this.formatValue(Object.prototype.hasOwnProperty.call(i, o) ? i[o] : void 0))
|
|
124
132
|
});
|
|
125
133
|
}), this;
|
|
126
134
|
}
|
|
127
|
-
for (const
|
|
128
|
-
if (Array.isArray(
|
|
129
|
-
this.convertFromArray(
|
|
135
|
+
for (const r of t) {
|
|
136
|
+
if (Array.isArray(r)) {
|
|
137
|
+
this.convertFromArray(r, e + 1);
|
|
130
138
|
continue;
|
|
131
139
|
}
|
|
132
140
|
this.pushElement({
|
|
133
|
-
tag:
|
|
134
|
-
value: this.formatValue(
|
|
135
|
-
indent:
|
|
141
|
+
tag: s.LI,
|
|
142
|
+
value: this.formatValue(r),
|
|
143
|
+
indent: e
|
|
136
144
|
});
|
|
137
145
|
}
|
|
138
146
|
return this;
|
|
139
147
|
}
|
|
140
|
-
convertFromObject(
|
|
141
|
-
if (
|
|
142
|
-
return this.convertFromPrimitive(
|
|
143
|
-
for (const [
|
|
148
|
+
convertFromObject(t, e = 0) {
|
|
149
|
+
if (t == null)
|
|
150
|
+
return this.convertFromPrimitive(t), this;
|
|
151
|
+
for (const [r, i] of Object.entries(t))
|
|
144
152
|
if (this.pushElement({
|
|
145
|
-
tag:
|
|
146
|
-
value:
|
|
147
|
-
level:
|
|
153
|
+
tag: s.HEADING,
|
|
154
|
+
value: r,
|
|
155
|
+
level: e + 1
|
|
148
156
|
})) {
|
|
149
|
-
if (Array.isArray(
|
|
150
|
-
this.convertFromArray(
|
|
151
|
-
tag:
|
|
157
|
+
if (Array.isArray(i)) {
|
|
158
|
+
this.convertFromArray(i), this.pushElement({
|
|
159
|
+
tag: s.BR
|
|
152
160
|
});
|
|
153
161
|
continue;
|
|
154
162
|
}
|
|
155
|
-
if (typeof
|
|
156
|
-
this.convertFromObject(
|
|
163
|
+
if (typeof i == "object") {
|
|
164
|
+
this.convertFromObject(i, e + 1);
|
|
157
165
|
continue;
|
|
158
166
|
}
|
|
159
|
-
this.convertFromPrimitive(
|
|
167
|
+
this.convertFromPrimitive(i);
|
|
160
168
|
}
|
|
161
169
|
return this;
|
|
162
170
|
}
|
|
163
|
-
convertFromPrimitive(
|
|
171
|
+
convertFromPrimitive(t) {
|
|
164
172
|
return this.pushElement({
|
|
165
|
-
tag:
|
|
166
|
-
value: this.formatValue(
|
|
173
|
+
tag: s.P,
|
|
174
|
+
value: this.formatValue(t)
|
|
167
175
|
}), this;
|
|
168
176
|
}
|
|
169
|
-
formatValue(
|
|
170
|
-
return
|
|
177
|
+
formatValue(t) {
|
|
178
|
+
return t == null ? "" : typeof t == "object" ? JSON.stringify(t) : String(t).trim();
|
|
171
179
|
}
|
|
172
180
|
}
|
|
173
181
|
export {
|
|
174
|
-
|
|
182
|
+
c as Converter
|
|
175
183
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
(function(s,
|
|
1
|
+
(function(s,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(s=typeof globalThis<"u"?globalThis:s||self,r(s.JSON2Markdown={}))})(this,function(s){"use strict";var m=Object.defineProperty;var y=(s,r,u)=>r in s?m(s,r,{enumerable:!0,configurable:!0,writable:!0,value:u}):s[r]=u;var c=(s,r,u)=>(y(s,typeof r!="symbol"?r+"":r,u),u);const r={A:"a",BLOCKQUOTE:"blockquote",BR:"br",HEADING:"heading",HR:"hr",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"},u=o=>{if(typeof o=="string")try{return JSON.parse(o)}catch{return}},f=/^(?:-|\d+\.)$/,p=o=>{let t=o.length;for(;t>0&&o[t-1]===`
|
|
2
|
+
`;)t--;return o.length-t<2?o:`${o.slice(0,t)}
|
|
3
|
+
`};class h{constructor(t){c(this,"data");c(this,"elements",[]);c(this,"onConvert",t=>t);this.setData(t)}static toMarkdown(t,n){return new h(t).toMarkdown(n)}toMarkdown(t){this.convert(t);const n=this.elements.map(e=>{switch(e.tag){case r.A:return`[${e.value}](${e.href})
|
|
2
4
|
|
|
3
|
-
`;case
|
|
5
|
+
`;case r.BLOCKQUOTE:return`> ${e.value}
|
|
4
6
|
|
|
5
|
-
`;case
|
|
6
|
-
`;case
|
|
7
|
+
`;case r.BR:return`
|
|
8
|
+
`;case r.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,e.level)))} ${e.value}
|
|
7
9
|
|
|
8
|
-
`;case
|
|
10
|
+
`;case r.HR:return`---
|
|
9
11
|
|
|
10
|
-
`;case
|
|
12
|
+
`;case r.IMG:return`
|
|
11
13
|
|
|
12
|
-
`;case
|
|
13
|
-
`," ").
|
|
14
|
-
`}case
|
|
14
|
+
`;case r.LI:{const i=a=>a.replaceAll(`
|
|
15
|
+
`," ").split(/(\s+)/).filter((d,l,v)=>l%2===0||!f.test(v[l-1])).join("");return`${" ".repeat(e.indent)}- ${i(e.value)}
|
|
16
|
+
`}case r.P:return`${e.value}
|
|
15
17
|
|
|
16
|
-
`;case
|
|
17
|
-
${
|
|
18
|
+
`;case r.PRE:return`\`\`\`
|
|
19
|
+
${e.value}
|
|
18
20
|
\`\`\`
|
|
19
21
|
|
|
20
|
-
`;case
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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"})});
|
|
22
|
+
`;case r.TD:{const i=a=>a.replaceAll("|","\\|").replace(/\r\n|\r|\n/g,"<br>");return`| ${e.values.map(i).join(" | ")} |
|
|
23
|
+
`}case r.TR:return`| ${e.values.join(" | ")} |
|
|
24
|
+
| ${e.values.map(()=>"---").join(" | ")} |
|
|
25
|
+
`;default:return""}}).join("");return p(n)}setData(t){const n=u(t);return this.data=n??t,this}getElements(){return this.elements}pushElement(t){const n=this.onConvert(t);return n?(this.elements.push(n),!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,n=0){if(t==null)return this.convertFromPrimitive(t),this;if(t.length>0&&t.every(e=>typeof e=="object"&&e!==null&&Object.keys(e).length>0)){const e=[...new Set(t.map(Object.keys).flat())];return this.pushElement({tag:r.TR,values:e}),t.forEach(i=>{this.pushElement({tag:r.TD,values:e.map(a=>this.formatValue(Object.prototype.hasOwnProperty.call(i,a)?i[a]:void 0))})}),this}for(const e of t){if(Array.isArray(e)){this.convertFromArray(e,n+1);continue}this.pushElement({tag:r.LI,value:this.formatValue(e),indent:n})}return this}convertFromObject(t,n=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[e,i]of Object.entries(t))if(this.pushElement({tag:r.HEADING,value:e,level:n+1})){if(Array.isArray(i)){this.convertFromArray(i),this.pushElement({tag:r.BR});continue}if(typeof i=="object"){this.convertFromObject(i,n+1);continue}this.convertFromPrimitive(i)}return this}convertFromPrimitive(t){return this.pushElement({tag:r.P,value:this.formatValue(t)}),this}formatValue(t){return t==null?"":typeof t=="object"?JSON.stringify(t):String(t).trim()}}s.Converter=h,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|