From a958305e58d9a2bdecf6d1b3b5df420410cd2fa6 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 6 May 2022 22:16:56 +0200 Subject: [PATCH] Clone Zola Tera instance for Markdown filter --- CHANGELOG.md | 1 + components/site/src/tpls.rs | 17 ++++++++-------- components/templates/src/filters.rs | 31 +++++++++++------------------ 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc373166..4cda93ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ also specify classes on headers now - Add `render` attribute to taxonomies configuration in `config.toml`, for when you don't want to render any pages related to that taxonomy - Serialize `transparent` field from front-matter of sections +- Use Zola Tera instance for markdown filter: this means you have access to the same Tera functions as in shortcodes ## 0.15.3 (2022-01-23) diff --git a/components/site/src/tpls.rs b/components/site/src/tpls.rs index 8a91cb90..98d80665 100644 --- a/components/site/src/tpls.rs +++ b/components/site/src/tpls.rs @@ -4,14 +4,6 @@ use templates::{filters, global_fns}; /// Adds global fns that are to be available to shortcodes while rendering markdown pub fn register_early_global_fns(site: &mut Site) -> TeraResult<()> { - site.tera.register_filter( - "markdown", - filters::MarkdownFilter::new( - site.base_path.clone(), - site.config.clone(), - site.permalinks.clone(), - )?, - ); site.tera.register_filter( "num_format", filters::NumFormatFilter::new(&site.config.default_language), @@ -69,6 +61,15 @@ pub fn register_early_global_fns(site: &mut Site) -> TeraResult<()> { ), ); + site.tera.register_filter( + "markdown", + filters::MarkdownFilter::new( + site.config.clone(), + site.permalinks.clone(), + site.tera.clone(), + ), + ); + Ok(()) } diff --git a/components/templates/src/filters.rs b/components/templates/src/filters.rs index 39115b2a..36a41baa 100644 --- a/components/templates/src/filters.rs +++ b/components/templates/src/filters.rs @@ -1,7 +1,6 @@ use std::borrow::Cow; use std::collections::HashMap; use std::hash::BuildHasher; -use std::path::PathBuf; use config::Config; use libs::base64::{decode, encode}; @@ -11,7 +10,6 @@ use libs::tera::{ }; use markdown::{render_content, RenderContext}; -use crate::load_tera; #[derive(Debug)] pub struct MarkdownFilter { @@ -22,12 +20,11 @@ pub struct MarkdownFilter { impl MarkdownFilter { pub fn new( - path: PathBuf, config: Config, permalinks: HashMap, - ) -> TeraResult { - let tera = load_tera(&path, &config).map_err(TeraError::msg)?; - Ok(Self { config, permalinks, tera }) + tera: Tera, + ) -> Self { + Self { config, permalinks, tera } } } @@ -113,17 +110,16 @@ impl TeraFilter for NumFormatFilter { #[cfg(test)] mod tests { - use std::{collections::HashMap, path::PathBuf}; + use std::{collections::HashMap}; - use libs::tera::{to_value, Error as TeraError, Filter}; + use libs::tera::{to_value, Tera, Filter}; use super::{base64_decode, base64_encode, MarkdownFilter, NumFormatFilter}; use config::Config; #[test] fn markdown_filter() { - let result = MarkdownFilter::new(PathBuf::new(), Config::default(), HashMap::new()) - .unwrap() + let result = MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default()) .filter(&to_value(&"# Hey").unwrap(), &HashMap::new()); assert!(result.is_ok()); assert_eq!(result.unwrap(), to_value(&"

Hey

\n").unwrap()); @@ -137,7 +133,7 @@ mod tests { let args = HashMap::new(); let config = Config::default(); let permalinks = HashMap::new(); - let mut tera = super::load_tera(&PathBuf::new(), &config).map_err(TeraError::msg).unwrap(); + let mut tera = Tera::default(); tera.add_raw_template("shortcodes/explicitlang.html", "a{{ lang }}a").unwrap(); let filter = MarkdownFilter { config, permalinks, tera }; let result = filter.filter(&to_value(&"{{ explicitlang(lang='jp') }}").unwrap(), &args); @@ -151,7 +147,7 @@ mod tests { let mut args = HashMap::new(); args.insert("inline".to_string(), to_value(true).unwrap()); let result = - MarkdownFilter::new(PathBuf::new(), Config::default(), HashMap::new()).unwrap().filter( + MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default()).filter( &to_value(&"Using `map`, `filter`, and `fold` instead of `for`").unwrap(), &args, ); @@ -165,7 +161,7 @@ mod tests { let mut args = HashMap::new(); args.insert("inline".to_string(), to_value(true).unwrap()); let result = - MarkdownFilter::new(PathBuf::new(), Config::default(), HashMap::new()).unwrap().filter( + MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default()).filter( &to_value( &r#" |id|author_id| timestamp_created|title |content | @@ -190,15 +186,13 @@ mod tests { config.markdown.external_links_target_blank = true; let md = "Hello :smile: ..."; - let result = MarkdownFilter::new(PathBuf::new(), config.clone(), HashMap::new()) - .unwrap() + let result = MarkdownFilter::new(config.clone(), HashMap::new(), Tera::default()) .filter(&to_value(&md).unwrap(), &HashMap::new()); assert!(result.is_ok()); assert_eq!(result.unwrap(), to_value(&"

Hello https://google.com 😄 …

\n").unwrap()); let md = "```py\ni=0\n```"; - let result = MarkdownFilter::new(PathBuf::new(), config, HashMap::new()) - .unwrap() + let result = MarkdownFilter::new(config, HashMap::new(), Tera::default()) .filter(&to_value(&md).unwrap(), &HashMap::new()); assert!(result.is_ok()); assert!(result.unwrap().as_str().unwrap().contains("style")); @@ -209,8 +203,7 @@ mod tests { let mut permalinks = HashMap::new(); permalinks.insert("blog/_index.md".to_string(), "/foo/blog".to_string()); let md = "Hello. Check out [my blog](@/blog/_index.md)!"; - let result = MarkdownFilter::new(PathBuf::new(), Config::default(), permalinks) - .unwrap() + let result = MarkdownFilter::new(Config::default(), permalinks, Tera::default()) .filter(&to_value(&md).unwrap(), &HashMap::new()); assert!(result.is_ok()); assert_eq!(