From a24851790c945008a7197eb334c5364960832faa Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 4 Oct 2017 09:23:25 +0900 Subject: [PATCH] Fix bug with colocated folders --- CHANGELOG.md | 1 + components/content/src/page.rs | 50 ++++++++++++++++--- .../content/posts/with-assets/index.md | 1 - 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8021ffb..da0eb2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Remove deprecated `link` param of `get_url` - Add 1337 color scheme - Defaults to compressed Sass output +- Fix regression wrt co-located assets slug detecting ## 0.1.3 (2017-08-31) diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 6f0ad10..0775eb6 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -86,7 +86,15 @@ impl Page { if let Some(ref slug) = page.meta.slug { slug.trim().to_string() } else { - slugify(page.file.name.clone()) + if page.file.name == "index" { + if let Some(parent) = page.file.path.parent() { + slugify(parent.file_name().unwrap().to_str().unwrap()) + } else { + slugify(page.file.name.clone()) + } + } else { + slugify(page.file.name.clone()) + } } }; @@ -210,6 +218,7 @@ impl ser::Serialize for Page { #[cfg(test)] mod tests { use std::collections::HashMap; + use std::io::Write; use std::fs::{File, create_dir}; use std::path::Path; @@ -311,25 +320,54 @@ Hello world } #[test] - fn page_with_assets_gets_right_parent_path() { + fn page_with_assets_gets_right_info() { let tmp_dir = TempDir::new("example").expect("create temp dir"); let path = tmp_dir.path(); create_dir(&path.join("content")).expect("create content temp dir"); create_dir(&path.join("content").join("posts")).expect("create posts temp dir"); - let nested_path = path.join("content").join("posts").join("assets"); + let nested_path = path.join("content").join("posts").join("with-assets"); create_dir(&nested_path).expect("create nested temp dir"); - File::create(nested_path.join("index.md")).unwrap(); + let mut f = File::create(nested_path.join("index.md")).unwrap(); + f.write_all(b"+++\n+++\n").unwrap(); File::create(nested_path.join("example.js")).unwrap(); File::create(nested_path.join("graph.jpg")).unwrap(); File::create(nested_path.join("fail.png")).unwrap(); - let res = Page::parse( + let res = Page::from_file( nested_path.join("index.md").as_path(), - "+++\nurl=\"hey\"+++\n", &Config::default() ); assert!(res.is_ok()); let page = res.unwrap(); assert_eq!(page.file.parent, path.join("content").join("posts")); + assert_eq!(page.slug, "with-assets"); + assert_eq!(page.assets.len(), 3); + assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/"); + } + + #[test] + fn page_with_assets_and_slug_overrides_path() { + let tmp_dir = TempDir::new("example").expect("create temp dir"); + let path = tmp_dir.path(); + create_dir(&path.join("content")).expect("create content temp dir"); + create_dir(&path.join("content").join("posts")).expect("create posts temp dir"); + let nested_path = path.join("content").join("posts").join("with-assets"); + create_dir(&nested_path).expect("create nested temp dir"); + let mut f = File::create(nested_path.join("index.md")).unwrap(); + f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap(); + File::create(nested_path.join("example.js")).unwrap(); + File::create(nested_path.join("graph.jpg")).unwrap(); + File::create(nested_path.join("fail.png")).unwrap(); + + let res = Page::from_file( + nested_path.join("index.md").as_path(), + &Config::default() + ); + assert!(res.is_ok()); + let page = res.unwrap(); + assert_eq!(page.file.parent, path.join("content").join("posts")); + assert_eq!(page.slug, "hey"); + assert_eq!(page.assets.len(), 3); + assert_eq!(page.permalink, "http://a-website.com/posts/hey/"); } } diff --git a/components/site/test_site/content/posts/with-assets/index.md b/components/site/test_site/content/posts/with-assets/index.md index 2000dfd..59ad0f6 100644 --- a/components/site/test_site/content/posts/with-assets/index.md +++ b/components/site/test_site/content/posts/with-assets/index.md @@ -1,7 +1,6 @@ +++ title = "With assets" description = "hey there" -slug = "with-assets" +++ Hello world