@kklab/json2markdown 0.0.27 → 0.0.29

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,6 +1,6 @@
1
- var h = Object.defineProperty;
2
- var l = (i, r, t) => r in i ? h(i, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[r] = t;
3
- var a = (i, r, t) => (l(i, typeof r != "symbol" ? r + "" : r, t), t);
1
+ var c = Object.defineProperty;
2
+ var h = (i, t, r) => t in i ? c(i, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[t] = r;
3
+ var a = (i, t, r) => (h(i, typeof t != "symbol" ? t + "" : t, r), r);
4
4
  const e = {
5
5
  A: "a",
6
6
  BLOCKQUOTE: "blockquote",
@@ -13,7 +13,7 @@ const e = {
13
13
  PRE: "pre",
14
14
  TD: "td",
15
15
  TR: "tr"
16
- }, v = (i) => {
16
+ }, l = (i) => {
17
17
  if (typeof i == "string")
18
18
  try {
19
19
  return JSON.parse(i);
@@ -22,37 +22,37 @@ const e = {
22
22
  }
23
23
  };
24
24
  class u {
25
- constructor(r) {
25
+ constructor(t) {
26
26
  a(this, "data");
27
27
  a(this, "elements", []);
28
- a(this, "onConvert", (r) => r);
29
- this.setData(r);
28
+ a(this, "onConvert", (t) => t);
29
+ this.setData(t);
30
30
  }
31
31
  /**
32
32
  * Converts the provided data into Markdown format.
33
33
  */
34
- static toMarkdown(r, t) {
35
- return new u(r).toMarkdown(t);
34
+ static toMarkdown(t, r) {
35
+ return new u(t).toMarkdown(r);
36
36
  }
37
37
  /**
38
38
  * Converts the provided data into Markdown format.
39
39
  */
40
- toMarkdown(r) {
41
- return this.convert(r), this.elements.map((t) => {
42
- switch (t.tag) {
40
+ toMarkdown(t) {
41
+ return this.convert(t), this.elements.map((r) => {
42
+ switch (r.tag) {
43
43
  case e.A:
44
- return `[${t.value}](${t.href})
44
+ return `[${r.value}](${r.href})
45
45
 
46
46
  `;
47
47
  case e.BLOCKQUOTE:
48
- return `> ${t.value}
48
+ return `> ${r.value}
49
49
 
50
50
  `;
51
51
  case e.BR:
52
52
  return `
53
53
  `;
54
54
  case e.HEADING:
55
- return `${"#".repeat(Math.max(1, Math.min(6, t.level)))} ${t.value}
55
+ return `${"#".repeat(Math.max(1, Math.min(6, r.level)))} ${r.value}
56
56
 
57
57
  `;
58
58
  case e.HR:
@@ -60,34 +60,33 @@ class u {
60
60
 
61
61
  `;
62
62
  case e.IMG:
63
- return `![${t.alt}](${t.src})
63
+ return `![${r.alt}](${r.src})
64
64
 
65
65
  `;
66
66
  case e.LI: {
67
67
  const s = (n) => n.replaceAll(`
68
68
  `, " ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)", "g"), (o) => o.trim());
69
- return `${" ".repeat(t.indent)}- ${s(t.value)}
69
+ return `${" ".repeat(r.indent)}- ${s(r.value)}
70
70
  `;
71
71
  }
72
72
  case e.P:
73
- return `${t.value}
73
+ return `${r.value}
74
74
 
75
75
  `;
76
76
  case e.PRE:
77
77
  return `\`\`\`
78
- ${t.value}
78
+ ${r.value}
79
79
  \`\`\`
80
80
 
81
81
  `;
82
82
  case e.TD: {
83
- const s = (n) => n.replaceAll("|", "\\|").replaceAll(`
84
- `, "<br>");
85
- return `| ${t.values.map(s).join(" | ")} |
83
+ const s = (n) => n.replaceAll("|", "\\|").replace(/\r\n|\r|\n/g, "<br>");
84
+ return `| ${r.values.map(s).join(" | ")} |
86
85
  `;
87
86
  }
88
87
  case e.TR:
89
- return `| ${t.values.join(" | ")} |
90
- | ${t.values.map(() => "---").join(" | ")} |
88
+ return `| ${r.values.join(" | ")} |
89
+ | ${r.values.map(() => "---").join(" | ")} |
91
90
  `;
92
91
  default:
93
92
  return "";
@@ -95,58 +94,56 @@ ${t.value}
95
94
  }).join("").replace(/\n{2,}$/, `
96
95
  `);
97
96
  }
98
- setData(r) {
99
- const t = v(r);
100
- return this.data = t ?? r, this;
97
+ setData(t) {
98
+ const r = l(t);
99
+ return this.data = r ?? t, this;
101
100
  }
102
101
  getElements() {
103
102
  return this.elements;
104
103
  }
105
- pushElement(r) {
106
- const t = this.onConvert(r);
107
- return t ? (this.elements.push(t), !0) : !1;
104
+ pushElement(t) {
105
+ const r = this.onConvert(t);
106
+ return r ? (this.elements.push(r), !0) : !1;
108
107
  }
109
- convert(r) {
110
- return r && (this.onConvert = r), Array.isArray(this.data) ? this.convertFromArray(this.data) : typeof this.data == "object" ? this.convertFromObject(this.data) : this.convertFromPrimitive(this.data);
108
+ convert(t) {
109
+ 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);
111
110
  }
112
- convertFromArray(r, t = 0) {
113
- if (r == null)
114
- return this.convertFromPrimitive(r), this;
115
- if (r.length > 0 && r.every((s) => typeof s == "object" && s !== null && Object.keys(s).length > 0)) {
116
- const [s] = r;
117
- this.pushElement({
111
+ convertFromArray(t, r = 0) {
112
+ if (t == null)
113
+ return this.convertFromPrimitive(t), this;
114
+ if (t.length > 0 && t.every((s) => typeof s == "object" && s !== null && Object.keys(s).length > 0)) {
115
+ const s = [...new Set(t.map(Object.keys).flat())];
116
+ return this.pushElement({
118
117
  tag: e.TR,
119
- values: Object.keys(s)
120
- });
121
- const n = Object.keys(s);
122
- return r.forEach((o) => {
118
+ values: s
119
+ }), t.forEach((n) => {
123
120
  this.pushElement({
124
121
  tag: e.TD,
125
- values: n.map((c) => this.formatValue(o[c]))
122
+ values: s.map((o) => this.formatValue(n[o]))
126
123
  });
127
124
  }), this;
128
125
  }
129
- for (const s of r) {
126
+ for (const s of t) {
130
127
  if (Array.isArray(s)) {
131
- this.convertFromArray(s, t + 1);
128
+ this.convertFromArray(s, r + 1);
132
129
  continue;
133
130
  }
134
131
  this.pushElement({
135
132
  tag: e.LI,
136
133
  value: this.formatValue(s),
137
- indent: t
134
+ indent: r
138
135
  });
139
136
  }
140
137
  return this;
141
138
  }
142
- convertFromObject(r, t = 0) {
143
- if (r == null)
144
- return this.convertFromPrimitive(r), this;
145
- for (const [s, n] of Object.entries(r))
139
+ convertFromObject(t, r = 0) {
140
+ if (t == null)
141
+ return this.convertFromPrimitive(t), this;
142
+ for (const [s, n] of Object.entries(t))
146
143
  if (this.pushElement({
147
144
  tag: e.HEADING,
148
145
  value: s,
149
- level: t + 1
146
+ level: r + 1
150
147
  })) {
151
148
  if (Array.isArray(n)) {
152
149
  this.convertFromArray(n), this.pushElement({
@@ -155,21 +152,21 @@ ${t.value}
155
152
  continue;
156
153
  }
157
154
  if (typeof n == "object") {
158
- this.convertFromObject(n, t + 1);
155
+ this.convertFromObject(n, r + 1);
159
156
  continue;
160
157
  }
161
158
  this.convertFromPrimitive(n);
162
159
  }
163
160
  return this;
164
161
  }
165
- convertFromPrimitive(r) {
162
+ convertFromPrimitive(t) {
166
163
  return this.pushElement({
167
164
  tag: e.P,
168
- value: this.formatValue(r)
165
+ value: this.formatValue(t)
169
166
  }), this;
170
167
  }
171
- formatValue(r) {
172
- return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) : String(r).trim();
168
+ formatValue(t) {
169
+ return t == null ? "" : typeof t == "object" ? JSON.stringify(t) : String(t).trim();
173
170
  }
174
171
  }
175
172
  export {
package/dist/index.umd.js CHANGED
@@ -1,25 +1,24 @@
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.JSON2Markdown={}))})(this,function(n){"use strict";var l=Object.defineProperty;var v=(n,e,o)=>e in n?l(n,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[e]=o;var a=(n,e,o)=>(v(n,typeof e!="symbol"?e+"":e,o),o);const e={A:"a",BLOCKQUOTE:"blockquote",BR:"br",HEADING:"heading",HR:"hr",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"},o=h=>{if(typeof h=="string")try{return JSON.parse(h)}catch{return}};class c{constructor(t){a(this,"data");a(this,"elements",[]);a(this,"onConvert",t=>t);this.setData(t)}static toMarkdown(t,r){return new c(t).toMarkdown(r)}toMarkdown(t){return this.convert(t),this.elements.map(r=>{switch(r.tag){case e.A:return`[${r.value}](${r.href})
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 f=Object.defineProperty;var l=(s,r,o)=>r in s?f(s,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):s[r]=o;var a=(s,r,o)=>(l(s,typeof r!="symbol"?r+"":r,o),o);const r={A:"a",BLOCKQUOTE:"blockquote",BR:"br",HEADING:"heading",HR:"hr",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"},o=h=>{if(typeof h=="string")try{return JSON.parse(h)}catch{return}};class c{constructor(t){a(this,"data");a(this,"elements",[]);a(this,"onConvert",t=>t);this.setData(t)}static toMarkdown(t,e){return new c(t).toMarkdown(e)}toMarkdown(t){return this.convert(t),this.elements.map(e=>{switch(e.tag){case r.A:return`[${e.value}](${e.href})
2
2
 
3
- `;case e.BLOCKQUOTE:return`> ${r.value}
3
+ `;case r.BLOCKQUOTE:return`> ${e.value}
4
4
 
5
- `;case e.BR:return`
6
- `;case e.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,r.level)))} ${r.value}
5
+ `;case r.BR:return`
6
+ `;case r.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,e.level)))} ${e.value}
7
7
 
8
- `;case e.HR:return`---
8
+ `;case r.HR:return`---
9
9
 
10
- `;case e.IMG:return`![${r.alt}](${r.src})
10
+ `;case r.IMG:return`![${e.alt}](${e.src})
11
11
 
12
- `;case e.LI:{const s=i=>i.replaceAll(`
13
- `," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(r.indent)}- ${s(r.value)}
14
- `}case e.P:return`${r.value}
12
+ `;case r.LI:{const n=i=>i.replaceAll(`
13
+ `," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(e.indent)}- ${n(e.value)}
14
+ `}case r.P:return`${e.value}
15
15
 
16
- `;case e.PRE:return`\`\`\`
17
- ${r.value}
16
+ `;case r.PRE:return`\`\`\`
17
+ ${e.value}
18
18
  \`\`\`
19
19
 
20
- `;case e.TD:{const s=i=>i.replaceAll("|","\\|").replaceAll(`
21
- `,"<br>");return`| ${r.values.map(s).join(" | ")} |
22
- `}case e.TR:return`| ${r.values.join(" | ")} |
23
- | ${r.values.map(()=>"---").join(" | ")} |
20
+ `;case r.TD:{const n=i=>i.replaceAll("|","\\|").replace(/\r\n|\r|\n/g,"<br>");return`| ${e.values.map(n).join(" | ")} |
21
+ `}case r.TR:return`| ${e.values.join(" | ")} |
22
+ | ${e.values.map(()=>"---").join(" | ")} |
24
23
  `;default:return""}}).join("").replace(/\n{2,}$/,`
25
- `)}setData(t){const r=o(t);return this.data=r??t,this}getElements(){return this.elements}pushElement(t){const r=this.onConvert(t);return r?(this.elements.push(r),!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,r=0){if(t==null)return this.convertFromPrimitive(t),this;if(t.length>0&&t.every(s=>typeof s=="object"&&s!==null&&Object.keys(s).length>0)){const[s]=t;this.pushElement({tag:e.TR,values:Object.keys(s)});const i=Object.keys(s);return t.forEach(u=>{this.pushElement({tag:e.TD,values:i.map(f=>this.formatValue(u[f]))})}),this}for(const s of t){if(Array.isArray(s)){this.convertFromArray(s,r+1);continue}this.pushElement({tag:e.LI,value:this.formatValue(s),indent:r})}return this}convertFromObject(t,r=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[s,i]of Object.entries(t))if(this.pushElement({tag:e.HEADING,value:s,level:r+1})){if(Array.isArray(i)){this.convertFromArray(i),this.pushElement({tag:e.BR});continue}if(typeof i=="object"){this.convertFromObject(i,r+1);continue}this.convertFromPrimitive(i)}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):String(t).trim()}}n.Converter=c,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
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"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kklab/json2markdown",
3
3
  "private": false,
4
- "version": "0.0.27",
4
+ "version": "0.0.29",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {