diff --git a/src/viewer/markdown.rs b/src/viewer/markdown.rs new file mode 100644 index 0000000..0b5f886 --- /dev/null +++ b/src/viewer/markdown.rs @@ -0,0 +1,49 @@ +//! Markdown 檢視器 + +use anyhow::Result; + +pub struct MarkdownViewer { + content: Option, +} + +impl MarkdownViewer { + pub fn new() -> Self { + Self { content: None } + } + + pub fn load(&mut self, content: &str) -> Result { + self.content = Some(content.to_string()); + self.render() + } + + pub fn render(&self) -> Result { + let content = self.content.as_ref() + .ok_or_else(|| anyhow::anyhow!("No content loaded"))?; + + let parser = pulldown_cmark::Parser::new(content); + let mut html = String::new(); + pulldown_cmark::html::push_html(&mut html, parser); + + Ok(html) + } + + pub fn get_html_template(&self) -> String { + r#" + + + + + + + +
CONTENT_PLACEHOLDER
+ + +"#.replace("CONTENT_PLACEHOLDER", &self.render().unwrap_or_default()) + } +}