rainbeam_shared/
ui.rs

1//! Ui utilities
2use std::collections::HashSet;
3use ammonia::Builder;
4use comrak::{markdown_to_html, Options};
5
6/// Render markdown input into HTML
7pub fn render_markdown(input: &str) -> String {
8    let mut options = Options::default();
9
10    options.extension.table = true;
11    options.extension.superscript = true;
12    options.extension.strikethrough = true;
13    options.extension.autolink = true;
14    options.extension.header_ids = Option::Some(String::new());
15    options.extension.tagfilter = true;
16    options.render.unsafe_ = true;
17    // options.render.escape = true;
18    options.parse.smart = false;
19
20    let html = markdown_to_html(input, &options);
21
22    let mut allowed_attributes = HashSet::new();
23    allowed_attributes.insert("id");
24    allowed_attributes.insert("class");
25    allowed_attributes.insert("ref");
26    allowed_attributes.insert("aria-label");
27    allowed_attributes.insert("lang");
28    allowed_attributes.insert("title");
29    allowed_attributes.insert("align");
30
31    allowed_attributes.insert("data-color");
32    allowed_attributes.insert("data-font-family");
33
34    Builder::default()
35        .generic_attributes(allowed_attributes)
36        .clean(&html)
37        .to_string()
38        .replace(
39            "src=\"",
40            "loading=\"lazy\" src=\"/api/v0/util/ext/image?img=",
41        )
42        .replace("--&gt;", "<align class=\"right\">")
43        .replace("-&gt;", "<align class=\"center\">")
44        .replace("&lt;-", "</align>")
45}