feat(filters): add markdown filter
Diff
Cargo.toml | 3 ++-
src/main.rs | 1 +
src/util/filters.rs | 17 +++++++++++++++++
src/util/templating.rs | 6 +++++-
4 files changed, 25 insertions(+), 2 deletions(-)
@@ -13,4 +13,5 @@ serde_json = "1.0.100"
walkdir = "2.3.3"
read_pipe = "0.1.2"
minijinja = "1.0.3"
gumdrop = "0.8.1"
\ No newline at end of file
gumdrop = "0.8.1"
markdown = "1.0.0-alpha.10"
\ No newline at end of file
@@ -9,6 +9,7 @@ mod data {
}
mod util {
pub mod templating;
pub mod filters;
pub mod journal;
}
@@ -0,0 +1,17 @@
use markdown::CompileOptions;
pub fn markdown(value: String) -> String {
let result = markdown::to_html_with_options(&value, &markdown::Options {
compile: CompileOptions {
allow_dangerous_html: true,
allow_dangerous_protocol: true,
..CompileOptions::default()
},
..markdown::Options::gfm()
});
result.unwrap_or_default()
}
pub fn register_filters(env: &mut minijinja::Environment<'_>) {
env.add_filter("markdown", markdown);
}
\ No newline at end of file
@@ -8,6 +8,8 @@ use minijinja::{Environment, context};
use crate::data::formats;
use crate::util::journal;
use super::filters;
pub fn get_templates(template: String) -> HashMap<String, String> {
let template_path: PathBuf = PathBuf::from_str(&template).unwrap();
let mut templates: HashMap<String, String> = HashMap::new();
@@ -25,12 +27,14 @@ pub fn get_templates(template: String) -> HashMap<String, String> {
}
pub fn render_html(config: formats::RenderConfig, journal: journal::Journal, templates: &HashMap<String, String>) -> String {
let mut env: Environment<'_> = Environment::new();
let env: &mut Environment<'_> = &mut Environment::new();
for (name, source) in templates {
let _ = env.add_template(&name, &source).unwrap();
}
filters::register_filters(env);
let tmpl: minijinja::Template<'_, '_> = env.get_template(&config.template_index).unwrap();
tmpl.render(context!(config => config, journal => journal)).unwrap_or_default()
}
\ No newline at end of file