@kklab/json2markdown 0.0.9 → 0.0.11
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 +46 -38
- package/dist/index.umd.js +4 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
1
|
+
var f = Object.defineProperty;
|
|
2
|
+
var m = (s, t, r) => t in s ? f(s, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : s[t] = r;
|
|
3
|
+
var u = (s, t, r) => (m(s, typeof t != "symbol" ? t + "" : t, r), r);
|
|
4
|
+
const e = {
|
|
5
5
|
BR: "br",
|
|
6
6
|
HEADING: "heading",
|
|
7
7
|
LI: "li",
|
|
@@ -10,16 +10,16 @@ const r = {
|
|
|
10
10
|
TR: "tr"
|
|
11
11
|
};
|
|
12
12
|
class a {
|
|
13
|
-
constructor(t,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.onConvert =
|
|
13
|
+
constructor(t, r = {}) {
|
|
14
|
+
u(this, "elements", []);
|
|
15
|
+
u(this, "onConvert");
|
|
16
|
+
this.onConvert = r.onConvert ?? ((n) => n), this.convert(t);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* Converts the provided data into Markdown format.
|
|
20
20
|
*/
|
|
21
|
-
static toMarkdown(t,
|
|
22
|
-
return new a(t,
|
|
21
|
+
static toMarkdown(t, r = {}) {
|
|
22
|
+
return new a(t, r).toMarkdown();
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
25
|
* Converts the provided data into Markdown format.
|
|
@@ -27,26 +27,26 @@ class a {
|
|
|
27
27
|
toMarkdown() {
|
|
28
28
|
return this.elements.map((t) => {
|
|
29
29
|
switch (t.tag) {
|
|
30
|
-
case
|
|
30
|
+
case e.BR:
|
|
31
31
|
return `
|
|
32
32
|
`;
|
|
33
|
-
case
|
|
33
|
+
case e.HEADING:
|
|
34
34
|
return `${"#".repeat(Math.max(1, Math.min(6, t.level)))} ${t.value}
|
|
35
35
|
|
|
36
36
|
`;
|
|
37
|
-
case
|
|
37
|
+
case e.LI:
|
|
38
38
|
return `${" ".repeat(t.indent)}- ${t.value}
|
|
39
39
|
`;
|
|
40
|
-
case
|
|
40
|
+
case e.P:
|
|
41
41
|
return `${t.value}
|
|
42
42
|
|
|
43
43
|
`;
|
|
44
|
-
case
|
|
44
|
+
case e.TD:
|
|
45
45
|
return `| ${t.values.join(" | ")} |
|
|
46
46
|
`;
|
|
47
|
-
case
|
|
48
|
-
return `| ${t.values.
|
|
49
|
-
${t.values.map(() => "
|
|
47
|
+
case e.TR:
|
|
48
|
+
return `| ${t.values.join(" | ")} |
|
|
49
|
+
| ${t.values.map(() => "---").join(" | ")} |
|
|
50
50
|
`;
|
|
51
51
|
default:
|
|
52
52
|
return "";
|
|
@@ -72,49 +72,57 @@ ${t.values.map(() => "| ---").join(" ")} |
|
|
|
72
72
|
this.convertFromPrimitive(t);
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
convertFromArray(t,
|
|
75
|
+
convertFromArray(t, r = 0) {
|
|
76
|
+
if (!t)
|
|
77
|
+
return this;
|
|
76
78
|
for (const n of t) {
|
|
77
79
|
if (Array.isArray(n)) {
|
|
78
|
-
this.convertFromArray(n,
|
|
80
|
+
this.convertFromArray(n, r + 1);
|
|
79
81
|
continue;
|
|
80
82
|
}
|
|
81
|
-
this.pushElement({
|
|
82
|
-
tag:
|
|
83
|
+
n && this.pushElement({
|
|
84
|
+
tag: e.LI,
|
|
83
85
|
value: this.formatValue(n),
|
|
84
|
-
indent:
|
|
86
|
+
indent: r
|
|
85
87
|
});
|
|
86
88
|
}
|
|
87
89
|
return this;
|
|
88
90
|
}
|
|
89
|
-
convertFromObject(t,
|
|
91
|
+
convertFromObject(t, r = 0) {
|
|
92
|
+
if (!t)
|
|
93
|
+
return this;
|
|
90
94
|
for (const [n, i] of Object.entries(t)) {
|
|
91
95
|
if (this.pushElement({
|
|
92
|
-
tag:
|
|
96
|
+
tag: e.HEADING,
|
|
93
97
|
value: n,
|
|
94
|
-
level:
|
|
98
|
+
level: r + 1
|
|
95
99
|
}), Array.isArray(i)) {
|
|
96
|
-
const [
|
|
97
|
-
if (
|
|
100
|
+
const [o] = i;
|
|
101
|
+
if (!o)
|
|
102
|
+
continue;
|
|
103
|
+
if (typeof o == "object") {
|
|
104
|
+
if (Object.keys(o).length === 0)
|
|
105
|
+
continue;
|
|
98
106
|
this.pushElement({
|
|
99
|
-
tag:
|
|
100
|
-
values: Object.keys(
|
|
107
|
+
tag: e.TR,
|
|
108
|
+
values: Object.keys(o)
|
|
101
109
|
}), i.forEach((c) => {
|
|
102
110
|
this.pushElement({
|
|
103
|
-
tag:
|
|
111
|
+
tag: e.TD,
|
|
104
112
|
values: Object.values(c).map((h) => this.formatValue(h))
|
|
105
113
|
});
|
|
106
114
|
}), this.pushElement({
|
|
107
|
-
tag:
|
|
115
|
+
tag: e.BR
|
|
108
116
|
});
|
|
109
117
|
continue;
|
|
110
118
|
}
|
|
111
119
|
this.convertFromArray(i), this.pushElement({
|
|
112
|
-
tag:
|
|
120
|
+
tag: e.BR
|
|
113
121
|
});
|
|
114
122
|
continue;
|
|
115
123
|
}
|
|
116
124
|
if (typeof i == "object") {
|
|
117
|
-
this.convertFromObject(i,
|
|
125
|
+
this.convertFromObject(i, r + 1);
|
|
118
126
|
continue;
|
|
119
127
|
}
|
|
120
128
|
this.convertFromPrimitive(i);
|
|
@@ -122,13 +130,13 @@ ${t.values.map(() => "| ---").join(" ")} |
|
|
|
122
130
|
return this;
|
|
123
131
|
}
|
|
124
132
|
convertFromPrimitive(t) {
|
|
125
|
-
return this.pushElement({
|
|
126
|
-
tag:
|
|
133
|
+
return t ? this.pushElement({
|
|
134
|
+
tag: e.P,
|
|
127
135
|
value: this.formatValue(t)
|
|
128
|
-
});
|
|
136
|
+
}) : this;
|
|
129
137
|
}
|
|
130
138
|
formatValue(t) {
|
|
131
|
-
return Array.isArray(t) ? t.map((
|
|
139
|
+
return t ? Array.isArray(t) ? t.map((r) => this.formatValue(r)).join(", ") : typeof t == "object" ? JSON.stringify(t) : String(t).trim() : "";
|
|
132
140
|
}
|
|
133
141
|
}
|
|
134
142
|
export {
|
package/dist/index.umd.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(r,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(r=typeof globalThis<"u"?globalThis:r||self,e(r.JSON2MD={}))})(this,function(r){"use strict";var f=Object.defineProperty;var m=(r,e,i)=>e in r?f(r,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):r[e]=i;var c=(r,e,i)=>(m(r,typeof e!="symbol"?e+"":e,i),i);const e={BR:"br",HEADING:"heading",LI:"li",P:"p",TD:"td",TR:"tr"};class i{constructor(t,n={}){c(this,"elements",[]);c(this,"onConvert");this.onConvert=n.onConvert??(o=>o),this.convert(t)}static toMarkdown(t,n={}){return new i(t,n).toMarkdown()}toMarkdown(){return this.elements.map(t=>{switch(t.tag){case e.BR:return`
|
|
2
2
|
`;case e.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,t.level)))} ${t.value}
|
|
3
3
|
|
|
4
4
|
`;case e.LI:return`${" ".repeat(t.indent)}- ${t.value}
|
|
5
5
|
`;case e.P:return`${t.value}
|
|
6
6
|
|
|
7
7
|
`;case e.TD:return`| ${t.values.join(" | ")} |
|
|
8
|
-
`;case e.TR:return`| ${t.values.
|
|
9
|
-
${t.values.map(()=>"
|
|
10
|
-
`;default:return""}}).join("")}getElements(){return this.elements}pushElement(t){return this.elements.push(this.onConvert(t)),this}convert(t){if(t){if(Array.isArray(t)){this.convertFromArray(t);return}if(typeof t=="object"){this.convertFromObject(t);return}this.convertFromPrimitive(t)}}convertFromArray(t,
|
|
8
|
+
`;case e.TR:return`| ${t.values.join(" | ")} |
|
|
9
|
+
| ${t.values.map(()=>"---").join(" | ")} |
|
|
10
|
+
`;default:return""}}).join("")}getElements(){return this.elements}pushElement(t){return this.elements.push(this.onConvert(t)),this}convert(t){if(t){if(Array.isArray(t)){this.convertFromArray(t);return}if(typeof t=="object"){this.convertFromObject(t);return}this.convertFromPrimitive(t)}}convertFromArray(t,n=0){if(!t)return this;for(const o of t){if(Array.isArray(o)){this.convertFromArray(o,n+1);continue}o&&this.pushElement({tag:e.LI,value:this.formatValue(o),indent:n})}return this}convertFromObject(t,n=0){if(!t)return this;for(const[o,s]of Object.entries(t)){if(this.pushElement({tag:e.HEADING,value:o,level:n+1}),Array.isArray(s)){const[u]=s;if(!u)continue;if(typeof u=="object"){if(Object.keys(u).length===0)continue;this.pushElement({tag:e.TR,values:Object.keys(u)}),s.forEach(a=>{this.pushElement({tag:e.TD,values:Object.values(a).map(h=>this.formatValue(h))})}),this.pushElement({tag:e.BR});continue}this.convertFromArray(s),this.pushElement({tag:e.BR});continue}if(typeof s=="object"){this.convertFromObject(s,n+1);continue}this.convertFromPrimitive(s)}return this}convertFromPrimitive(t){return t?this.pushElement({tag:e.P,value:this.formatValue(t)}):this}formatValue(t){return t?Array.isArray(t)?t.map(n=>this.formatValue(n)).join(", "):typeof t=="object"?JSON.stringify(t):String(t).trim():""}}r.Converter=i,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|