use pulldown_cmark::{Options, Parser, html};
use crate::components::{ComponentRegistry, expand_components};
use crate::error::PageError;
use crate::models::{CompiledPage, UncompiledPage};
fn markdown_options() -> Options {
Options::ENABLE_STRIKETHROUGH
| Options::ENABLE_TABLES
| Options::ENABLE_FOOTNOTES
| Options::ENABLE_TASKLISTS
}
pub fn markdown_to_html(body: &str) -> String {
let parser = Parser::new_ext(body, markdown_options());
let mut content_html = String::with_capacity(body.len().saturating_mul(2));
html::push_html(&mut content_html, parser);
content_html
}
pub fn compile_markdown(
page: UncompiledPage,
registry: &ComponentRegistry,
) -> Result<CompiledPage, PageError> {
let source = page.into_inner();
let path = source.source_path.clone();
let expanded = expand_components(&source.body, registry, &path)?;
let parser = Parser::new_ext(&expanded, markdown_options());
let mut content_html = String::with_capacity(expanded.len().saturating_mul(2));
html::push_html(&mut content_html, parser);
Ok(CompiledPage::new(source, content_html))
}
pub fn compile_all(
pages: Vec<UncompiledPage>,
registry: &ComponentRegistry,
) -> Result<Vec<CompiledPage>, PageError> {
use rayon::prelude::*;
pages
.into_par_iter()
.map(|page| compile_markdown(page, registry))
.collect()
}