use pulldown_cmark::{html, Options, Parser};

pub fn md_to_html(content: &str) -> String {
    let mut opts = Options::empty();
    opts.insert(Options::ENABLE_TABLES);
    opts.insert(Options::ENABLE_FOOTNOTES);
    opts.insert(Options::ENABLE_STRIKETHROUGH);
    opts.insert(Options::ENABLE_TASKLISTS);
    opts.insert(Options::ENABLE_HEADING_ATTRIBUTES);
    let parser = Parser::new_ext(content, opts);
    let mut body = String::new();
    html::push_html(&mut body, parser);
    body
}

const HTML: &str = include_str!("page.html");

pub fn page(title: &str, content: &str) -> String {
    HTML.replace("{__TITLE__}", title)
        .replace("{__CONTENT__}", content)
}

pub fn render_page(title: &str, content: &str) -> String {
    let content = content
        .replace(
            "<code class=\"language-mermaid\">",
            "<div class=\"mermaid\">",
        )
        .replace("</code>", "</div>");
    page(title, &content).replace("startOnLoad:false", "startOnLoad:true")
}
