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
This commit is contained in:
Carson Page 2017-11-10 10:46:14 -06:00 committed by Vincent Prouillet
parent d43e32dfcc
commit 8759323a16
3 changed files with 41 additions and 3 deletions

View file

@ -59,7 +59,13 @@ pub fn make_get_url(permalinks: HashMap<String, String>, config: Config) -> Glob
.map_or(false, |c| {
from_value::<bool>(c.clone()).unwrap_or(false)
});
let trailing_slash = args
.get("trailing_slash")
.map_or(true, |c| {
from_value::<bool>(c.clone()).unwrap_or(true)
});
match args.get("path") {
Some(val) => match from_value::<String>(val.clone()) {
Ok(v) => {
@ -72,6 +78,10 @@ pub fn make_get_url(permalinks: HashMap<String, String>, 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() {

View file

@ -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.

View file

@ -7,8 +7,8 @@
<meta name="description" content="{% block description %}{{ config.description }}{% endblock description %}">
<meta name="author" content="{{ config.extra.author }}">
<title>{% block title %}{{ config.title }}{% endblock title %}</title>
<link rel="stylesheet" href="{{ get_url(path="site.css") }}"/>
<link rel="icon" href="{{ get_url(path="favicon.ico") }}">
<link rel="stylesheet" href="{{ get_url(path="site.css", trailing_slash=false) }}"/>
<link rel="icon" href="{{ get_url(path="favicon.ico", trailing_slash=false) }}">
</head>
<body>