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
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)

View File

@ -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(())
}

View File

@ -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<String, String>,
) -> TeraResult<Self> {
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(&"<h1 id=\"hey\">Hey</h1>\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 <https://google.com> :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(&"<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 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!(