@kklab/json2markdown 0.0.18 → 0.0.20

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
- var h = Object.defineProperty;
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);
1
+ var c = Object.defineProperty;
2
+ var f = (n, r, t) => r in n ? c(n, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[r] = t;
3
+ var o = (n, r, t) => (f(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,59 +14,68 @@ const e = {
12
14
  TD: "td",
13
15
  TR: "tr"
14
16
  };
15
- class c {
16
- constructor(r, t = {}) {
17
+ class h {
18
+ constructor(r) {
19
+ o(this, "data");
17
20
  o(this, "elements", []);
18
- o(this, "onConvert");
19
- this.onConvert = t.onConvert ?? ((n) => n), 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 h(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.map((t) => typeof t == "string" ? t.replaceAll(`
63
- `, "<br>") : t).join(" | ")} |
73
+ return `| ${t.values.map((s) => typeof s == "string" ? s.replaceAll(`
74
+ `, "<br>") : s).join(" | ")} |
64
75
  `;
65
76
  case e.TR:
66
- return `| ${r.values.join(" | ")} |
67
- | ${r.values.map(() => "---").join(" | ")} |
77
+ return `| ${t.values.join(" | ")} |
78
+ | ${t.values.map(() => "---").join(" | ")} |
68
79
  `;
69
80
  default:
70
81
  return "";
@@ -79,35 +90,33 @@ ${r.value}
79
90
  return t ? (this.elements.push(t), !0) : !1;
80
91
  }
81
92
  convert(r) {
82
- if (Array.isArray(r)) {
83
- this.convertFromArray(r);
84
- return;
85
- }
86
- this.convertFromObject(r);
93
+ return r && (this.onConvert = r), Array.isArray(this.data) && this.convertFromArray(this.data), this.convertFromObject(this.data), this;
87
94
  }
88
95
  convertFromArray(r, t = 0) {
89
96
  if (r == null)
90
97
  return this.convertFromPrimitive(r), this;
91
- if (r.length > 0 && r.every((n) => typeof n == "object" && n !== null && Object.keys(n).length > 0)) {
92
- const [n] = r;
93
- return this.pushElement({
98
+ if (r.length > 0 && r.every((s) => typeof s == "object" && s !== null && Object.keys(s).length > 0)) {
99
+ const [s] = r;
100
+ this.pushElement({
94
101
  tag: e.TR,
95
- values: Object.keys(n)
96
- }), r.forEach((i) => {
102
+ values: Object.keys(s)
103
+ });
104
+ const i = Object.keys(s);
105
+ return r.forEach((u) => {
97
106
  this.pushElement({
98
107
  tag: e.TD,
99
- values: Object.values(i).map((u) => this.formatValue(u))
108
+ values: i.map((a) => this.formatValue(u[a]))
100
109
  });
101
110
  }), this;
102
111
  }
103
- for (const n of r) {
104
- if (Array.isArray(n)) {
105
- this.convertFromArray(n, t + 1);
112
+ for (const s of r) {
113
+ if (Array.isArray(s)) {
114
+ this.convertFromArray(s, t + 1);
106
115
  continue;
107
116
  }
108
117
  this.pushElement({
109
118
  tag: e.LI,
110
- value: this.formatValue(n),
119
+ value: this.formatValue(s),
111
120
  indent: t
112
121
  });
113
122
  }
@@ -116,10 +125,10 @@ ${r.value}
116
125
  convertFromObject(r, t = 0) {
117
126
  if (r == null)
118
127
  return this.convertFromPrimitive(r), this;
119
- for (const [n, i] of Object.entries(r))
128
+ for (const [s, i] of Object.entries(r))
120
129
  if (this.pushElement({
121
130
  tag: e.HEADING,
122
- value: n,
131
+ value: s,
123
132
  level: t + 1
124
133
  })) {
125
134
  if (Array.isArray(i)) {
@@ -147,5 +156,5 @@ ${r.value}
147
156
  }
148
157
  }
149
158
  export {
150
- c as Converter
159
+ h as Converter
151
160
  };
package/dist/index.umd.js CHANGED
@@ -1,19 +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(i,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(i=typeof globalThis<"u"?globalThis:i||self,r(i.JSON2Markdown={}))})(this,function(i){"use strict";var a=Object.defineProperty;var f=(i,r,s)=>r in i?a(i,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[r]=s;var u=(i,r,s)=>(f(i,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.map(e=>typeof e=="string"?e.replaceAll(`
16
- `,"<br>"):e).join(" | ")} |
17
- `;case t.TR:return`| ${r.values.join(" | ")} |
18
- | ${r.values.map(()=>"---").join(" | ")} |
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"})});
19
+ `;case r.TD:return`| ${e.values.map(n=>typeof n=="string"?n.replaceAll(`
20
+ `,"<br>"):n).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(n=>typeof n=="object"&&n!==null&&Object.keys(n).length>0)){const[n]=t;this.pushElement({tag:r.TR,values:Object.keys(n)});const o=Object.keys(n);return t.forEach(c=>{this.pushElement({tag:r.TD,values:o.map(h=>this.formatValue(c[h]))})}),this}for(const n of t){if(Array.isArray(n)){this.convertFromArray(n,e+1);continue}this.pushElement({tag:r.LI,value:this.formatValue(n),indent:e})}return this}convertFromObject(t,e=0){if(t==null)return this.convertFromPrimitive(t),this;for(const[n,o]of Object.entries(t))if(this.pushElement({tag:r.HEADING,value:n,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}}i.Converter=s,Object.defineProperty(i,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.18",
4
+ "version": "0.0.20",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "devDependencies": {
17
17
  "@eslint/js": "^9.11.0",
18
- "@kklab/stryle": "^0.0.10",
18
+ "@kklab/stryle": "^0.0.14",
19
19
  "@stylistic/eslint-plugin": "^2.9.0",
20
20
  "@types/eslint__js": "^8.42.3",
21
21
  "@types/node": "^22.5.5",