123456789101112131415161718192021222324252627282930313233343536373839404142 |
- use pulldown_cmark::{html, Parser};
- pub struct Theme {
- root_path: String,
- parser_options: pulldown_cmark::Options,
- }
- impl Theme {
- pub fn new(root_path: String) -> Self {
- let mut parser_options = pulldown_cmark::Options::empty();
- parser_options.insert(pulldown_cmark::Options::ENABLE_STRIKETHROUGH);
- parser_options.insert(pulldown_cmark::Options::ENABLE_TABLES);
- parser_options.insert(pulldown_cmark::Options::ENABLE_FOOTNOTES);
- Theme {
- root_path,
- parser_options,
- }
- }
- // Renders markdown as html
- pub fn markdown_as_html(&self, path: &String) -> Result<String, String> {
- log::trace!("Rendering file '{}'", path);
- let path = format!("{}.md", path);
- let file = std::fs::read_to_string(&path);
- match file {
- Ok(content) => {
- let parser = Parser::new_ext(content.as_str(), self.parser_options);
- let mut html_output = String::new();
- html::push_html(&mut html_output, parser);
- Ok(html_output)
- }
- Err(error) => {
- log::error!("File located at path {} was not found.", path);
- Err(error.to_string())
- }
- }
- }
- }
|