flatmarkdown-ast2html 0.1.7 → 0.7.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/README.md +42 -194
- package/{LICENSE → dist/LICENSE} +21 -21
- package/dist/index.global.js +252 -34
- package/package.json +13 -30
- package/dist/index.cjs +0 -268
- package/dist/index.d.cts +0 -38
- package/dist/index.d.ts +0 -38
- package/dist/index.js +0 -241
package/package.json
CHANGED
|
@@ -1,50 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flatmarkdown-ast2html",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Renders the Flat Markdown AST to HTML. The wasm build of the Rust crate flatmarkdown-ast2html.",
|
|
9
5
|
"unpkg": "./dist/index.global.js",
|
|
6
|
+
"jsdelivr": "./dist/index.global.js",
|
|
10
7
|
"files": [
|
|
11
8
|
"dist"
|
|
12
9
|
],
|
|
13
10
|
"repository": {
|
|
14
11
|
"type": "git",
|
|
15
|
-
"url": "git+https://github.com/petajournal/flatmarkdown
|
|
12
|
+
"url": "git+https://github.com/petajournal/flatmarkdown.git",
|
|
13
|
+
"directory": "flatmarkdown-ast2html-wasm"
|
|
16
14
|
},
|
|
17
15
|
"bugs": {
|
|
18
|
-
"url": "https://github.com/petajournal/flatmarkdown
|
|
19
|
-
},
|
|
20
|
-
"homepage": "https://github.com/petajournal/flatmarkdown-ast2html#readme",
|
|
21
|
-
"exports": {
|
|
22
|
-
".": {
|
|
23
|
-
"types": "./dist/index.d.ts",
|
|
24
|
-
"import": "./dist/index.js",
|
|
25
|
-
"require": "./dist/index.cjs"
|
|
26
|
-
}
|
|
16
|
+
"url": "https://github.com/petajournal/flatmarkdown/issues"
|
|
27
17
|
},
|
|
18
|
+
"homepage": "https://github.com/petajournal/flatmarkdown/tree/main/flatmarkdown-ast2html-wasm#readme",
|
|
28
19
|
"scripts": {
|
|
29
|
-
"build": "
|
|
30
|
-
"test": "
|
|
31
|
-
"prepublishOnly": "npm run build"
|
|
20
|
+
"build": "node build.mjs",
|
|
21
|
+
"test": "node --test",
|
|
22
|
+
"prepublishOnly": "npm run build && npm test"
|
|
32
23
|
},
|
|
33
24
|
"keywords": [
|
|
34
25
|
"flatmarkdown",
|
|
35
26
|
"ast",
|
|
36
27
|
"html",
|
|
37
28
|
"markdown",
|
|
38
|
-
"
|
|
29
|
+
"wasm"
|
|
39
30
|
],
|
|
40
31
|
"author": "Hidekazu Kubota",
|
|
41
|
-
"license": "MIT"
|
|
42
|
-
|
|
43
|
-
"flatmarkdown-ast": "^0.2.0"
|
|
44
|
-
},
|
|
45
|
-
"devDependencies": {
|
|
46
|
-
"tsup": "^8.0.0",
|
|
47
|
-
"vitest": "^3.0.0",
|
|
48
|
-
"typescript": "^5.0.0"
|
|
49
|
-
}
|
|
50
|
-
}
|
|
32
|
+
"license": "MIT"
|
|
33
|
+
}
|
package/dist/index.cjs
DELETED
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
renderToHtml: () => renderToHtml
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(index_exports);
|
|
26
|
-
|
|
27
|
-
// src/renderer.ts
|
|
28
|
-
function defaultContext(options = {}) {
|
|
29
|
-
return {
|
|
30
|
-
options,
|
|
31
|
-
inHeaderRow: false,
|
|
32
|
-
tableAlignments: [],
|
|
33
|
-
cellIndex: 0,
|
|
34
|
-
inTightList: false,
|
|
35
|
-
tightList: false
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
function escapeHtml(str) {
|
|
39
|
-
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
40
|
-
}
|
|
41
|
-
function renderChildren(node, ctx) {
|
|
42
|
-
if (!node.children) return "";
|
|
43
|
-
return node.children.map((child) => renderNode(child, ctx)).join("");
|
|
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
|
-
}
|
|
49
|
-
function extractText(node) {
|
|
50
|
-
if (node.type === "text") return node.value;
|
|
51
|
-
if ("children" in node && node.children) {
|
|
52
|
-
return node.children.map(extractText).join("");
|
|
53
|
-
}
|
|
54
|
-
return "";
|
|
55
|
-
}
|
|
56
|
-
function renderNode(node, ctx) {
|
|
57
|
-
switch (node.type) {
|
|
58
|
-
// ── Block Nodes ──
|
|
59
|
-
case "document":
|
|
60
|
-
return renderChildren(node, ctx);
|
|
61
|
-
case "paragraph": {
|
|
62
|
-
const inner = renderChildren(node, ctx);
|
|
63
|
-
if (ctx.tightList) return `${inner}
|
|
64
|
-
`;
|
|
65
|
-
return `<p>${inner}</p>
|
|
66
|
-
`;
|
|
67
|
-
}
|
|
68
|
-
case "heading": {
|
|
69
|
-
const tag = `h${node.level}`;
|
|
70
|
-
return `<${tag}>${renderChildren(node, ctx)}</${tag}>
|
|
71
|
-
`;
|
|
72
|
-
}
|
|
73
|
-
case "code_block": {
|
|
74
|
-
const info = node.info;
|
|
75
|
-
const cls = info ? ` class="language-${escapeHtml(info)}"` : "";
|
|
76
|
-
return `<pre><code${cls}>${escapeHtml(node.literal)}</code></pre>
|
|
77
|
-
`;
|
|
78
|
-
}
|
|
79
|
-
case "block_quote":
|
|
80
|
-
return `<blockquote>
|
|
81
|
-
${renderChildren(node, ctx)}</blockquote>
|
|
82
|
-
`;
|
|
83
|
-
case "multiline_block_quote":
|
|
84
|
-
return `<blockquote>
|
|
85
|
-
${renderChildren(node, ctx)}</blockquote>
|
|
86
|
-
`;
|
|
87
|
-
case "list": {
|
|
88
|
-
const childCtx = { ...ctx, inTightList: node.tight };
|
|
89
|
-
if (node.list_type === "ordered") {
|
|
90
|
-
const startAttr = node.start != null && node.start !== 1 ? ` start="${node.start}"` : "";
|
|
91
|
-
return `<ol${startAttr}>
|
|
92
|
-
${renderChildren(node, childCtx)}</ol>
|
|
93
|
-
`;
|
|
94
|
-
}
|
|
95
|
-
return `<ul>
|
|
96
|
-
${renderChildren(node, childCtx)}</ul>
|
|
97
|
-
`;
|
|
98
|
-
}
|
|
99
|
-
case "item":
|
|
100
|
-
return `<li>${renderItemChildren(node, ctx, ctx.inTightList)}</li>
|
|
101
|
-
`;
|
|
102
|
-
case "task_item": {
|
|
103
|
-
const checked = node.symbol != null ? ' checked=""' : "";
|
|
104
|
-
return `<li><input type="checkbox"${checked} disabled="" />${renderItemChildren(node, ctx, true)}</li>
|
|
105
|
-
`;
|
|
106
|
-
}
|
|
107
|
-
case "table": {
|
|
108
|
-
const childCtx = { ...ctx, tableAlignments: node.alignments };
|
|
109
|
-
return `<table>
|
|
110
|
-
${renderChildren(node, childCtx)}</table>
|
|
111
|
-
`;
|
|
112
|
-
}
|
|
113
|
-
case "table_row": {
|
|
114
|
-
const cells = (node.children || []).map((cell, i) => {
|
|
115
|
-
const cellCtx = {
|
|
116
|
-
...ctx,
|
|
117
|
-
inHeaderRow: node.header,
|
|
118
|
-
cellIndex: i
|
|
119
|
-
};
|
|
120
|
-
return renderNode(cell, cellCtx);
|
|
121
|
-
});
|
|
122
|
-
return `<tr>
|
|
123
|
-
${cells.join("")}</tr>
|
|
124
|
-
`;
|
|
125
|
-
}
|
|
126
|
-
case "table_cell": {
|
|
127
|
-
const tag = ctx.inHeaderRow ? "th" : "td";
|
|
128
|
-
const alignment = ctx.tableAlignments[ctx.cellIndex];
|
|
129
|
-
const alignAttr = alignment && alignment !== "none" ? ` align="${alignment}"` : "";
|
|
130
|
-
return `<${tag}${alignAttr}>${renderChildren(node, ctx)}</${tag}>
|
|
131
|
-
`;
|
|
132
|
-
}
|
|
133
|
-
case "thematic_break":
|
|
134
|
-
return "<hr />\n";
|
|
135
|
-
case "html_block":
|
|
136
|
-
return node.literal;
|
|
137
|
-
case "footnote_definition": {
|
|
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)}`;
|
|
144
|
-
return `<div class="footnote" id="fn-${name}">
|
|
145
|
-
${body}</div>
|
|
146
|
-
`;
|
|
147
|
-
}
|
|
148
|
-
case "description_list":
|
|
149
|
-
return `<dl>
|
|
150
|
-
${renderChildren(node, ctx)}</dl>
|
|
151
|
-
`;
|
|
152
|
-
case "description_item":
|
|
153
|
-
return renderChildren(node, ctx);
|
|
154
|
-
case "description_term":
|
|
155
|
-
return `<dt>${renderChildren(node, ctx)}</dt>
|
|
156
|
-
`;
|
|
157
|
-
case "description_details":
|
|
158
|
-
return `<dd>${renderChildren(node, ctx)}</dd>
|
|
159
|
-
`;
|
|
160
|
-
case "alert":
|
|
161
|
-
return `<div class="alert alert-${escapeHtml(node.alert_type)}">
|
|
162
|
-
${renderChildren(node, ctx)}</div>
|
|
163
|
-
`;
|
|
164
|
-
case "front_matter":
|
|
165
|
-
return "";
|
|
166
|
-
case "heex_block":
|
|
167
|
-
return renderChildren(node, ctx);
|
|
168
|
-
case "subtext":
|
|
169
|
-
return renderChildren(node, ctx);
|
|
170
|
-
// ── Inline Nodes ──
|
|
171
|
-
case "text":
|
|
172
|
-
return escapeHtml(node.value);
|
|
173
|
-
case "softbreak":
|
|
174
|
-
return "\n";
|
|
175
|
-
case "linebreak":
|
|
176
|
-
return "<br />\n";
|
|
177
|
-
case "emph":
|
|
178
|
-
return `<em>${renderChildren(node, ctx)}</em>`;
|
|
179
|
-
case "strong":
|
|
180
|
-
return `<strong>${renderChildren(node, ctx)}</strong>`;
|
|
181
|
-
case "strikethrough":
|
|
182
|
-
return `<del>${renderChildren(node, ctx)}</del>`;
|
|
183
|
-
case "underline":
|
|
184
|
-
return `<u>${renderChildren(node, ctx)}</u>`;
|
|
185
|
-
case "highlight":
|
|
186
|
-
return `<mark>${renderChildren(node, ctx)}</mark>`;
|
|
187
|
-
case "superscript":
|
|
188
|
-
return `<sup>${renderChildren(node, ctx)}</sup>`;
|
|
189
|
-
case "subscript":
|
|
190
|
-
return `<sub>${renderChildren(node, ctx)}</sub>`;
|
|
191
|
-
case "spoilered_text":
|
|
192
|
-
return `<span class="spoiler">${renderChildren(node, ctx)}</span>`;
|
|
193
|
-
case "code":
|
|
194
|
-
return `<code>${escapeHtml(node.literal)}</code>`;
|
|
195
|
-
case "link": {
|
|
196
|
-
const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
|
|
197
|
-
return `<a href="${escapeHtml(node.url)}"${titleAttr}>${renderChildren(node, ctx)}</a>`;
|
|
198
|
-
}
|
|
199
|
-
case "image": {
|
|
200
|
-
const alt = extractText(node);
|
|
201
|
-
const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
|
|
202
|
-
return `<img src="${escapeHtml(node.url)}" alt="${escapeHtml(alt)}"${titleAttr} />`;
|
|
203
|
-
}
|
|
204
|
-
case "wikilink": {
|
|
205
|
-
const opts = ctx.options.wikilink;
|
|
206
|
-
const tagName = opts?.tagName ?? "a";
|
|
207
|
-
const urlAttr = opts?.urlAttr ?? "href";
|
|
208
|
-
const urlPrefix = opts?.urlPrefix ?? "";
|
|
209
|
-
const extraAttrs = opts?.attrs ?? { "data-wikilink": "true" };
|
|
210
|
-
const url = `${urlPrefix}${node.url}`;
|
|
211
|
-
let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
|
|
212
|
-
for (const [key, val] of Object.entries(extraAttrs)) {
|
|
213
|
-
attrStr += ` ${key}="${escapeHtml(val)}"`;
|
|
214
|
-
}
|
|
215
|
-
return `<${tagName} ${attrStr}>${renderChildren(node, ctx)}</${tagName}>`;
|
|
216
|
-
}
|
|
217
|
-
case "hashtag": {
|
|
218
|
-
const opts = ctx.options.hashtag;
|
|
219
|
-
const tagName = opts?.tagName ?? "a";
|
|
220
|
-
const urlAttr = opts?.urlAttr ?? "href";
|
|
221
|
-
const urlPrefix = opts?.urlPrefix ?? "";
|
|
222
|
-
const extraAttrs = opts?.attrs ?? { "data-hashtag": "true" };
|
|
223
|
-
const url = `${urlPrefix}${node.value}`;
|
|
224
|
-
let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
|
|
225
|
-
for (const [key, val] of Object.entries(extraAttrs)) {
|
|
226
|
-
attrStr += ` ${key}="${escapeHtml(val)}"`;
|
|
227
|
-
}
|
|
228
|
-
return `<${tagName} ${attrStr}>#${escapeHtml(node.value)}</${tagName}>`;
|
|
229
|
-
}
|
|
230
|
-
// The label is shown instead of `ix`: an item body is parsed alone, so `ix`
|
|
231
|
-
// restarts at 1 in every item.
|
|
232
|
-
case "footnote_reference": {
|
|
233
|
-
const name = escapeHtml(node.name);
|
|
234
|
-
return `<sup id="fnref-${name}" class="footnote-ref"><a href="#fn-${name}">[${name}]</a></sup>`;
|
|
235
|
-
}
|
|
236
|
-
case "shortcode":
|
|
237
|
-
return node.emoji;
|
|
238
|
-
case "math": {
|
|
239
|
-
if (node.display_math) {
|
|
240
|
-
return `<pre><code class="math math-display">${escapeHtml(node.literal)}</code></pre>
|
|
241
|
-
`;
|
|
242
|
-
}
|
|
243
|
-
return `<code class="math math-inline">${escapeHtml(node.literal)}</code>`;
|
|
244
|
-
}
|
|
245
|
-
case "html_inline":
|
|
246
|
-
return node.value;
|
|
247
|
-
case "heex_inline":
|
|
248
|
-
return node.value;
|
|
249
|
-
case "raw":
|
|
250
|
-
return node.value;
|
|
251
|
-
case "escaped":
|
|
252
|
-
return "";
|
|
253
|
-
case "escaped_tag":
|
|
254
|
-
return escapeHtml(node.value);
|
|
255
|
-
default: {
|
|
256
|
-
const _exhaustive = node;
|
|
257
|
-
return "";
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
function renderToHtml(ast, options) {
|
|
262
|
-
const node = typeof ast === "string" ? JSON.parse(ast) : ast;
|
|
263
|
-
return renderNode(node, defaultContext(options));
|
|
264
|
-
}
|
|
265
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
266
|
-
0 && (module.exports = {
|
|
267
|
-
renderToHtml
|
|
268
|
-
});
|
package/dist/index.d.cts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { AstNode } from 'flatmarkdown-ast';
|
|
2
|
-
export { AlertNode, AstNode, BlockQuoteNode, CodeBlockNode, CodeNode, DescriptionDetailsNode, DescriptionItemNode, DescriptionListNode, DescriptionTermNode, DocumentNode, EmphNode, EscapedNode, EscapedTagNode, FootnoteDefinitionNode, FootnoteReferenceNode, FrontMatterNode, HashtagNode, HeadingNode, HeexBlockNode, HeexInlineNode, HighlightNode, HtmlBlockNode, HtmlInlineNode, ImageNode, ItemNode, LinebreakNode, LinkNode, ListNode, MathNode, MultilineBlockQuoteNode, ParagraphNode, RawNode, ShortcodeNode, SoftbreakNode, SpoileredTextNode, StrikethroughNode, StrongNode, SubscriptNode, SubtextNode, SuperscriptNode, TableCellNode, TableNode, TableRowNode, TaskItemNode, TextNode, ThematicBreakNode, UnderlineNode, WikilinkNode } from 'flatmarkdown-ast';
|
|
3
|
-
|
|
4
|
-
interface WikiLinkOptions {
|
|
5
|
-
/** Tag name. Default: 'a'. Use 'Link' for React Router, etc. */
|
|
6
|
-
tagName?: string;
|
|
7
|
-
/** URL attribute name. Default: 'href'. Use 'to' for React Router, etc. */
|
|
8
|
-
urlAttr?: string;
|
|
9
|
-
/** Prefix prepended to the wikilink url. Default: '' */
|
|
10
|
-
urlPrefix?: string;
|
|
11
|
-
/** Extra attributes added to the tag. Default: { 'data-wikilink': 'true' } */
|
|
12
|
-
attrs?: Record<string, string>;
|
|
13
|
-
}
|
|
14
|
-
interface HashtagOptions {
|
|
15
|
-
/** Tag name. Default: 'a'. Use 'Link' for React Router, etc. */
|
|
16
|
-
tagName?: string;
|
|
17
|
-
/** URL attribute name. Default: 'href'. Use 'to' for React Router, etc. */
|
|
18
|
-
urlAttr?: string;
|
|
19
|
-
/** Prefix prepended to the tag name to form the url. Default: '' */
|
|
20
|
-
urlPrefix?: string;
|
|
21
|
-
/** Extra attributes added to the tag. Default: { 'data-hashtag': 'true' } */
|
|
22
|
-
attrs?: Record<string, string>;
|
|
23
|
-
}
|
|
24
|
-
interface RenderOptions {
|
|
25
|
-
wikilink?: WikiLinkOptions;
|
|
26
|
-
hashtag?: HashtagOptions;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Render a flatmarkdown AST to HTML.
|
|
31
|
-
*
|
|
32
|
-
* @param ast - Either a parsed AstNode object or a JSON string of the AST
|
|
33
|
-
* @param options - Rendering options (e.g. wikilink customization)
|
|
34
|
-
* @returns HTML string
|
|
35
|
-
*/
|
|
36
|
-
declare function renderToHtml(ast: AstNode | string, options?: RenderOptions): string;
|
|
37
|
-
|
|
38
|
-
export { type HashtagOptions, type RenderOptions, type WikiLinkOptions, renderToHtml };
|
package/dist/index.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { AstNode } from 'flatmarkdown-ast';
|
|
2
|
-
export { AlertNode, AstNode, BlockQuoteNode, CodeBlockNode, CodeNode, DescriptionDetailsNode, DescriptionItemNode, DescriptionListNode, DescriptionTermNode, DocumentNode, EmphNode, EscapedNode, EscapedTagNode, FootnoteDefinitionNode, FootnoteReferenceNode, FrontMatterNode, HashtagNode, HeadingNode, HeexBlockNode, HeexInlineNode, HighlightNode, HtmlBlockNode, HtmlInlineNode, ImageNode, ItemNode, LinebreakNode, LinkNode, ListNode, MathNode, MultilineBlockQuoteNode, ParagraphNode, RawNode, ShortcodeNode, SoftbreakNode, SpoileredTextNode, StrikethroughNode, StrongNode, SubscriptNode, SubtextNode, SuperscriptNode, TableCellNode, TableNode, TableRowNode, TaskItemNode, TextNode, ThematicBreakNode, UnderlineNode, WikilinkNode } from 'flatmarkdown-ast';
|
|
3
|
-
|
|
4
|
-
interface WikiLinkOptions {
|
|
5
|
-
/** Tag name. Default: 'a'. Use 'Link' for React Router, etc. */
|
|
6
|
-
tagName?: string;
|
|
7
|
-
/** URL attribute name. Default: 'href'. Use 'to' for React Router, etc. */
|
|
8
|
-
urlAttr?: string;
|
|
9
|
-
/** Prefix prepended to the wikilink url. Default: '' */
|
|
10
|
-
urlPrefix?: string;
|
|
11
|
-
/** Extra attributes added to the tag. Default: { 'data-wikilink': 'true' } */
|
|
12
|
-
attrs?: Record<string, string>;
|
|
13
|
-
}
|
|
14
|
-
interface HashtagOptions {
|
|
15
|
-
/** Tag name. Default: 'a'. Use 'Link' for React Router, etc. */
|
|
16
|
-
tagName?: string;
|
|
17
|
-
/** URL attribute name. Default: 'href'. Use 'to' for React Router, etc. */
|
|
18
|
-
urlAttr?: string;
|
|
19
|
-
/** Prefix prepended to the tag name to form the url. Default: '' */
|
|
20
|
-
urlPrefix?: string;
|
|
21
|
-
/** Extra attributes added to the tag. Default: { 'data-hashtag': 'true' } */
|
|
22
|
-
attrs?: Record<string, string>;
|
|
23
|
-
}
|
|
24
|
-
interface RenderOptions {
|
|
25
|
-
wikilink?: WikiLinkOptions;
|
|
26
|
-
hashtag?: HashtagOptions;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Render a flatmarkdown AST to HTML.
|
|
31
|
-
*
|
|
32
|
-
* @param ast - Either a parsed AstNode object or a JSON string of the AST
|
|
33
|
-
* @param options - Rendering options (e.g. wikilink customization)
|
|
34
|
-
* @returns HTML string
|
|
35
|
-
*/
|
|
36
|
-
declare function renderToHtml(ast: AstNode | string, options?: RenderOptions): string;
|
|
37
|
-
|
|
38
|
-
export { type HashtagOptions, type RenderOptions, type WikiLinkOptions, renderToHtml };
|
package/dist/index.js
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
// src/renderer.ts
|
|
2
|
-
function defaultContext(options = {}) {
|
|
3
|
-
return {
|
|
4
|
-
options,
|
|
5
|
-
inHeaderRow: false,
|
|
6
|
-
tableAlignments: [],
|
|
7
|
-
cellIndex: 0,
|
|
8
|
-
inTightList: false,
|
|
9
|
-
tightList: false
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
function escapeHtml(str) {
|
|
13
|
-
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
14
|
-
}
|
|
15
|
-
function renderChildren(node, ctx) {
|
|
16
|
-
if (!node.children) return "";
|
|
17
|
-
return node.children.map((child) => renderNode(child, ctx)).join("");
|
|
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
|
-
}
|
|
23
|
-
function extractText(node) {
|
|
24
|
-
if (node.type === "text") return node.value;
|
|
25
|
-
if ("children" in node && node.children) {
|
|
26
|
-
return node.children.map(extractText).join("");
|
|
27
|
-
}
|
|
28
|
-
return "";
|
|
29
|
-
}
|
|
30
|
-
function renderNode(node, ctx) {
|
|
31
|
-
switch (node.type) {
|
|
32
|
-
// ── Block Nodes ──
|
|
33
|
-
case "document":
|
|
34
|
-
return renderChildren(node, ctx);
|
|
35
|
-
case "paragraph": {
|
|
36
|
-
const inner = renderChildren(node, ctx);
|
|
37
|
-
if (ctx.tightList) return `${inner}
|
|
38
|
-
`;
|
|
39
|
-
return `<p>${inner}</p>
|
|
40
|
-
`;
|
|
41
|
-
}
|
|
42
|
-
case "heading": {
|
|
43
|
-
const tag = `h${node.level}`;
|
|
44
|
-
return `<${tag}>${renderChildren(node, ctx)}</${tag}>
|
|
45
|
-
`;
|
|
46
|
-
}
|
|
47
|
-
case "code_block": {
|
|
48
|
-
const info = node.info;
|
|
49
|
-
const cls = info ? ` class="language-${escapeHtml(info)}"` : "";
|
|
50
|
-
return `<pre><code${cls}>${escapeHtml(node.literal)}</code></pre>
|
|
51
|
-
`;
|
|
52
|
-
}
|
|
53
|
-
case "block_quote":
|
|
54
|
-
return `<blockquote>
|
|
55
|
-
${renderChildren(node, ctx)}</blockquote>
|
|
56
|
-
`;
|
|
57
|
-
case "multiline_block_quote":
|
|
58
|
-
return `<blockquote>
|
|
59
|
-
${renderChildren(node, ctx)}</blockquote>
|
|
60
|
-
`;
|
|
61
|
-
case "list": {
|
|
62
|
-
const childCtx = { ...ctx, inTightList: node.tight };
|
|
63
|
-
if (node.list_type === "ordered") {
|
|
64
|
-
const startAttr = node.start != null && node.start !== 1 ? ` start="${node.start}"` : "";
|
|
65
|
-
return `<ol${startAttr}>
|
|
66
|
-
${renderChildren(node, childCtx)}</ol>
|
|
67
|
-
`;
|
|
68
|
-
}
|
|
69
|
-
return `<ul>
|
|
70
|
-
${renderChildren(node, childCtx)}</ul>
|
|
71
|
-
`;
|
|
72
|
-
}
|
|
73
|
-
case "item":
|
|
74
|
-
return `<li>${renderItemChildren(node, ctx, ctx.inTightList)}</li>
|
|
75
|
-
`;
|
|
76
|
-
case "task_item": {
|
|
77
|
-
const checked = node.symbol != null ? ' checked=""' : "";
|
|
78
|
-
return `<li><input type="checkbox"${checked} disabled="" />${renderItemChildren(node, ctx, true)}</li>
|
|
79
|
-
`;
|
|
80
|
-
}
|
|
81
|
-
case "table": {
|
|
82
|
-
const childCtx = { ...ctx, tableAlignments: node.alignments };
|
|
83
|
-
return `<table>
|
|
84
|
-
${renderChildren(node, childCtx)}</table>
|
|
85
|
-
`;
|
|
86
|
-
}
|
|
87
|
-
case "table_row": {
|
|
88
|
-
const cells = (node.children || []).map((cell, i) => {
|
|
89
|
-
const cellCtx = {
|
|
90
|
-
...ctx,
|
|
91
|
-
inHeaderRow: node.header,
|
|
92
|
-
cellIndex: i
|
|
93
|
-
};
|
|
94
|
-
return renderNode(cell, cellCtx);
|
|
95
|
-
});
|
|
96
|
-
return `<tr>
|
|
97
|
-
${cells.join("")}</tr>
|
|
98
|
-
`;
|
|
99
|
-
}
|
|
100
|
-
case "table_cell": {
|
|
101
|
-
const tag = ctx.inHeaderRow ? "th" : "td";
|
|
102
|
-
const alignment = ctx.tableAlignments[ctx.cellIndex];
|
|
103
|
-
const alignAttr = alignment && alignment !== "none" ? ` align="${alignment}"` : "";
|
|
104
|
-
return `<${tag}${alignAttr}>${renderChildren(node, ctx)}</${tag}>
|
|
105
|
-
`;
|
|
106
|
-
}
|
|
107
|
-
case "thematic_break":
|
|
108
|
-
return "<hr />\n";
|
|
109
|
-
case "html_block":
|
|
110
|
-
return node.literal;
|
|
111
|
-
case "footnote_definition": {
|
|
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)}`;
|
|
118
|
-
return `<div class="footnote" id="fn-${name}">
|
|
119
|
-
${body}</div>
|
|
120
|
-
`;
|
|
121
|
-
}
|
|
122
|
-
case "description_list":
|
|
123
|
-
return `<dl>
|
|
124
|
-
${renderChildren(node, ctx)}</dl>
|
|
125
|
-
`;
|
|
126
|
-
case "description_item":
|
|
127
|
-
return renderChildren(node, ctx);
|
|
128
|
-
case "description_term":
|
|
129
|
-
return `<dt>${renderChildren(node, ctx)}</dt>
|
|
130
|
-
`;
|
|
131
|
-
case "description_details":
|
|
132
|
-
return `<dd>${renderChildren(node, ctx)}</dd>
|
|
133
|
-
`;
|
|
134
|
-
case "alert":
|
|
135
|
-
return `<div class="alert alert-${escapeHtml(node.alert_type)}">
|
|
136
|
-
${renderChildren(node, ctx)}</div>
|
|
137
|
-
`;
|
|
138
|
-
case "front_matter":
|
|
139
|
-
return "";
|
|
140
|
-
case "heex_block":
|
|
141
|
-
return renderChildren(node, ctx);
|
|
142
|
-
case "subtext":
|
|
143
|
-
return renderChildren(node, ctx);
|
|
144
|
-
// ── Inline Nodes ──
|
|
145
|
-
case "text":
|
|
146
|
-
return escapeHtml(node.value);
|
|
147
|
-
case "softbreak":
|
|
148
|
-
return "\n";
|
|
149
|
-
case "linebreak":
|
|
150
|
-
return "<br />\n";
|
|
151
|
-
case "emph":
|
|
152
|
-
return `<em>${renderChildren(node, ctx)}</em>`;
|
|
153
|
-
case "strong":
|
|
154
|
-
return `<strong>${renderChildren(node, ctx)}</strong>`;
|
|
155
|
-
case "strikethrough":
|
|
156
|
-
return `<del>${renderChildren(node, ctx)}</del>`;
|
|
157
|
-
case "underline":
|
|
158
|
-
return `<u>${renderChildren(node, ctx)}</u>`;
|
|
159
|
-
case "highlight":
|
|
160
|
-
return `<mark>${renderChildren(node, ctx)}</mark>`;
|
|
161
|
-
case "superscript":
|
|
162
|
-
return `<sup>${renderChildren(node, ctx)}</sup>`;
|
|
163
|
-
case "subscript":
|
|
164
|
-
return `<sub>${renderChildren(node, ctx)}</sub>`;
|
|
165
|
-
case "spoilered_text":
|
|
166
|
-
return `<span class="spoiler">${renderChildren(node, ctx)}</span>`;
|
|
167
|
-
case "code":
|
|
168
|
-
return `<code>${escapeHtml(node.literal)}</code>`;
|
|
169
|
-
case "link": {
|
|
170
|
-
const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
|
|
171
|
-
return `<a href="${escapeHtml(node.url)}"${titleAttr}>${renderChildren(node, ctx)}</a>`;
|
|
172
|
-
}
|
|
173
|
-
case "image": {
|
|
174
|
-
const alt = extractText(node);
|
|
175
|
-
const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
|
|
176
|
-
return `<img src="${escapeHtml(node.url)}" alt="${escapeHtml(alt)}"${titleAttr} />`;
|
|
177
|
-
}
|
|
178
|
-
case "wikilink": {
|
|
179
|
-
const opts = ctx.options.wikilink;
|
|
180
|
-
const tagName = opts?.tagName ?? "a";
|
|
181
|
-
const urlAttr = opts?.urlAttr ?? "href";
|
|
182
|
-
const urlPrefix = opts?.urlPrefix ?? "";
|
|
183
|
-
const extraAttrs = opts?.attrs ?? { "data-wikilink": "true" };
|
|
184
|
-
const url = `${urlPrefix}${node.url}`;
|
|
185
|
-
let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
|
|
186
|
-
for (const [key, val] of Object.entries(extraAttrs)) {
|
|
187
|
-
attrStr += ` ${key}="${escapeHtml(val)}"`;
|
|
188
|
-
}
|
|
189
|
-
return `<${tagName} ${attrStr}>${renderChildren(node, ctx)}</${tagName}>`;
|
|
190
|
-
}
|
|
191
|
-
case "hashtag": {
|
|
192
|
-
const opts = ctx.options.hashtag;
|
|
193
|
-
const tagName = opts?.tagName ?? "a";
|
|
194
|
-
const urlAttr = opts?.urlAttr ?? "href";
|
|
195
|
-
const urlPrefix = opts?.urlPrefix ?? "";
|
|
196
|
-
const extraAttrs = opts?.attrs ?? { "data-hashtag": "true" };
|
|
197
|
-
const url = `${urlPrefix}${node.value}`;
|
|
198
|
-
let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
|
|
199
|
-
for (const [key, val] of Object.entries(extraAttrs)) {
|
|
200
|
-
attrStr += ` ${key}="${escapeHtml(val)}"`;
|
|
201
|
-
}
|
|
202
|
-
return `<${tagName} ${attrStr}>#${escapeHtml(node.value)}</${tagName}>`;
|
|
203
|
-
}
|
|
204
|
-
// The label is shown instead of `ix`: an item body is parsed alone, so `ix`
|
|
205
|
-
// restarts at 1 in every item.
|
|
206
|
-
case "footnote_reference": {
|
|
207
|
-
const name = escapeHtml(node.name);
|
|
208
|
-
return `<sup id="fnref-${name}" class="footnote-ref"><a href="#fn-${name}">[${name}]</a></sup>`;
|
|
209
|
-
}
|
|
210
|
-
case "shortcode":
|
|
211
|
-
return node.emoji;
|
|
212
|
-
case "math": {
|
|
213
|
-
if (node.display_math) {
|
|
214
|
-
return `<pre><code class="math math-display">${escapeHtml(node.literal)}</code></pre>
|
|
215
|
-
`;
|
|
216
|
-
}
|
|
217
|
-
return `<code class="math math-inline">${escapeHtml(node.literal)}</code>`;
|
|
218
|
-
}
|
|
219
|
-
case "html_inline":
|
|
220
|
-
return node.value;
|
|
221
|
-
case "heex_inline":
|
|
222
|
-
return node.value;
|
|
223
|
-
case "raw":
|
|
224
|
-
return node.value;
|
|
225
|
-
case "escaped":
|
|
226
|
-
return "";
|
|
227
|
-
case "escaped_tag":
|
|
228
|
-
return escapeHtml(node.value);
|
|
229
|
-
default: {
|
|
230
|
-
const _exhaustive = node;
|
|
231
|
-
return "";
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
function renderToHtml(ast, options) {
|
|
236
|
-
const node = typeof ast === "string" ? JSON.parse(ast) : ast;
|
|
237
|
-
return renderNode(node, defaultContext(options));
|
|
238
|
-
}
|
|
239
|
-
export {
|
|
240
|
-
renderToHtml
|
|
241
|
-
};
|