From 808e429c10f71d0b06ce92dd46fcaabef78769b8 Mon Sep 17 00:00:00 2001 From: berrysweet Date: Thu, 6 Jul 2023 22:13:29 +0200 Subject: [PATCH] feat(filters): add markdown filter --- Cargo.toml | 3 ++- src/main.rs | 1 + src/util/filters.rs | 17 +++++++++++++++++ src/util/templating.rs | 6 +++++- 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/util/filters.rs diff --git a/Cargo.toml b/Cargo.toml index b92992c..67b69d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/main.rs b/src/main.rs index 40cdecb..a80aaa8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod data { } mod util { pub mod templating; + pub mod filters; pub mod journal; } diff --git a/src/util/filters.rs b/src/util/filters.rs new file mode 100644 index 0000000..7657126 --- /dev/null +++ b/src/util/filters.rs @@ -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 diff --git a/src/util/templating.rs b/src/util/templating.rs index 4eb6293..d17185e 100644 --- a/src/util/templating.rs +++ b/src/util/templating.rs @@ -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 { let template_path: PathBuf = PathBuf::from_str(&template).unwrap(); let mut templates: HashMap = HashMap::new(); @@ -25,12 +27,14 @@ pub fn get_templates(template: String) -> HashMap { } pub fn render_html(config: formats::RenderConfig, journal: journal::Journal, templates: &HashMap) -> 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 -- libgit2 1.7.2