1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use crate::ast::{
Document,
tokens::{
Attributes,
token::{
Block, EmphasisLevel, Inline, ListItem, Table as AstTable, TableCellAlignment,
TaskState,
},
},
};
use napi_derive::napi;
use serde::{Deserialize, Serialize};
/// Options that control how the AST is rendered to HTML.
#[napi(object)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RenderOptions {
/// If `true`, emit only the body content (no `<!DOCTYPE>`, `<html>`,
/// `<head>`, or `<body>` wrapper). If `false`, emit a full HTML
/// document. Default: `true` (fragment).
pub fragment: bool,
/// Include a `<meta charset="utf-8">` in the head (only relevant when
/// `fragment` is `false`). Default: `true`.
pub charset: bool,
/// Include a `<meta name="viewport" content="width=device-width,
/// initial-scale=1.0">` in the head (only relevant when `fragment` is
/// `false`). Default: `true`.
pub viewport: bool,
/// Optional `<title>` for the HTML head (only relevant when
/// `fragment` is `false`). Default: `None`.
pub title: Option<String>,
/// Optional additional CSS classes to add to `<body>` (only relevant
/// when `fragment` is `false`). Default: `None`.
pub body_class: Option<String>,
/// Optional inline CSS to inject in a `<style>` tag in the head.
/// Default: `None`.
pub style: Option<String>,
}
#[napi]
impl Default for RenderOptions {
fn default() -> Self {
Self {
fragment: true,
charset: true,
viewport: true,
title: None,
body_class: None,
style: None,
}
}
}
/// Convenience: create `Some(true)` for fragment mode (backward-compatible
/// with the old `render_document(doc, Some(bool))` API).
#[napi]
impl From<bool> for RenderOptions {
fn from(fragment: bool) -> Self {
Self {
fragment,
..Default::default()
}
}
}
pub fn render_document_html(doc: &Document, opts: Option<RenderOptions>) -> String {
let mut r = AstToHtml::new(opts);
r.render_document(doc);
r.finish()
}
pub struct AstToHtml {
out: String,
/// When `true`, inline text is HTML-escaped.
escape: bool,
opts: RenderOptions,
}
impl AstToHtml {
/// Create a new renderer with the given options.
///
/// Pass `None` to use [`RenderOptions::default`] (fragment mode).
pub fn new(opts: Option<RenderOptions>) -> Self {
let options = if opts.is_none() {
RenderOptions::default()
} else {
opts.unwrap()
};
Self {
out: String::new(),
escape: true,
opts: options,
}
}
/// Consume the renderer and return the HTML string.
pub fn finish(self) -> String {
self.out
}
/// Render a [`Document`] and return the HTML string immediately
/// (consuming the renderer).
///
/// This is a convenience wrapper around [`render_document`](Self::render_document)
/// + [`finish`](Self::finish).
///
/// # Example
///
/// ```ignore
/// use peisar_ast::{Document, AstOptions};
/// use peisar_html::AstToHtml;
///
/// let doc = Document::parse("Hi.\n", &AstOptions::default(), None);
/// let html = AstToHtml::default().render_document_owned(&doc);
/// assert_eq!(html, "<p>Hi.</p>\n");
/// ```
#[allow(unused)]
pub fn render_document_owned(mut self, doc: &Document) -> String {
self.render_document(doc);
self.finish()
}
fn esc(&mut self, text: &str) {
if self.escape {
for c in text.chars() {
match c {
'&' => self.out.push_str("&"),
'<' => self.out.push_str("<"),
'>' => self.out.push_str(">"),
'"' => self.out.push_str("""),
'\'' => self.out.push_str("'"),
_ => self.out.push(c),
}
}
} else {
self.out.push_str(text);
}
}
/// Emit Kramdown attributes as an HTML attribute string (with a leading
/// space if non-empty).
fn emit_attrs(&mut self, attrs: Option<&Attributes>) {
if let Some(a) = attrs {
let s = a.to_html_attr_string();
if !s.is_empty() {
self.out.push(' ');
self.out.push_str(&s);
}
}
}
// -----------------------------------------------------------------------
// Top-level entry
// -----------------------------------------------------------------------
/// Render a full [`Document`] into the internal buffer.
///
/// When `RenderOptions::fragment` is `true` (the default) only the body
/// content is emitted. When `false`, a complete `<!DOCTYPE html>`
/// document with `<html>`, `<head>`, and `<body>` wrappers is produced.
pub fn render_document(&mut self, doc: &Document) {
if self.opts.fragment {
for b in &doc.children {
self.render_block(b);
}
} else {
self.out.push_str("<!DOCTYPE html>\n<html>\n<head>\n");
if self.opts.charset {
self.out.push_str("<meta charset=\"utf-8\">\n");
}
if self.opts.viewport {
self.out.push_str(
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n",
);
}
if let Some(title) = self.opts.title.clone() {
self.out.push_str("<title>");
self.esc(&title);
self.out.push_str("</title>\n");
}
if let Some(style) = self.opts.style.clone() {
self.out.push_str("<style>\n");
self.out.push_str(&style);
self.out.push_str("\n</style>\n");
}
self.out.push_str("</head>\n<body");
if let Some(bc) = self.opts.body_class.clone() {
self.out.push_str(" class=\"");
self.esc_no_quote(&bc);
self.out.push_str("\"");
}
self.out.push_str(">\n");
for b in &doc.children {
self.render_block(b);
}
self.out.push_str("</body>\n</html>\n");
}
}
// -----------------------------------------------------------------------
// Blocks
// -----------------------------------------------------------------------
/// Render a single block-level node.
pub fn render_block(&mut self, block: &Block) {
match block {
Block::Heading {
level,
children,
attrs,
..
} => {
self.out.push_str(&format!("<h{}", level));
self.emit_attrs(attrs.as_ref());
self.out.push('>');
for inline in children {
self.render_inline(inline);
}
self.out.push_str(&format!("</h{}>\n", level));
}
Block::Paragraph {
children, attrs, ..
} => {
self.out.push_str("<p");
self.emit_attrs(attrs.as_ref());
self.out.push('>');
for inline in children {
self.render_inline(inline);
}
self.out.push_str("</p>\n");
}
Block::CodeBlock {
lang, code, attrs, ..
} => {
self.out.push_str("<pre><code");
if let Some(l) = lang {
if !l.is_empty() {
self.out.push_str(" class=\"language-");
self.esc_no_quote(l);
self.out.push_str("\"");
}
}
self.emit_attrs(attrs.as_ref());
self.out.push('>');
// Code blocks are never escaped for *content* when we want
// raw output, but we must escape < > & for valid HTML.
self.esc(code);
self.out.push_str("</code></pre>\n");
}
Block::BlockQuote {
children, attrs, ..
} => {
self.out.push_str("<blockquote");
self.emit_attrs(attrs.as_ref());
self.out.push_str(">\n");
for b in children {
self.render_block(b);
}
self.out.push_str("</blockquote>\n");
}
Block::List {
ordered,
items,
attrs,
..
} => {
let tag = if *ordered { "ol" } else { "ul" };
self.out.push('<');
self.out.push_str(tag);
self.emit_attrs(attrs.as_ref());
self.out.push_str(">\n");
for item in items {
self.render_list_item(item);
}
self.out.push_str(&format!("</{}>\n", tag));
}
Block::ThematicBreak { .. } => {
self.out.push_str("<hr>\n");
}
Block::HtmlBlock { html, .. } => {
self.out.push_str(html);
if !html.ends_with('\n') {
self.out.push('\n');
}
}
Block::Table { table, attrs, .. } => {
self.render_table(table, attrs);
}
Block::LinkReferenceDefinition { .. } => {
// Not rendered as visible HTML.
}
Block::Comment { value, .. } => {
self.out.push_str("<!-- ");
self.out.push_str(value);
self.out.push_str(" -->\n");
}
}
}
/// Render a single list item (`<li>`).
fn render_list_item(&mut self, item: &ListItem) {
self.out.push_str("<li");
if let Some(task) = &item.task {
let checked = matches!(task, TaskState::Checked);
self.out.push_str(" class=\"task-list-item\"");
if checked {
self.out.push_str(" data-checked=\"true\"");
}
}
self.out.push('>');
if let Some(task) = &item.task {
let checked = matches!(task, TaskState::Checked);
self.out
.push_str("<input type=\"checkbox\" class=\"task-list-item-checkbox\"");
if checked {
self.out.push_str(" checked");
}
self.out.push_str(" disabled> ");
}
// Render nested blocks. If the item contains a single paragraph we
// render it inline (no wrapping <p>) per CommonMark rendering
// convention for tight lists.
let tight = item.children.len() == 1
&& matches!(item.children.first(), Some(Block::Paragraph { .. }));
if tight {
if let Some(Block::Paragraph { children, .. }) = item.children.first() {
for inline in children {
self.render_inline(inline);
}
}
} else {
self.out.push('\n');
for b in &item.children {
self.render_block(b);
}
}
self.out.push_str("</li>\n");
}
/// Render a GFM table.
fn render_table(&mut self, table: &AstTable, attrs: &Option<Attributes>) {
self.out.push_str("<table");
self.emit_attrs(attrs.as_ref());
self.out.push_str(">\n<thead>\n<tr>\n");
// Header
for (i, cell) in table.header.cells.iter().enumerate() {
self.out.push_str("<th");
self.emit_align(table.alignments.get(i));
self.out.push('>');
for inline in &cell.children {
self.render_inline(inline);
}
self.out.push_str("</th>\n");
}
self.out.push_str("</tr>\n</thead>\n<tbody>\n");
// Body rows
for row in &table.rows {
self.out.push_str("<tr>\n");
for (i, cell) in row.cells.iter().enumerate() {
self.out.push_str("<td");
self.emit_align(table.alignments.get(i));
self.out.push('>');
for inline in &cell.children {
self.render_inline(inline);
}
self.out.push_str("</td>\n");
}
self.out.push_str("</tr>\n");
}
self.out.push_str("</tbody>\n</table>\n");
}
/// Emit a `style="text-align: …"` attribute for a table column alignment.
fn emit_align(&mut self, align: Option<&TableCellAlignment>) {
if let Some(a) = align {
let css = match a {
TableCellAlignment::Default | TableCellAlignment::Left => return,
TableCellAlignment::Center => "text-align: center",
TableCellAlignment::Right => "text-align: right",
};
self.out.push_str(" style=\"");
self.out.push_str(css);
self.out.push('"');
}
}
// -----------------------------------------------------------------------
// Inlines
// -----------------------------------------------------------------------
/// Render a single inline-level node.
pub fn render_inline(&mut self, inline: &Inline) {
match inline {
Inline::Text { value, .. } => {
self.esc(value);
}
Inline::Emphasis {
level, children, ..
} => {
let (open, close) = match level {
EmphasisLevel::Italic => ("<em>", "</em>"),
EmphasisLevel::Bold => ("<strong>", "</strong>"),
};
self.out.push_str(open);
for child in children {
self.render_inline(child);
}
self.out.push_str(close);
}
Inline::Code { code, .. } => {
self.out.push_str("<code>");
self.esc(code);
self.out.push_str("</code>");
}
Inline::HtmlInline { html, .. } => {
// Raw HTML — emit as-is.
self.out.push_str(html);
}
Inline::Strikethrough { children, .. } => {
self.out.push_str("<del>");
for child in children {
self.render_inline(child);
}
self.out.push_str("</del>");
}
Inline::HardBreak { .. } => {
self.out.push_str("<br>\n");
}
Inline::SoftBreak { .. } => {
self.out.push('\n');
}
Inline::Image {
alt, url, title, ..
} => {
self.out.push_str("<img src=\"");
self.esc_attr(url);
self.out.push_str("\" alt=\"");
self.esc(alt);
self.out.push('"');
if let Some(t) = title {
self.out.push_str(" title=\"");
self.esc(t);
self.out.push('"');
}
self.out.push('>');
}
Inline::Link {
text, url, title, ..
} => {
self.out.push_str("<a href=\"");
self.esc_attr(url);
self.out.push('"');
if let Some(t) = title {
self.out.push_str(" title=\"");
self.esc(t);
self.out.push('"');
}
self.out.push('>');
for child in text {
self.render_inline(child);
}
self.out.push_str("</a>");
}
Inline::LinkReference {
text, url, title, ..
} => {
self.out.push_str("<a href=\"");
self.esc_attr(url);
self.out.push('"');
if let Some(t) = title {
self.out.push_str(" title=\"");
self.esc(t);
self.out.push('"');
}
self.out.push('>');
for child in text {
self.render_inline(child);
}
self.out.push_str("</a>");
}
}
}
// -----------------------------------------------------------------------
// Small helpers
// -----------------------------------------------------------------------
/// Escape text for use inside an HTML attribute value (escapes `&`, `"`,
/// `<`, `>`).
fn esc_attr(&mut self, text: &str) {
for c in text.chars() {
match c {
'&' => self.out.push_str("&"),
'"' => self.out.push_str("""),
'<' => self.out.push_str("<"),
'>' => self.out.push_str(">"),
_ => self.out.push(c),
}
}
}
/// Escape text without escaping single quotes — used for class names and
/// other attribute values that won't contain `'`.
fn esc_no_quote(&mut self, text: &str) {
for c in text.chars() {
match c {
'&' => self.out.push_str("&"),
'"' => self.out.push_str("""),
'<' => self.out.push_str("<"),
'>' => self.out.push_str(">"),
_ => self.out.push(c),
}
}
}
}
#[cfg(test)]
mod tests {
use super::{RenderOptions, render_document_html};
use crate::ast::{AstOptions, Document};
#[test]
fn body_class_is_not_written_to_html_element() {
let doc = Document::parse("Text", &AstOptions::default(), None);
let html = render_document_html(
&doc,
Some(RenderOptions {
fragment: false,
body_class: Some("markdown-body".to_string()),
..RenderOptions::default()
}),
);
assert!(html.contains("<html>\n<head>"));
assert!(html.contains("<body class=\"markdown-body\">"));
assert!(!html.contains("<html class="));
}
}
impl Default for AstToHtml {
fn default() -> Self {
Self::new(None)
}
}