From 8759323a164184e130d88ec4d7d8bc0495780f29 Mon Sep 17 00:00:00 2001 From: Carson Page Date: Fri, 10 Nov 2017 10:46:14 -0600 Subject: [PATCH] Add trailing_slash opt. to get_url (#173) * Added inital trailing_slash impl * Added simple test * Updated docs website to use trailing_slash option * Updated documentation to reflect new trailing_slash option * Added combined cachebust and trailing_slash test --- components/templates/src/global_fns.rs | 33 ++++++++++++++++++- .../documentation/templates/overview.md | 7 ++++ docs/templates/index.html | 4 +-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/components/templates/src/global_fns.rs b/components/templates/src/global_fns.rs index 438369f0..538baad3 100644 --- a/components/templates/src/global_fns.rs +++ b/components/templates/src/global_fns.rs @@ -59,7 +59,13 @@ pub fn make_get_url(permalinks: HashMap, config: Config) -> Glob .map_or(false, |c| { from_value::(c.clone()).unwrap_or(false) }); - + + let trailing_slash = args + .get("trailing_slash") + .map_or(true, |c| { + from_value::(c.clone()).unwrap_or(true) + }); + match args.get("path") { Some(val) => match from_value::(val.clone()) { Ok(v) => { @@ -72,6 +78,10 @@ pub fn make_get_url(permalinks: HashMap, config: Config) -> Glob } else { // anything else let mut permalink = config.make_permalink(&v); + if !trailing_slash && permalink.ends_with("/") { + permalink.pop(); // Removes the slash + } + if cachebust { permalink = format!("{}?t={}", permalink, config.build_timestamp.unwrap()); } @@ -105,6 +115,27 @@ mod tests { args.insert("cachebust".to_string(), to_value(true).unwrap()); assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/?t=1"); } + + #[test] + fn can_remove_trailing_slashes() { + let config = Config::default(); + let static_fn = make_get_url(HashMap::new(), config); + let mut args = HashMap::new(); + args.insert("path".to_string(), to_value("app.css").unwrap()); + args.insert("trailing_slash".to_string(), to_value(false).unwrap()); + assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css"); + } + + #[test] + fn can_remove_slashes_and_cachebust() { + let config = Config::default(); + let static_fn = make_get_url(HashMap::new(), config); + let mut args = HashMap::new(); + args.insert("path".to_string(), to_value("app.css").unwrap()); + args.insert("trailing_slash".to_string(), to_value(false).unwrap()); + args.insert("cachebust".to_string(), to_value(true).unwrap()); + assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css?t=1"); + } #[test] fn can_link_to_some_static_file() { diff --git a/docs/content/documentation/templates/overview.md b/docs/content/documentation/templates/overview.md index c69ad73e..78160f92 100644 --- a/docs/content/documentation/templates/overview.md +++ b/docs/content/documentation/templates/overview.md @@ -70,5 +70,12 @@ we want to link to the file that is located at `static/css/app.css`: {{ get_url(path="css/app.css") }} ``` +For assets it is reccommended that you pass `trailing_slash=false` to the `get_url` function. This prevents errors +when dealing with certain hosting providers. An example is: + +```jinja2 +{{ get_url(path="css/app.css", trailing_slash=false) }} +``` + In the case of non-internal links, you can also add a cachebust of the format `?t=1290192` at the end of a URL by passing `cachebust=true` to the `get_url` function. diff --git a/docs/templates/index.html b/docs/templates/index.html index 88b041eb..c366dbb9 100644 --- a/docs/templates/index.html +++ b/docs/templates/index.html @@ -7,8 +7,8 @@ {% block title %}{{ config.title }}{% endblock title %} - - + +