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