@kklab/json2markdown 0.0.29 → 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, 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
- 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,15 +13,22 @@ 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 {
31
+ class c {
25
32
  constructor(t) {
26
33
  a(this, "data");
27
34
  a(this, "elements", []);
@@ -31,137 +38,139 @@ class u {
31
38
  /**
32
39
  * Converts the provided data into Markdown format.
33
40
  */
34
- static toMarkdown(t, r) {
35
- return new u(t).toMarkdown(r);
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
47
  toMarkdown(t) {
41
- return this.convert(t), this.elements.map((r) => {
48
+ this.convert(t);
49
+ const e = this.elements.map((r) => {
42
50
  switch (r.tag) {
43
- case e.A:
51
+ case s.A:
44
52
  return `[${r.value}](${r.href})
45
53
 
46
54
  `;
47
- case e.BLOCKQUOTE:
55
+ case s.BLOCKQUOTE:
48
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:
62
+ case s.HEADING:
55
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:
70
+ case s.IMG:
63
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(r.indent)}- ${s(r.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:
80
+ case s.P:
73
81
  return `${r.value}
74
82
 
75
83
  `;
76
- case e.PRE:
84
+ case s.PRE:
77
85
  return `\`\`\`
78
86
  ${r.value}
79
87
  \`\`\`
80
88
 
81
89
  `;
82
- case e.TD: {
83
- const s = (n) => n.replaceAll("|", "\\|").replace(/\r\n|\r|\n/g, "<br>");
84
- return `| ${r.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(" | ")} |
85
93
  `;
86
94
  }
87
- case e.TR:
95
+ case s.TR:
88
96
  return `| ${r.values.join(" | ")} |
89
97
  | ${r.values.map(() => "---").join(" | ")} |
90
98
  `;
91
99
  default:
92
100
  return "";
93
101
  }
94
- }).join("").replace(/\n{2,}$/, `
95
- `);
102
+ }).join("");
103
+ return m(e);
96
104
  }
97
105
  setData(t) {
98
- const r = l(t);
99
- return this.data = r ?? t, this;
106
+ const e = f(t);
107
+ return this.data = e ?? t, this;
100
108
  }
101
109
  getElements() {
102
110
  return this.elements;
103
111
  }
104
112
  pushElement(t) {
105
- const r = this.onConvert(t);
106
- return r ? (this.elements.push(r), !0) : !1;
113
+ const e = this.onConvert(t);
114
+ return e ? (this.elements.push(e), !0) : !1;
107
115
  }
108
116
  convert(t) {
109
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);
110
118
  }
111
- convertFromArray(t, r = 0) {
119
+ convertFromArray(t, e = 0) {
112
120
  if (t == null)
113
121
  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())];
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())];
116
124
  return this.pushElement({
117
- tag: e.TR,
118
- values: s
119
- }), t.forEach((n) => {
125
+ tag: s.TR,
126
+ values: r
127
+ }), t.forEach((i) => {
120
128
  this.pushElement({
121
- tag: e.TD,
122
- 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))
123
132
  });
124
133
  }), this;
125
134
  }
126
- for (const s of t) {
127
- if (Array.isArray(s)) {
128
- this.convertFromArray(s, r + 1);
135
+ for (const r of t) {
136
+ if (Array.isArray(r)) {
137
+ this.convertFromArray(r, e + 1);
129
138
  continue;
130
139
  }
131
140
  this.pushElement({
132
- tag: e.LI,
133
- value: this.formatValue(s),
134
- indent: r
141
+ tag: s.LI,
142
+ value: this.formatValue(r),
143
+ indent: e
135
144
  });
136
145
  }
137
146
  return this;
138
147
  }
139
- convertFromObject(t, r = 0) {
148
+ convertFromObject(t, e = 0) {
140
149
  if (t == null)
141
150
  return this.convertFromPrimitive(t), this;
142
- for (const [s, n] of Object.entries(t))
151
+ for (const [r, i] of Object.entries(t))
143
152
  if (this.pushElement({
144
- tag: e.HEADING,
145
- value: s,
146
- level: r + 1
153
+ tag: s.HEADING,
154
+ value: r,
155
+ level: e + 1
147
156
  })) {
148
- if (Array.isArray(n)) {
149
- this.convertFromArray(n), this.pushElement({
150
- tag: e.BR
157
+ if (Array.isArray(i)) {
158
+ this.convertFromArray(i), this.pushElement({
159
+ tag: s.BR
151
160
  });
152
161
  continue;
153
162
  }
154
- if (typeof n == "object") {
155
- this.convertFromObject(n, r + 1);
163
+ if (typeof i == "object") {
164
+ this.convertFromObject(i, e + 1);
156
165
  continue;
157
166
  }
158
- this.convertFromPrimitive(n);
167
+ this.convertFromPrimitive(i);
159
168
  }
160
169
  return this;
161
170
  }
162
171
  convertFromPrimitive(t) {
163
172
  return this.pushElement({
164
- tag: e.P,
173
+ tag: s.P,
165
174
  value: this.formatValue(t)
166
175
  }), this;
167
176
  }
@@ -170,5 +179,5 @@ ${r.value}
170
179
  }
171
180
  }
172
181
  export {
173
- u as Converter
182
+ c as Converter
174
183
  };
package/dist/index.umd.js CHANGED
@@ -1,4 +1,6 @@
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})
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
5
  `;case r.BLOCKQUOTE:return`> ${e.value}
4
6
 
@@ -9,16 +11,15 @@
9
11
 
10
12
  `;case r.IMG:return`![${e.alt}](${e.src})
11
13
 
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.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)}
14
16
  `}case r.P:return`${e.value}
15
17
 
16
18
  `;case r.PRE:return`\`\`\`
17
19
  ${e.value}
18
20
  \`\`\`
19
21
 
20
- `;case r.TD:{const n=i=>i.replaceAll("|","\\|").replace(/\r\n|\r|\n/g,"<br>");return`| ${e.values.map(n).join(" | ")} |
22
+ `;case r.TD:{const i=a=>a.replaceAll("|","\\|").replace(/\r\n|\r|\n/g,"<br>");return`| ${e.values.map(i).join(" | ")} |
21
23
  `}case r.TR:return`| ${e.values.join(" | ")} |
22
24
  | ${e.values.map(()=>"---").join(" | ")} |
23
- `;default:return""}}).join("").replace(/\n{2,}$/,`
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"})});
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.29",
4
+ "version": "0.0.31",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {