Clone Zola Tera instance for Markdown filter

This commit is contained in:
Vincent Prouillet 2022-05-06 22:16:56 +02:00
parent ee8de20a6e
commit a958305e58
3 changed files with 22 additions and 27 deletions

View File

@ -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 - Add `render` attribute to taxonomies configuration in `config.toml`, for when you don't want to render
any pages related to that taxonomy any pages related to that taxonomy
- Serialize `transparent` field from front-matter of sections - 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) ## 0.15.3 (2022-01-23)

View File

@ -4,14 +4,6 @@ use templates::{filters, global_fns};
/// Adds global fns that are to be available to shortcodes while rendering markdown /// Adds global fns that are to be available to shortcodes while rendering markdown
pub fn register_early_global_fns(site: &mut Site) -> TeraResult<()> { 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( site.tera.register_filter(
"num_format", "num_format",
filters::NumFormatFilter::new(&site.config.default_language), 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(()) Ok(())
} }

View File

@ -1,7 +1,6 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::BuildHasher; use std::hash::BuildHasher;
use std::path::PathBuf;
use config::Config; use config::Config;
use libs::base64::{decode, encode}; use libs::base64::{decode, encode};
@ -11,7 +10,6 @@ use libs::tera::{
}; };
use markdown::{render_content, RenderContext}; use markdown::{render_content, RenderContext};
use crate::load_tera;
#[derive(Debug)] #[derive(Debug)]
pub struct MarkdownFilter { pub struct MarkdownFilter {
@ -22,12 +20,11 @@ pub struct MarkdownFilter {
impl MarkdownFilter { impl MarkdownFilter {
pub fn new( pub fn new(
path: PathBuf,
config: Config, config: Config,
permalinks: HashMap<String, String>, permalinks: HashMap<String, String>,
) -> TeraResult<Self> { tera: Tera,
let tera = load_tera(&path, &config).map_err(TeraError::msg)?; ) -> Self {
Ok(Self { config, permalinks, tera }) Self { config, permalinks, tera }
} }
} }
@ -113,17 +110,16 @@ impl TeraFilter for NumFormatFilter {
#[cfg(test)] #[cfg(test)]
mod tests { 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 super::{base64_decode, base64_encode, MarkdownFilter, NumFormatFilter};
use config::Config; use config::Config;
#[test] #[test]
fn markdown_filter() { fn markdown_filter() {
let result = MarkdownFilter::new(PathBuf::new(), Config::default(), HashMap::new()) let result = MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default())
.unwrap()
.filter(&to_value(&"# Hey").unwrap(), &HashMap::new()); .filter(&to_value(&"# Hey").unwrap(), &HashMap::new());
assert!(result.is_ok()); assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"<h1 id=\"hey\">Hey</h1>\n").unwrap()); assert_eq!(result.unwrap(), to_value(&"<h1 id=\"hey\">Hey</h1>\n").unwrap());
@ -137,7 +133,7 @@ mod tests {
let args = HashMap::new(); let args = HashMap::new();
let config = Config::default(); let config = Config::default();
let permalinks = HashMap::new(); 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(); tera.add_raw_template("shortcodes/explicitlang.html", "a{{ lang }}a").unwrap();
let filter = MarkdownFilter { config, permalinks, tera }; let filter = MarkdownFilter { config, permalinks, tera };
let result = filter.filter(&to_value(&"{{ explicitlang(lang='jp') }}").unwrap(), &args); let result = filter.filter(&to_value(&"{{ explicitlang(lang='jp') }}").unwrap(), &args);
@ -151,7 +147,7 @@ mod tests {
let mut args = HashMap::new(); let mut args = HashMap::new();
args.insert("inline".to_string(), to_value(true).unwrap()); args.insert("inline".to_string(), to_value(true).unwrap());
let result = 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(), &to_value(&"Using `map`, `filter`, and `fold` instead of `for`").unwrap(),
&args, &args,
); );
@ -165,7 +161,7 @@ mod tests {
let mut args = HashMap::new(); let mut args = HashMap::new();
args.insert("inline".to_string(), to_value(true).unwrap()); args.insert("inline".to_string(), to_value(true).unwrap());
let result = let result =
MarkdownFilter::new(PathBuf::new(), Config::default(), HashMap::new()).unwrap().filter( MarkdownFilter::new(Config::default(), HashMap::new(), Tera::default()).filter(
&to_value( &to_value(
&r#" &r#"
|id|author_id| timestamp_created|title |content | |id|author_id| timestamp_created|title |content |
@ -190,15 +186,13 @@ mod tests {
config.markdown.external_links_target_blank = true; config.markdown.external_links_target_blank = true;
let md = "Hello <https://google.com> :smile: ..."; let md = "Hello <https://google.com> :smile: ...";
let result = MarkdownFilter::new(PathBuf::new(), config.clone(), HashMap::new()) let result = MarkdownFilter::new(config.clone(), HashMap::new(), Tera::default())
.unwrap()
.filter(&to_value(&md).unwrap(), &HashMap::new()); .filter(&to_value(&md).unwrap(), &HashMap::new());
assert!(result.is_ok()); assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(&"<p>Hello <a rel=\"noopener\" target=\"_blank\" href=\"https://google.com\">https://google.com</a> 😄 …</p>\n").unwrap()); assert_eq!(result.unwrap(), to_value(&"<p>Hello <a rel=\"noopener\" target=\"_blank\" href=\"https://google.com\">https://google.com</a> 😄 …</p>\n").unwrap());
let md = "```py\ni=0\n```"; let md = "```py\ni=0\n```";
let result = MarkdownFilter::new(PathBuf::new(), config, HashMap::new()) let result = MarkdownFilter::new(config, HashMap::new(), Tera::default())
.unwrap()
.filter(&to_value(&md).unwrap(), &HashMap::new()); .filter(&to_value(&md).unwrap(), &HashMap::new());
assert!(result.is_ok()); assert!(result.is_ok());
assert!(result.unwrap().as_str().unwrap().contains("style")); assert!(result.unwrap().as_str().unwrap().contains("style"));
@ -209,8 +203,7 @@ mod tests {
let mut permalinks = HashMap::new(); let mut permalinks = HashMap::new();
permalinks.insert("blog/_index.md".to_string(), "/foo/blog".to_string()); permalinks.insert("blog/_index.md".to_string(), "/foo/blog".to_string());
let md = "Hello. Check out [my blog](@/blog/_index.md)!"; let md = "Hello. Check out [my blog](@/blog/_index.md)!";
let result = MarkdownFilter::new(PathBuf::new(), Config::default(), permalinks) let result = MarkdownFilter::new(Config::default(), permalinks, Tera::default())
.unwrap()
.filter(&to_value(&md).unwrap(), &HashMap::new()); .filter(&to_value(&md).unwrap(), &HashMap::new());
assert!(result.is_ok()); assert!(result.is_ok());
assert_eq!( assert_eq!(