Skip to content

Commit

Permalink
Clone Zola Tera instance for Markdown filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed May 6, 2022
1 parent ee8de20 commit a958305
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 9 additions & 8 deletions components/site/src/tpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(())
}

Expand Down
31 changes: 12 additions & 19 deletions components/templates/src/filters.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -11,7 +10,6 @@ use libs::tera::{
};
use markdown::{render_content, RenderContext};

use crate::load_tera;

#[derive(Debug)]
pub struct MarkdownFilter {
Expand All @@ -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 }
}
}

Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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,
);
Expand All @@ -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 |
Expand All @@ -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"));
Expand All @@ -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!(
Expand Down

0 comments on commit a958305

Please sign in to comment.