@kklab/json2markdown 0.0.28 → 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
1
  var c = Object.defineProperty;
2
- var h = (i, r, t) => r in i ? c(i, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[r] = t;
3
- var a = (i, r, t) => (h(i, typeof r != "symbol" ? r + "" : r, t), t);
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",
@@ -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,56 +94,56 @@ ${t.value}
95
94
  }).join("").replace(/\n{2,}$/, `
96
95
  `);
97
96
  }
98
- setData(r) {
99
- const t = l(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 = [...new Set(r.map(Object.keys).flat())];
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())];
117
116
  return this.pushElement({
118
117
  tag: e.TR,
119
118
  values: s
120
- }), r.forEach((n) => {
119
+ }), t.forEach((n) => {
121
120
  this.pushElement({
122
121
  tag: e.TD,
123
122
  values: s.map((o) => this.formatValue(n[o]))
124
123
  });
125
124
  }), this;
126
125
  }
127
- for (const s of r) {
126
+ for (const s of t) {
128
127
  if (Array.isArray(s)) {
129
- this.convertFromArray(s, t + 1);
128
+ this.convertFromArray(s, r + 1);
130
129
  continue;
131
130
  }
132
131
  this.pushElement({
133
132
  tag: e.LI,
134
133
  value: this.formatValue(s),
135
- indent: t
134
+ indent: r
136
135
  });
137
136
  }
138
137
  return this;
139
138
  }
140
- convertFromObject(r, t = 0) {
141
- if (r == null)
142
- return this.convertFromPrimitive(r), this;
143
- 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))
144
143
  if (this.pushElement({
145
144
  tag: e.HEADING,
146
145
  value: s,
147
- level: t + 1
146
+ level: r + 1
148
147
  })) {
149
148
  if (Array.isArray(n)) {
150
149
  this.convertFromArray(n), this.pushElement({
@@ -153,21 +152,21 @@ ${t.value}
153
152
  continue;
154
153
  }
155
154
  if (typeof n == "object") {
156
- this.convertFromObject(n, t + 1);
155
+ this.convertFromObject(n, r + 1);
157
156
  continue;
158
157
  }
159
158
  this.convertFromPrimitive(n);
160
159
  }
161
160
  return this;
162
161
  }
163
- convertFromPrimitive(r) {
162
+ convertFromPrimitive(t) {
164
163
  return this.pushElement({
165
164
  tag: e.P,
166
- value: this.formatValue(r)
165
+ value: this.formatValue(t)
167
166
  }), this;
168
167
  }
169
- formatValue(r) {
170
- 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();
171
170
  }
172
171
  }
173
172
  export {
package/dist/index.umd.js CHANGED
@@ -1,25 +1,24 @@
1
- (function(s,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(s=typeof globalThis<"u"?globalThis:s||self,e(s.JSON2Markdown={}))})(this,function(s){"use strict";var f=Object.defineProperty;var l=(s,e,o)=>e in s?f(s,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):s[e]=o;var a=(s,e,o)=>(l(s,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 n=i=>i.replaceAll(`
13
- `," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(r.indent)}- ${n(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 n=i=>i.replaceAll("|","\\|").replaceAll(`
21
- `,"<br>");return`| ${r.values.map(n).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(n=>typeof n=="object"&&n!==null&&Object.keys(n).length>0)){const n=[...new Set(t.map(Object.keys).flat())];return this.pushElement({tag:e.TR,values:n}),t.forEach(i=>{this.pushElement({tag:e.TD,values:n.map(u=>this.formatValue(i[u]))})}),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,i]of Object.entries(t))if(this.pushElement({tag:e.HEADING,value:n,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()}}s.Converter=c,Object.defineProperty(s,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.28",
4
+ "version": "0.0.29",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {