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
//! What a plain CommonMark and GFM renderer shows of a document (spec ยง14), for an editor that shows how a document
//! degrades: the text through markdown-rs with GFM on and CompMD's syntax off. Raw HTML is escaped and unsafe link
//! protocols are dropped, as markdown-rs does by default.
/// The HTML a plain GFM renderer gives for `text`.
///
/// ```
/// let html = compmd::plain::html(":: chart[Weekly installs] {src=a.csv}\n\n~~old~~\n");
/// assert_eq!(html, "<p>:: chart[Weekly installs] {src=a.csv}</p>\n<p><del>old</del></p>\n");
/// ```
#[must_use]
pub fn html(text: &str) -> String {
// Markdown has no syntax errors; only MDX, which GFM doesn't turn on, gives one.
markdown::to_html_with_options(text, &markdown::Options::gfm())
.unwrap_or_else(|message| message.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn component_lines_stay_one_line_of_text() {
let html = html(
"::: card {title=\"Setup\"}\nInside.\n::: /card\n\n| a |\n|---|\n| 1 |\n\n<script>x</script>\n",
);
assert!(
html.contains("<p>::: card {title="Setup"}\nInside.\n::: /card</p>"),
"{html}"
);
assert!(html.contains("<table>"), "GFM tables: {html}");
assert!(
html.contains("<script>"),
"raw HTML is escaped: {html}"
);
}
}