@kklab/json2markdown 0.0.17 → 0.0.19

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/README.md CHANGED
@@ -1,56 +1,91 @@
1
- # json2markdown
1
+ # JSON2Markdown
2
2
 
3
- A simple JSON to Markdown converter that transforms JSON data into easily readable text.
3
+ A simple JSON to Markdown converter that transforms JSON data into Markdown format. It can be used for transforming structured data into a readable format.
4
4
 
5
- ## Usage
5
+ ## Getting Started
6
6
 
7
7
  ### Using with ES Modules
8
8
 
9
- Import the module and use it in your code:
9
+ To get started with ES Modules, simply import the module and use it in your code:
10
10
 
11
11
  ```js
12
12
  import { Converter } from '@kklab/json2markdown';
13
13
 
14
14
  const output = Converter.toMarkdown({
15
15
  'Hello, World!': 'It works!',
16
- }, {
17
- onConvert: (element) => {
18
- if (element.tag === 'heading') {
19
- element.level += 1;
20
- }
21
- return element;
22
- },
23
16
  });
24
17
 
25
- console.log(output);
26
18
  // Output:
27
- // ## Hello, World!\n\nIt works!\n\n
19
+ // # Hello, World!\n\nIt works!\n\n
28
20
  ```
29
21
 
30
22
  ### Using with UMD Modules
31
23
 
32
- Include the UMD script in your HTML file and use it:
24
+ Alternatively, if you're using UMD modules, include the script in your HTML file and use it in your code:
33
25
 
34
26
  ```html
35
27
  <script src="https://unpkg.com/@kklab/json2markdown/dist/index.umd.js"></script>
36
28
  <script>
37
- const output = window.JSON2MD.Converter.toMarkdown({
29
+ const output = window.JSON2Markdown.Converter.toMarkdown({
38
30
  'Hello, World!': 'It works!',
39
- }, {
40
- onConvert: (element) => {
41
- if (element.tag === 'heading') {
42
- element.level += 1;
43
- }
44
- return element;
45
- },
46
31
  });
47
32
 
48
- console.log(JSON.stringify(output));
49
33
  // Output:
50
- // ## Hello, World!\n\nIt works!\n\n
34
+ // # Hello, World!\n\nIt works!\n\n
51
35
  </script>
52
36
  ```
53
37
 
38
+ ## Usage
39
+
40
+ ### Basic Usage
41
+
42
+ The `Converter` can be initialized with either a JSON string or an object, where both the keys and values will be converted into Markdown elements.
43
+
44
+ ```js
45
+ const converter = new Converter({
46
+ 'Hello, World!': 'It works!',
47
+ });
48
+
49
+ const output = converter.toMarkdown();
50
+
51
+ // Output:
52
+ // # Hello, World!\n\nIt works!\n\n
53
+ ```
54
+
55
+ ### Callback Function
56
+
57
+ The callback function in the `toMarkdown` method allows you to transform generated Markdown elements during the conversion process. It is called for each element, giving you the opportunity to modify its properties before returning it.
58
+
59
+ ```js
60
+ const converter = new Converter({
61
+ 'Hello, World!': 'It works!',
62
+ });
63
+
64
+ const output = converter.toMarkdown((element) => {
65
+ if (element.tag === 'heading') {
66
+ element.level += 1;
67
+ }
68
+ return element;
69
+ });
70
+
71
+ // Output:
72
+ // ## Hello, World!\n\nIt works!\n\n
73
+ ```
74
+
75
+ ## Available Tags
76
+
77
+ | Name | Description |
78
+ | --- | --- |
79
+ | `A` | Represents an anchor (`a`) tag. |
80
+ | `BR` | Represents a line break (`br`) tag. |
81
+ | `HEADING` | Represents a heading (`heading`) tag. |
82
+ | `IMG` | Represents an image (`img`) tag. |
83
+ | `LI` | Represents a list item (`li`) tag. |
84
+ | `P` | Represents a paragraph (`p`) tag. |
85
+ | `PRE` | Represents a preformatted text (`pre`) tag. |
86
+ | `TD` | Represents table data (`td`) tag. |
87
+ | `TR` | Represents a table row (`tr`) tag. |
88
+
54
89
  ## Development
55
90
 
56
91
  To start a local development server, run:
