use pulldown_cmark::{Event, Options, Parser, html};
use crate::index::detect_front_matter;
pub fn render_preview_html(text: &str) -> String {
let body_offset = detect_front_matter(text).unwrap_or(0);
let body = &text[body_offset..];
let options = Options::ENABLE_TABLES
| Options::ENABLE_TASKLISTS
| Options::ENABLE_STRIKETHROUGH
| Options::ENABLE_FOOTNOTES
| Options::ENABLE_MATH;
let parser = Parser::new_ext(body, options).map(|event| match event {
Event::Html(s) | Event::InlineHtml(s) => Event::Text(s),
Event::InlineMath(code) => {
Event::Html(format!("<code class=\"math-inline\">{}</code>", html_escape(&code)).into())
}
Event::DisplayMath(code) => Event::Html(
format!(
"<pre class=\"math-block\"><code>{}</code></pre>\n",
html_escape(&code)
)
.into(),
),
other => other,
});
let mut out = String::new();
html::push_html(&mut out, parser);
out
}
fn html_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}