@kklab/json2markdown 0.0.21 → 0.0.22

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 f = Object.defineProperty;
2
- var v = (n, r, t) => r in n ? f(n, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[r] = t;
3
- var o = (n, r, t) => (v(n, typeof r != "symbol" ? r + "" : r, t), t);
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 o = (n, r, t) => (l(n, typeof r != "symbol" ? r + "" : r, t), t);
4
4
  const e = {
5
5
  A: "a",
6
6
  BLOCKQUOTE: "blockquote",
@@ -14,7 +14,7 @@ const e = {
14
14
  TD: "td",
15
15
  TR: "tr"
16
16
  };
17
- class c {
17
+ class a {
18
18
  constructor(r) {
19
19
  o(this, "data");
20
20
  o(this, "elements", []);
@@ -25,7 +25,7 @@ class c {
25
25
  * Converts the provided data into Markdown format.
26
26
  */
27
27
  static toMarkdown(r, t) {
28
- return new c(r).toMarkdown(t);
28
+ return new a(r).toMarkdown(t);
29
29
  }
30
30
  /**
31
31
  * Converts the provided data into Markdown format.
@@ -57,7 +57,8 @@ class c {
57
57
 
58
58
  `;
59
59
  case e.LI:
60
- return `${" ".repeat(t.indent)}- ${t.value}
60
+ return `${" ".repeat(t.indent)}- ${t.value.replaceAll(`
61
+ `, " ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)", "g"), (s) => s.trim())}
61
62
  `;
62
63
  case e.P:
63
64
  return `${t.value}
@@ -70,8 +71,8 @@ ${t.value}
70
71
 
71
72
  `;
72
73
  case e.TD:
73
- return `| ${t.values.map((s) => typeof s == "string" ? s.replaceAll(`
74
- `, "<br>") : s).join(" | ")} |
74
+ return `| ${t.values.map((s) => s.replaceAll(`
75
+ `, "<br>").replaceAll("|", "\\|")).join(" | ")} |
75
76
  `;
76
77
  case e.TR:
77
78
  return `| ${t.values.join(" | ")} |
@@ -102,13 +103,10 @@ ${t.value}
102
103
  values: Object.keys(s)
103
104
  });
104
105
  const i = Object.keys(s);
105
- return r.forEach((a) => {
106
+ return r.forEach((u) => {
106
107
  this.pushElement({
107
108
  tag: e.TD,
108
- values: i.map((h) => {
109
- const u = this.formatValue(a[h]);
110
- return typeof u == "string" ? u.replaceAll("|", "\\|") : u;
111
- })
109
+ values: i.map((c) => this.formatValue(u[c]))
112
110
  });
113
111
  }), this;
114
112
  }
@@ -155,9 +153,9 @@ ${t.value}
155
153
  }), this;
156
154
  }
157
155
  formatValue(r) {
158
- return r == null ? "" : Array.isArray(r) ? r.map((t) => this.formatValue(t)).join(", ") : typeof r == "object" ? JSON.stringify(r) : typeof r == "string" ? r.trim() : r;
156
+ 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);
159
157
  }
160
158
  }
161
159
  export {
162
- c as Converter
160
+ a as Converter
163
161
  };
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(i,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(i=typeof globalThis<"u"?globalThis:i||self,r(i.JSON2Markdown={}))})(this,function(i){"use strict";var f=Object.defineProperty;var l=(i,r,s)=>r in i?f(i,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[r]=s;var u=(i,r,s)=>(l(i,typeof r!="symbol"?r+"":r,s),s);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 s{constructor(t){u(this,"data");u(this,"elements",[]);u(this,"onConvert",t=>t);this.data=typeof t=="string"?JSON.parse(t):t}static toMarkdown(t,e){return new s(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,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 f=(n,r,s)=>r in n?h(n,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[r]=s;var u=(n,r,s)=>(f(n,typeof r!="symbol"?r+"":r,s),s);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 s{constructor(t){u(this,"data");u(this,"elements",[]);u(this,"onConvert",t=>t);this.data=typeof t=="string"?JSON.parse(t):t}static toMarkdown(t,e){return new s(t).toMarkdown(e)}toMarkdown(t){return this.convert(t),this.elements.map(e=>{switch(e.tag){case r.A:return`[${e.value}](${e.href})
2
2
 
3
3
  `;case r.BLOCKQUOTE:return`> ${e.value}
4
4
 
@@ -9,15 +9,16 @@
9
9
 
10
10
  `;case r.IMG:return`![${e.alt}](${e.src})
11
11
 
12
- `;case r.LI:return`${" ".repeat(e.indent)}- ${e.value}
12
+ `;case r.LI:return`${" ".repeat(e.indent)}- ${e.value.replaceAll(`
13
+ `," ").replace(new RegExp("(?<=^|\\s)(-\\s*|\\d+\\.\\s*)","g"),i=>i.trim())}
13
14
  `;case r.P:return`${e.value}
14
15
 
15
16
  `;case r.PRE:return`\`\`\`
16
17
  ${e.value}
17
18
  \`\`\`
18
19
 
19
- `;case r.TD:return`| ${e.values.map(n=>typeof n=="string"?n.replaceAll(`
20
- `,"<br>"):n).join(" | ")} |
20
+ `;case r.TD:return`| ${e.values.map(i=>i.replaceAll(`
21
+ `,"<br>").replaceAll("|","\\|")).join(" | ")} |
21
22
  `;case r.TR:return`| ${e.values.join(" | ")} |
22
23
  | ${e.values.map(()=>"---").join(" | ")} |
23
- `;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(n=>typeof n=="object"&&n!==null&&Object.keys(n).length>0)){const[n]=t;this.pushElement({tag:r.TR,values:Object.keys(n)});const o=Object.keys(n);return t.forEach(a=>{this.pushElement({tag:r.TD,values:o.map(h=>{const c=this.formatValue(a[h]);return typeof c=="string"?c.replaceAll("|","\\|"):c})})}),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,o]of Object.entries(t))if(this.pushElement({tag:r.HEADING,value:n,level:e+1})){if(Array.isArray(o)){this.convertFromArray(o),this.pushElement({tag:r.BR});continue}if(typeof o=="object"){this.convertFromObject(o,e+1);continue}this.convertFromPrimitive(o)}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():t}}i.Converter=s,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
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(i=>typeof i=="object"&&i!==null&&Object.keys(i).length>0)){const[i]=t;this.pushElement({tag:r.TR,values:Object.keys(i)});const o=Object.keys(i);return t.forEach(a=>{this.pushElement({tag:r.TD,values:o.map(c=>this.formatValue(a[c]))})}),this}for(const i of t){if(Array.isArray(i)){this.convertFromArray(i,e+1);continue}this.pushElement({tag:r.LI,value:this.formatValue(i),indent:e})}return this}convertFromObject(t,e=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[i,o]of Object.entries(t))if(this.pushElement({tag:r.HEADING,value:i,level:e+1})){if(Array.isArray(o)){this.convertFromArray(o),this.pushElement({tag:r.BR});continue}if(typeof o=="object"){this.convertFromObject(o,e+1);continue}this.convertFromPrimitive(o)}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=s,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
@@ -27,12 +27,12 @@ export interface ImageElement {
27
27
  }
28
28
  export interface ListItemElement {
29
29
  tag: typeof Tag.LI;
30
- value: unknown;
30
+ value: string;
31
31
  indent: number;
32
32
  }
33
33
  export interface ParagraphElement {
34
34
  tag: typeof Tag.P;
35
- value: unknown;
35
+ value: string;
36
36
  }
37
37
  export interface PreformattedTextElement {
38
38
  tag: typeof Tag.PRE;
@@ -40,11 +40,11 @@ export interface PreformattedTextElement {
40
40
  }
41
41
  export interface TableDataElement {
42
42
  tag: typeof Tag.TD;
43
- values: unknown[];
43
+ values: string[];
44
44
  }
45
45
  export interface TableRowElement {
46
46
  tag: typeof Tag.TR;
47
- values: unknown[];
47
+ values: string[];
48
48
  }
49
49
  type Element = AnchorElement | BlockquoteElement | BreakElement | HeadingElement | HorizontalRuleElement | ImageElement | ListItemElement | ParagraphElement | PreformattedTextElement | TableDataElement | TableRowElement;
50
50
  export default Element;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kklab/json2markdown",
3
3
  "private": false,
4
- "version": "0.0.21",
4
+ "version": "0.0.22",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {