flatmarkdown-ast2html 0.1.6 → 0.6.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/package.json CHANGED
@@ -1,50 +1,33 @@
1
1
  {
2
2
  "name": "flatmarkdown-ast2html",
3
- "version": "0.1.6",
4
- "description": "A library to convert FlatMarkdown AST to HTML.",
5
- "type": "module",
6
- "main": "./dist/index.cjs",
7
- "module": "./dist/index.js",
8
- "types": "./dist/index.d.ts",
3
+ "version": "0.6.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-ast2html.git"
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-ast2html/issues"
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": "tsup",
30
- "test": "vitest run",
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
- "parser"
29
+ "wasm"
39
30
  ],
40
31
  "author": "Hidekazu Kubota",
41
- "license": "MIT",
42
- "dependencies": {
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
- }
32
+ "license": "MIT"
50
33
  }
package/dist/index.cjs DELETED
@@ -1,265 +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
- tightList: false
35
- };
36
- }
37
- function escapeHtml(str) {
38
- return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
39
- }
40
- function renderChildren(node, ctx) {
41
- if (!node.children) return "";
42
- return node.children.map((child) => renderNode(child, ctx)).join("");
43
- }
44
- function extractText(node) {
45
- if (node.type === "text") return node.value;
46
- if ("children" in node && node.children) {
47
- return node.children.map(extractText).join("");
48
- }
49
- return "";
50
- }
51
- function renderNode(node, ctx) {
52
- switch (node.type) {
53
- // ── Block Nodes ──
54
- case "document":
55
- return renderChildren(node, ctx);
56
- case "paragraph": {
57
- const inner = renderChildren(node, ctx);
58
- if (ctx.tightList) return `${inner}
59
- `;
60
- return `<p>${inner}</p>
61
- `;
62
- }
63
- case "heading": {
64
- const tag = `h${node.level}`;
65
- return `<${tag}>${renderChildren(node, ctx)}</${tag}>
66
- `;
67
- }
68
- case "code_block": {
69
- const info = node.info;
70
- const cls = info ? ` class="language-${escapeHtml(info)}"` : "";
71
- return `<pre><code${cls}>${escapeHtml(node.literal)}</code></pre>
72
- `;
73
- }
74
- case "block_quote":
75
- return `<blockquote>
76
- ${renderChildren(node, ctx)}</blockquote>
77
- `;
78
- case "multiline_block_quote":
79
- return `<blockquote>
80
- ${renderChildren(node, ctx)}</blockquote>
81
- `;
82
- case "list": {
83
- if (node.list_type === "ordered") {
84
- const startAttr = node.start != null && node.start !== 1 ? ` start="${node.start}"` : "";
85
- return `<ol${startAttr}>
86
- ${renderChildren(node, ctx)}</ol>
87
- `;
88
- }
89
- return `<ul>
90
- ${renderChildren(node, ctx)}</ul>
91
- `;
92
- }
93
- case "item": {
94
- const childCtx = { ...ctx, tightList: node.tight };
95
- return `<li>${renderChildren(node, childCtx)}</li>
96
- `;
97
- }
98
- case "task_item": {
99
- const checked = node.symbol != null ? ' checked=""' : "";
100
- const childCtx = { ...ctx, tightList: true };
101
- return `<li><input type="checkbox"${checked} disabled="" />${renderChildren(node, childCtx)}</li>
102
- `;
103
- }
104
- case "table": {
105
- const childCtx = { ...ctx, tableAlignments: node.alignments };
106
- return `<table>
107
- ${renderChildren(node, childCtx)}</table>
108
- `;
109
- }
110
- case "table_row": {
111
- const cells = (node.children || []).map((cell, i) => {
112
- const cellCtx = {
113
- ...ctx,
114
- inHeaderRow: node.header,
115
- cellIndex: i
116
- };
117
- return renderNode(cell, cellCtx);
118
- });
119
- return `<tr>
120
- ${cells.join("")}</tr>
121
- `;
122
- }
123
- case "table_cell": {
124
- const tag = ctx.inHeaderRow ? "th" : "td";
125
- const alignment = ctx.tableAlignments[ctx.cellIndex];
126
- const alignAttr = alignment && alignment !== "none" ? ` align="${alignment}"` : "";
127
- return `<${tag}${alignAttr}>${renderChildren(node, ctx)}</${tag}>
128
- `;
129
- }
130
- case "thematic_break":
131
- return "<hr />\n";
132
- case "html_block":
133
- return node.literal;
134
- case "footnote_definition": {
135
- const name = escapeHtml(node.name);
136
- const label = `<a href="#fnref-${name}" class="footnote-label">[${name}]:</a>`;
137
- const [first, ...rest] = node.children ?? [];
138
- const body = first?.type === "paragraph" ? `<p>${label} ${renderChildren(first, ctx)}</p>
139
- ${rest.map((c) => renderNode(c, ctx)).join("")}` : `${label}
140
- ${renderChildren(node, ctx)}`;
141
- return `<div class="footnote" id="fn-${name}">
142
- ${body}</div>
143
- `;
144
- }
145
- case "description_list":
146
- return `<dl>
147
- ${renderChildren(node, ctx)}</dl>
148
- `;
149
- case "description_item":
150
- return renderChildren(node, ctx);
151
- case "description_term":
152
- return `<dt>${renderChildren(node, ctx)}</dt>
153
- `;
154
- case "description_details":
155
- return `<dd>${renderChildren(node, ctx)}</dd>
156
- `;
157
- case "alert":
158
- return `<div class="alert alert-${escapeHtml(node.alert_type)}">
159
- ${renderChildren(node, ctx)}</div>
160
- `;
161
- case "front_matter":
162
- return "";
163
- case "heex_block":
164
- return renderChildren(node, ctx);
165
- case "subtext":
166
- return renderChildren(node, ctx);
167
- // ── Inline Nodes ──
168
- case "text":
169
- return escapeHtml(node.value);
170
- case "softbreak":
171
- return "\n";
172
- case "linebreak":
173
- return "<br />\n";
174
- case "emph":
175
- return `<em>${renderChildren(node, ctx)}</em>`;
176
- case "strong":
177
- return `<strong>${renderChildren(node, ctx)}</strong>`;
178
- case "strikethrough":
179
- return `<del>${renderChildren(node, ctx)}</del>`;
180
- case "underline":
181
- return `<u>${renderChildren(node, ctx)}</u>`;
182
- case "highlight":
183
- return `<mark>${renderChildren(node, ctx)}</mark>`;
184
- case "superscript":
185
- return `<sup>${renderChildren(node, ctx)}</sup>`;
186
- case "subscript":
187
- return `<sub>${renderChildren(node, ctx)}</sub>`;
188
- case "spoilered_text":
189
- return `<span class="spoiler">${renderChildren(node, ctx)}</span>`;
190
- case "code":
191
- return `<code>${escapeHtml(node.literal)}</code>`;
192
- case "link": {
193
- const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
194
- return `<a href="${escapeHtml(node.url)}"${titleAttr}>${renderChildren(node, ctx)}</a>`;
195
- }
196
- case "image": {
197
- const alt = extractText(node);
198
- const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
199
- return `<img src="${escapeHtml(node.url)}" alt="${escapeHtml(alt)}"${titleAttr} />`;
200
- }
201
- case "wikilink": {
202
- const opts = ctx.options.wikilink;
203
- const tagName = opts?.tagName ?? "a";
204
- const urlAttr = opts?.urlAttr ?? "href";
205
- const urlPrefix = opts?.urlPrefix ?? "";
206
- const extraAttrs = opts?.attrs ?? { "data-wikilink": "true" };
207
- const url = `${urlPrefix}${node.url}`;
208
- let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
209
- for (const [key, val] of Object.entries(extraAttrs)) {
210
- attrStr += ` ${key}="${escapeHtml(val)}"`;
211
- }
212
- return `<${tagName} ${attrStr}>${renderChildren(node, ctx)}</${tagName}>`;
213
- }
214
- case "hashtag": {
215
- const opts = ctx.options.hashtag;
216
- const tagName = opts?.tagName ?? "a";
217
- const urlAttr = opts?.urlAttr ?? "href";
218
- const urlPrefix = opts?.urlPrefix ?? "";
219
- const extraAttrs = opts?.attrs ?? { "data-hashtag": "true" };
220
- const url = `${urlPrefix}${node.value}`;
221
- let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
222
- for (const [key, val] of Object.entries(extraAttrs)) {
223
- attrStr += ` ${key}="${escapeHtml(val)}"`;
224
- }
225
- return `<${tagName} ${attrStr}>#${escapeHtml(node.value)}</${tagName}>`;
226
- }
227
- // The label is shown instead of `ix`: an item body is parsed alone, so `ix`
228
- // restarts at 1 in every item.
229
- case "footnote_reference": {
230
- const name = escapeHtml(node.name);
231
- return `<sup id="fnref-${name}" class="footnote-ref"><a href="#fn-${name}">[${name}]</a></sup>`;
232
- }
233
- case "shortcode":
234
- return node.emoji;
235
- case "math": {
236
- if (node.display_math) {
237
- return `<pre><code class="math math-display">${escapeHtml(node.literal)}</code></pre>
238
- `;
239
- }
240
- return `<code class="math math-inline">${escapeHtml(node.literal)}</code>`;
241
- }
242
- case "html_inline":
243
- return node.value;
244
- case "heex_inline":
245
- return node.value;
246
- case "raw":
247
- return node.value;
248
- case "escaped":
249
- return "";
250
- case "escaped_tag":
251
- return escapeHtml(node.value);
252
- default: {
253
- const _exhaustive = node;
254
- return "";
255
- }
256
- }
257
- }
258
- function renderToHtml(ast, options) {
259
- const node = typeof ast === "string" ? JSON.parse(ast) : ast;
260
- return renderNode(node, defaultContext(options));
261
- }
262
- // Annotate the CommonJS export names for ESM import in node:
263
- 0 && (module.exports = {
264
- renderToHtml
265
- });
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,238 +0,0 @@
1
- // src/renderer.ts
2
- function defaultContext(options = {}) {
3
- return {
4
- options,
5
- inHeaderRow: false,
6
- tableAlignments: [],
7
- cellIndex: 0,
8
- tightList: false
9
- };
10
- }
11
- function escapeHtml(str) {
12
- return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
13
- }
14
- function renderChildren(node, ctx) {
15
- if (!node.children) return "";
16
- return node.children.map((child) => renderNode(child, ctx)).join("");
17
- }
18
- function extractText(node) {
19
- if (node.type === "text") return node.value;
20
- if ("children" in node && node.children) {
21
- return node.children.map(extractText).join("");
22
- }
23
- return "";
24
- }
25
- function renderNode(node, ctx) {
26
- switch (node.type) {
27
- // ── Block Nodes ──
28
- case "document":
29
- return renderChildren(node, ctx);
30
- case "paragraph": {
31
- const inner = renderChildren(node, ctx);
32
- if (ctx.tightList) return `${inner}
33
- `;
34
- return `<p>${inner}</p>
35
- `;
36
- }
37
- case "heading": {
38
- const tag = `h${node.level}`;
39
- return `<${tag}>${renderChildren(node, ctx)}</${tag}>
40
- `;
41
- }
42
- case "code_block": {
43
- const info = node.info;
44
- const cls = info ? ` class="language-${escapeHtml(info)}"` : "";
45
- return `<pre><code${cls}>${escapeHtml(node.literal)}</code></pre>
46
- `;
47
- }
48
- case "block_quote":
49
- return `<blockquote>
50
- ${renderChildren(node, ctx)}</blockquote>
51
- `;
52
- case "multiline_block_quote":
53
- return `<blockquote>
54
- ${renderChildren(node, ctx)}</blockquote>
55
- `;
56
- case "list": {
57
- if (node.list_type === "ordered") {
58
- const startAttr = node.start != null && node.start !== 1 ? ` start="${node.start}"` : "";
59
- return `<ol${startAttr}>
60
- ${renderChildren(node, ctx)}</ol>
61
- `;
62
- }
63
- return `<ul>
64
- ${renderChildren(node, ctx)}</ul>
65
- `;
66
- }
67
- case "item": {
68
- const childCtx = { ...ctx, tightList: node.tight };
69
- return `<li>${renderChildren(node, childCtx)}</li>
70
- `;
71
- }
72
- case "task_item": {
73
- const checked = node.symbol != null ? ' checked=""' : "";
74
- const childCtx = { ...ctx, tightList: true };
75
- return `<li><input type="checkbox"${checked} disabled="" />${renderChildren(node, childCtx)}</li>
76
- `;
77
- }
78
- case "table": {
79
- const childCtx = { ...ctx, tableAlignments: node.alignments };
80
- return `<table>
81
- ${renderChildren(node, childCtx)}</table>
82
- `;
83
- }
84
- case "table_row": {
85
- const cells = (node.children || []).map((cell, i) => {
86
- const cellCtx = {
87
- ...ctx,
88
- inHeaderRow: node.header,
89
- cellIndex: i
90
- };
91
- return renderNode(cell, cellCtx);
92
- });
93
- return `<tr>
94
- ${cells.join("")}</tr>
95
- `;
96
- }
97
- case "table_cell": {
98
- const tag = ctx.inHeaderRow ? "th" : "td";
99
- const alignment = ctx.tableAlignments[ctx.cellIndex];
100
- const alignAttr = alignment && alignment !== "none" ? ` align="${alignment}"` : "";
101
- return `<${tag}${alignAttr}>${renderChildren(node, ctx)}</${tag}>
102
- `;
103
- }
104
- case "thematic_break":
105
- return "<hr />\n";
106
- case "html_block":
107
- return node.literal;
108
- case "footnote_definition": {
109
- const name = escapeHtml(node.name);
110
- const label = `<a href="#fnref-${name}" class="footnote-label">[${name}]:</a>`;
111
- const [first, ...rest] = node.children ?? [];
112
- const body = first?.type === "paragraph" ? `<p>${label} ${renderChildren(first, ctx)}</p>
113
- ${rest.map((c) => renderNode(c, ctx)).join("")}` : `${label}
114
- ${renderChildren(node, ctx)}`;
115
- return `<div class="footnote" id="fn-${name}">
116
- ${body}</div>
117
- `;
118
- }
119
- case "description_list":
120
- return `<dl>
121
- ${renderChildren(node, ctx)}</dl>
122
- `;
123
- case "description_item":
124
- return renderChildren(node, ctx);
125
- case "description_term":
126
- return `<dt>${renderChildren(node, ctx)}</dt>
127
- `;
128
- case "description_details":
129
- return `<dd>${renderChildren(node, ctx)}</dd>
130
- `;
131
- case "alert":
132
- return `<div class="alert alert-${escapeHtml(node.alert_type)}">
133
- ${renderChildren(node, ctx)}</div>
134
- `;
135
- case "front_matter":
136
- return "";
137
- case "heex_block":
138
- return renderChildren(node, ctx);
139
- case "subtext":
140
- return renderChildren(node, ctx);
141
- // ── Inline Nodes ──
142
- case "text":
143
- return escapeHtml(node.value);
144
- case "softbreak":
145
- return "\n";
146
- case "linebreak":
147
- return "<br />\n";
148
- case "emph":
149
- return `<em>${renderChildren(node, ctx)}</em>`;
150
- case "strong":
151
- return `<strong>${renderChildren(node, ctx)}</strong>`;
152
- case "strikethrough":
153
- return `<del>${renderChildren(node, ctx)}</del>`;
154
- case "underline":
155
- return `<u>${renderChildren(node, ctx)}</u>`;
156
- case "highlight":
157
- return `<mark>${renderChildren(node, ctx)}</mark>`;
158
- case "superscript":
159
- return `<sup>${renderChildren(node, ctx)}</sup>`;
160
- case "subscript":
161
- return `<sub>${renderChildren(node, ctx)}</sub>`;
162
- case "spoilered_text":
163
- return `<span class="spoiler">${renderChildren(node, ctx)}</span>`;
164
- case "code":
165
- return `<code>${escapeHtml(node.literal)}</code>`;
166
- case "link": {
167
- const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
168
- return `<a href="${escapeHtml(node.url)}"${titleAttr}>${renderChildren(node, ctx)}</a>`;
169
- }
170
- case "image": {
171
- const alt = extractText(node);
172
- const titleAttr = node.title ? ` title="${escapeHtml(node.title)}"` : "";
173
- return `<img src="${escapeHtml(node.url)}" alt="${escapeHtml(alt)}"${titleAttr} />`;
174
- }
175
- case "wikilink": {
176
- const opts = ctx.options.wikilink;
177
- const tagName = opts?.tagName ?? "a";
178
- const urlAttr = opts?.urlAttr ?? "href";
179
- const urlPrefix = opts?.urlPrefix ?? "";
180
- const extraAttrs = opts?.attrs ?? { "data-wikilink": "true" };
181
- const url = `${urlPrefix}${node.url}`;
182
- let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
183
- for (const [key, val] of Object.entries(extraAttrs)) {
184
- attrStr += ` ${key}="${escapeHtml(val)}"`;
185
- }
186
- return `<${tagName} ${attrStr}>${renderChildren(node, ctx)}</${tagName}>`;
187
- }
188
- case "hashtag": {
189
- const opts = ctx.options.hashtag;
190
- const tagName = opts?.tagName ?? "a";
191
- const urlAttr = opts?.urlAttr ?? "href";
192
- const urlPrefix = opts?.urlPrefix ?? "";
193
- const extraAttrs = opts?.attrs ?? { "data-hashtag": "true" };
194
- const url = `${urlPrefix}${node.value}`;
195
- let attrStr = `${urlAttr}="${escapeHtml(url)}"`;
196
- for (const [key, val] of Object.entries(extraAttrs)) {
197
- attrStr += ` ${key}="${escapeHtml(val)}"`;
198
- }
199
- return `<${tagName} ${attrStr}>#${escapeHtml(node.value)}</${tagName}>`;
200
- }
201
- // The label is shown instead of `ix`: an item body is parsed alone, so `ix`
202
- // restarts at 1 in every item.
203
- case "footnote_reference": {
204
- const name = escapeHtml(node.name);
205
- return `<sup id="fnref-${name}" class="footnote-ref"><a href="#fn-${name}">[${name}]</a></sup>`;
206
- }
207
- case "shortcode":
208
- return node.emoji;
209
- case "math": {
210
- if (node.display_math) {
211
- return `<pre><code class="math math-display">${escapeHtml(node.literal)}</code></pre>
212
- `;
213
- }
214
- return `<code class="math math-inline">${escapeHtml(node.literal)}</code>`;
215
- }
216
- case "html_inline":
217
- return node.value;
218
- case "heex_inline":
219
- return node.value;
220
- case "raw":
221
- return node.value;
222
- case "escaped":
223
- return "";
224
- case "escaped_tag":
225
- return escapeHtml(node.value);
226
- default: {
227
- const _exhaustive = node;
228
- return "";
229
- }
230
- }
231
- }
232
- function renderToHtml(ast, options) {
233
- const node = typeof ast === "string" ? JSON.parse(ast) : ast;
234
- return renderNode(node, defaultContext(options));
235
- }
236
- export {
237
- renderToHtml
238
- };