@kklab/json2markdown 0.0.26 → 0.0.27

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.
@@ -13,6 +13,7 @@ declare class Converter {
13
13
  * Converts the provided data into Markdown format.
14
14
  */
15
15
  toMarkdown(callback?: ConvertHandler): string;
16
+ setData(data: unknown): this;
16
17
  getElements(): Element[];
17
18
  private pushElement;
18
19
  private convert;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  var h = Object.defineProperty;
2
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 u = (i, r, t) => (l(i, typeof r != "symbol" ? r + "" : r, t), t);
3
+ var a = (i, r, t) => (l(i, typeof r != "symbol" ? r + "" : r, t), t);
4
4
  const e = {
5
5
  A: "a",
6
6
  BLOCKQUOTE: "blockquote",
@@ -13,19 +13,26 @@ const e = {
13
13
  PRE: "pre",
14
14
  TD: "td",
15
15
  TR: "tr"
16
+ }, v = (i) => {
17
+ if (typeof i == "string")
18
+ try {
19
+ return JSON.parse(i);
20
+ } catch {
21
+ return;
22
+ }
16
23
  };
17
- class a {
24
+ class u {
18
25
  constructor(r) {
19
- u(this, "data");
20
- u(this, "elements", []);
21
- u(this, "onConvert", (r) => r);
22
- this.data = typeof r == "string" ? JSON.parse(r) : r;
26
+ a(this, "data");
27
+ a(this, "elements", []);
28
+ a(this, "onConvert", (r) => r);
29
+ this.setData(r);
23
30
  }
24
31
  /**
25
32
  * Converts the provided data into Markdown format.
26
33
  */
27
34
  static toMarkdown(r, t) {
28
- return new a(r).toMarkdown(t);
35
+ return new u(r).toMarkdown(t);
29
36
  }
30
37
  /**
31
38
  * Converts the provided data into Markdown format.
@@ -85,7 +92,12 @@ ${t.value}
85
92
  default:
86
93
  return "";
87
94
  }
88
- }).join("");
95
+ }).join("").replace(/\n{2,}$/, `
96
+ `);
97
+ }
98
+ setData(r) {
99
+ const t = v(r);
100
+ return this.data = t ?? r, this;
89
101
  }
90
102
  getElements() {
91
103
  return this.elements;
@@ -95,7 +107,7 @@ ${t.value}
95
107
  return t ? (this.elements.push(t), !0) : !1;
96
108
  }
97
109
  convert(r) {
98
- return r && (this.onConvert = r), Array.isArray(this.data) ? this.convertFromArray(this.data) : this.convertFromObject(this.data);
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);
99
111
  }
100
112
  convertFromArray(r, t = 0) {
101
113
  if (r == null)
@@ -157,9 +169,9 @@ ${t.value}
157
169
  }), this;
158
170
  }
159
171
  formatValue(r) {
160
- return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) : typeof r == "string" ? r.trim() : String(r);
172
+ return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) : String(r).trim();
161
173
  }
162
174
  }
163
175
  export {
164
- a as Converter
176
+ u as Converter
165
177
  };
package/dist/index.umd.js CHANGED
@@ -1,24 +1,25 @@
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 h=Object.defineProperty;var f=(s,r,o)=>r in s?h(s,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):s[r]=o;var c=(s,r,o)=>(f(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"};class o{constructor(t){c(this,"data");c(this,"elements",[]);c(this,"onConvert",t=>t);this.data=typeof t=="string"?JSON.parse(t):t}static toMarkdown(t,e){return new o(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(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})
2
2
 
3
- `;case r.BLOCKQUOTE:return`> ${e.value}
3
+ `;case e.BLOCKQUOTE:return`> ${r.value}
4
4
 
5
- `;case r.BR:return`
6
- `;case r.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,e.level)))} ${e.value}
5
+ `;case e.BR:return`
6
+ `;case e.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,r.level)))} ${r.value}
7
7
 
8
- `;case r.HR:return`---
8
+ `;case e.HR:return`---
9
9
 
10
- `;case r.IMG:return`![${e.alt}](${e.src})
10
+ `;case e.IMG:return`![${r.alt}](${r.src})
11
11
 
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}
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}
15
15
 
16
- `;case r.PRE:return`\`\`\`
17
- ${e.value}
16
+ `;case e.PRE:return`\`\`\`
17
+ ${r.value}
18
18
  \`\`\`
19
19
 
20
- `;case r.TD:{const n=i=>i.replaceAll("|","\\|").replaceAll(`
21
- `,"<br>");return`| ${e.values.map(n).join(" | ")} |
22
- `}case r.TR:return`| ${e.values.join(" | ")} |
23
- | ${e.values.map(()=>"---").join(" | ")} |
24
- `;default:return""}}).join("")}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):this.convertFromObject(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]=t;this.pushElement({tag:r.TR,values:Object.keys(n)});const i=Object.keys(n);return t.forEach(u=>{this.pushElement({tag:r.TD,values:i.map(a=>this.formatValue(u[a]))})}),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?"":Array.isArray(t)?t.map(e=>this.formatValue(e)).join(", "):typeof t=="object"?JSON.stringify(t):typeof t=="string"?t.trim():String(t)}}s.Converter=o,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
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(" | ")} |
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"})});
@@ -0,0 +1,2 @@
1
+ import { default as safeParseJson } from './safeParseJson';
2
+ export { safeParseJson, };
@@ -0,0 +1,2 @@
1
+ declare const safeParseJson: (data: unknown) => object | undefined;
2
+ export default safeParseJson;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kklab/json2markdown",
3
3
  "private": false,
4
- "version": "0.0.26",
4
+ "version": "0.0.27",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {