30 lines
964 B
Rust
30 lines
964 B
Rust
use std::fs::File;
|
|
use std::io::Read;
|
|
|
|
use pulldown_cmark::{html, Parser};
|
|
|
|
use config::SinglePageConfigs;
|
|
|
|
pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, String> {
|
|
if let Some(parent) = config.page_config_path.parent() {
|
|
let path = parent.with_file_name(&config.page_config.page.content_path);
|
|
match File::open(&path) {
|
|
Ok(mut file) => {
|
|
let mut content = String::new();
|
|
file.read_to_string(&mut content).ok();
|
|
let parser = Parser::new(&content);
|
|
|
|
let mut markdown = String::new();
|
|
html::push_html(&mut markdown, parser);
|
|
Ok(markdown)
|
|
}
|
|
Err(_) => Err(format!("Failed to open file: {}", path.to_str().unwrap())),
|
|
}
|
|
} else {
|
|
Err(format!(
|
|
"Failed to get page config parent: {}",
|
|
config.page_config_path.to_str().unwrap()
|
|
))
|
|
}
|
|
}
|