@kklab/json2markdown 0.0.27 → 0.0.28

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, 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
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);
@@ -96,7 +96,7 @@ ${t.value}
96
96
  `);
97
97
  }
98
98
  setData(r) {
99
- const t = v(r);
99
+ const t = l(r);
100
100
  return this.data = t ?? r, this;
101
101
  }
102
102
  getElements() {
@@ -113,16 +113,14 @@ ${t.value}
113
113
  if (r == null)
114
114
  return this.convertFromPrimitive(r), this;
115
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({
116
+ const s = [...new Set(r.map(Object.keys).flat())];
117
+ return this.pushElement({
118
118
  tag: e.TR,
119
- values: Object.keys(s)
120
- });
121
- const n = Object.keys(s);
122
- return r.forEach((o) => {
119
+ values: s
120
+ }), r.forEach((n) => {
123
121
  this.pushElement({
124
122
  tag: e.TD,
125
- values: n.map((c) => this.formatValue(o[c]))
123
+ values: s.map((o) => this.formatValue(n[o]))
126
124
  });
127
125
  }), this;
128
126
  }
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
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,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})
2
2
 
3
3
  `;case e.BLOCKQUOTE:return`> ${r.value}
4
4
 
@@ -9,17 +9,17 @@
9
9
 
10
10
  `;case e.IMG:return`![${r.alt}](${r.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)}
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
14
  `}case e.P:return`${r.value}
15
15
 
16
16
  `;case e.PRE:return`\`\`\`
17
17
  ${r.value}
18
18
  \`\`\`
19
19
 
20
- `;case e.TD:{const s=i=>i.replaceAll("|","\\|").replaceAll(`
21
- `,"<br>");return`| ${r.values.map(s).join(" | ")} |
20
+ `;case e.TD:{const n=i=>i.replaceAll("|","\\|").replaceAll(`
21
+ `,"<br>");return`| ${r.values.map(n).join(" | ")} |
22
22
  `}case e.TR:return`| ${r.values.join(" | ")} |
23
23
  | ${r.values.map(()=>"---").join(" | ")} |
24
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(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"})});
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"})});
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.28",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {