@kklab/json2markdown 0.0.15 → 0.0.17

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.
@@ -1,8 +1,11 @@
1
1
  declare const Tags: {
2
+ readonly A: "a";
2
3
  readonly BR: "br";
3
4
  readonly HEADING: "heading";
5
+ readonly IMG: "img";
4
6
  readonly LI: "li";
5
7
  readonly P: "p";
8
+ readonly PRE: "pre";
6
9
  readonly TD: "td";
7
10
  readonly TR: "tr";
8
11
  };
package/dist/index.js CHANGED
@@ -1,25 +1,28 @@
1
- var c = Object.defineProperty;
2
- var a = (s, r, t) => r in s ? c(s, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[r] = t;
3
- var o = (s, r, t) => (a(s, typeof r != "symbol" ? r + "" : r, t), t);
4
- const n = {
1
+ var h = Object.defineProperty;
2
+ var a = (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) => (a(n, typeof r != "symbol" ? r + "" : r, t), t);
4
+ const e = {
5
+ A: "a",
5
6
  BR: "br",
6
7
  HEADING: "heading",
8
+ IMG: "img",
7
9
  LI: "li",
8
10
  P: "p",
11
+ PRE: "pre",
9
12
  TD: "td",
10
13
  TR: "tr"
11
14
  };
12
- class h {
15
+ class c {
13
16
  constructor(r, t = {}) {
14
17
  o(this, "elements", []);
15
18
  o(this, "onConvert");
16
- this.onConvert = t.onConvert ?? ((e) => e), r && this.convert(r);
19
+ this.onConvert = t.onConvert ?? ((i) => i), r && this.convert(r);
17
20
  }
18
21
  /**
19
22
  * Converts the provided data into Markdown format.
20
23
  */
21
24
  static toMarkdown(r, t = {}) {
22
- return new h(r, t).toMarkdown();
25
+ return new c(r, t).toMarkdown();
23
26
  }
24
27
  /**
25
28
  * Converts the provided data into Markdown format.
@@ -27,24 +30,38 @@ class h {
27
30
  toMarkdown() {
28
31
  return this.elements.map((r) => {
29
32
  switch (r.tag) {
30
- case n.BR:
33
+ case e.A:
34
+ return `[${r.value}](${r.href})
35
+
36
+ `;
37
+ case e.BR:
31
38
  return `
32
39
  `;
33
- case n.HEADING:
40
+ case e.HEADING:
34
41
  return `${"#".repeat(Math.max(1, Math.min(6, r.level)))} ${r.value}
35
42
 
36
43
  `;
37
- case n.LI:
44
+ case e.IMG:
45
+ return `![${r.alt}](${r.src})
46
+
47
+ `;
48
+ case e.LI:
38
49
  return `${" ".repeat(r.indent)}- ${r.value}
39
50
  `;
40
- case n.P:
51
+ case e.P:
41
52
  return `${r.value}
42
53
 
43
54
  `;
44
- case n.TD:
55
+ case e.PRE:
56
+ return `\`\`\`
57
+ ${r.value}
58
+ \`\`\`
59
+
60
+ `;
61
+ case e.TD:
45
62
  return `| ${r.values.join(" | ")} |
46
63
  `;
47
- case n.TR:
64
+ case e.TR:
48
65
  return `| ${r.values.join(" | ")} |
49
66
  | ${r.values.map(() => "---").join(" | ")} |
50
67
  `;
@@ -70,26 +87,26 @@ class h {
70
87
  convertFromArray(r, t = 0) {
71
88
  if (r == null)
72
89
  return this.convertFromPrimitive(r), this;
73
- if (r.length > 0 && r.every((e) => typeof e == "object" && e !== null && Object.keys(e).length > 0)) {
74
- const [e] = r;
90
+ if (r.length > 0 && r.every((i) => typeof i == "object" && i !== null && Object.keys(i).length > 0)) {
91
+ const [i] = r;
75
92
  return this.pushElement({
76
- tag: n.TR,
77
- values: Object.keys(e)
78
- }), r.forEach((i) => {
93
+ tag: e.TR,
94
+ values: Object.keys(i)
95
+ }), r.forEach((s) => {
79
96
  this.pushElement({
80
- tag: n.TD,
81
- values: Object.values(i).map((u) => this.formatValue(u))
97
+ tag: e.TD,
98
+ values: Object.values(s).map((u) => this.formatValue(u))
82
99
  });
83
100
  }), this;
84
101
  }
85
- for (const e of r) {
86
- if (Array.isArray(e)) {
87
- this.convertFromArray(e, t + 1);
102
+ for (const i of r) {
103
+ if (Array.isArray(i)) {
104
+ this.convertFromArray(i, t + 1);
88
105
  continue;
89
106
  }
90
107
  this.pushElement({
91
- tag: n.LI,
92
- value: this.formatValue(e),
108
+ tag: e.LI,
109
+ value: this.formatValue(i),
93
110
  indent: t
94
111
  });
95
112
  }
@@ -98,29 +115,29 @@ class h {
98
115
  convertFromObject(r, t = 0) {
99
116
  if (r == null)
100
117
  return this.convertFromPrimitive(r), this;
101
- for (const [e, i] of Object.entries(r))
118
+ for (const [i, s] of Object.entries(r))
102
119
  if (this.pushElement({
103
- tag: n.HEADING,
104
- value: e,
120
+ tag: e.HEADING,
121
+ value: i,
105
122
  level: t + 1
106
123
  })) {
107
- if (Array.isArray(i)) {
108
- this.convertFromArray(i), this.pushElement({
109
- tag: n.BR
124
+ if (Array.isArray(s)) {
125
+ this.convertFromArray(s), this.pushElement({
126
+ tag: e.BR
110
127
  });
111
128
  continue;
112
129
  }
113
- if (typeof i == "object") {
114
- this.convertFromObject(i, t + 1);
130
+ if (typeof s == "object") {
131
+ this.convertFromObject(s, t + 1);
115
132
  continue;
116
133
  }
117
- this.convertFromPrimitive(i);
134
+ this.convertFromPrimitive(s);
118
135
  }
119
136
  return this;
120
137
  }
121
138
  convertFromPrimitive(r) {
122
139
  return this.pushElement({
123
- tag: n.P,
140
+ tag: e.P,
124
141
  value: this.formatValue(r)
125
142
  }), this;
126
143
  }
@@ -129,5 +146,5 @@ class h {
129
146
  }
130
147
  }
131
148
  export {
132
- h as Converter
149
+ c as Converter
133
150
  };
package/dist/index.umd.js CHANGED
@@ -1,10 +1,18 @@
1
- (function(i,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(i=typeof globalThis<"u"?globalThis:i||self,e(i.JSON2MD={}))})(this,function(i){"use strict";var h=Object.defineProperty;var f=(i,e,o)=>e in i?h(i,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):i[e]=o;var u=(i,e,o)=>(f(i,typeof e!="symbol"?e+"":e,o),o);const e={BR:"br",HEADING:"heading",LI:"li",P:"p",TD:"td",TR:"tr"};class o{constructor(t,r={}){u(this,"elements",[]);u(this,"onConvert");this.onConvert=r.onConvert??(n=>n),t&&this.convert(t)}static toMarkdown(t,r={}){return new o(t,r).toMarkdown()}toMarkdown(){return this.elements.map(t=>{switch(t.tag){case e.BR:return`
2
- `;case e.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,t.level)))} ${t.value}
1
+ (function(i,t){typeof exports=="object"&&typeof module<"u"?t(exports):typeof define=="function"&&define.amd?define(["exports"],t):(i=typeof globalThis<"u"?globalThis:i||self,t(i.JSON2MD={}))})(this,function(i){"use strict";var h=Object.defineProperty;var f=(i,t,s)=>t in i?h(i,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[t]=s;var u=(i,t,s)=>(f(i,typeof t!="symbol"?t+"":t,s),s);const t={A:"a",BR:"br",HEADING:"heading",IMG:"img",LI:"li",P:"p",PRE:"pre",TD:"td",TR:"tr"};class s{constructor(r,e={}){u(this,"elements",[]);u(this,"onConvert");this.onConvert=e.onConvert??(n=>n),r&&this.convert(r)}static toMarkdown(r,e={}){return new s(r,e).toMarkdown()}toMarkdown(){return this.elements.map(r=>{switch(r.tag){case t.A:return`[${r.value}](${r.href})
3
2
 
4
- `;case e.LI:return`${" ".repeat(t.indent)}- ${t.value}
5
- `;case e.P:return`${t.value}
3
+ `;case t.BR:return`
4
+ `;case t.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,r.level)))} ${r.value}
6
5
 
7
- `;case e.TD:return`| ${t.values.join(" | ")} |
8
- `;case e.TR:return`| ${t.values.join(" | ")} |
9
- | ${t.values.map(()=>"---").join(" | ")} |
10
- `;default:return""}}).join("")}getElements(){return this.elements}pushElement(t){const r=this.onConvert(t);return r?(this.elements.push(r),!0):!1}convert(t){if(Array.isArray(t)){this.convertFromArray(t);return}this.convertFromObject(t)}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]=t;return this.pushElement({tag:e.TR,values:Object.keys(n)}),t.forEach(s=>{this.pushElement({tag:e.TD,values:Object.values(s).map(c=>this.formatValue(c))})}),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,s]of Object.entries(t))if(this.pushElement({tag:e.HEADING,value:n,level:r+1})){if(Array.isArray(s)){this.convertFromArray(s),this.pushElement({tag:e.BR});continue}if(typeof s=="object"){this.convertFromObject(s,r+1);continue}this.convertFromPrimitive(s)}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):typeof t=="string"?t.trim():t}}i.Converter=o,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
6
+ `;case t.IMG:return`![${r.alt}](${r.src})
7
+
8
+ `;case t.LI:return`${" ".repeat(r.indent)}- ${r.value}
9
+ `;case t.P:return`${r.value}
10
+
11
+ `;case t.PRE:return`\`\`\`
12
+ ${r.value}
13
+ \`\`\`
14
+
15
+ `;case t.TD:return`| ${r.values.join(" | ")} |
16
+ `;case t.TR:return`| ${r.values.join(" | ")} |
17
+ | ${r.values.map(()=>"---").join(" | ")} |
18
+ `;default:return""}}).join("")}getElements(){return this.elements}pushElement(r){const e=this.onConvert(r);return e?(this.elements.push(e),!0):!1}convert(r){if(Array.isArray(r)){this.convertFromArray(r);return}this.convertFromObject(r)}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;return this.pushElement({tag:t.TR,values:Object.keys(n)}),r.forEach(o=>{this.pushElement({tag:t.TD,values:Object.values(o).map(c=>this.formatValue(c))})}),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,o]of Object.entries(r))if(this.pushElement({tag:t.HEADING,value:n,level:e+1})){if(Array.isArray(o)){this.convertFromArray(o),this.pushElement({tag:t.BR});continue}if(typeof o=="object"){this.convertFromObject(o,e+1);continue}this.convertFromPrimitive(o)}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():r}}i.Converter=s,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
@@ -1,5 +1,10 @@
1
1
  import { default as Tags } from '../constants/Tags';
2
2
  import { default as HeadingLevel } from './HeadingLevel';
3
+ export interface AnchorElement {
4
+ tag: typeof Tags.A;
5
+ href: string;
6
+ value: string;
7
+ }
3
8
  export interface BreakElement {
4
9
  tag: typeof Tags.BR;
5
10
  }
@@ -8,6 +13,11 @@ export interface HeadingElement {
8
13
  value: string;
9
14
  level: HeadingLevel;
10
15
  }
16
+ export interface ImageElement {
17
+ tag: typeof Tags.IMG;
18
+ src: string;
19
+ alt: string;
20
+ }
11
21
  export interface ListItemElement {
12
22
  tag: typeof Tags.LI;
13
23
  value: unknown;
@@ -17,6 +27,10 @@ export interface ParagraphElement {
17
27
  tag: typeof Tags.P;
18
28
  value: unknown;
19
29
  }
30
+ export interface PreformattedTextElement {
31
+ tag: typeof Tags.PRE;
32
+ value: string;
33
+ }
20
34
  export interface TableDataElement {
21
35
  tag: typeof Tags.TD;
22
36
  values: unknown[];
@@ -25,5 +39,5 @@ export interface TableRowElement {
25
39
  tag: typeof Tags.TR;
26
40
  values: unknown[];
27
41
  }
28
- type Element = BreakElement | HeadingElement | ListItemElement | ParagraphElement | TableDataElement | TableRowElement;
42
+ type Element = AnchorElement | BreakElement | HeadingElement | ImageElement | ListItemElement | ParagraphElement | PreformattedTextElement | TableDataElement | TableRowElement;
29
43
  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.15",
4
+ "version": "0.0.17",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {