@kklab/json2markdown 0.0.16 → 0.0.18

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 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);
2
+ var a = (s, r, t) => r in s ? h(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
4
  const e = {
5
5
  A: "a",
6
6
  BR: "br",
@@ -16,7 +16,7 @@ class c {
16
16
  constructor(r, t = {}) {
17
17
  o(this, "elements", []);
18
18
  o(this, "onConvert");
19
- this.onConvert = t.onConvert ?? ((i) => i), r && this.convert(r);
19
+ this.onConvert = t.onConvert ?? ((n) => n), r && this.convert(r);
20
20
  }
21
21
  /**
22
22
  * Converts the provided data into Markdown format.
@@ -31,7 +31,9 @@ class c {
31
31
  return this.elements.map((r) => {
32
32
  switch (r.tag) {
33
33
  case e.A:
34
- return `[${r.value}](${r.href})`;
34
+ return `[${r.value}](${r.href})
35
+
36
+ `;
35
37
  case e.BR:
36
38
  return `
37
39
  `;
@@ -40,7 +42,9 @@ class c {
40
42
 
41
43
  `;
42
44
  case e.IMG:
43
- return `![${r.alt}](${r.src})`;
45
+ return `![${r.alt}](${r.src})
46
+
47
+ `;
44
48
  case e.LI:
45
49
  return `${" ".repeat(r.indent)}- ${r.value}
46
50
  `;
@@ -55,7 +59,8 @@ ${r.value}
55
59
 
56
60
  `;
57
61
  case e.TD:
58
- return `| ${r.values.join(" | ")} |
62
+ return `| ${r.values.map((t) => typeof t == "string" ? t.replaceAll(`
63
+ `, "<br>") : t).join(" | ")} |
59
64
  `;
60
65
  case e.TR:
61
66
  return `| ${r.values.join(" | ")} |
@@ -83,26 +88,26 @@ ${r.value}
83
88
  convertFromArray(r, t = 0) {
84
89
  if (r == null)
85
90
  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;
91
+ if (r.length > 0 && r.every((n) => typeof n == "object" && n !== null && Object.keys(n).length > 0)) {
92
+ const [n] = r;
88
93
  return this.pushElement({
89
94
  tag: e.TR,
90
- values: Object.keys(i)
91
- }), r.forEach((s) => {
95
+ values: Object.keys(n)
96
+ }), r.forEach((i) => {
92
97
  this.pushElement({
93
98
  tag: e.TD,
94
- values: Object.values(s).map((u) => this.formatValue(u))
99
+ values: Object.values(i).map((u) => this.formatValue(u))
95
100
  });
96
101
  }), this;
97
102
  }
98
- for (const i of r) {
99
- if (Array.isArray(i)) {
100
- this.convertFromArray(i, t + 1);
103
+ for (const n of r) {
104
+ if (Array.isArray(n)) {
105
+ this.convertFromArray(n, t + 1);
101
106
  continue;
102
107
  }
103
108
  this.pushElement({
104
109
  tag: e.LI,
105
- value: this.formatValue(i),
110
+ value: this.formatValue(n),
106
111
  indent: t
107
112
  });
108
113
  }
@@ -111,23 +116,23 @@ ${r.value}
111
116
  convertFromObject(r, t = 0) {
112
117
  if (r == null)
113
118
  return this.convertFromPrimitive(r), this;
114
- for (const [i, s] of Object.entries(r))
119
+ for (const [n, i] of Object.entries(r))
115
120
  if (this.pushElement({
116
121
  tag: e.HEADING,
117
- value: i,
122
+ value: n,
118
123
  level: t + 1
119
124
  })) {
120
- if (Array.isArray(s)) {
121
- this.convertFromArray(s), this.pushElement({
125
+ if (Array.isArray(i)) {
126
+ this.convertFromArray(i), this.pushElement({
122
127
  tag: e.BR
123
128
  });
124
129
  continue;
125
130
  }
126
- if (typeof s == "object") {
127
- this.convertFromObject(s, t + 1);
131
+ if (typeof i == "object") {
132
+ this.convertFromObject(i, t + 1);
128
133
  continue;
129
134
  }
130
- this.convertFromPrimitive(s);
135
+ this.convertFromPrimitive(i);
131
136
  }
132
137
  return this;
133
138
  }
package/dist/index.umd.js CHANGED
@@ -1,14 +1,19 @@
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`
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})
2
+
3
+ `;case t.BR:return`
2
4
  `;case t.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,r.level)))} ${r.value}
3
5
 
4
- `;case t.IMG:return`![${r.alt}](${r.src})`;case t.LI:return`${" ".repeat(r.indent)}- ${r.value}
6
+ `;case t.IMG:return`![${r.alt}](${r.src})
7
+
8
+ `;case t.LI:return`${" ".repeat(r.indent)}- ${r.value}
5
9
  `;case t.P:return`${r.value}
6
10
 
7
11
  `;case t.PRE:return`\`\`\`
8
12
  ${r.value}
9
13
  \`\`\`
10
14
 
11
- `;case t.TD:return`| ${r.values.join(" | ")} |
15
+ `;case t.TD:return`| ${r.values.map(e=>typeof e=="string"?e.replaceAll(`
16
+ `,"<br>"):e).join(" | ")} |
12
17
  `;case t.TR:return`| ${r.values.join(" | ")} |
13
18
  | ${r.values.map(()=>"---").join(" | ")} |
14
19
  `;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"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kklab/json2markdown",
3
3
  "private": false,
4
- "version": "0.0.16",
4
+ "version": "0.0.18",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -9,11 +9,13 @@
9
9
  "build": "tsc -p ./tsconfig.build.json && vite build",
10
10
  "preview": "vite preview",
11
11
  "lint": "eslint .",
12
- "test": "vitest"
12
+ "test": "npm run test:unit -- --run",
13
+ "test:unit": "vitest",
14
+ "release": "npm run test && npm run build && npm publish --access public"
13
15
  },
14
16
  "devDependencies": {
15
17
  "@eslint/js": "^9.11.0",
16
- "@kklab/stryle": "^0.0.2",
18
+ "@kklab/stryle": "^0.0.10",
17
19
  "@stylistic/eslint-plugin": "^2.9.0",
18
20
  "@types/eslint__js": "^8.42.3",
19
21
  "@types/node": "^22.5.5",