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/README.md CHANGED
@@ -1,225 +1,73 @@
1
1
  # flatmarkdown-ast2html
2
2
 
3
- Renders a [flatmarkdown](https://github.com/sosuisen/flatmarkdown) JSON AST to HTML in TypeScript.
3
+ Renders the [Flat Markdown](https://github.com/petajournal/flatmarkdown) AST to HTML in a web page.
4
4
 
5
- The `flatmarkdown` Rust crate exposes `markdown_to_ast()` which converts Markdown into a JSON AST. This library takes that AST and produces HTML — no Rust toolchain needed at render time.
5
+ This npm package is the Rust crate [flatmarkdown-ast2html](https://crates.io/crates/flatmarkdown-ast2html), built as WebAssembly.
6
+ So the package and the crate give the same HTML.
7
+ The version of the package is the version of the crate.
6
8
 
7
- ## Install
9
+ The HTML is described in the [README of the crate](https://github.com/petajournal/flatmarkdown/tree/main/flatmarkdown-ast2html#readme).
8
10
 
9
- ```bash
10
- bun add flatmarkdown-ast2html
11
- # or
12
- npm install flatmarkdown-ast2html
13
- ```
14
-
15
- ## Browser / WebView (IIFE)
11
+ ## Use
16
12
 
17
- A minified IIFE build is included for non-module environments (plain `<script>` tags, WebView2, Electron, etc.).
13
+ The package is one JavaScript file, `dist/index.global.js`.
14
+ The WebAssembly code is inside that file, so a page needs no second file.
15
+ Load the file with a `<script>` tag.
18
16
 
19
17
  ```html
20
- <script src="https://unpkg.com/flatmarkdown-ast2html@0.1.3"></script>
18
+ <script src="https://unpkg.com/flatmarkdown-ast2html@0.6.0"></script>
21
19
  <script>
22
- var html = FmAst2Html.renderToHtml(astJson);
23
- document.getElementById('content').innerHTML = html;
20
+ const html = FmAst2Html.renderToHtml(
21
+ '{"type":"paragraph","children":[{"type":"text","value":"Hello"}]}',
22
+ );
23
+ // "<p>Hello</p>\n"
24
24
  </script>
25
25
  ```
26
26
 
27
- The global `FmAst2Html` object exposes `renderToHtml(ast, options?)`.
28
-
29
- To pin a specific version, use `@0.1.3` in the URL. Without a version tag, unpkg serves the latest.
30
-
31
- ## Build
32
-
33
- ```bash
34
- npm install
35
- npm run build # outputs dist/ with ESM, CJS, IIFE, and .d.tsう
36
- npm test # runs vitest
37
- npm publish # publish to npm (also available via unpkg)
38
- ```
39
-
40
- ## Quick Start
41
-
42
- ```ts
43
- import { renderToHtml } from 'flatmarkdown-ast2html';
44
-
45
- // From a parsed AST object
46
- const ast = {
47
- type: 'document',
48
- children: [
49
- {
50
- type: 'paragraph',
51
- children: [
52
- { type: 'text', value: 'Hello ' },
53
- { type: 'strong', children: [{ type: 'text', value: 'world' }] },
54
- ],
55
- },
56
- ],
57
- };
58
-
59
- const html = renderToHtml(ast);
60
- // => '<p>Hello <strong>world</strong></p>\n'
61
-
62
- // From a JSON string (e.g. directly from flatmarkdown CLI/WASM output)
63
- const json = '{"type":"document","children":[{"type":"paragraph","children":[{"type":"text","value":"Hi"}]}]}';
64
- const html2 = renderToHtml(json);
65
- // => '<p>Hi</p>\n'
66
- ```
67
-
68
- ## API
69
-
70
- ### `renderToHtml(ast, options?)`
71
-
72
- ```ts
73
- function renderToHtml(ast: AstNode | string, options?: RenderOptions): string;
74
- ```
75
-
76
- | Parameter | Type | Description |
77
- |-----------|------|-------------|
78
- | `ast` | `AstNode \| string` | Parsed AST object or JSON string |
79
- | `options` | `RenderOptions` | Optional rendering configuration |
80
-
81
- Returns an HTML string.
27
+ The script adds one global object, `FmAst2Html`.
82
28
 
83
- ### `RenderOptions`
29
+ ### `FmAst2Html.renderToHtml(ast)`
84
30
 
85
- ```ts
86
- interface RenderOptions {
87
- wikilink?: WikiLinkOptions;
88
- hashtag?: HashtagOptions;
89
- }
90
- ```
91
-
92
- ### `WikiLinkOptions`
31
+ `ast` is the AST as a JSON string, or as an object.
32
+ The function returns the HTML as a string. It does not return a promise.
93
33
 
94
- Customize how `[[wikilinks]]` are rendered. This makes the output compatible with any routing library or framework.
34
+ The function throws when `ast` cannot be read as an AST.
35
+ For example, it throws for a string that is not JSON.
36
+ A node of an unknown `type` is not an error. Only its children are rendered.
95
37
 
96
- ```ts
97
- interface WikiLinkOptions {
98
- tagName?: string; // default: 'a'
99
- urlAttr?: string; // default: 'href'
100
- urlPrefix?: string; // default: ''
101
- attrs?: Record<string, string>; // default: { 'data-wikilink': 'true' }
102
- }
103
- ```
38
+ The HTML holds the raw HTML of the Markdown as it is.
39
+ Do not render the AST of Markdown that you do not trust.
104
40
 
105
- | Field | Default | Description |
106
- |-------|---------|-------------|
107
- | `tagName` | `'a'` | HTML/component tag name. Use `'Link'` for React Router, `'A'` for SolidJS, etc. |
108
- | `urlAttr` | `'href'` | Attribute name for the URL. Use `'to'` for React Router, `'routerLink'` for Angular. |
109
- | `urlPrefix` | `''` | String prepended to the wikilink URL value. |
110
- | `attrs` | `{ 'data-wikilink': 'true' }` | Extra attributes on the tag. Replaces defaults when provided. |
41
+ ## Changes from 0.1
111
42
 
112
- #### Examples
43
+ Versions 0.1.x were written in TypeScript. From 0.6.0, the package is built from Rust.
113
44
 
114
- **Default** — `[[page-name|label]]` renders as:
45
+ - The second argument of `renderToHtml`, the options for wikilinks and hashtags, is removed.
46
+ - The ES module build, the CommonJS build, and the TypeScript types are removed.
47
+ - The types of the AST are not exported. They are in the npm package [flatmarkdown-ast](https://www.npmjs.com/package/flatmarkdown-ast).
48
+ - The file is about 240KB. It was about 4KB.
115
49
 
116
- ```html
117
- <a href="page-name" data-wikilink="true">label</a>
118
- ```
50
+ ## Build
119
51
 
120
- **React Router**:
121
-
122
- ```ts
123
- renderToHtml(ast, {
124
- wikilink: {
125
- tagName: 'Link',
126
- urlAttr: 'to',
127
- urlPrefix: '/wiki/',
128
- attrs: { className: 'wikilink', 'data-wikilink': 'true' },
129
- },
130
- });
131
- ```
52
+ You need Rust with the target `wasm32-unknown-unknown`, [wasm-pack](https://rustwasm.github.io/wasm-pack/), and Node.js.
132
53
 
133
- ```html
134
- <Link to="/wiki/page-name" className="wikilink" data-wikilink="true">label</Link>
54
+ ```sh
55
+ rustup target add wasm32-unknown-unknown
56
+ cargo install wasm-pack --locked
135
57
  ```
136
58
 
137
- **URL prefix only**:
59
+ Run these in this folder.
138
60
 
139
- ```ts
140
- renderToHtml(ast, {
141
- wikilink: { urlPrefix: '/docs/' },
142
- });
61
+ ```sh
62
+ npm run build
63
+ npm test
143
64
  ```
144
65
 
145
- ```html
146
- <a href="/docs/page-name" data-wikilink="true">label</a>
147
- ```
148
-
149
- ### `HashtagOptions`
150
-
151
- Customize how `#hashtag` nodes are rendered. The fields are the same as `WikiLinkOptions`. The url is `urlPrefix` followed by the tag name, and the default extra attribute is `{ 'data-hashtag': 'true' }`.
152
-
153
- ```html
154
- <a href="todo" data-hashtag="true">#todo</a>
155
- ```
66
+ `npm run build` writes `dist/index.global.js`.
67
+ `npm test` runs that file in Node.js, as a page would run it. Build before you test.
156
68
 
157
- ## HTML Output Reference
158
-
159
- ### Block Nodes
160
-
161
- | AST `type` | HTML |
162
- |------------|------|
163
- | `document` | Children concatenated (no wrapper) |
164
- | `paragraph` | `<p>…</p>` (omitted in tight lists) |
165
- | `heading` | `<h1>`–`<h6>` based on `level` |
166
- | `code_block` | `<pre><code class="language-{info}">…</code></pre>` |
167
- | `block_quote` | `<blockquote>…</blockquote>` |
168
- | `multiline_block_quote` | `<blockquote>…</blockquote>` |
169
- | `list` (bullet) | `<ul>…</ul>` |
170
- | `list` (ordered) | `<ol start="{start}">…</ol>` |
171
- | `item` | `<li>…</li>` |
172
- | `task_item` | `<li><input type="checkbox" checked="" disabled="" />…</li>` |
173
- | `table` | `<table>…</table>` |
174
- | `table_row` | `<tr>…</tr>` |
175
- | `table_cell` | `<td>…</td>` or `<th>…</th>` (header row), with `align` attr |
176
- | `thematic_break` | `<hr />` |
177
- | `html_block` | Raw passthrough |
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
- | `description_list` | `<dl>…</dl>` |
180
- | `description_term` | `<dt>…</dt>` |
181
- | `description_details` | `<dd>…</dd>` |
182
- | `alert` | `<div class="alert alert-{type}">…</div>` |
183
- | `front_matter` | Skipped (not rendered) |
184
-
185
- ### Inline Nodes
186
-
187
- | AST `type` | HTML |
188
- |------------|------|
189
- | `text` | HTML-escaped text |
190
- | `softbreak` | `\n` |
191
- | `linebreak` | `<br />` |
192
- | `emph` | `<em>…</em>` |
193
- | `strong` | `<strong>…</strong>` |
194
- | `strikethrough` | `<del>…</del>` |
195
- | `underline` | `<u>…</u>` |
196
- | `highlight` | `<mark>…</mark>` |
197
- | `superscript` | `<sup>…</sup>` |
198
- | `subscript` | `<sub>…</sub>` |
199
- | `spoilered_text` | `<span class="spoiler">…</span>` |
200
- | `code` | `<code>…</code>` |
201
- | `link` | `<a href="{url}" title="{title}">…</a>` |
202
- | `image` | `<img src="{url}" alt="…" title="{title}" />` |
203
- | `hashtag` | `<a href="{name}" data-hashtag="true">#{name}</a>` |
204
- | `wikilink` | `<a href="{url}" data-wikilink="true">…</a>` (configurable) |
205
- | `footnote_reference` | `<sup id="fnref-{name}" class="footnote-ref"><a href="#fn-{name}">[{name}]</a></sup>` |
206
- | `shortcode` | Emoji character directly |
207
- | `math` (inline) | `<code class="math math-inline">…</code>` |
208
- | `math` (display) | `<pre><code class="math math-display">…</code></pre>` |
209
- | `html_inline` | Raw passthrough |
210
- | `escaped` | Skipped |
211
- | `escaped_tag` | HTML-escaped value |
212
-
213
- ## Supported AST Node Types
214
-
215
- 46 node types total, matching the `flatmarkdown` Rust crate output.
216
-
217
- **Block (23):** `document`, `paragraph`, `heading`, `code_block`, `block_quote`, `multiline_block_quote`, `list`, `item`, `task_item`, `table`, `table_row`, `table_cell`, `thematic_break`, `html_block`, `footnote_definition`, `description_list`, `description_item`, `description_term`, `description_details`, `alert`, `front_matter`, `heex_block`, `subtext`
218
-
219
- **Inline (24):** `text`, `softbreak`, `linebreak`, `emph`, `strong`, `strikethrough`, `underline`, `highlight`, `superscript`, `subscript`, `spoilered_text`, `code`, `link`, `image`, `footnote_reference`, `shortcode`, `math`, `html_inline`, `heex_inline`, `raw`, `escaped`, `escaped_tag`, `wikilink`, `hashtag`
220
-
221
- All node types are exported as individual TypeScript interfaces, plus the `AstNode` discriminated union.
69
+ The package has no npm dependencies. You do not need `npm install`.
222
70
 
223
71
  ## License
224
72
 
225
- MIT
73
+ MIT
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Hidekazu Kubota
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hidekazu Kubota
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.