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 CHANGED
@@ -1,4 +1,4 @@
1
- # Simple Custom Markdown Converter
1
+ # Simple Customize Markdown Converter
2
2
  This simple library help you convert Markdown to HTML and customize it.
3
3
 
4
4
  ## Feature
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", chlidren: [] };
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
- chlidren: [{ type: "Paragraph", children: childrens }]
232
+ children: childrens
236
233
  };
237
234
  };
238
235
  const rows = [];
@@ -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): string;
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.chlidren.map(c => this.render(c)).join("")}</${tag}>`;
57
+ return `<${tag} ${align}>${cell.children.map(c => this.render(c)).join("")}</${tag}>`;
58
58
  }).join("");
59
59
  return `<tr>${cells}</tr>`;
60
60
  };
@@ -86,5 +86,5 @@ export type TableRow = {
86
86
  };
87
87
  export type TableCell = {
88
88
  align: "left" | "center" | "right";
89
- chlidren: Node[];
89
+ children: Node[];
90
90
  };
@@ -1,10 +1,31 @@
1
1
  import { Node } from "./node";
2
2
  /**
3
- * Option to customize how AST nodes are renderes into HTML
3
+ * Function type for rendering an AST node to HTML.
4
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
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?: Partial<Record<Node["type"], (node: any, children: string[]) => string>>;
42
+ elements?: RenderElements;
23
43
  };
44
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple-customize-markdown-converter",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Convert Markdown to your customize HTML",
5
5
  "keywords": [
6
6
  "markdown",