simple-customize-markdown-converter 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Regiko04
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,67 @@
1
+ # Simple Custom Markdown Converter
2
+ This simple library help you convert Markdown to HTML and customize it.
3
+
4
+ ## Feature
5
+ Currently, this lib only supports:
6
+ - Headings (#, ##, …)
7
+ - Paragraphs
8
+ - Bold (\*\*text\*\*)
9
+ - Italic (\*text\* or \_text\_)
10
+ - Inline code (\`code\`)
11
+ - Code blocks (\`\`\`lang ... \`\`\`)
12
+
13
+ And customizable renderer for all elements
14
+
15
+ ## Install
16
+ ```bash
17
+ npm install simple-custom-markdown-converter
18
+ ```
19
+
20
+ ## Usage
21
+ #### 1. Convert markdown to HTML
22
+ ```js
23
+ const input = `
24
+ # Hello World
25
+ This is **bold** and *italic*
26
+ `
27
+ console.log(convertMarkdownToHTML(input))
28
+ ```
29
+ Output:
30
+ ```html
31
+ <h1>Hello World</h1>
32
+ <p>This is <strong>bold</strong> and <em>italic</em></p>
33
+ ```
34
+
35
+ #### 2. Customize your converter
36
+ You can also customize which HTML should be rendered which every commmon Markdown syntax.
37
+
38
+ For example: change `<h1>` to `<h5>`, wrap paragraphs in `<div>`, or style bold text:
39
+ ```ts
40
+ const renderOptions: RenderOption = {
41
+ elements: {
42
+ Header: (node, children) => {
43
+ //Customize for only Heading 1
44
+ if (node.level === 1) {
45
+ return `<h5 class="custom-h1">${children.join("")}</h5>`
46
+ }
47
+ //Keep all remain Heading
48
+ return `<h${node.level}>${children.join("")}</h${node.level}>`
49
+ },
50
+ Paragraph: (_node, children) => `<div class="paragraph">${children.join("")}</div>`,
51
+ Bold: (_node, children) => `<b class="bold-text">${children.join("")}</b>`,
52
+ }
53
+ }
54
+
55
+ const input = `
56
+ # Title
57
+ Hello **World**
58
+ `
59
+
60
+ console.log(convertMarkdownToHTML(input, renderOptions))
61
+ ```
62
+
63
+ Output:
64
+ ```html
65
+ <h5 class="custom-h1">Title</h5>
66
+ <div class="paragraph">Hello <b class="bold-text">World</b></div>
67
+ ```
@@ -0,0 +1,15 @@
1
+ import { RenderOption } from "./types/renderOptions";
2
+ export { RenderOption };
3
+ /**
4
+ * Convert a Markdown string into HTML.
5
+ * @param input - The Markdown source string
6
+ * @param options - Optional rendering options
7
+ * @returns The rendered HTML string
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const html = convertMarkdownToHTML("Hello **world**")
12
+ * // => <p>Hello <strong>world</strong></p>
13
+ * ```
14
+ */
15
+ export declare function convertMarkdownToHTML(input: string, options?: RenderOption): string;
package/dist/index.js ADDED
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.convertMarkdownToHTML = convertMarkdownToHTML;
7
+ const lexer_1 = __importDefault(require("./lexer"));
8
+ const parser_1 = require("./parser");
9
+ const renderer_1 = __importDefault(require("./renderer"));
10
+ /**
11
+ * Convert a Markdown string into HTML.
12
+ * @param input - The Markdown source string
13
+ * @param options - Optional rendering options
14
+ * @returns The rendered HTML string
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const html = convertMarkdownToHTML("Hello **world**")
19
+ * // => <p>Hello <strong>world</strong></p>
20
+ * ```
21
+ */
22
+ function convertMarkdownToHTML(input, options = {}) {
23
+ const tokens = new lexer_1.default(input).tokenize();
24
+ const nodes = new parser_1.Parser(tokens).parse();
25
+ return new renderer_1.default(options).render(nodes);
26
+ }
@@ -0,0 +1,23 @@
1
+ import { Token } from "./types/token";
2
+ export default class Lexer {
3
+ input: string;
4
+ pos: number;
5
+ listToken: Token[];
6
+ constructor(input: string);
7
+ /**
8
+ * Tokenize the markdown into a list of tokens.
9
+ * @returns List of tokens
10
+ */
11
+ tokenize(): Token[];
12
+ private peek;
13
+ private next;
14
+ private startsWith;
15
+ private isEndOfFile;
16
+ private getLastToken;
17
+ private handleHeader;
18
+ private handleCodeBlock;
19
+ private handleTextBlock;
20
+ private handleItalic;
21
+ private handleBold;
22
+ private handleInlineBlock;
23
+ }
package/dist/lexer.js ADDED
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Lexer {
4
+ constructor(input) {
5
+ this.pos = 0;
6
+ this.listToken = [];
7
+ this.input = input;
8
+ }
9
+ /**
10
+ * Tokenize the markdown into a list of tokens.
11
+ * @returns List of tokens
12
+ */
13
+ tokenize() {
14
+ const TOKEN_HANDLER = [
15
+ { match: (lex) => lex.startsWith("```"), emit: (lex) => lex.handleCodeBlock() },
16
+ { match: (lex) => lex.startsWith("**"), emit: (lex) => lex.handleBold() },
17
+ { match: (lex) => lex.peek() === "`", emit: (lex) => lex.handleInlineBlock() },
18
+ { match: (lex) => lex.peek() === "#", emit: (lex) => lex.handleHeader() },
19
+ { match: (lex) => lex.peek() === "*" || lex.peek() === "_", emit: (lex) => lex.handleItalic() },
20
+ { match: (lex) => lex.peek() === "\n", emit: (lex) => lex.listToken.push({ type: "NewLine" }) },
21
+ ];
22
+ while (!this.isEndOfFile()) {
23
+ let matched = false;
24
+ for (const handler of TOKEN_HANDLER) {
25
+ if (handler.match(this)) {
26
+ handler.emit(this);
27
+ matched = true;
28
+ break;
29
+ }
30
+ }
31
+ if (!matched) {
32
+ this.handleTextBlock();
33
+ }
34
+ this.next();
35
+ }
36
+ this.listToken.push({ type: "EOF" });
37
+ return this.listToken;
38
+ }
39
+ //Get current character with offset
40
+ peek(offset = 0) {
41
+ const i = this.pos + offset;
42
+ return i < this.input.length ? this.input[i] : null;
43
+ }
44
+ //Move cursor by amount
45
+ next(amount = 1) {
46
+ this.pos += amount;
47
+ }
48
+ //If current cursor startsWith given str
49
+ startsWith(str) {
50
+ return this.input.slice(this.pos, this.pos + str.length) === str;
51
+ }
52
+ isEndOfFile() {
53
+ return this.pos >= this.input.length;
54
+ }
55
+ getLastToken() {
56
+ return this.listToken[this.listToken.length - 1];
57
+ }
58
+ handleHeader() {
59
+ const lastToken = this.getLastToken();
60
+ if (!lastToken || lastToken.type === "NewLine") {
61
+ this.listToken.push({ type: "Header", level: 1 });
62
+ }
63
+ else if (lastToken.type === "Header") {
64
+ lastToken.level++;
65
+ }
66
+ this.next();
67
+ if (this.peek() === " ") {
68
+ this.next();
69
+ this.pos--;
70
+ }
71
+ }
72
+ handleCodeBlock() {
73
+ let lang = "";
74
+ let content = "";
75
+ this.next(3); //Skip open block
76
+ while (!this.isEndOfFile() && this.peek() !== "\n") {
77
+ lang += this.peek();
78
+ this.next();
79
+ }
80
+ this.next(); //Skip \n
81
+ while (!this.isEndOfFile() && !this.startsWith("```")) {
82
+ content += this.peek();
83
+ this.next();
84
+ }
85
+ this.next(2); //Skip close block (due to next() after each tokenize iteration)
86
+ this.listToken.push({ "type": "CodeBlock", lang: lang.trim(), content: content });
87
+ }
88
+ handleTextBlock() {
89
+ const currentChar = this.peek();
90
+ if (currentChar === null)
91
+ return;
92
+ const lastToken = this.getLastToken();
93
+ if (lastToken?.type === "Text")
94
+ lastToken.value += currentChar;
95
+ else
96
+ this.listToken.push({ type: "Text", value: currentChar });
97
+ }
98
+ handleItalic() {
99
+ this.listToken.push({ type: "Italic" });
100
+ }
101
+ handleBold() {
102
+ this.listToken.push({ type: "Bold" });
103
+ this.next(); //Skip *
104
+ }
105
+ handleInlineBlock() {
106
+ let content = "";
107
+ this.next(); //Skip open block
108
+ while (!this.isEndOfFile() && !this.startsWith("`")) {
109
+ content += this.peek();
110
+ this.next();
111
+ }
112
+ // this.next() //Skip close block
113
+ this.listToken.push({ "type": "InlineCode", content: content });
114
+ }
115
+ }
116
+ exports.default = Lexer;
package/dist/node.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,23 @@
1
+ import { Node } from "./types/node";
2
+ import { Token } from "./types/token";
3
+ export declare class Parser {
4
+ listToken: Token[];
5
+ pos: number;
6
+ constructor(listToken: Token[]);
7
+ /**
8
+ * Parse a list token to a node
9
+ * @return A parsed abstract syntax tree (AST)
10
+ */
11
+ parse(): Node;
12
+ private peek;
13
+ private next;
14
+ private isEnd;
15
+ private parseBlocks;
16
+ private parseParagraph;
17
+ private parseCodeBlock;
18
+ private parseHeader;
19
+ private parseBold;
20
+ private parseItalic;
21
+ private parseInlineCode;
22
+ private parseInlineUntil;
23
+ }
package/dist/parser.js ADDED
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Parser = void 0;
4
+ class Parser {
5
+ constructor(listToken) {
6
+ this.pos = 0;
7
+ this.listToken = listToken;
8
+ }
9
+ /**
10
+ * Parse a list token to a node
11
+ * @return A parsed abstract syntax tree (AST)
12
+ */
13
+ parse() {
14
+ return {
15
+ type: "Document",
16
+ children: this.parseBlocks()
17
+ };
18
+ }
19
+ peek(offset = 0) {
20
+ const i = this.pos + offset;
21
+ return i < this.listToken.length ? this.listToken[i] : null;
22
+ }
23
+ next(amount = 1) {
24
+ this.pos += amount;
25
+ }
26
+ isEnd() {
27
+ return this.peek()?.type === "EOF";
28
+ }
29
+ parseBlocks() {
30
+ const listNode = [];
31
+ while (!this.isEnd()) {
32
+ const currentNode = this.peek();
33
+ if (!currentNode)
34
+ break;
35
+ switch (currentNode.type) {
36
+ case "Header": {
37
+ listNode.push(this.parseHeader());
38
+ break;
39
+ }
40
+ case "CodeBlock":
41
+ {
42
+ listNode.push(this.parseCodeBlock());
43
+ this.next();
44
+ }
45
+ break;
46
+ case "NewLine": {
47
+ this.next(); // skip
48
+ break;
49
+ }
50
+ default: listNode.push(this.parseParagraph());
51
+ }
52
+ }
53
+ return listNode;
54
+ }
55
+ parseParagraph() {
56
+ return {
57
+ type: "Paragraph",
58
+ children: this.parseInlineUntil("NewLine")
59
+ };
60
+ }
61
+ parseCodeBlock() {
62
+ const tok = this.peek();
63
+ return {
64
+ type: "CodeBlock",
65
+ lang: tok?.type === "CodeBlock" ? tok.lang : "",
66
+ content: tok?.type === "CodeBlock" ? tok.content : ""
67
+ };
68
+ }
69
+ parseHeader() {
70
+ const currentNode = this.peek();
71
+ this.next();
72
+ return {
73
+ type: "Header",
74
+ level: currentNode?.type === "Header" ? currentNode.level : 1,
75
+ children: this.parseInlineUntil("NewLine") //Temp
76
+ };
77
+ }
78
+ parseBold() {
79
+ this.next(); // skip marker
80
+ return { type: "Bold", children: this.parseInlineUntil("Bold") };
81
+ }
82
+ parseItalic() {
83
+ this.next(); // skip marker
84
+ return { type: "Italic", children: this.parseInlineUntil("Italic") };
85
+ }
86
+ parseInlineCode() {
87
+ const tok = this.peek();
88
+ this.next();
89
+ return {
90
+ type: "InlineCode",
91
+ content: tok?.type === "InlineCode" ? tok.content : ""
92
+ };
93
+ }
94
+ parseInlineUntil(stopType) {
95
+ const listNode = [];
96
+ while (!this.isEnd() && this.peek()?.type !== stopType) {
97
+ const currentNode = this.peek();
98
+ if (!currentNode)
99
+ break;
100
+ switch (currentNode.type) {
101
+ case "Bold": {
102
+ listNode.push(this.parseBold());
103
+ break;
104
+ }
105
+ case "Italic": {
106
+ listNode.push(this.parseItalic());
107
+ break;
108
+ }
109
+ case "InlineCode": {
110
+ listNode.push(this.parseInlineCode());
111
+ break;
112
+ }
113
+ case "Text": {
114
+ listNode.push({ type: "Text", value: currentNode.value });
115
+ }
116
+ default: this.next();
117
+ }
118
+ }
119
+ this.next(); //Skip stop token
120
+ return listNode;
121
+ }
122
+ }
123
+ exports.Parser = Parser;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,15 @@
1
+ import { Node } from "./types/node";
2
+ import { RenderOption } from "./types/renderOptions";
3
+ export default class Renderer {
4
+ option: RenderOption;
5
+ constructor(option: RenderOption);
6
+ /**
7
+ * Render a Node (AST) to a HTML string according renderer options
8
+ *
9
+ * @param node - The abstract syntax tree (AST) from the Parser
10
+ * @returns The rendered HTML string.
11
+ */
12
+ render(node: Node): string;
13
+ private handleRender;
14
+ private escapeHtml;
15
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Renderer {
4
+ constructor(option) {
5
+ this.option = option;
6
+ }
7
+ /**
8
+ * Render a Node (AST) to a HTML string according renderer options
9
+ *
10
+ * @param node - The abstract syntax tree (AST) from the Parser
11
+ * @returns The rendered HTML string.
12
+ */
13
+ render(node) {
14
+ //Get proper handler type
15
+ const handler = this.handleRender(node.type);
16
+ //If node have children, recursive to handle all node's children
17
+ const children = "children" in node ? node.children.map((ele) => this.render(ele)) : [];
18
+ return handler(node, children);
19
+ }
20
+ handleRender(type) {
21
+ const defaultRender = {
22
+ Document: (_node, children) => children.join(""),
23
+ Paragraph: (_node, children) => `<p>${children.join("")}</p>`,
24
+ Header: (node, children) => `<h${node.level}>${children.join("")}</h${node.level}>`,
25
+ InlineCode: (node) => `<code>${this.escapeHtml(node.content)}</code>`,
26
+ CodeBlock: (node) => `<pre><code class="lang-${node.lang}">${this.escapeHtml(node.content)}</code></pre>`,
27
+ Bold: (_node, children) => `<strong>${children.join("")}</strong>`,
28
+ Italic: (_node, children) => `<em>${children.join("")}</em>`,
29
+ Text: (node) => node.value
30
+ };
31
+ return this.option.elements?.[type] ?? defaultRender[type];
32
+ }
33
+ escapeHtml(str) {
34
+ return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
35
+ }
36
+ }
37
+ exports.default = Renderer;
package/dist/token.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,43 @@
1
+ /**
2
+ * AST (Abstract Syntax Tree) node definition.
3
+ *
4
+ * Each node represents a Markdown construct and some special nodes (Document, Paragraph).
5
+ * Some nodes are containers (have `children`), while others are leaf nodes (contain text).
6
+ *
7
+ * Variants:
8
+ * - Document: Root node, contains all other nodes.
9
+ * - Paragraph: A block of text, contain inline nodes.
10
+ * - Header: A header with given `level` (1-6)
11
+ * - Bold: Bold text
12
+ * - Italic: Italic text
13
+ * - InlineCode: Inline code snippet, with `content`
14
+ * - CodeBlock: A code block, with `lang` and `content`
15
+ * - Text: Raw text content.
16
+ */
17
+ export type Node = {
18
+ type: "Document";
19
+ children: Node[];
20
+ } | {
21
+ type: "Paragraph";
22
+ children: Node[];
23
+ } | {
24
+ type: "Header";
25
+ level: number;
26
+ children: Node[];
27
+ } | {
28
+ type: "Bold";
29
+ children: Node[];
30
+ } | {
31
+ type: "Italic";
32
+ children: Node[];
33
+ } | {
34
+ type: "InlineCode";
35
+ content: string;
36
+ } | {
37
+ type: "CodeBlock";
38
+ lang: string;
39
+ content: string;
40
+ } | {
41
+ type: "Text";
42
+ value: string;
43
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,23 @@
1
+ import { Node } from "./node";
2
+ /**
3
+ * Option to customize how AST nodes are renderes into HTML
4
+ *
5
+ * @property elements? - A mapping of AST node types to custom render functions.
6
+ * - The key is the `Node` type (e.g. `"Header"`, `"Text"`).
7
+ * - The value is a function `(node, children) => string` that define how to render HTML string. With `node` is a AST `Node`. `children` is the node's childrens
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const renderOptions: RenderOption = {
12
+ * elements: {
13
+ * Paragraph: (_node, children) => `<div class="paragraph">${children.join("")}</div>`,
14
+ * Bold: (_node, children) => `<b class="bold-text">${children.join("")}</b>`,
15
+ * }
16
+ * }
17
+ * ```
18
+ *
19
+ * @todo Update`node` type in value function from `any` to `Node`
20
+ */
21
+ export type RenderOption = {
22
+ elements?: Partial<Record<Node["type"], (node: any, children: string[]) => string>>;
23
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Token produced by the Markdown lexer.
3
+ *
4
+ * Each token represents the smallest meaningful unit of Markdown syntax
5
+ * before being parsed into the AST.
6
+ *
7
+ * Variants:
8
+ * - Header: Markdown header (`#`), with a `level` (1–6).
9
+ * - CodeBlock: Fenced code block (` ``` `), with optional `lang` and its `content`.
10
+ * - NewLine: Line break (`\n`).
11
+ * - Text: Plain text content.
12
+ * - Bold: Bold marker (`**`).
13
+ * - Italic: Italic marker (`*` or `_`).
14
+ * - InlineCode: Inline code snippet (`` ` ``), with its `content`.
15
+ * - EOF: A special token, this is the end of input.
16
+ */
17
+ export type Token = {
18
+ type: "Header";
19
+ level: number;
20
+ } | {
21
+ type: "CodeBlock";
22
+ lang: string;
23
+ content: string;
24
+ } | {
25
+ type: "NewLine";
26
+ } | {
27
+ type: "Text";
28
+ value: string;
29
+ } | {
30
+ type: "Bold";
31
+ } | {
32
+ type: "Italic";
33
+ } | {
34
+ type: "InlineCode";
35
+ content: string;
36
+ } | {
37
+ type: "EOF";
38
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "simple-customize-markdown-converter",
3
+ "version": "1.0.0",
4
+ "description": "Convert Markdown to your customize HTML",
5
+ "keywords": [
6
+ "markdown",
7
+ "html",
8
+ "converter"
9
+ ],
10
+ "author": "Regiko04",
11
+ "license": "MIT",
12
+ "main": "dist/index.js",
13
+ "module": "dist/index.js",
14
+ "types": "dist/index.d.ts",
15
+ "files": [
16
+ "dist",
17
+ "LICENSE",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "test": "jest",
22
+ "build": "tsc",
23
+ "start": "ts-node src/index.ts"
24
+ },
25
+ "devDependencies": {
26
+ "@types/jest": "^30.0.0",
27
+ "@types/node": "^24.3.3",
28
+ "jest": "^30.1.3",
29
+ "ts-jest": "^29.4.1",
30
+ "ts-node": "^10.9.2",
31
+ "typescript": "^5.9.2"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/Riiichan04/simple-custom-markdown-converter.git"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/Riiichan04/simple-custom-markdown-converter/issues"
39
+ },
40
+ "homepage": "https://github.com/Riiichan04/simple-custom-markdown-converter#readme"
41
+ }