@kklab/json2markdown 0.0.11 → 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
@@ -9,7 +9,7 @@ const e = {
9
9
  TD: "td",
10
10
  TR: "tr"
11
11
  };
12
- class a {
12
+ class c {
13
13
  constructor(t, r = {}) {
14
14
  u(this, "elements", []);
15
15
  u(this, "onConvert");
@@ -19,7 +19,7 @@ class a {
19
19
  * Converts the provided data into Markdown format.
20
20
  */
21
21
  static toMarkdown(t, r = {}) {
22
- return new a(t, r).toMarkdown();
22
+ return new c(t, r).toMarkdown();
23
23
  }
24
24
  /**
25
25
  * Converts the provided data into Markdown format.
@@ -57,7 +57,8 @@ class a {
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) {
@@ -91,54 +92,55 @@ class a {
91
92
  convertFromObject(t, r = 0) {
92
93
  if (!t)
93
94
  return this;
94
- for (const [n, i] of Object.entries(t)) {
95
+ for (const [n, i] of Object.entries(t))
95
96
  if (this.pushElement({
96
97
  tag: e.HEADING,
97
98
  value: n,
98
99
  level: r + 1
99
- }), Array.isArray(i)) {
100
- const [o] = i;
101
- if (!o)
102
- continue;
103
- if (typeof o == "object") {
104
- if (Object.keys(o).length === 0)
100
+ })) {
101
+ if (Array.isArray(i)) {
102
+ const [o] = i;
103
+ if (!o)
105
104
  continue;
106
- this.pushElement({
107
- tag: e.TR,
108
- values: Object.keys(o)
109
- }), i.forEach((c) => {
105
+ if (typeof o == "object") {
106
+ if (Object.keys(o).length === 0)
107
+ continue;
110
108
  this.pushElement({
111
- tag: e.TD,
112
- 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
113
118
  });
114
- }), this.pushElement({
119
+ continue;
120
+ }
121
+ this.convertFromArray(i), this.pushElement({
115
122
  tag: e.BR
116
123
  });
117
124
  continue;
118
125
  }
119
- this.convertFromArray(i), this.pushElement({
120
- tag: e.BR
121
- });
122
- continue;
123
- }
124
- if (typeof i == "object") {
125
- this.convertFromObject(i, r + 1);
126
- continue;
126
+ if (typeof i == "object") {
127
+ this.convertFromObject(i, r + 1);
128
+ continue;
129
+ }
130
+ this.convertFromPrimitive(i);
127
131
  }
128
- this.convertFromPrimitive(i);
129
- }
130
132
  return this;
131
133
  }
132
134
  convertFromPrimitive(t) {
133
- return t ? this.pushElement({
135
+ return t ? (this.pushElement({
134
136
  tag: e.P,
135
137
  value: this.formatValue(t)
136
- }) : this;
138
+ }), this) : this;
137
139
  }
138
140
  formatValue(t) {
139
141
  return t ? Array.isArray(t) ? t.map((r) => this.formatValue(r)).join(", ") : typeof t == "object" ? JSON.stringify(t) : String(t).trim() : "";
140
142
  }
141
143
  }
142
144
  export {
143
- a as Converter
145
+ c as Converter
144
146
  };
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
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`
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}
@@ -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){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"})});
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.11",
4
+ "version": "0.0.12",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {