Skip to content

Commit

Permalink
Fix overriding built-in shortcodes
Browse files Browse the repository at this point in the history
Closes #1947
  • Loading branch information
Keats committed Aug 7, 2022
1 parent bfae359 commit cc058ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 0.16.1 (unreleased)

- Fix many Windows bugs
- Fix overriding built-in shortcodes

## 0.16.0 (2022-07-16)

Expand Down
23 changes: 17 additions & 6 deletions components/utils/src/templates.rs
Expand Up @@ -56,11 +56,13 @@ pub fn get_shortcodes(tera: &Tera) -> HashMap<String, ShortcodeDefinition> {

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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}

0 comments on commit cc058ab

Please sign in to comment.