@kklab/json2markdown 0.0.23 → 0.0.24

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 h = Object.defineProperty;
2
- var l = (n, r, t) => r in n ? h(n, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[r] = t;
3
- var u = (n, r, t) => (l(n, typeof r != "symbol" ? r + "" : r, t), t);
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 c = (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,25 @@ const e = {
13
13
  PRE: "pre",
14
14
  TD: "td",
15
15
  TR: "tr"
16
+ }, f = (i) => i.replaceAll("<", "&lt;").replaceAll(">", "&gt;"), p = (i) => {
17
+ try {
18
+ return JSON.parse(i);
19
+ } catch {
20
+ return;
21
+ }
16
22
  };
17
- class a {
23
+ class u {
18
24
  constructor(r) {
19
- u(this, "data");
20
- u(this, "elements", []);
21
- u(this, "onConvert", (r) => r);
25
+ c(this, "data");
26
+ c(this, "elements", []);
27
+ c(this, "onConvert", (r) => r);
22
28
  this.data = typeof r == "string" ? JSON.parse(r) : r;
23
29
  }
24
30
  /**
25
31
  * Converts the provided data into Markdown format.
26
32
  */
27
33
  static toMarkdown(r, t) {
28
- return new a(r).toMarkdown(t);
34
+ return new u(r).toMarkdown(t);
29
35
  }
30
36
  /**
31
37
  * Converts the provided data into Markdown format.
@@ -57,7 +63,7 @@ class a {
57
63
 
58
64
  `;
59
65
  case e.LI: {
60
- const s = (i) => i.replaceAll(`
66
+ const s = (n) => n.replaceAll(`
61
67
  `, " ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)", "g"), (o) => o.trim());
62
68
  return `${" ".repeat(t.indent)}- ${s(t.value)}
63
69
  `;
@@ -73,8 +79,11 @@ ${t.value}
73
79
 
74
80
  `;
75
81
  case e.TD: {
76
- const s = (i) => i.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("|", "\\|").replaceAll(`
77
- `, "<br>");
82
+ const s = (n) => {
83
+ const o = p(n);
84
+ return typeof o == "object" ? `<pre><code>${f(JSON.stringify(o, null, 2))}</code></pre>` : n.replaceAll(`
85
+ `, "<br>").replaceAll("|", "\\|");
86
+ };
78
87
  return `| ${t.values.map(s).join(" | ")} |
79
88
  `;
80
89
  }
@@ -106,11 +115,11 @@ ${t.value}
106
115
  tag: e.TR,
107
116
  values: Object.keys(s)
108
117
  });
109
- const i = Object.keys(s);
118
+ const n = Object.keys(s);
110
119
  return r.forEach((o) => {
111
120
  this.pushElement({
112
121
  tag: e.TD,
113
- values: i.map((c) => this.formatValue(o[c]))
122
+ values: n.map((a) => this.formatValue(o[a]))
114
123
  });
115
124
  }), this;
116
125
  }
@@ -130,23 +139,23 @@ ${t.value}
130
139
  convertFromObject(r, t = 0) {
131
140
  if (r == null)
132
141
  return this.convertFromPrimitive(r), this;
133
- for (const [s, i] of Object.entries(r))
142
+ for (const [s, n] of Object.entries(r))
134
143
  if (this.pushElement({
135
144
  tag: e.HEADING,
136
145
  value: s,
137
146
  level: t + 1
138
147
  })) {
139
- if (Array.isArray(i)) {
140
- this.convertFromArray(i), this.pushElement({
148
+ if (Array.isArray(n)) {
149
+ this.convertFromArray(n), this.pushElement({
141
150
  tag: e.BR
142
151
  });
143
152
  continue;
144
153
  }
145
- if (typeof i == "object") {
146
- this.convertFromObject(i, t + 1);
154
+ if (typeof n == "object") {
155
+ this.convertFromObject(n, t + 1);
147
156
  continue;
148
157
  }
149
- this.convertFromPrimitive(i);
158
+ this.convertFromPrimitive(n);
150
159
  }
151
160
  return this;
152
161
  }
@@ -161,5 +170,5 @@ ${t.value}
161
170
  }
162
171
  }
163
172
  export {
164
- a as Converter
173
+ u as Converter
165
174
  };
package/dist/index.umd.js CHANGED
@@ -1,24 +1,24 @@
1
- (function(n,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(n=typeof globalThis<"u"?globalThis:n||self,r(n.JSON2Markdown={}))})(this,function(n){"use strict";var h=Object.defineProperty;var l=(n,r,o)=>r in n?h(n,r,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[r]=o;var c=(n,r,o)=>(l(n,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(s,t){typeof exports=="object"&&typeof module<"u"?t(exports):typeof define=="function"&&define.amd?define(["exports"],t):(s=typeof globalThis<"u"?globalThis:s||self,t(s.JSON2Markdown={}))})(this,function(s){"use strict";var p=Object.defineProperty;var v=(s,t,o)=>t in s?p(s,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):s[t]=o;var c=(s,t,o)=>(v(s,typeof t!="symbol"?t+"":t,o),o);const t={A:"a",BLOCKQUOTE:"blockquote",BR:"br",HEADING:"heading",HR:"hr",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"},o=h=>h.replaceAll("<","&lt;").replaceAll(">","&gt;"),l=h=>{try{return JSON.parse(h)}catch{return}};class a{constructor(r){c(this,"data");c(this,"elements",[]);c(this,"onConvert",r=>r);this.data=typeof r=="string"?JSON.parse(r):r}static toMarkdown(r,e){return new a(r).toMarkdown(e)}toMarkdown(r){return this.convert(r),this.elements.map(e=>{switch(e.tag){case t.A:return`[${e.value}](${e.href})
2
2
 
3
- `;case r.BLOCKQUOTE:return`> ${e.value}
3
+ `;case t.BLOCKQUOTE:return`> ${e.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 t.BR:return`
6
+ `;case t.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,e.level)))} ${e.value}
7
7
 
8
- `;case r.HR:return`---
8
+ `;case t.HR:return`---
9
9
 
10
- `;case r.IMG:return`![${e.alt}](${e.src})
10
+ `;case t.IMG:return`![${e.alt}](${e.src})
11
11
 
12
- `;case r.LI:{const s=i=>i.replaceAll(`
13
- `," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),u=>u.trim());return`${" ".repeat(e.indent)}- ${s(e.value)}
14
- `}case r.P:return`${e.value}
12
+ `;case t.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 t.P:return`${e.value}
15
15
 
16
- `;case r.PRE:return`\`\`\`
16
+ `;case t.PRE:return`\`\`\`
17
17
  ${e.value}
18
18
  \`\`\`
19
19
 
20
- `;case r.TD:{const s=i=>i.replaceAll("<","&lt;").replaceAll(">","&gt;").replaceAll("|","\\|").replaceAll(`
21
- `,"<br>");return`| ${e.values.map(s).join(" | ")} |
22
- `}case r.TR:return`| ${e.values.join(" | ")} |
20
+ `;case t.TD:{const n=i=>{const u=l(i);return typeof u=="object"?`<pre><code>${o(JSON.stringify(u,null,2))}</code></pre>`:i.replaceAll(`
21
+ `,"<br>").replaceAll("|","\\|")};return`| ${e.values.map(n).join(" | ")} |
22
+ `}case t.TR:return`| ${e.values.join(" | ")} |
23
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),this}convertFromArray(t,e=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:r.TR,values:Object.keys(s)});const i=Object.keys(s);return t.forEach(u=>{this.pushElement({tag:r.TD,values:i.map(a=>this.formatValue(u[a]))})}),this}for(const s of t){if(Array.isArray(s)){this.convertFromArray(s,e+1);continue}this.pushElement({tag:r.LI,value:this.formatValue(s),indent:e})}return this}convertFromObject(t,e=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[s,i]of Object.entries(t))if(this.pushElement({tag:r.HEADING,value:s,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)}}n.Converter=o,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
24
+ `;default:return""}}).join("")}getElements(){return this.elements}pushElement(r){const e=this.onConvert(r);return e?(this.elements.push(e),!0):!1}convert(r){return r&&(this.onConvert=r),Array.isArray(this.data)&&this.convertFromArray(this.data),this.convertFromObject(this.data),this}convertFromArray(r,e=0){if(r==null)return this.convertFromPrimitive(r),this;if(r.length>0&&r.every(n=>typeof n=="object"&&n!==null&&Object.keys(n).length>0)){const[n]=r;this.pushElement({tag:t.TR,values:Object.keys(n)});const i=Object.keys(n);return r.forEach(u=>{this.pushElement({tag:t.TD,values:i.map(f=>this.formatValue(u[f]))})}),this}for(const n of r){if(Array.isArray(n)){this.convertFromArray(n,e+1);continue}this.pushElement({tag:t.LI,value:this.formatValue(n),indent:e})}return this}convertFromObject(r,e=0){if(r==null)return this.convertFromPrimitive(r),this;for(const[n,i]of Object.entries(r))if(this.pushElement({tag:t.HEADING,value:n,level:e+1})){if(Array.isArray(i)){this.convertFromArray(i),this.pushElement({tag:t.BR});continue}if(typeof i=="object"){this.convertFromObject(i,e+1);continue}this.convertFromPrimitive(i)}return this}convertFromPrimitive(r){return this.pushElement({tag:t.P,value:this.formatValue(r)}),this}formatValue(r){return r==null?"":Array.isArray(r)?r.map(e=>this.formatValue(e)).join(", "):typeof r=="object"?JSON.stringify(r):typeof r=="string"?r.trim():String(r)}}s.Converter=a,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,2 @@
1
+ declare const escapeHTML: (str: string) => string;
2
+ export default escapeHTML;
@@ -0,0 +1,3 @@
1
+ import { default as escapeHTML } from './escapeHTML';
2
+ import { default as safeParseJSON } from './safeParseJSON';
3
+ export { escapeHTML, safeParseJSON, };
@@ -0,0 +1,2 @@
1
+ declare const safeParseJSON: (str: string) => any;
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.23",
4
+ "version": "0.0.24",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {