@kklab/json2markdown 0.0.13 → 0.0.15

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