@kklab/json2markdown 0.0.7 → 0.0.8
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/constants/Tags.d.ts +9 -0
- package/dist/constants/index.d.ts +2 -0
- package/dist/converter/Converter.d.ts +18 -19
- package/dist/index.js +122 -55
- package/dist/index.umd.js +8 -8
- package/dist/types/Element.d.ts +29 -0
- package/dist/types/HeadingLevel.d.ts +2 -0
- package/dist/types/index.d.ts +3 -2
- package/package.json +3 -4
- package/dist/types/MarkdownSchema.d.ts +0 -12
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/toTitleCase.d.ts +0 -2
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Element } from '../types';
|
|
2
|
+
interface ConverterOptions {
|
|
3
|
+
onConvert?: (element: Element) => Element;
|
|
4
|
+
}
|
|
2
5
|
declare class Converter {
|
|
6
|
+
private elements;
|
|
7
|
+
private onConvert;
|
|
8
|
+
constructor(data: unknown, options?: ConverterOptions);
|
|
3
9
|
/**
|
|
4
|
-
* Converts
|
|
5
|
-
*
|
|
6
|
-
* This method processes the data and identifies headings, unordered lists,
|
|
7
|
-
* and paragraph elements, formatting them according to Markdown syntax.
|
|
8
|
-
*
|
|
9
|
-
* @param data - A record representing the input data to be converted.
|
|
10
|
-
* @returns A string formatted in Markdown based on the input data.
|
|
10
|
+
* Converts the provided data into Markdown format.
|
|
11
11
|
*/
|
|
12
|
-
static toMarkdown(data:
|
|
12
|
+
static toMarkdown(data: unknown, options?: ConverterOptions): string;
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* This method recursively traverses the data object to build a schema that
|
|
17
|
-
* identifies headings, lists, and paragraphs, which will later be converted to Markdown.
|
|
18
|
-
*
|
|
19
|
-
* @param data - A record representing the input data.
|
|
20
|
-
* @param level - The current heading level (default is 1).
|
|
21
|
-
* @param schema - An array that accumulates the schema entries (default is an empty array).
|
|
22
|
-
* @returns An array representing the schema for the Markdown conversion.
|
|
14
|
+
* Converts the provided data into Markdown format.
|
|
23
15
|
*/
|
|
24
|
-
|
|
16
|
+
toMarkdown(): string;
|
|
17
|
+
getElements(): Element[];
|
|
18
|
+
private pushElement;
|
|
19
|
+
private convert;
|
|
20
|
+
private convertFromArray;
|
|
21
|
+
private convertFromObject;
|
|
22
|
+
private convertFromPrimitive;
|
|
23
|
+
private formatValue;
|
|
25
24
|
}
|
|
26
25
|
export default Converter;
|
package/dist/index.js
CHANGED
|
@@ -1,69 +1,136 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var m = Object.defineProperty;
|
|
2
|
+
var l = (o, t, e) => t in o ? m(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
|
|
3
|
+
var s = (o, t, e) => (l(o, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
const r = {
|
|
5
|
+
BR: "br",
|
|
6
|
+
HEADING: "heading",
|
|
7
|
+
LI: "li",
|
|
8
|
+
P: "p",
|
|
9
|
+
TD: "td",
|
|
10
|
+
TR: "tr"
|
|
11
|
+
};
|
|
12
|
+
class a {
|
|
13
|
+
constructor(t, e = {}) {
|
|
14
|
+
s(this, "elements", []);
|
|
15
|
+
s(this, "onConvert");
|
|
16
|
+
this.onConvert = e.onConvert ?? ((n) => n), this.convert(t);
|
|
17
|
+
}
|
|
3
18
|
/**
|
|
4
|
-
* Converts
|
|
5
|
-
*
|
|
6
|
-
* This method processes the data and identifies headings, unordered lists,
|
|
7
|
-
* and paragraph elements, formatting them according to Markdown syntax.
|
|
8
|
-
*
|
|
9
|
-
* @param data - A record representing the input data to be converted.
|
|
10
|
-
* @returns A string formatted in Markdown based on the input data.
|
|
19
|
+
* Converts the provided data into Markdown format.
|
|
11
20
|
*/
|
|
12
|
-
static toMarkdown(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
static toMarkdown(t, e = {}) {
|
|
22
|
+
return new a(t, e).toMarkdown();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Converts the provided data into Markdown format.
|
|
26
|
+
*/
|
|
27
|
+
toMarkdown() {
|
|
28
|
+
return this.elements.map((t) => {
|
|
29
|
+
switch (t.tag) {
|
|
30
|
+
case r.BR:
|
|
31
|
+
return `
|
|
32
|
+
`;
|
|
33
|
+
case r.HEADING:
|
|
34
|
+
return `${"#".repeat(Math.max(1, Math.min(6, t.level)))} ${t.value}
|
|
21
35
|
|
|
22
|
-
|
|
36
|
+
`;
|
|
37
|
+
case r.LI:
|
|
38
|
+
return `${" ".repeat(t.indent)}- ${t.value}
|
|
39
|
+
`;
|
|
40
|
+
case r.P:
|
|
41
|
+
return `${t.value}
|
|
23
42
|
|
|
24
43
|
`;
|
|
44
|
+
case r.TD:
|
|
45
|
+
return `| ${t.values.join(" | ")} |
|
|
46
|
+
`;
|
|
47
|
+
case r.TR:
|
|
48
|
+
return `| ${t.values.map((e) => e).join(" | ")} |
|
|
49
|
+
${t.values.map(() => "| ---").join(" ")} |
|
|
50
|
+
`;
|
|
51
|
+
default:
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
25
54
|
}).join("");
|
|
26
55
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
getElements() {
|
|
57
|
+
return this.elements;
|
|
58
|
+
}
|
|
59
|
+
pushElement(t) {
|
|
60
|
+
return this.elements.push(this.onConvert(t)), this;
|
|
61
|
+
}
|
|
62
|
+
convert(t) {
|
|
63
|
+
if (t) {
|
|
64
|
+
if (Array.isArray(t)) {
|
|
65
|
+
this.convertFromArray(t);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (typeof t == "object") {
|
|
69
|
+
this.convertFromObject(t);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.convertFromPrimitive(t);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
convertFromArray(t, e = 0) {
|
|
76
|
+
for (const n of t) {
|
|
77
|
+
if (Array.isArray(n)) {
|
|
78
|
+
this.convertFromArray(n, e + 1);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
this.pushElement({
|
|
82
|
+
tag: r.LI,
|
|
83
|
+
value: this.formatValue(n),
|
|
84
|
+
indent: e
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
convertFromObject(t, e = 0) {
|
|
90
|
+
for (const [n, i] of Object.entries(t)) {
|
|
91
|
+
if (this.pushElement({
|
|
92
|
+
tag: r.HEADING,
|
|
93
|
+
value: n,
|
|
94
|
+
level: e + 1
|
|
95
|
+
}), Array.isArray(i)) {
|
|
96
|
+
const [u] = i;
|
|
97
|
+
if (typeof u == "object") {
|
|
98
|
+
this.pushElement({
|
|
99
|
+
tag: r.TR,
|
|
100
|
+
values: Object.keys(u)
|
|
101
|
+
}), i.forEach((c) => {
|
|
102
|
+
this.pushElement({
|
|
103
|
+
tag: r.TD,
|
|
104
|
+
values: Object.values(c).map((h) => this.formatValue(h))
|
|
105
|
+
});
|
|
106
|
+
}), this.pushElement({
|
|
107
|
+
tag: r.BR
|
|
108
|
+
});
|
|
60
109
|
continue;
|
|
61
110
|
}
|
|
62
|
-
|
|
111
|
+
this.convertFromArray(i), this.pushElement({
|
|
112
|
+
tag: r.BR
|
|
113
|
+
});
|
|
114
|
+
continue;
|
|
63
115
|
}
|
|
64
|
-
|
|
116
|
+
if (typeof i == "object") {
|
|
117
|
+
this.convertFromObject(i, e + 1);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
this.convertFromPrimitive(i);
|
|
121
|
+
}
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
convertFromPrimitive(t) {
|
|
125
|
+
return this.pushElement({
|
|
126
|
+
tag: r.P,
|
|
127
|
+
value: this.formatValue(t)
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
formatValue(t) {
|
|
131
|
+
return Array.isArray(t) ? t.map((e) => this.formatValue(e)).join(", ") : typeof t == "object" ? JSON.stringify(t) : String(t).trim();
|
|
65
132
|
}
|
|
66
133
|
}
|
|
67
134
|
export {
|
|
68
|
-
|
|
135
|
+
a as Converter
|
|
69
136
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
(function(
|
|
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 l=Object.defineProperty;var h=(n,e,i)=>e in n?l(n,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):n[e]=i;var u=(n,e,i)=>(h(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={}){u(this,"elements",[]);u(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}
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
`:typeof t.p=="boolean"?`${c(String(t.p))}
|
|
4
|
+
`;case e.LI:return`${" ".repeat(t.indent)}- ${t.value}
|
|
5
|
+
`;case e.P:return`${t.value}
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
`;case e.TD:return`| ${t.values.join(" | ")} |
|
|
8
|
+
`;case e.TR:return`| ${t.values.map(r=>r).join(" | ")} |
|
|
9
|
+
${t.values.map(()=>"| ---").join(" ")} |
|
|
10
|
+
`;default:return""}}).join("")}getElements(){return this.elements}pushElement(t){return this.elements.push(this.onConvert(t)),this}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){for(const o of t){if(Array.isArray(o)){this.convertFromArray(o,r+1);continue}this.pushElement({tag:e.LI,value:this.formatValue(o),indent:r})}return this}convertFromObject(t,r=0){for(const[o,s]of Object.entries(t)){if(this.pushElement({tag:e.HEADING,value:o,level:r+1}),Array.isArray(s)){const[a]=s;if(typeof a=="object"){this.pushElement({tag:e.TR,values:Object.keys(a)}),s.forEach(c=>{this.pushElement({tag:e.TD,values:Object.values(c).map(f=>this.formatValue(f))})}),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 this.pushElement({tag:e.P,value:this.formatValue(t)})}formatValue(t){return 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"})});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { default as Tags } from '../constants/Tags';
|
|
2
|
+
import { default as HeadingLevel } from './HeadingLevel';
|
|
3
|
+
export interface BreakElement {
|
|
4
|
+
tag: typeof Tags.BR;
|
|
5
|
+
}
|
|
6
|
+
export interface HeadingElement {
|
|
7
|
+
tag: typeof Tags.HEADING;
|
|
8
|
+
value: string;
|
|
9
|
+
level: HeadingLevel;
|
|
10
|
+
}
|
|
11
|
+
export interface ListItemElement {
|
|
12
|
+
tag: typeof Tags.LI;
|
|
13
|
+
value: string;
|
|
14
|
+
indent: number;
|
|
15
|
+
}
|
|
16
|
+
export interface ParagraphElement {
|
|
17
|
+
tag: typeof Tags.P;
|
|
18
|
+
value: string;
|
|
19
|
+
}
|
|
20
|
+
export interface TableDataElement {
|
|
21
|
+
tag: typeof Tags.TD;
|
|
22
|
+
values: string[];
|
|
23
|
+
}
|
|
24
|
+
export interface TableRowElement {
|
|
25
|
+
tag: typeof Tags.TR;
|
|
26
|
+
values: string[];
|
|
27
|
+
}
|
|
28
|
+
type Element = BreakElement | HeadingElement | ListItemElement | ParagraphElement | TableDataElement | TableRowElement;
|
|
29
|
+
export default Element;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { default as
|
|
2
|
-
|
|
1
|
+
import { default as Element } from './Element';
|
|
2
|
+
import { default as HeadingLevel } from './HeadingLevel';
|
|
3
|
+
export type { Element, HeadingLevel, };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kklab/json2markdown",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.8",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
@@ -11,11 +11,10 @@
|
|
|
11
11
|
"lint": "eslint .",
|
|
12
12
|
"test": "vitest"
|
|
13
13
|
},
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"@kklab/stryle": "^0.0.1"
|
|
16
|
-
},
|
|
17
14
|
"devDependencies": {
|
|
18
15
|
"@eslint/js": "^9.11.0",
|
|
16
|
+
"@kklab/stryle": "^0.0.2",
|
|
17
|
+
"@stylistic/eslint-plugin": "^2.9.0",
|
|
19
18
|
"@types/eslint__js": "^8.42.3",
|
|
20
19
|
"@types/node": "^22.5.5",
|
|
21
20
|
"eslint": "^9.11.0",
|
package/dist/utils/index.d.ts
DELETED