From abafe66a512d33eb2ec4516b8750d44ffdbcf76c Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sun, 7 Aug 2022 20:28:53 +0200 Subject: [PATCH] Fix overriding built-in shortcodes Closes #1947 --- CHANGELOG.md | 1 + components/utils/src/templates.rs | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7ff32e0a5..9d1164e76a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 0.16.1 (unreleased) - Fix many Windows bugs +- Fix overriding built-in shortcodes ## 0.16.0 (2022-07-16) diff --git a/components/utils/src/templates.rs b/components/utils/src/templates.rs index 62188a304a..90847c764b 100644 --- a/components/utils/src/templates.rs +++ b/components/utils/src/templates.rs @@ -56,11 +56,13 @@ pub fn get_shortcodes(tera: &Tera) -> HashMap { if template.name.starts_with("__zola_builtins/shortcodes/") { let head_len = "__zola_builtins/shortcodes/".len(); - shortcode_definitions.insert( - identifier[head_len..(identifier.len() - ext_len - 1)].to_string(), - ShortcodeDefinition::new(file_type, &template.name), - ); - continue; + let name = &identifier[head_len..(identifier.len() - ext_len - 1)]; + // We don't keep the built-ins one if the user provided one + if shortcode_definitions.contains_key(name) { + continue; + } + shortcode_definitions + .insert(name.to_string(), ShortcodeDefinition::new(file_type, &template.name)); } } @@ -147,7 +149,7 @@ pub fn check_template_fallbacks<'a>( #[cfg(test)] mod tests { - use crate::templates::check_template_fallbacks; + use crate::templates::{check_template_fallbacks, get_shortcodes}; use super::rewrite_theme_paths; use libs::tera::Tera; @@ -193,4 +195,13 @@ mod tests { Some("hyde/templates/theme-only.html") ); } + + #[test] + fn can_overwrite_builtin_shortcodes() { + let mut tera = Tera::parse("test-templates/*.html").unwrap(); + tera.add_raw_template("__zola_builtins/shortcodes/youtube.html", "Builtin").unwrap(); + tera.add_raw_template("shortcodes/youtube.html", "Hello").unwrap(); + let definitions = get_shortcodes(&tera); + assert_eq!(definitions["youtube"].tera_name, "shortcodes/youtube.html"); + } }