Use lang code in permalinks

This commit is contained in:
Vincent Prouillet 2018-12-28 10:42:26 +01:00
parent e50d3daad1
commit b0f6963e4c
3 changed files with 38 additions and 5 deletions

View file

@ -2,6 +2,7 @@
## 0.6.0 (unreleased) ## 0.6.0 (unreleased)
- Add support for content in multiple languages
## 0.5.1 (2018-12-14) ## 0.5.1 (2018-12-14)

View file

@ -152,11 +152,17 @@ impl Page {
if let Some(ref p) = page.meta.path { if let Some(ref p) = page.meta.path {
page.path = p.trim().trim_left_matches('/').to_string(); page.path = p.trim().trim_left_matches('/').to_string();
} else { } else {
page.path = if page.file.components.is_empty() { let mut path = if page.file.components.is_empty() {
page.slug.clone() page.slug.clone()
} else { } else {
format!("{}/{}", page.file.components.join("/"), page.slug) format!("{}/{}", page.file.components.join("/"), page.slug)
}; };
if let Some(ref lang) = page.lang {
path = format!("{}/{}", lang, path);
}
page.path = path;
} }
if !page.path.ends_with('/') { if !page.path.ends_with('/') {
page.path = format!("{}/", page.path); page.path = format!("{}/", page.path);
@ -580,7 +586,8 @@ Bonjour le monde"#
assert!(res.is_ok()); assert!(res.is_ok());
let page = res.unwrap(); let page = res.unwrap();
assert_eq!(page.lang, Some("fr".to_string())); assert_eq!(page.lang, Some("fr".to_string()));
assert_eq!(page.slug, "hello".to_string()); assert_eq!(page.slug, "hello");
assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
} }
#[test] #[test]
@ -597,6 +604,25 @@ Bonjour le monde"#
let page = res.unwrap(); let page = res.unwrap();
assert_eq!(page.meta.date, Some("2018-10-08".to_string())); assert_eq!(page.meta.date, Some("2018-10-08".to_string()));
assert_eq!(page.lang, Some("fr".to_string())); assert_eq!(page.lang, Some("fr".to_string()));
assert_eq!(page.slug, "hello".to_string()); assert_eq!(page.slug, "hello");
assert_eq!(page.permalink, "http://a-website.com/fr/hello/");
}
#[test]
fn i18n_frontmatter_path_overrides_default_permalink() {
let mut config = Config::default();
config.languages.push(Language {code: String::from("fr"), rss: false});
let content = r#"
+++
path = "bonjour"
+++
Bonjour le monde"#
.to_string();
let res = Page::parse(Path::new("hello.fr.md"), &content, &config);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.lang, Some("fr".to_string()));
assert_eq!(page.slug, "hello");
assert_eq!(page.permalink, "http://a-website.com/bonjour/");
} }
} }

View file

@ -89,7 +89,12 @@ impl Section {
let (word_count, reading_time) = get_reading_analytics(&section.raw_content); let (word_count, reading_time) = get_reading_analytics(&section.raw_content);
section.word_count = Some(word_count); section.word_count = Some(word_count);
section.reading_time = Some(reading_time); section.reading_time = Some(reading_time);
section.path = format!("{}/", section.file.components.join("/")); let path = format!("{}/", section.file.components.join("/"));
if let Some(ref lang) = section.lang {
section.path = format!("{}/{}", lang, path);
} else {
section.path = path;
}
section.components = section section.components = section
.path .path
.split('/') .split('/')
@ -302,9 +307,10 @@ mod tests {
+++ +++
Bonjour le monde"# Bonjour le monde"#
.to_string(); .to_string();
let res = Section::parse(Path::new("hello.fr.md"), &content, &config); let res = Section::parse(Path::new("content/hello/nested/_index.fr.md"), &content, &config);
assert!(res.is_ok()); assert!(res.is_ok());
let section = res.unwrap(); let section = res.unwrap();
assert_eq!(section.lang, Some("fr".to_string())); assert_eq!(section.lang, Some("fr".to_string()));
assert_eq!(section.permalink, "http://a-website.com/fr/hello/nested/");
} }
} }