simple-customize-markdown-converter 1.0.4 → 1.0.6
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 +1 -1
- package/dist/parser.js +3 -6
- package/dist/renderer.d.ts +3 -1
- package/dist/renderer.js +3 -3
- package/dist/types/node.d.ts +1 -1
- package/dist/types/renderOptions.d.ts +27 -6
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/parser.js
CHANGED
|
@@ -168,10 +168,7 @@ class Parser {
|
|
|
168
168
|
if (["ListItem", "TaskItem", "ListEnd"].includes(tok.type)) {
|
|
169
169
|
break;
|
|
170
170
|
}
|
|
171
|
-
children.push(
|
|
172
|
-
type: "Paragraph",
|
|
173
|
-
children: this.parseInlineUntil("NewLine")
|
|
174
|
-
});
|
|
171
|
+
children.push(...this.parseInlineUntil("NewLine"));
|
|
175
172
|
}
|
|
176
173
|
return currentToken?.type === "TaskItem" ? {
|
|
177
174
|
type: "TaskItem",
|
|
@@ -227,12 +224,12 @@ class Parser {
|
|
|
227
224
|
const parseCell = () => {
|
|
228
225
|
const cellStartToken = this.peek();
|
|
229
226
|
if (cellStartToken?.type !== "CellStart")
|
|
230
|
-
return { align: "left",
|
|
227
|
+
return { align: "left", children: [] };
|
|
231
228
|
this.next(); // skip CellStart token
|
|
232
229
|
const childrens = this.parseInlineUntil("CellEnd");
|
|
233
230
|
return {
|
|
234
231
|
align: cellStartToken.align,
|
|
235
|
-
|
|
232
|
+
children: childrens
|
|
236
233
|
};
|
|
237
234
|
};
|
|
238
235
|
const rows = [];
|
package/dist/renderer.d.ts
CHANGED
|
@@ -9,7 +9,9 @@ export default class Renderer {
|
|
|
9
9
|
* @param node - The abstract syntax tree (AST) from the Parser
|
|
10
10
|
* @returns The rendered HTML string.
|
|
11
11
|
*/
|
|
12
|
-
render(node: Node
|
|
12
|
+
render<K extends Node["type"]>(node: Extract<Node, {
|
|
13
|
+
type: K;
|
|
14
|
+
}>): string;
|
|
13
15
|
private handleRender;
|
|
14
16
|
private renderTable;
|
|
15
17
|
private escapeHtml;
|
package/dist/renderer.js
CHANGED
|
@@ -25,7 +25,7 @@ class Renderer {
|
|
|
25
25
|
//Container nodes
|
|
26
26
|
CodeBlock: (node) => `<pre><code class="lang-${node.lang}">${this.escapeHtml(node.content)}</code></pre>`,
|
|
27
27
|
Header: (node, children) => `<h${node.level}${node.level <= 2 ? ' style="border-bottom: 1px solid #d1d9e0b3"' : ''}>${children.join("")}</h${node.level}>`,
|
|
28
|
-
Quote: (_node, children) => `<blockquote>${children.join("")}</blockquote>`,
|
|
28
|
+
Quote: (_node, children) => `<blockquote style="margin:0; padding:0 1em; color:#59636e; border-left:.25em solid #d1d9e0;">${children.join("")}</blockquote>`,
|
|
29
29
|
//For list nodes
|
|
30
30
|
List: (node, children) => node.ordered ? `<ol>${children.join("")}</ol>` : `<ul>${children.join("")}</ul>`,
|
|
31
31
|
ListItem: (_node, children) => `<li>${children.join("")}</li>`,
|
|
@@ -44,7 +44,7 @@ class Renderer {
|
|
|
44
44
|
//For table nodes
|
|
45
45
|
Table: (node, children) => this.renderTable(node, children),
|
|
46
46
|
};
|
|
47
|
-
return this.option.elements?.[type] ?? defaultRender[type];
|
|
47
|
+
return (this.option.elements?.[type] ?? defaultRender[type]);
|
|
48
48
|
}
|
|
49
49
|
renderTable(node, children) {
|
|
50
50
|
if (node.type === "Table") {
|
|
@@ -54,7 +54,7 @@ class Renderer {
|
|
|
54
54
|
const tag = row.isHeader ? "th" : "td";
|
|
55
55
|
const cells = row.cells.map(cell => {
|
|
56
56
|
const align = `style="text-align:${cell.align}"`;
|
|
57
|
-
return `<${tag} ${align}>${cell.
|
|
57
|
+
return `<${tag} ${align}>${cell.children.map(c => this.render(c)).join("")}</${tag}>`;
|
|
58
58
|
}).join("");
|
|
59
59
|
return `<tr>${cells}</tr>`;
|
|
60
60
|
};
|
package/dist/types/node.d.ts
CHANGED
|
@@ -1,10 +1,31 @@
|
|
|
1
1
|
import { Node } from "./node";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Function type for rendering an AST node to HTML.
|
|
4
4
|
*
|
|
5
|
-
* @
|
|
6
|
-
* - The
|
|
7
|
-
*
|
|
5
|
+
* @template T - A subtype of `Node` corresponding to the render node
|
|
6
|
+
* @param node - The AST node to render
|
|
7
|
+
* @param children - Rendered HTML strings of the node's children
|
|
8
|
+
* @returns A HTML string representation of the node
|
|
9
|
+
*/
|
|
10
|
+
type NodeRenderer<T extends Node = Node> = (node: T, children: string[]) => string;
|
|
11
|
+
/**
|
|
12
|
+
* A mapping of AST node types to custom render functions.
|
|
13
|
+
*
|
|
14
|
+
* - The key is a `Node["type"]` string literal (e.g. `"Header"`, `"Paragraph"`)
|
|
15
|
+
* - The value is a function `(node, children) => string`:
|
|
16
|
+
* - `node` is a `Node` with its attribute depending on its `type`.
|
|
17
|
+
* (e.g. `"Header"` nodes include `level`, `"CodeBlock"` nodes include `lang` and `content`, etc)
|
|
18
|
+
* - `children` is the array of rendered strings of its children.
|
|
19
|
+
*/
|
|
20
|
+
export type RenderElements = {
|
|
21
|
+
[K in Node["type"]]?: NodeRenderer<Extract<Node, {
|
|
22
|
+
type: K;
|
|
23
|
+
}>>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Options to customize how AST nodes are renderes into HTML
|
|
27
|
+
*
|
|
28
|
+
* @property elements - Optional custom rendered for one or more node types
|
|
8
29
|
*
|
|
9
30
|
* @example
|
|
10
31
|
* ```ts
|
|
@@ -16,8 +37,8 @@ import { Node } from "./node";
|
|
|
16
37
|
* }
|
|
17
38
|
* ```
|
|
18
39
|
*
|
|
19
|
-
* @todo Update`node` type in value function from `any` to `Node`
|
|
20
40
|
*/
|
|
21
41
|
export type RenderOption = {
|
|
22
|
-
elements?:
|
|
42
|
+
elements?: RenderElements;
|
|
23
43
|
};
|
|
44
|
+
export {};
|