@kklab/json2markdown 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 KKLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # json2markdown
2
+
3
+ A simple JSON to Markdown converter that transforms JSON data into easily readable text.
4
+
5
+ ## Installation
6
+
7
+ To install the package, run:
8
+
9
+ ```bash
10
+ npm install @memochou1993/json2markdown
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Using with ES Modules
16
+
17
+ Import the converter and use it in your code:
18
+
19
+ ```js
20
+ import { Converter } from '@memochou1993/json2markdown';
21
+
22
+ const markdown = Converter.toMarkdown({
23
+ title: 'Hello, World!',
24
+ });
25
+
26
+ // Output:
27
+ // # Title
28
+ //
29
+ // Hello, World!
30
+ ```
31
+
32
+ ### Using with UMD Modules
33
+
34
+ Include the UMD script in your HTML file and use it:
35
+
36
+ ```js
37
+ const markdown = window.JSON2MD.Converter.toMarkdown({
38
+ title: 'Hello, World!',
39
+ });
40
+
41
+ // Output:
42
+ // # Title
43
+ //
44
+ // Hello, World!
45
+ ```
46
+
47
+ ## Development
48
+
49
+ To start a local development server, run:
50
+
51
+ ```bash
52
+ npm run dev
53
+ ```
54
+
55
+ ## Testing
56
+
57
+ To run the tests for this package, run:
58
+
59
+ ```bash
60
+ npm run test
61
+ ```
@@ -0,0 +1,6 @@
1
+ import { MarkdownSchema } from './types';
2
+ declare class Converter {
3
+ static toMarkdown(data: Record<string, unknown>): string;
4
+ static toMarkdownSchema(data: Record<string, unknown>, level?: number, schema?: MarkdownSchema[]): MarkdownSchema[];
5
+ }
6
+ export default Converter;
@@ -0,0 +1,2 @@
1
+ import { default as Converter } from './Converter';
2
+ export { Converter, };
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ const c = /^(a|an|and|as|at|but|by|for|if|in|is|nor|of|on|or|the|to|with)$/i, f = (u) => u.replace(/[_-]/g, " ").split(" ").map((n, i) => i === 0 || !c.test(n) ? `${n.charAt(0).toUpperCase()}${n.slice(1).toLowerCase()}` : n.toLowerCase()).join(" ");
2
+ class p {
3
+ static toMarkdown(n) {
4
+ const i = Array.from({ length: 6 }, (t, a) => `h${a + 1}`);
5
+ return p.toMarkdownSchema(n).map((t) => {
6
+ const a = i.findIndex((o) => t[o]);
7
+ return a !== -1 ? `${"#".repeat(a + 1)} ${f(t[i[a]])}
8
+
9
+ ` : t.ul ? `${t.ul.map((o) => `- ${o}
10
+ `).join("")}
11
+ ` : typeof t.p == "boolean" ? `${f(String(t.p))}
12
+
13
+ ` : `${t.p}
14
+
15
+ `;
16
+ }).join("");
17
+ }
18
+ static toMarkdownSchema(n, i = 1, t = []) {
19
+ if (!n)
20
+ return [];
21
+ const a = `h${Math.min(i, 6)}`;
22
+ for (let [o, r] of Object.entries(n))
23
+ if (!o.startsWith("_")) {
24
+ if (o = o.trim(), typeof r == "string" && (r = r.trim().replaceAll("\\n", `
25
+ `)), t.push({ [a]: o }), Array.isArray(r)) {
26
+ const [e] = r;
27
+ if (Array.isArray(e) && e.length > 0) {
28
+ t.push({ ul: r.map((s) => JSON.stringify(s)) });
29
+ continue;
30
+ }
31
+ if (typeof e == "object") {
32
+ p.toMarkdownSchema(e, i + 1, t);
33
+ continue;
34
+ }
35
+ t.push({ ul: r.map((s) => typeof s == "string" ? s : JSON.stringify(s)) });
36
+ continue;
37
+ }
38
+ if (typeof r == "object") {
39
+ p.toMarkdownSchema(r, i + 1, t);
40
+ continue;
41
+ }
42
+ t.push({ p: r });
43
+ }
44
+ return t;
45
+ }
46
+ }
47
+ export {
48
+ p as Converter
49
+ };
@@ -0,0 +1,10 @@
1
+ (function(f,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(f=typeof globalThis<"u"?globalThis:f||self,s(f.JSON2MD={}))})(this,function(f){"use strict";const s=/^(a|an|and|as|at|but|by|for|if|in|is|nor|of|on|or|the|to|with)$/i,c=y=>y.replace(/[_-]/g," ").split(" ").map((i,o)=>o===0||!s.test(i)?`${i.charAt(0).toUpperCase()}${i.slice(1).toLowerCase()}`:i.toLowerCase()).join(" ");class a{static toMarkdown(i){const o=Array.from({length:6},(t,r)=>`h${r+1}`);return a.toMarkdownSchema(i).map(t=>{const r=o.findIndex(e=>t[e]);return r!==-1?`${"#".repeat(r+1)} ${c(t[o[r]])}
2
+
3
+ `:t.ul?`${t.ul.map(e=>`- ${e}
4
+ `).join("")}
5
+ `:typeof t.p=="boolean"?`${c(String(t.p))}
6
+
7
+ `:`${t.p}
8
+
9
+ `}).join("")}static toMarkdownSchema(i,o=1,t=[]){if(!i)return[];const r=`h${Math.min(o,6)}`;for(let[e,n]of Object.entries(i))if(!e.startsWith("_")){if(e=e.trim(),typeof n=="string"&&(n=n.trim().replaceAll("\\n",`
10
+ `)),t.push({[r]:e}),Array.isArray(n)){const[p]=n;if(Array.isArray(p)&&p.length>0){t.push({ul:n.map(u=>JSON.stringify(u))});continue}if(typeof p=="object"){a.toMarkdownSchema(p,o+1,t);continue}t.push({ul:n.map(u=>typeof u=="string"?u:JSON.stringify(u))});continue}if(typeof n=="object"){a.toMarkdownSchema(n,o+1,t);continue}t.push({p:n})}return t}}f.Converter=a,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,12 @@
1
+ interface MarkdownSchema {
2
+ [key: string]: string | string[] | boolean | undefined;
3
+ h1?: string;
4
+ h2?: string;
5
+ h3?: string;
6
+ h4?: string;
7
+ h5?: string;
8
+ h6?: string;
9
+ p?: string | boolean;
10
+ ul?: string[];
11
+ }
12
+ export default MarkdownSchema;
@@ -0,0 +1,2 @@
1
+ import { default as MarkdownSchema } from './MarkdownSchema';
2
+ export type { MarkdownSchema, };
@@ -0,0 +1,2 @@
1
+ import { default as toTitleCase } from './toTitleCase';
2
+ export { toTitleCase, };
@@ -0,0 +1,2 @@
1
+ declare const toTitleCase: (str: string) => string;
2
+ export default toTitleCase;
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@kklab/json2markdown",
3
+ "private": false,
4
+ "version": "0.0.1",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "scripts": {
8
+ "dev": "vite",
9
+ "build": "tsc -p ./tsconfig.build.json && vite build",
10
+ "preview": "vite preview",
11
+ "lint": "eslint .",
12
+ "test": "vitest"
13
+ },
14
+ "devDependencies": {
15
+ "@eslint/js": "^9.11.0",
16
+ "@types/eslint__js": "^8.42.3",
17
+ "@types/node": "^22.5.5",
18
+ "eslint": "^9.11.0",
19
+ "globals": "^15.9.0",
20
+ "typescript": "^5.5.4",
21
+ "typescript-eslint": "^8.6.0",
22
+ "vite": "^4.4.5",
23
+ "vite-plugin-dts": "^4.2.1",
24
+ "vitest": "^2.1.1"
25
+ },
26
+ "main": "dist/index.js",
27
+ "types": "dist/index.d.ts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "exports": {
32
+ ".": {
33
+ "import": "./dist/index.js",
34
+ "require": "./dist/index.umd.js"
35
+ }
36
+ }
37
+ }