@kklab/json2markdown 0.0.10 → 0.0.12

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