@kklab/json2markdown 0.0.28 → 0.0.31

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