From c67655e29f2cac04dc9500a5d0e7338f6d658dd2 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 16 Feb 2023 22:41:01 +0100 Subject: [PATCH] Fix colocated assets in content directory Closes #2101 --- CHANGELOG.md | 4 ++++ components/content/src/file_info.rs | 1 + components/content/src/page.rs | 2 +- components/site/src/lib.rs | 7 ++----- components/site/tests/site.rs | 11 +++++++++-- docs/content/hello/index.md | 4 ++++ test_site/content/colocated-assets/index.md | 3 +++ test_site/templates/page.html | 1 + 8 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 docs/content/hello/index.md create mode 100644 test_site/content/colocated-assets/index.md diff --git a/CHANGELOG.md b/CHANGELOG.md index d8ca8e2a..0d3ab895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.17.1 (unreleased) + +- Fix bugs with colocated directories in the root `content` directory + ## 0.17.0 (2023-02-16) ### Breaking diff --git a/components/content/src/file_info.rs b/components/content/src/file_info.rs index 37ffcf29..a94cb431 100644 --- a/components/content/src/file_info.rs +++ b/components/content/src/file_info.rs @@ -77,6 +77,7 @@ impl FileInfo { val.push('/'); val }); + components.pop(); // also set parent_path to grandparent instead parent = parent.parent().unwrap().to_path_buf(); diff --git a/components/content/src/page.rs b/components/content/src/page.rs index c51d05b1..94672b2b 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -157,7 +157,7 @@ impl Page { } } else { let mut path = if page.file.components.is_empty() { - if page.file.name == "index" { + if page.file.name == "index" && page.file.colocated_path.is_none() { String::new() } else { page.slug.clone() diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 67ca956e..b75a8cf5 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -277,8 +277,8 @@ impl Site { if page.file.filename == "index.md" { let is_invalid = match page.components.last() { Some(last) => components.contains(last), - // content/index.md is always invalid - None => true, + // content/index.md is always invalid, but content/colocated/index.md is ok + None => page.file.colocated_path.is_none(), }; if is_invalid { @@ -472,9 +472,6 @@ impl Site { } } - // We can't have a page called index.md when there is a _index.md in the same folder - if page.file.filename == "index.md" {} - self.permalinks.insert(page.file.relative.clone(), page.permalink.clone()); if render_md { let insert_anchor = diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 60e5adaa..64936124 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -21,7 +21,7 @@ fn can_parse_site() { let library = site.library.read().unwrap(); // Correct number of pages (sections do not count as pages, draft are ignored) - assert_eq!(library.pages.len(), 33); + assert_eq!(library.pages.len(), 34); let posts_path = path.join("content").join("posts"); // Make sure the page with a url doesn't have any sections @@ -39,7 +39,7 @@ fn can_parse_site() { // And that the sections are correct let index_section = library.sections.get(&path.join("content").join("_index.md")).unwrap(); assert_eq!(index_section.subsections.len(), 5); - assert_eq!(index_section.pages.len(), 3); + assert_eq!(index_section.pages.len(), 4); assert!(index_section.ancestors.is_empty()); let posts_section = library.sections.get(&posts_path.join("_index.md")).unwrap(); @@ -221,6 +221,13 @@ fn can_build_site_without_live_reload() { "robots.txt", "Sitemap: https://replace-this-with-your-url.com/sitemap.xml" )); + + // And + assert!(file_contains!( + public, + "colocated-assets/index.html", + "Assets in root content directory" + )); } #[test] diff --git a/docs/content/hello/index.md b/docs/content/hello/index.md new file mode 100644 index 00000000..008be635 --- /dev/null +++ b/docs/content/hello/index.md @@ -0,0 +1,4 @@ ++++ ++++ + +Hello \ No newline at end of file diff --git a/test_site/content/colocated-assets/index.md b/test_site/content/colocated-assets/index.md new file mode 100644 index 00000000..528f72b7 --- /dev/null +++ b/test_site/content/colocated-assets/index.md @@ -0,0 +1,3 @@ ++++ +title = "Assets in root content directory" ++++ \ No newline at end of file diff --git a/test_site/templates/page.html b/test_site/templates/page.html index e8cf68d6..b60bcfd0 100644 --- a/test_site/templates/page.html +++ b/test_site/templates/page.html @@ -1,6 +1,7 @@ {% extends "index.html" %} {% block content %} + {{ page.title | safe }} {{ page.content | safe }} {{ page.relative_path | safe }} {{ page.toc }}