@omer-x/md-to-json 1.0.0
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.cjs +159 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +122 -0
- package/package.json +58 -0
package/dist/index.cjs
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// src/index.ts
|
31
|
+
var src_exports = {};
|
32
|
+
__export(src_exports, {
|
33
|
+
convertMarkdownToJSON: () => convertMarkdownToJSON,
|
34
|
+
default: () => src_default
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
37
|
+
|
38
|
+
// src/core/convertMarkdownToJSON.ts
|
39
|
+
var import_markdown_it = __toESM(require("markdown-it"), 1);
|
40
|
+
|
41
|
+
// src/core/getTokenCloserType.ts
|
42
|
+
function getTokenCloserType(tokenType) {
|
43
|
+
switch (tokenType) {
|
44
|
+
case "bullet_list_open":
|
45
|
+
return "bullet_list_close";
|
46
|
+
case "list_item_open":
|
47
|
+
return "list_item_close";
|
48
|
+
case "paragraph_open":
|
49
|
+
return "paragraph_close";
|
50
|
+
case "heading_open":
|
51
|
+
return "heading_close";
|
52
|
+
case "fence":
|
53
|
+
return "fence";
|
54
|
+
default:
|
55
|
+
return null;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
// src/core/findNextCloserIndex.ts
|
60
|
+
function findNextCloserIndex(tokens) {
|
61
|
+
const nextToken = tokens[0];
|
62
|
+
if (!nextToken) return 0;
|
63
|
+
const closerType = getTokenCloserType(nextToken.type);
|
64
|
+
if (!closerType) throw new Error("UNKNOWN_CLOSER_TYPE", { cause: nextToken.type });
|
65
|
+
const closerIndex = tokens.findIndex((t) => t.type === closerType);
|
66
|
+
return Math.max(0, closerIndex);
|
67
|
+
}
|
68
|
+
|
69
|
+
// src/core/chunkifyTokens.ts
|
70
|
+
function chunkifyTokens(tokens) {
|
71
|
+
const clone = [...tokens];
|
72
|
+
const slices = [];
|
73
|
+
while (clone.length) {
|
74
|
+
const slice = clone.splice(0, findNextCloserIndex(clone) + 1);
|
75
|
+
slices.push(slice);
|
76
|
+
}
|
77
|
+
return slices;
|
78
|
+
}
|
79
|
+
|
80
|
+
// src/core/getContentOfTokenChunk.ts
|
81
|
+
function getContentOfTokenChunk(chunk) {
|
82
|
+
if (chunk[0]?.type === "fence") {
|
83
|
+
return [chunk[0].content];
|
84
|
+
}
|
85
|
+
const internalPart = chunk.slice(1, -1);
|
86
|
+
if (internalPart.length < 1) throw new Error("IRREGULAR_CHUNK", { cause: chunk });
|
87
|
+
if (internalPart.length > 1) {
|
88
|
+
const tokenChunks = chunkifyTokens(internalPart);
|
89
|
+
return [tokenChunks.map((subChunk) => {
|
90
|
+
return getContentOfTokenChunk(subChunk).join("\n");
|
91
|
+
})];
|
92
|
+
} else {
|
93
|
+
const token = internalPart[0];
|
94
|
+
if (token.type === "inline") {
|
95
|
+
return [token.content];
|
96
|
+
} else {
|
97
|
+
throw new Error("IRREGULAR_CHUNK", { cause: chunk });
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
// src/core/bundleChunks.ts
|
103
|
+
function bundleChunks(bundle, chunk) {
|
104
|
+
const opener = chunk[0];
|
105
|
+
if (!opener) throw new Error("EMPTY_CHUNK", { cause: chunk });
|
106
|
+
if (opener.type === "heading_open") {
|
107
|
+
const [title] = getContentOfTokenChunk(chunk);
|
108
|
+
return [
|
109
|
+
...bundle,
|
110
|
+
{ title, content: [] }
|
111
|
+
];
|
112
|
+
}
|
113
|
+
const newItems = getContentOfTokenChunk(chunk);
|
114
|
+
const lastSection = bundle[bundle.length - 1];
|
115
|
+
if (!lastSection) {
|
116
|
+
return [
|
117
|
+
{ content: [...newItems] }
|
118
|
+
];
|
119
|
+
}
|
120
|
+
return [
|
121
|
+
...bundle.slice(0, -1),
|
122
|
+
{
|
123
|
+
...lastSection,
|
124
|
+
content: [
|
125
|
+
...lastSection.content,
|
126
|
+
...newItems
|
127
|
+
]
|
128
|
+
}
|
129
|
+
];
|
130
|
+
}
|
131
|
+
|
132
|
+
// src/core/convertMarkdownToJSON.ts
|
133
|
+
function convertMarkdownToJSON(markdown) {
|
134
|
+
try {
|
135
|
+
const md = new import_markdown_it.default();
|
136
|
+
const tokens = md.parse(markdown, {});
|
137
|
+
const tokenChunks = chunkifyTokens(tokens);
|
138
|
+
return {
|
139
|
+
success: true,
|
140
|
+
sections: tokenChunks.reduce(bundleChunks, [])
|
141
|
+
};
|
142
|
+
} catch (error) {
|
143
|
+
if (error instanceof Error) {
|
144
|
+
return {
|
145
|
+
success: false,
|
146
|
+
errorCode: error.message,
|
147
|
+
errorCause: error.cause
|
148
|
+
};
|
149
|
+
}
|
150
|
+
throw error;
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
// src/index.ts
|
155
|
+
var src_default = convertMarkdownToJSON;
|
156
|
+
// Annotate the CommonJS export names for ESM import in node:
|
157
|
+
0 && (module.exports = {
|
158
|
+
convertMarkdownToJSON
|
159
|
+
});
|
package/dist/index.d.cts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
type Section = {
|
2
|
+
title?: string;
|
3
|
+
content: (string | string[])[];
|
4
|
+
};
|
5
|
+
|
6
|
+
type ConversionResult = {
|
7
|
+
success: true;
|
8
|
+
sections: Section[];
|
9
|
+
} | {
|
10
|
+
success: false;
|
11
|
+
errorCode: "UNKNOWN_CLOSER_TYPE" | "EMPTY_CHUNK" | "IRREGULAR_CHUNK";
|
12
|
+
errorCause?: unknown;
|
13
|
+
};
|
14
|
+
declare function convertMarkdownToJSON(markdown: string): ConversionResult;
|
15
|
+
|
16
|
+
export { convertMarkdownToJSON, convertMarkdownToJSON as default };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
type Section = {
|
2
|
+
title?: string;
|
3
|
+
content: (string | string[])[];
|
4
|
+
};
|
5
|
+
|
6
|
+
type ConversionResult = {
|
7
|
+
success: true;
|
8
|
+
sections: Section[];
|
9
|
+
} | {
|
10
|
+
success: false;
|
11
|
+
errorCode: "UNKNOWN_CLOSER_TYPE" | "EMPTY_CHUNK" | "IRREGULAR_CHUNK";
|
12
|
+
errorCause?: unknown;
|
13
|
+
};
|
14
|
+
declare function convertMarkdownToJSON(markdown: string): ConversionResult;
|
15
|
+
|
16
|
+
export { convertMarkdownToJSON, convertMarkdownToJSON as default };
|
package/dist/index.js
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
// src/core/convertMarkdownToJSON.ts
|
2
|
+
import MarkdownIt from "markdown-it";
|
3
|
+
|
4
|
+
// src/core/getTokenCloserType.ts
|
5
|
+
function getTokenCloserType(tokenType) {
|
6
|
+
switch (tokenType) {
|
7
|
+
case "bullet_list_open":
|
8
|
+
return "bullet_list_close";
|
9
|
+
case "list_item_open":
|
10
|
+
return "list_item_close";
|
11
|
+
case "paragraph_open":
|
12
|
+
return "paragraph_close";
|
13
|
+
case "heading_open":
|
14
|
+
return "heading_close";
|
15
|
+
case "fence":
|
16
|
+
return "fence";
|
17
|
+
default:
|
18
|
+
return null;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
// src/core/findNextCloserIndex.ts
|
23
|
+
function findNextCloserIndex(tokens) {
|
24
|
+
const nextToken = tokens[0];
|
25
|
+
if (!nextToken) return 0;
|
26
|
+
const closerType = getTokenCloserType(nextToken.type);
|
27
|
+
if (!closerType) throw new Error("UNKNOWN_CLOSER_TYPE", { cause: nextToken.type });
|
28
|
+
const closerIndex = tokens.findIndex((t) => t.type === closerType);
|
29
|
+
return Math.max(0, closerIndex);
|
30
|
+
}
|
31
|
+
|
32
|
+
// src/core/chunkifyTokens.ts
|
33
|
+
function chunkifyTokens(tokens) {
|
34
|
+
const clone = [...tokens];
|
35
|
+
const slices = [];
|
36
|
+
while (clone.length) {
|
37
|
+
const slice = clone.splice(0, findNextCloserIndex(clone) + 1);
|
38
|
+
slices.push(slice);
|
39
|
+
}
|
40
|
+
return slices;
|
41
|
+
}
|
42
|
+
|
43
|
+
// src/core/getContentOfTokenChunk.ts
|
44
|
+
function getContentOfTokenChunk(chunk) {
|
45
|
+
if (chunk[0]?.type === "fence") {
|
46
|
+
return [chunk[0].content];
|
47
|
+
}
|
48
|
+
const internalPart = chunk.slice(1, -1);
|
49
|
+
if (internalPart.length < 1) throw new Error("IRREGULAR_CHUNK", { cause: chunk });
|
50
|
+
if (internalPart.length > 1) {
|
51
|
+
const tokenChunks = chunkifyTokens(internalPart);
|
52
|
+
return [tokenChunks.map((subChunk) => {
|
53
|
+
return getContentOfTokenChunk(subChunk).join("\n");
|
54
|
+
})];
|
55
|
+
} else {
|
56
|
+
const token = internalPart[0];
|
57
|
+
if (token.type === "inline") {
|
58
|
+
return [token.content];
|
59
|
+
} else {
|
60
|
+
throw new Error("IRREGULAR_CHUNK", { cause: chunk });
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
// src/core/bundleChunks.ts
|
66
|
+
function bundleChunks(bundle, chunk) {
|
67
|
+
const opener = chunk[0];
|
68
|
+
if (!opener) throw new Error("EMPTY_CHUNK", { cause: chunk });
|
69
|
+
if (opener.type === "heading_open") {
|
70
|
+
const [title] = getContentOfTokenChunk(chunk);
|
71
|
+
return [
|
72
|
+
...bundle,
|
73
|
+
{ title, content: [] }
|
74
|
+
];
|
75
|
+
}
|
76
|
+
const newItems = getContentOfTokenChunk(chunk);
|
77
|
+
const lastSection = bundle[bundle.length - 1];
|
78
|
+
if (!lastSection) {
|
79
|
+
return [
|
80
|
+
{ content: [...newItems] }
|
81
|
+
];
|
82
|
+
}
|
83
|
+
return [
|
84
|
+
...bundle.slice(0, -1),
|
85
|
+
{
|
86
|
+
...lastSection,
|
87
|
+
content: [
|
88
|
+
...lastSection.content,
|
89
|
+
...newItems
|
90
|
+
]
|
91
|
+
}
|
92
|
+
];
|
93
|
+
}
|
94
|
+
|
95
|
+
// src/core/convertMarkdownToJSON.ts
|
96
|
+
function convertMarkdownToJSON(markdown) {
|
97
|
+
try {
|
98
|
+
const md = new MarkdownIt();
|
99
|
+
const tokens = md.parse(markdown, {});
|
100
|
+
const tokenChunks = chunkifyTokens(tokens);
|
101
|
+
return {
|
102
|
+
success: true,
|
103
|
+
sections: tokenChunks.reduce(bundleChunks, [])
|
104
|
+
};
|
105
|
+
} catch (error) {
|
106
|
+
if (error instanceof Error) {
|
107
|
+
return {
|
108
|
+
success: false,
|
109
|
+
errorCode: error.message,
|
110
|
+
errorCause: error.cause
|
111
|
+
};
|
112
|
+
}
|
113
|
+
throw error;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
// src/index.ts
|
118
|
+
var src_default = convertMarkdownToJSON;
|
119
|
+
export {
|
120
|
+
convertMarkdownToJSON,
|
121
|
+
src_default as default
|
122
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
{
|
2
|
+
"name": "@omer-x/md-to-json",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "Converts markdown strings into a human-readable JSON format",
|
5
|
+
"keywords": [
|
6
|
+
"markdown",
|
7
|
+
"converter",
|
8
|
+
"json",
|
9
|
+
"human-readable"
|
10
|
+
],
|
11
|
+
"repository": {
|
12
|
+
"type": "git",
|
13
|
+
"url": "git+https://github.com/omermecitoglu/md-to-json.git"
|
14
|
+
},
|
15
|
+
"bugs": {
|
16
|
+
"url": "https://github.com/omermecitoglu/md-to-json/issues"
|
17
|
+
},
|
18
|
+
"homepage": "https://github.com/omermecitoglu/md-to-json#readme",
|
19
|
+
"private": false,
|
20
|
+
"publishConfig": {
|
21
|
+
"access": "public"
|
22
|
+
},
|
23
|
+
"author": {
|
24
|
+
"name": "Omer Mecitoglu",
|
25
|
+
"email": "omer.mecitoglu@gmail.com",
|
26
|
+
"url": "https://omermecitoglu.github.io"
|
27
|
+
},
|
28
|
+
"license": "MIT",
|
29
|
+
"type": "module",
|
30
|
+
"files": [
|
31
|
+
"dist/"
|
32
|
+
],
|
33
|
+
"exports": {
|
34
|
+
".": {
|
35
|
+
"types": "./dist/index.d.ts",
|
36
|
+
"import": "./dist/index.js",
|
37
|
+
"require": "./dist/index.cjs"
|
38
|
+
}
|
39
|
+
},
|
40
|
+
"scripts": {
|
41
|
+
"test": "jest",
|
42
|
+
"dev": "tsup --watch",
|
43
|
+
"build": "tsup"
|
44
|
+
},
|
45
|
+
"dependencies": {
|
46
|
+
"markdown-it": "^14.1.0"
|
47
|
+
},
|
48
|
+
"devDependencies": {
|
49
|
+
"@omer-x/eslint-config": "^2.1.2",
|
50
|
+
"@omer-x/openapi-types": "^1.2.0",
|
51
|
+
"@types/markdown-it": "^14.1.2",
|
52
|
+
"eslint": "^9.17.0",
|
53
|
+
"ts-jest": "^29.2.5",
|
54
|
+
"ts-node": "^10.9.2",
|
55
|
+
"tsup": "^8.3.5",
|
56
|
+
"typescript": "^5.7.2"
|
57
|
+
}
|
58
|
+
}
|