From b0f6963e4ca1a693ad4d874f5c5e9a3026ee4381 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 28 Dec 2018 10:42:26 +0100 Subject: [PATCH] Use lang code in permalinks --- CHANGELOG.md | 1 + components/library/src/content/page.rs | 32 ++++++++++++++++++++--- components/library/src/content/section.rs | 10 +++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e99db89..6534237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.6.0 (unreleased) +- Add support for content in multiple languages ## 0.5.1 (2018-12-14) diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index d1b324f..870ef6b 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -152,11 +152,17 @@ impl Page { if let Some(ref p) = page.meta.path { page.path = p.trim().trim_left_matches('/').to_string(); } else { - page.path = if page.file.components.is_empty() { + let mut path = if page.file.components.is_empty() { page.slug.clone() } else { 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('/') { page.path = format!("{}/", page.path); @@ -580,7 +586,8 @@ Bonjour le monde"# assert!(res.is_ok()); let page = res.unwrap(); 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] @@ -597,6 +604,25 @@ Bonjour le monde"# let page = res.unwrap(); assert_eq!(page.meta.date, Some("2018-10-08".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/"); } } diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index 31a25ff..c8369eb 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -89,7 +89,12 @@ impl Section { let (word_count, reading_time) = get_reading_analytics(§ion.raw_content); section.word_count = Some(word_count); 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 .path .split('/') @@ -302,9 +307,10 @@ mod tests { +++ Bonjour le monde"# .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()); let section = res.unwrap(); assert_eq!(section.lang, Some("fr".to_string())); + assert_eq!(section.permalink, "http://a-website.com/fr/hello/nested/"); } }