pub mod node;
pub mod presets;
pub mod style;
pub use node::*;
pub use presets::*;
pub use style::*;
use crate::css::engine::CssEngine;
pub fn parse_markdown(markdown: &str) -> Result<Node, String> {
let (node, _page_config) = parse_markdown_with_css(markdown, "")?;
Ok(node)
}
fn build_engine_and_parse(
markdown: &str,
user_css: &str,
strict_mode: bool,
) -> Result<(Node, PageConfig), String> {
let html = crate::html::md_converter::markdown_to_html(markdown);
let doc = crate::html::parser::parse_html(&html);
let inline_css = doc.style_sheets.join("\n");
let combined_css = if user_css.is_empty() {
inline_css
} else if inline_css.is_empty() {
user_css.to_string()
} else {
format!("{}\n{}", user_css, inline_css)
};
let mut engine = CssEngine::new(DEFAULT_CSS)?.with_strict_mode(strict_mode);
if !combined_css.is_empty() {
engine = engine.with_user_css(&combined_css)?;
}
let mut node = crate::html::styled::html_to_styled_nodes(&doc, &engine);
if !matches!(node.kind, NodeKind::Document { .. }) {
node = Node::new(
NodeKind::Document {
children: vec![node],
},
Style::default(),
false,
);
}
let page_config = engine.page_config().clone();
Ok((node, page_config))
}
pub fn parse_markdown_with_css(
markdown: &str,
user_css: &str,
) -> Result<(Node, PageConfig), String> {
build_engine_and_parse(markdown, user_css, false)
}
pub fn parse_markdown_with_css_strict(
markdown: &str,
user_css: &str,
) -> Result<(Node, PageConfig), String> {
build_engine_and_parse(markdown, user_css, true)
}
pub fn parse_markdown_with_resolver(markdown: &str, engine: &CssEngine) -> Node {
let html = crate::html::md_converter::markdown_to_html(markdown);
let doc = crate::html::parser::parse_html(&html);
let mut node = crate::html::styled::html_to_styled_nodes(&doc, engine);
if !matches!(node.kind, NodeKind::Document { .. }) {
node = Node::new(
NodeKind::Document {
children: vec![node],
},
Style::default(),
false,
);
}
node
}