50 lines
1.2 KiB
Rust
50 lines
1.2 KiB
Rust
//! Markdown 檢視器
|
|
|
|
use anyhow::Result;
|
|
|
|
pub struct MarkdownViewer {
|
|
content: Option<String>,
|
|
}
|
|
|
|
impl MarkdownViewer {
|
|
pub fn new() -> Self {
|
|
Self { content: None }
|
|
}
|
|
|
|
pub fn load(&mut self, content: &str) -> Result<String> {
|
|
self.content = Some(content.to_string());
|
|
self.render()
|
|
}
|
|
|
|
pub fn render(&self) -> Result<String> {
|
|
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#"
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<style>
|
|
body { font-family: system-ui; padding: 20px; max-width: 800px; margin: 0 auto; }
|
|
pre { background: #f4f4f4; padding: 10px; border-radius: 4px; overflow-x: auto; }
|
|
code { background: #f4f4f4; padding: 2px 6px; border-radius: 3px; }
|
|
img { max-width: 100%; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="content">CONTENT_PLACEHOLDER</div>
|
|
</body>
|
|
</html>
|
|
"#.replace("CONTENT_PLACEHOLDER", &self.render().unwrap_or_default())
|
|
}
|
|
}
|