@@ -1,7 +1,9 @@
1
- declare const Tags: {
1
+ declare const Tag: {
2
2
  readonly A: "a";
3
+ readonly BLOCKQUOTE: "blockquote";
3
4
  readonly BR: "br";
4
5
  readonly HEADING: "heading";
6
+ readonly HR: "hr";
5
7
  readonly IMG: "img";
6
8
  readonly LI: "li";
7
9
  readonly P: "p";
@@ -9,4 +11,4 @@ declare const Tags: {
9
11
  readonly TD: "td";
10
12
  readonly TR: "tr";
11
13
  };
12
- export default Tags;
14
+ export default Tag;
@@ -1,2 +1,2 @@
1
- import { default as Tags } from './Tags';
2
- export { Tags, };
1
+ import { default as Tag } from './Tag';
2
+ export { Tag, };
@@ -1,19 +1,18 @@
1
1
  import { Element } from '../types';
2
- interface ConverterOptions {
3
- onConvert?: (element: Element) => Element | undefined;
4
- }
2
+ type ConvertHandler = (element: Element) => Element | undefined;
5
3
  declare class Converter {
4
+ private data;
6
5
  private elements;
7
6
  private onConvert;
8
- constructor(data: unknown, options?: ConverterOptions);
7
+ constructor(data: unknown);
9
8
  /**
10
9
  * Converts the provided data into Markdown format.
11
10
  */
12
- static toMarkdown(data: unknown, options?: ConverterOptions): string;
11
+ static toMarkdown(data: unknown, callback?: ConvertHandler): string;
13
12
  /**
14
13
  * Converts the provided data into Markdown format.
15
14
  */
16
- toMarkdown(): string;
15
+ toMarkdown(callback?: ConvertHandler): string;
17
16
  getElements(): Element[];
18
17
  private pushElement;
19
18
  private convert;
package/dist/index.js CHANGED
@@ -1,10 +1,12 @@
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 c = (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) => (c(n, typeof r != "symbol" ? r + "" : r, t), t);
4
4
  const e = {
5
5
  A: "a",
6
+ BLOCKQUOTE: "blockquote",
6
7
  BR: "br",
7
8
  HEADING: "heading",
9
+ HR: "hr",
8
10
  IMG: "img",
9
11
  LI: "li",
10
12
  P: "p",
@@ -12,58 +14,68 @@ const e = {
12
14
  TD: "td",
13
15
  TR: "tr"
14
16
  };
15
- class c {
16
- constructor(r, t = {}) {
17
+ class a {
18
+ constructor(r) {
19
+ o(this, "data");
17
20
  o(this, "elements", []);
18
- o(this, "onConvert");
19
- this.onConvert = t.onConvert ?? ((i) => i), r && this.convert(r);
21
+ o(this, "onConvert", (r) => r);
22
+ this.data = typeof r == "string" ? JSON.parse(r) : r;
20
23
  }
21
24
  /**
22
25
  * Converts the provided data into Markdown format.
23
26
  */
24
- static toMarkdown(r, t = {}) {
25
- return new c(r, t).toMarkdown();
27
+ static toMarkdown(r, t) {
28
+ return new a(r).toMarkdown(t);
26
29
  }
27
30
  /**
28
31
  * Converts the provided data into Markdown format.
29
32
  */
30
- toMarkdown() {
31
- return this.elements.map((r) => {
32
- switch (r.tag) {
33
+ toMarkdown(r) {
34
+ return this.convert(r), this.elements.map((t) => {
35
+ switch (t.tag) {
33
36
  case e.A:
34
- return `[${r.value}](${r.href})
37
+ return `[${t.value}](${t.href})
38
+
39
+ `;
40
+ case e.BLOCKQUOTE:
41
+ return `> ${t.value}
35
42
 
36
43
  `;
37
44
  case e.BR:
38
45
  return `
39
46
  `;
40
47
  case e.HEADING:
41
- return `${"#".repeat(Math.max(1, Math.min(6, r.level)))} ${r.value}
48
+ return `${"#".repeat(Math.max(1, Math.min(6, t.level)))} ${t.value}
49
+
50
+ `;
51
+ case e.HR:
52
+ return `---
42
53
 
43
54
  `;
44
55
  case e.IMG:
45
- return `![${r.alt}](${r.src})
56
+ return `![${t.alt}](${t.src})
46
57
 
47
58
  `;
48
59
  case e.LI:
49
- return `${" ".repeat(r.indent)}- ${r.value}
60
+ return `${" ".repeat(t.indent)}- ${t.value}
50
61
  `;
51
62
  case e.P:
52
- return `${r.value}
63
+ return `${t.value}
53
64
 
54
65
  `;
55
66
  case e.PRE:
56
67
  return `\`\`\`
57
- ${r.value}
68
+ ${t.value}
58
69
  \`\`\`
59
70
 
60
71
  `;
61
72
  case e.TD:
62
- return `| ${r.values.join(" | ")} |
73
+ return `| ${t.values.map((s) => typeof s == "string" ? s.replaceAll(`
74
+ `, "<br>") : s).join(" | ")} |
63
75
  `;
64
76
  case e.TR:
65
- return `| ${r.values.join(" | ")} |
66
- | ${r.values.map(() => "---").join(" | ")} |
77
+ return `| ${t.values.join(" | ")} |
78
+ | ${t.values.map(() => "---").join(" | ")} |
67
79
  `;
68
80
  default:
69
81
  return "";
@@ -78,35 +90,31 @@ ${r.value}
78
90
  return t ? (this.elements.push(t), !0) : !1;
79
91
  }
80
92
  convert(r) {
81
- if (Array.isArray(r)) {
82
- this.convertFromArray(r);
83
- return;
84
- }
85
- this.convertFromObject(r);
93
+ return r && (this.onConvert = r), Array.isArray(this.data) && this.convertFromArray(this.data), this.convertFromObject(this.data), this;
86
94
  }
87
95
  convertFromArray(r, t = 0) {
88
96
  if (r == null)
89
97
  return this.convertFromPrimitive(r), this;
90
- if (r.length > 0 && r.every((i) => typeof i == "object" && i !== null && Object.keys(i).length > 0)) {
91
- const [i] = r;
98
+ if (r.length > 0 && r.every((s) => typeof s == "object" && s !== null && Object.keys(s).length > 0)) {
99
+ const [s] = r;
92
100
  return this.pushElement({
93
101
  tag: e.TR,
94
- values: Object.keys(i)
95
- }), r.forEach((s) => {
102
+ values: Object.keys(s)
103
+ }), r.forEach((i) => {
96
104
  this.pushElement({
97
105
  tag: e.TD,
98
- values: Object.values(s).map((u) => this.formatValue(u))
106
+ values: Object.values(i).map((u) => this.formatValue(u))
99
107
  });
100
108
  }), this;
101
109
  }
102
- for (const i of r) {
103
- if (Array.isArray(i)) {
104
- this.convertFromArray(i, t + 1);
110
+ for (const s of r) {
111
+ if (Array.isArray(s)) {
112
+ this.convertFromArray(s, t + 1);
105
113
  continue;
106
114
  }
107
115
  this.pushElement({
108
116
  tag: e.LI,
109
- value: this.formatValue(i),
117
+ value: this.formatValue(s),
110
118
  indent: t
111
119
  });
112
120
  }
@@ -115,23 +123,23 @@ ${r.value}
115
123
  convertFromObject(r, t = 0) {
116
124
  if (r == null)
117
125
  return this.convertFromPrimitive(r), this;
118
- for (const [i, s] of Object.entries(r))
126
+ for (const [s, i] of Object.entries(r))
119
127
  if (this.pushElement({
120
128
  tag: e.HEADING,
121
- value: i,
129
+ value: s,
122
130
  level: t + 1
123
131
  })) {
124
- if (Array.isArray(s)) {
125
- this.convertFromArray(s), this.pushElement({
132
+ if (Array.isArray(i)) {
133
+ this.convertFromArray(i), this.pushElement({
126
134
  tag: e.BR
127
135
  });
128
136
  continue;
129
137
  }
130
- if (typeof s == "object") {
131
- this.convertFromObject(s, t + 1);
138
+ if (typeof i == "object") {
139
+ this.convertFromObject(i, t + 1);
132
140
  continue;
133
141
  }
134
- this.convertFromPrimitive(s);
142
+ this.convertFromPrimitive(i);
135
143
  }
136
144
  return this;
137
145
  }
@@ -146,5 +154,5 @@ ${r.value}
146
154
  }
147
155
  }
148
156
  export {
149
- c as Converter
157
+ a as Converter
150
158
  };
package/dist/index.umd.js CHANGED
@@ -1,18 +1,23 @@
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})
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 a=Object.defineProperty;var c=(n,r,s)=>r in n?a(n,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[r]=s;var u=(n,r,s)=>(c(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
- `;case t.BR:return`
4
- `;case t.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,r.level)))} ${r.value}
3
+ `;case r.BLOCKQUOTE:return`> ${e.value}
5
4
 
6
- `;case t.IMG:return`![${r.alt}](${r.src})
5
+ `;case r.BR:return`
6
+ `;case r.HEADING:return`${"#".repeat(Math.max(1,Math.min(6,e.level)))} ${e.value}
7
7
 
8
- `;case t.LI:return`${" ".repeat(r.indent)}- ${r.value}
9
- `;case t.P:return`${r.value}
8
+ `;case r.HR:return`---
10
9
 
11
- `;case t.PRE:return`\`\`\`
12
- ${r.value}
10
+ `;case r.IMG:return`![${e.alt}](${e.src})
11
+
12
+ `;case r.LI:return`${" ".repeat(e.indent)}- ${e.value}
13
+ `;case r.P:return`${e.value}
14
+
15
+ `;case r.PRE:return`\`\`\`
16
+ ${e.value}
13
17
  \`\`\`
14
18
 
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"})});
19
+ `;case r.TD:return`| ${e.values.map(i=>typeof i=="string"?i.replaceAll(`
20
+ `,"<br>"):i).join(" | ")} |
21
+ `;case r.TR:return`| ${e.values.join(" | ")} |
22
+ | ${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(i=>typeof i=="object"&&i!==null&&Object.keys(i).length>0)){const[i]=t;return this.pushElement({tag:r.TR,values:Object.keys(i)}),t.forEach(o=>{this.pushElement({tag:r.TD,values:Object.values(o).map(h=>this.formatValue(h))})}),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():t}}n.Converter=s,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
@@ -1,43 +1,50 @@
1
- import { default as Tags } from '../constants/Tags';
1
+ import { Tag } from '../constants';
2
2
  import { default as HeadingLevel } from './HeadingLevel';
3
3
  export interface AnchorElement {
4
- tag: typeof Tags.A;
4
+ tag: typeof Tag.A;
5
5
  href: string;
6
6
  value: string;
7
7
  }
8
+ export interface BlockquoteElement {
9
+ tag: typeof Tag.BLOCKQUOTE;
10
+ value: string;
11
+ }
8
12
  export interface BreakElement {
9
- tag: typeof Tags.BR;
13
+ tag: typeof Tag.BR;
10
14
  }
11
15
  export interface HeadingElement {
12
- tag: typeof Tags.HEADING;
16
+ tag: typeof Tag.HEADING;
13
17
  value: string;
14
18
  level: HeadingLevel;
15
19
  }
20
+ export interface HorizontalRuleElement {
21
+ tag: typeof Tag.HR;
22
+ }
16
23
  export interface ImageElement {
17
- tag: typeof Tags.IMG;
24
+ tag: typeof Tag.IMG;
18
25
  src: string;
19
26
  alt: string;
20
27
  }
21
28
  export interface ListItemElement {
22
- tag: typeof Tags.LI;
29
+ tag: typeof Tag.LI;
23
30
  value: unknown;
24
31
  indent: number;
25
32
  }
26
33
  export interface ParagraphElement {
27
- tag: typeof Tags.P;
34
+ tag: typeof Tag.P;
28
35
  value: unknown;
29
36
  }
30
37
  export interface PreformattedTextElement {
31
- tag: typeof Tags.PRE;
38
+ tag: typeof Tag.PRE;
32
39
  value: string;
33
40
  }
34
41
  export interface TableDataElement {
35
- tag: typeof Tags.TD;
42
+ tag: typeof Tag.TD;
36
43
  values: unknown[];
37
44
  }
38
45
  export interface TableRowElement {
39
- tag: typeof Tags.TR;
46
+ tag: typeof Tag.TR;
40
47
  values: unknown[];
41
48
  }
42
- type Element = AnchorElement | BreakElement | HeadingElement | ImageElement | ListItemElement | ParagraphElement | PreformattedTextElement | TableDataElement | TableRowElement;
49
+ type Element = AnchorElement | BlockquoteElement | BreakElement | HeadingElement | HorizontalRuleElement | ImageElement | ListItemElement | ParagraphElement | PreformattedTextElement | TableDataElement | TableRowElement;
43
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.17",
4
+ "version": "0.0.19",
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.14",
17
19
  "@stylistic/eslint-plugin": "^2.9.0",
18
20
  "@types/eslint__js": "^8.42.3",
19
21
  "@types/node": "^22.5.5",