@kklab/json2markdown 0.0.15 → 0.0.16

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,34 @@ 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
+ case e.BR:
31
36
  return `
32
37
  `;
33
- case n.HEADING:
38
+ case e.HEADING:
34
39
  return `${"#".repeat(Math.max(1, Math.min(6, r.level)))} ${r.value}
35
40
 
36
41
  `;
37
- case n.LI:
42
+ case e.IMG:
43
+ return `![${r.alt}](${r.src})`;
44
+ case e.LI:
38
45
  return `${" ".repeat(r.indent)}- ${r.value}
39
46
  `;
40
- case n.P:
47
+ case e.P:
41
48
  return `${r.value}
42
49
 
43
50
  `;
44
- case n.TD:
51
+ case e.PRE:
52
+ return `\`\`\`
53
+ ${r.value}
54
+ \`\`\`
55
+
56
+ `;
57
+ case e.TD:
45
58
  return `| ${r.values.join(" | ")} |
46
59
  `;
47
- case n.TR:
60
+ case e.TR:
48
61
  return `| ${r.values.join(" | ")} |
49
62
  | ${r.values.map(() => "---").join(" | ")} |
50
63
  `;
@@ -70,26 +83,26 @@ class h {
70
83
  convertFromArray(r, t = 0) {
71
84
  if (r == null)
72
85
  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;
86
+ if (r.length > 0 && r.every((i) => typeof i == "object" && i !== null && Object.keys(i).length > 0)) {
87
+ const [i] = r;
75
88
  return this.pushElement({
76
- tag: n.TR,
77
- values: Object.keys(e)
78
- }), r.forEach((i) => {
89
+ tag: e.TR,
90
+ values: Object.keys(i)
91
+ }), r.forEach((s) => {
79
92
  this.pushElement({
80
- tag: n.TD,
81
- values: Object.values(i).map((u) => this.formatValue(u))
93
+ tag: e.TD,
94
+ values: Object.values(s).map((u) => this.formatValue(u))
82
95
  });
83
96
  }), this;
84
97
  }
85
- for (const e of r) {
86
- if (Array.isArray(e)) {
87
- this.convertFromArray(e, t + 1);
98
+ for (const i of r) {
99
+ if (Array.isArray(i)) {
100
+ this.convertFromArray(i, t + 1);
88
101
  continue;
89
102
  }
90
103
  this.pushElement({
91
- tag: n.LI,
92
- value: this.formatValue(e),
104
+ tag: e.LI,
105
+ value: this.formatValue(i),
93
106
  indent: t
94
107
  });
95
108
  }
@@ -98,29 +111,29 @@ class h {
98
111
  convertFromObject(r, t = 0) {
99
112
  if (r == null)
100
113
  return this.convertFromPrimitive(r), this;
101
- for (const [e, i] of Object.entries(r))
114
+ for (const [i, s] of Object.entries(r))
102
115
  if (this.pushElement({
103
- tag: n.HEADING,
104
- value: e,
116
+ tag: e.HEADING,
117
+ value: i,
105
118
  level: t + 1
106
119
  })) {
107
- if (Array.isArray(i)) {
108
- this.convertFromArray(i), this.pushElement({
109
- tag: n.BR
120
+ if (Array.isArray(s)) {
121
+ this.convertFromArray(s), this.pushElement({
122
+ tag: e.BR
110
123
  });
111
124
  continue;
112
125
  }
113
- if (typeof i == "object") {
114
- this.convertFromObject(i, t + 1);
126
+ if (typeof s == "object") {
127
+ this.convertFromObject(s, t + 1);
115
128
  continue;
116
129
  }
117
- this.convertFromPrimitive(i);
130
+ this.convertFromPrimitive(s);
118
131
  }
119
132
  return this;
120
133
  }
121
134
  convertFromPrimitive(r) {
122
135
  return this.pushElement({
123
- tag: n.P,
136
+ tag: e.P,
124
137
  value: this.formatValue(r)
125
138
  }), this;
126
139
  }
@@ -129,5 +142,5 @@ class h {
129
142
  }
130
143
  }
131
144
  export {
132
- h as Converter
145
+ c as Converter
133
146
  };
package/dist/index.umd.js CHANGED
@@ -1,10 +1,14 @@
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})`;case t.BR:return`
2
+ `;case t.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,r.level)))} ${r.value}
3
3
 
4
- `;case e.LI:return`${" ".repeat(t.indent)}- ${t.value}
5
- `;case e.P:return`${t.value}
4
+ `;case t.IMG:return`![${r.alt}](${r.src})`;case t.LI:return`${" ".repeat(r.indent)}- ${r.value}
5
+ `;case t.P:return`${r.value}
6
6
 
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"})});
7
+ `;case t.PRE:return`\`\`\`
8
+ ${r.value}
9
+ \`\`\`
10
+
11
+ `;case t.TD:return`| ${r.values.join(" | ")} |
12
+ `;case t.TR:return`| ${r.values.join(" | ")} |
13
+ | ${r.values.map(()=>"---").join(" | ")} |
14
+ `;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.16",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {