flatmarkdown-ast2html 0.1.5 → 0.1.7
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/index.cjs +17 -10
- package/dist/index.global.js +24 -23
- package/dist/index.js +17 -10
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -175,7 +175,7 @@ Customize how `#hashtag` nodes are rendered. The fields are the same as `WikiLin
|
|
|
175
175
|
| `table_cell` | `<td>…</td>` or `<th>…</th>` (header row), with `align` attr |
|
|
176
176
|
| `thematic_break` | `<hr />` |
|
|
177
177
|
| `html_block` | Raw passthrough |
|
|
178
|
-
| `footnote_definition` | `<div class="footnote" id="fn-{name}"><a href="#fnref-{name}" class="footnote-label">[{name}]:</a
|
|
178
|
+
| `footnote_definition` | `<div class="footnote" id="fn-{name}"><p><a href="#fnref-{name}" class="footnote-label">[{name}]:</a> …</p></div>` (the label stays in front of the body when the definition does not start with a paragraph) |
|
|
179
179
|
| `description_list` | `<dl>…</dl>` |
|
|
180
180
|
| `description_term` | `<dt>…</dt>` |
|
|
181
181
|
| `description_details` | `<dd>…</dd>` |
|
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,7 @@ function defaultContext(options = {}) {
|
|
|
31
31
|
inHeaderRow: false,
|
|
32
32
|
tableAlignments: [],
|
|
33
33
|
cellIndex: 0,
|
|
34
|
+
inTightList: false,
|
|
34
35
|
tightList: false
|
|
35
36
|
};
|
|
36
37
|
}
|
|
@@ -41,6 +42,10 @@ function renderChildren(node, ctx) {
|
|
|
41
42
|
if (!node.children) return "";
|
|
42
43
|
return node.children.map((child) => renderNode(child, ctx)).join("");
|
|
43
44
|
}
|
|
45
|
+
function renderItemChildren(node, ctx, tight) {
|
|
46
|
+
if (!node.children) return "";
|
|
47
|
+
return node.children.map((child) => renderNode(child, { ...ctx, tightList: tight && child.type === "paragraph" })).join("");
|
|
48
|
+
}
|
|
44
49
|
function extractText(node) {
|
|
45
50
|
if (node.type === "text") return node.value;
|
|
46
51
|
if ("children" in node && node.children) {
|
|
@@ -80,25 +85,23 @@ ${renderChildren(node, ctx)}</blockquote>
|
|
|
80
85
|
${renderChildren(node, ctx)}</blockquote>
|
|
81
86
|
`;
|
|
82
87
|
case "list": {
|
|
88
|
+
const childCtx = { ...ctx, inTightList: node.tight };
|
|
83
89
|
if (node.list_type === "ordered") {
|
|
84
90
|
const startAttr = node.start != null && node.start !== 1 ? ` start="${node.start}"` : "";
|
|
85
91
|
return `<ol${startAttr}>
|
|
86
|
-
${renderChildren(node,
|
|
92
|
+
${renderChildren(node, childCtx)}</ol>
|
|
87
93
|
`;
|
|
88
94
|
}
|
|
89
95
|
return `<ul>
|
|
90
|
-
${renderChildren(node,
|
|
96
|
+
${renderChildren(node, childCtx)}</ul>
|
|
91
97
|
`;
|
|
92
98
|
}
|
|
93
|
-
case "item":
|
|
94
|
-
|
|
95
|
-
return `<li>${renderChildren(node, childCtx)}</li>
|
|
99
|
+
case "item":
|
|
100
|
+
return `<li>${renderItemChildren(node, ctx, ctx.inTightList)}</li>
|
|
96
101
|
`;
|
|
97
|
-
}
|
|
98
102
|
case "task_item": {
|
|
99
103
|
const checked = node.symbol != null ? ' checked=""' : "";
|
|
100
|
-
|
|
101
|
-
return `<li><input type="checkbox"${checked} disabled="" />${renderChildren(node, childCtx)}</li>
|
|
104
|
+
return `<li><input type="checkbox"${checked} disabled="" />${renderItemChildren(node, ctx, true)}</li>
|
|
102
105
|
`;
|
|
103
106
|
}
|
|
104
107
|
case "table": {
|
|
@@ -133,9 +136,13 @@ ${cells.join("")}</tr>
|
|
|
133
136
|
return node.literal;
|
|
134
137
|
case "footnote_definition": {
|
|
135
138
|
const name = escapeHtml(node.name);
|
|
139
|
+
const label = `<a href="#fnref-${name}" class="footnote-label">[${name}]:</a>`;
|
|
140
|
+
const [first, ...rest] = node.children ?? [];
|
|
141
|
+
const body = first?.type === "paragraph" ? `<p>${label} ${renderChildren(first, ctx)}</p>
|
|
142
|
+
${rest.map((c) => renderNode(c, ctx)).join("")}` : `${label}
|
|
143
|
+
${renderChildren(node, ctx)}`;
|
|
136
144
|
return `<div class="footnote" id="fn-${name}">
|
|
137
|
-
|
|
138
|
-
${renderChildren(node, ctx)}</div>
|
|
145
|
+
${body}</div>
|
|
139
146
|
`;
|
|
140
147
|
}
|
|
141
148
|
case "description_list":
|
package/dist/index.global.js
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
|
-
"use strict";var FmAst2Html=(()=>{var
|
|
2
|
-
`:`<p>${
|
|
3
|
-
`}case"heading":{let
|
|
4
|
-
`}case"code_block":{let
|
|
1
|
+
"use strict";var FmAst2Html=(()=>{var h=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var _=(e,t)=>{for(var r in t)h(e,r,{get:t[r],enumerable:!0})},A=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of b(t))!k.call(e,i)&&i!==r&&h(e,i,{get:()=>t[i],enumerable:!(a=f(t,i))||a.enumerable});return e};var R=e=>A(h({},"__esModule",{value:!0}),e);var v={};_(v,{renderToHtml:()=>N});function y(e={}){return{options:e,inHeaderRow:!1,tableAlignments:[],cellIndex:0,inTightList:!1,tightList:!1}}function s(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}function n(e,t){return e.children?e.children.map(r=>d(r,t)).join(""):""}function g(e,t,r){return e.children?e.children.map(a=>d(a,{...t,tightList:r&&a.type==="paragraph"})).join(""):""}function m(e){return e.type==="text"?e.value:"children"in e&&e.children?e.children.map(m).join(""):""}function d(e,t){switch(e.type){case"document":return n(e,t);case"paragraph":{let r=n(e,t);return t.tightList?`${r}
|
|
2
|
+
`:`<p>${r}</p>
|
|
3
|
+
`}case"heading":{let r=`h${e.level}`;return`<${r}>${n(e,t)}</${r}>
|
|
4
|
+
`}case"code_block":{let r=e.info;return`<pre><code${r?` class="language-${s(r)}"`:""}>${s(e.literal)}</code></pre>
|
|
5
5
|
`}case"block_quote":return`<blockquote>
|
|
6
|
-
${n(e,
|
|
6
|
+
${n(e,t)}</blockquote>
|
|
7
7
|
`;case"multiline_block_quote":return`<blockquote>
|
|
8
|
-
${n(e,
|
|
9
|
-
`;case"list":return e.list_type==="ordered"?`<ol${e.start!=null&&e.start!==1?` start="${e.start}"`:""}>
|
|
8
|
+
${n(e,t)}</blockquote>
|
|
9
|
+
`;case"list":{let r={...t,inTightList:e.tight};return e.list_type==="ordered"?`<ol${e.start!=null&&e.start!==1?` start="${e.start}"`:""}>
|
|
10
10
|
${n(e,r)}</ol>
|
|
11
11
|
`:`<ul>
|
|
12
12
|
${n(e,r)}</ul>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
${n(e,
|
|
13
|
+
`}case"item":return`<li>${g(e,t,t.inTightList)}</li>
|
|
14
|
+
`;case"task_item":return`<li><input type="checkbox"${e.symbol!=null?' checked=""':""} disabled="" />${g(e,t,!0)}</li>
|
|
15
|
+
`;case"table":{let r={...t,tableAlignments:e.alignments};return`<table>
|
|
16
|
+
${n(e,r)}</table>
|
|
17
17
|
`}case"table_row":return`<tr>
|
|
18
|
-
${(e.children||[]).map((a,i)=>{let l={...
|
|
19
|
-
`;case"table_cell":{let t
|
|
18
|
+
${(e.children||[]).map((a,i)=>{let l={...t,inHeaderRow:e.header,cellIndex:i};return d(a,l)}).join("")}</tr>
|
|
19
|
+
`;case"table_cell":{let r=t.inHeaderRow?"th":"td",a=t.tableAlignments[t.cellIndex],i=a&&a!=="none"?` align="${a}"`:"";return`<${r}${i}>${n(e,t)}</${r}>
|
|
20
20
|
`}case"thematic_break":return`<hr />
|
|
21
|
-
`;case"html_block":return e.literal;case"footnote_definition":{let
|
|
22
|
-
|
|
23
|
-
${n(e,
|
|
21
|
+
`;case"html_block":return e.literal;case"footnote_definition":{let r=s(e.name),a=`<a href="#fnref-${r}" class="footnote-label">[${r}]:</a>`,[i,...l]=e.children??[],o=i?.type==="paragraph"?`<p>${a} ${n(i,t)}</p>
|
|
22
|
+
${l.map(c=>d(c,t)).join("")}`:`${a}
|
|
23
|
+
${n(e,t)}`;return`<div class="footnote" id="fn-${r}">
|
|
24
|
+
${o}</div>
|
|
24
25
|
`}case"description_list":return`<dl>
|
|
25
|
-
${n(e,
|
|
26
|
-
`;case"description_item":return n(e,
|
|
27
|
-
`;case"description_details":return`<dd>${n(e,
|
|
26
|
+
${n(e,t)}</dl>
|
|
27
|
+
`;case"description_item":return n(e,t);case"description_term":return`<dt>${n(e,t)}</dt>
|
|
28
|
+
`;case"description_details":return`<dd>${n(e,t)}</dd>
|
|
28
29
|
`;case"alert":return`<div class="alert alert-${s(e.alert_type)}">
|
|
29
|
-
${n(e,
|
|
30
|
-
`;case"front_matter":return"";case"heex_block":return n(e,
|
|
30
|
+
${n(e,t)}</div>
|
|
31
|
+
`;case"front_matter":return"";case"heex_block":return n(e,t);case"subtext":return n(e,t);case"text":return s(e.value);case"softbreak":return`
|
|
31
32
|
`;case"linebreak":return`<br />
|
|
32
|
-
`;case"emph":return`<em>${n(e,
|
|
33
|
-
`:`<code class="math math-inline">${s(e.literal)}</code>`;case"html_inline":return e.value;case"heex_inline":return e.value;case"raw":return e.value;case"escaped":return"";case"escaped_tag":return s(e.value);default:{let
|
|
33
|
+
`;case"emph":return`<em>${n(e,t)}</em>`;case"strong":return`<strong>${n(e,t)}</strong>`;case"strikethrough":return`<del>${n(e,t)}</del>`;case"underline":return`<u>${n(e,t)}</u>`;case"highlight":return`<mark>${n(e,t)}</mark>`;case"superscript":return`<sup>${n(e,t)}</sup>`;case"subscript":return`<sub>${n(e,t)}</sub>`;case"spoilered_text":return`<span class="spoiler">${n(e,t)}</span>`;case"code":return`<code>${s(e.literal)}</code>`;case"link":{let r=e.title?` title="${s(e.title)}"`:"";return`<a href="${s(e.url)}"${r}>${n(e,t)}</a>`}case"image":{let r=m(e),a=e.title?` title="${s(e.title)}"`:"";return`<img src="${s(e.url)}" alt="${s(r)}"${a} />`}case"wikilink":{let r=t.options.wikilink,a=r?.tagName??"a",i=r?.urlAttr??"href",l=r?.urlPrefix??"",o=r?.attrs??{"data-wikilink":"true"},c=`${l}${e.url}`,u=`${i}="${s(c)}"`;for(let[p,$]of Object.entries(o))u+=` ${p}="${s($)}"`;return`<${a} ${u}>${n(e,t)}</${a}>`}case"hashtag":{let r=t.options.hashtag,a=r?.tagName??"a",i=r?.urlAttr??"href",l=r?.urlPrefix??"",o=r?.attrs??{"data-hashtag":"true"},c=`${l}${e.value}`,u=`${i}="${s(c)}"`;for(let[p,$]of Object.entries(o))u+=` ${p}="${s($)}"`;return`<${a} ${u}>#${s(e.value)}</${a}>`}case"footnote_reference":{let r=s(e.name);return`<sup id="fnref-${r}" class="footnote-ref"><a href="#fn-${r}">[${r}]</a></sup>`}case"shortcode":return e.emoji;case"math":return e.display_math?`<pre><code class="math math-display">${s(e.literal)}</code></pre>
|
|
34
|
+
`:`<code class="math math-inline">${s(e.literal)}</code>`;case"html_inline":return e.value;case"heex_inline":return e.value;case"raw":return e.value;case"escaped":return"";case"escaped_tag":return s(e.value);default:{let r=e;return""}}}function N(e,t){let r=typeof e=="string"?JSON.parse(e):e;return d(r,y(t))}return R(v);})();
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ function defaultContext(options = {}) {
|
|
|
5
5
|
inHeaderRow: false,
|
|
6
6
|
tableAlignments: [],
|
|
7
7
|
cellIndex: 0,
|
|
8
|
+
inTightList: false,
|
|
8
9
|
tightList: false
|
|
9
10
|
};
|
|
10
11
|
}
|
|
@@ -15,6 +16,10 @@ function renderChildren(node, ctx) {
|
|
|
15
16
|
if (!node.children) return "";
|
|
16
17
|
return node.children.map((child) => renderNode(child, ctx)).join("");
|
|
17
18
|
}
|
|
19
|
+
function renderItemChildren(node, ctx, tight) {
|
|
20
|
+
if (!node.children) return "";
|
|
21
|
+
return node.children.map((child) => renderNode(child, { ...ctx, tightList: tight && child.type === "paragraph" })).join("");
|
|
22
|
+
}
|
|
18
23
|
function extractText(node) {
|
|
19
24
|
if (node.type === "text") return node.value;
|
|
20
25
|
if ("children" in node && node.children) {
|
|
@@ -54,25 +59,23 @@ ${renderChildren(node, ctx)}</blockquote>
|
|
|
54
59
|
${renderChildren(node, ctx)}</blockquote>
|
|
55
60
|
`;
|
|
56
61
|
case "list": {
|
|
62
|
+
const childCtx = { ...ctx, inTightList: node.tight };
|
|
57
63
|
if (node.list_type === "ordered") {
|
|
58
64
|
const startAttr = node.start != null && node.start !== 1 ? ` start="${node.start}"` : "";
|
|
59
65
|
return `<ol${startAttr}>
|
|
60
|
-
${renderChildren(node,
|
|
66
|
+
${renderChildren(node, childCtx)}</ol>
|
|
61
67
|
`;
|
|
62
68
|
}
|
|
63
69
|
return `<ul>
|
|
64
|
-
${renderChildren(node,
|
|
70
|
+
${renderChildren(node, childCtx)}</ul>
|
|
65
71
|
`;
|
|
66
72
|
}
|
|
67
|
-
case "item":
|
|
68
|
-
|
|
69
|
-
return `<li>${renderChildren(node, childCtx)}</li>
|
|
73
|
+
case "item":
|
|
74
|
+
return `<li>${renderItemChildren(node, ctx, ctx.inTightList)}</li>
|
|
70
75
|
`;
|
|
71
|
-
}
|
|
72
76
|
case "task_item": {
|
|
73
77
|
const checked = node.symbol != null ? ' checked=""' : "";
|
|
74
|
-
|
|
75
|
-
return `<li><input type="checkbox"${checked} disabled="" />${renderChildren(node, childCtx)}</li>
|
|
78
|
+
return `<li><input type="checkbox"${checked} disabled="" />${renderItemChildren(node, ctx, true)}</li>
|
|
76
79
|
`;
|
|
77
80
|
}
|
|
78
81
|
case "table": {
|
|
@@ -107,9 +110,13 @@ ${cells.join("")}</tr>
|
|
|
107
110
|
return node.literal;
|
|
108
111
|
case "footnote_definition": {
|
|
109
112
|
const name = escapeHtml(node.name);
|
|
113
|
+
const label = `<a href="#fnref-${name}" class="footnote-label">[${name}]:</a>`;
|
|
114
|
+
const [first, ...rest] = node.children ?? [];
|
|
115
|
+
const body = first?.type === "paragraph" ? `<p>${label} ${renderChildren(first, ctx)}</p>
|
|
116
|
+
${rest.map((c) => renderNode(c, ctx)).join("")}` : `${label}
|
|
117
|
+
${renderChildren(node, ctx)}`;
|
|
110
118
|
return `<div class="footnote" id="fn-${name}">
|
|
111
|
-
|
|
112
|
-
${renderChildren(node, ctx)}</div>
|
|
119
|
+
${body}</div>
|
|
113
120
|
`;
|
|
114
121
|
}
|
|
115
122
|
case "description_list":
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flatmarkdown-ast2html",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "A library to convert FlatMarkdown AST to HTML.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -47,4 +47,4 @@
|
|
|
47
47
|
"vitest": "^3.0.0",
|
|
48
48
|
"typescript": "^5.0.0"
|
|
49
49
|
}
|
|
50
|
-
}
|
|
50
|
+
}
|