@kklab/json2markdown 0.0.29 → 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 +69 -60
- package/dist/index.umd.js +7 -6
- 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,15 +13,22 @@ 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
|
|
31
|
+
class c {
|
|
25
32
|
constructor(t) {
|
|
26
33
|
a(this, "data");
|
|
27
34
|
a(this, "elements", []);
|
|
@@ -31,137 +38,139 @@ class u {
|
|
|
31
38
|
/**
|
|
32
39
|
* Converts the provided data into Markdown format.
|
|
33
40
|
*/
|
|
34
|
-
static toMarkdown(t,
|
|
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
47
|
toMarkdown(t) {
|
|
41
|
-
|
|
48
|
+
this.convert(t);
|
|
49
|
+
const e = this.elements.map((r) => {
|
|
42
50
|
switch (r.tag) {
|
|
43
|
-
case
|
|
51
|
+
case s.A:
|
|
44
52
|
return `[${r.value}](${r.href})
|
|
45
53
|
|
|
46
54
|
`;
|
|
47
|
-
case
|
|
55
|
+
case s.BLOCKQUOTE:
|
|
48
56
|
return `> ${r.value}
|
|
49
57
|
|
|
50
58
|
`;
|
|
51
|
-
case
|
|
59
|
+
case s.BR:
|
|
52
60
|
return `
|
|
53
61
|
`;
|
|
54
|
-
case
|
|
62
|
+
case s.HEADING:
|
|
55
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
|
|
70
|
+
case s.IMG:
|
|
63
71
|
return `
|
|
64
72
|
|
|
65
73
|
`;
|
|
66
|
-
case
|
|
67
|
-
const
|
|
68
|
-
`, " ").
|
|
69
|
-
return `${" ".repeat(r.indent)}- ${
|
|
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
|
|
80
|
+
case s.P:
|
|
73
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
|
-
return `| ${r.values.map(
|
|
90
|
+
case s.TD: {
|
|
91
|
+
const i = (o) => o.replaceAll("|", "\\|").replace(/\r\n|\r|\n/g, "<br>");
|
|
92
|
+
return `| ${r.values.map(i).join(" | ")} |
|
|
85
93
|
`;
|
|
86
94
|
}
|
|
87
|
-
case
|
|
95
|
+
case s.TR:
|
|
88
96
|
return `| ${r.values.join(" | ")} |
|
|
89
97
|
| ${r.values.map(() => "---").join(" | ")} |
|
|
90
98
|
`;
|
|
91
99
|
default:
|
|
92
100
|
return "";
|
|
93
101
|
}
|
|
94
|
-
}).join("")
|
|
95
|
-
|
|
102
|
+
}).join("");
|
|
103
|
+
return m(e);
|
|
96
104
|
}
|
|
97
105
|
setData(t) {
|
|
98
|
-
const
|
|
99
|
-
return this.data =
|
|
106
|
+
const e = f(t);
|
|
107
|
+
return this.data = e ?? t, this;
|
|
100
108
|
}
|
|
101
109
|
getElements() {
|
|
102
110
|
return this.elements;
|
|
103
111
|
}
|
|
104
112
|
pushElement(t) {
|
|
105
|
-
const
|
|
106
|
-
return
|
|
113
|
+
const e = this.onConvert(t);
|
|
114
|
+
return e ? (this.elements.push(e), !0) : !1;
|
|
107
115
|
}
|
|
108
116
|
convert(t) {
|
|
109
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);
|
|
110
118
|
}
|
|
111
|
-
convertFromArray(t,
|
|
119
|
+
convertFromArray(t, e = 0) {
|
|
112
120
|
if (t == null)
|
|
113
121
|
return this.convertFromPrimitive(t), this;
|
|
114
|
-
if (t.length > 0 && t.every((
|
|
115
|
-
const
|
|
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())];
|
|
116
124
|
return this.pushElement({
|
|
117
|
-
tag:
|
|
118
|
-
values:
|
|
119
|
-
}), t.forEach((
|
|
125
|
+
tag: s.TR,
|
|
126
|
+
values: r
|
|
127
|
+
}), t.forEach((i) => {
|
|
120
128
|
this.pushElement({
|
|
121
|
-
tag:
|
|
122
|
-
|
|
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))
|
|
123
132
|
});
|
|
124
133
|
}), this;
|
|
125
134
|
}
|
|
126
|
-
for (const
|
|
127
|
-
if (Array.isArray(
|
|
128
|
-
this.convertFromArray(
|
|
135
|
+
for (const r of t) {
|
|
136
|
+
if (Array.isArray(r)) {
|
|
137
|
+
this.convertFromArray(r, e + 1);
|
|
129
138
|
continue;
|
|
130
139
|
}
|
|
131
140
|
this.pushElement({
|
|
132
|
-
tag:
|
|
133
|
-
value: this.formatValue(
|
|
134
|
-
indent:
|
|
141
|
+
tag: s.LI,
|
|
142
|
+
value: this.formatValue(r),
|
|
143
|
+
indent: e
|
|
135
144
|
});
|
|
136
145
|
}
|
|
137
146
|
return this;
|
|
138
147
|
}
|
|
139
|
-
convertFromObject(t,
|
|
148
|
+
convertFromObject(t, e = 0) {
|
|
140
149
|
if (t == null)
|
|
141
150
|
return this.convertFromPrimitive(t), this;
|
|
142
|
-
for (const [
|
|
151
|
+
for (const [r, i] of Object.entries(t))
|
|
143
152
|
if (this.pushElement({
|
|
144
|
-
tag:
|
|
145
|
-
value:
|
|
146
|
-
level:
|
|
153
|
+
tag: s.HEADING,
|
|
154
|
+
value: r,
|
|
155
|
+
level: e + 1
|
|
147
156
|
})) {
|
|
148
|
-
if (Array.isArray(
|
|
149
|
-
this.convertFromArray(
|
|
150
|
-
tag:
|
|
157
|
+
if (Array.isArray(i)) {
|
|
158
|
+
this.convertFromArray(i), this.pushElement({
|
|
159
|
+
tag: s.BR
|
|
151
160
|
});
|
|
152
161
|
continue;
|
|
153
162
|
}
|
|
154
|
-
if (typeof
|
|
155
|
-
this.convertFromObject(
|
|
163
|
+
if (typeof i == "object") {
|
|
164
|
+
this.convertFromObject(i, e + 1);
|
|
156
165
|
continue;
|
|
157
166
|
}
|
|
158
|
-
this.convertFromPrimitive(
|
|
167
|
+
this.convertFromPrimitive(i);
|
|
159
168
|
}
|
|
160
169
|
return this;
|
|
161
170
|
}
|
|
162
171
|
convertFromPrimitive(t) {
|
|
163
172
|
return this.pushElement({
|
|
164
|
-
tag:
|
|
173
|
+
tag: s.P,
|
|
165
174
|
value: this.formatValue(t)
|
|
166
175
|
}), this;
|
|
167
176
|
}
|
|
@@ -170,5 +179,5 @@ ${r.value}
|
|
|
170
179
|
}
|
|
171
180
|
}
|
|
172
181
|
export {
|
|
173
|
-
|
|
182
|
+
c as Converter
|
|
174
183
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
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
|
|
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
5
|
`;case r.BLOCKQUOTE:return`> ${e.value}
|
|
4
6
|
|
|
@@ -9,16 +11,15 @@
|
|
|
9
11
|
|
|
10
12
|
`;case r.IMG:return`
|
|
11
13
|
|
|
12
|
-
`;case r.LI:{const
|
|
13
|
-
`," ").
|
|
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)}
|
|
14
16
|
`}case r.P:return`${e.value}
|
|
15
17
|
|
|
16
18
|
`;case r.PRE:return`\`\`\`
|
|
17
19
|
${e.value}
|
|
18
20
|
\`\`\`
|
|
19
21
|
|
|
20
|
-
`;case r.TD:{const
|
|
22
|
+
`;case r.TD:{const i=a=>a.replaceAll("|","\\|").replace(/\r\n|\r|\n/g,"<br>");return`| ${e.values.map(i).join(" | ")} |
|
|
21
23
|
`}case r.TR:return`| ${e.values.join(" | ")} |
|
|
22
24
|
| ${e.values.map(()=>"---").join(" | ")} |
|
|
23
|
-
`;default:return""}}).join("").
|
|
24
|
-
`)}setData(t){const e=o(t);return this.data=e??t,this}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):typeof this.data=="object"?this.convertFromObject(this.data):this.convertFromPrimitive(this.data)}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=[...new Set(t.map(Object.keys).flat())];return this.pushElement({tag:r.TR,values:n}),t.forEach(i=>{this.pushElement({tag:r.TD,values:n.map(u=>this.formatValue(i[u]))})}),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,i]of Object.entries(t))if(this.pushElement({tag:r.HEADING,value:n,level:e+1})){if(Array.isArray(i)){this.convertFromArray(i),this.pushElement({tag:r.BR});continue}if(typeof i=="object"){this.convertFromObject(i,e+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=c,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
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"})});
|