Skip to content

Commit

Permalink
Handle pages named index.md in sections
Browse files Browse the repository at this point in the history
Closes #2082
  • Loading branch information
Keats committed Feb 16, 2023
1 parent bb17100 commit ee4cbb6
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ for breaking changes with libsass: look for "beginning in Dart Sass"
This will error if 2 values are set
- Code blocks content are no longer included in the search index
- Remove built-ins shortcodes

- Having a file called `index.md` in a folder with a `_index.md` is now an error

### Other

Expand Down
46 changes: 23 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion components/content/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ impl Page {
}
} else {
let mut path = if page.file.components.is_empty() {
page.slug.clone()
if page.file.name == "index" {
String::new()
} else {
page.slug.clone()
}
} else {
format!("{}/{}", page.file.components.join("/"), page.slug)
};
Expand Down
50 changes: 39 additions & 11 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod sass;
pub mod sitemap;
pub mod tpls;

use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fs::{remove_dir_all, remove_file};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, RwLock};
Expand Down Expand Up @@ -179,6 +179,12 @@ impl Site {
.collect();
allowed_index_filenames.push("_index.md".to_string());

// We will insert colocated pages (those with a index.md filename)
// at the end to detect pages that are actually errors:
// when there is both a _index.md and index.md in the same folder
let mut pages = Vec::new();
let mut components = HashSet::new();

loop {
let entry: DirEntry = match dir_walker.next() {
None => break,
Expand Down Expand Up @@ -242,8 +248,9 @@ impl Site {
for index_file in index_files {
let section =
Section::from_file(index_file.path(), &self.config, &self.base_path)?;
components.extend(section.file.components.clone());

// if the section is drafted we can skip the enitre dir
// if the section is drafted we can skip the entire dir
if section.meta.draft && !self.include_drafts {
dir_walker.skip_current_dir();
continue;
Expand All @@ -253,19 +260,37 @@ impl Site {
}
} else {
let page = Page::from_file(path, &self.config, &self.base_path)?;
pages.push(page);
}
}
self.create_default_index_sections()?;

// should we skip drafts?
if page.meta.draft && !self.include_drafts {
continue;
for page in pages {
// should we skip drafts?
if page.meta.draft && !self.include_drafts {
continue;
}

// We are only checking it on load and not in add_page since we have access to
// all the components there.
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,
};

if is_invalid {
bail!("We can't have a page called `index.md` in the same folder as an index section in {:?}", page.file.parent);
}
pages_insert_anchors.insert(
page.file.path.clone(),
self.find_parent_section_insert_anchor(&page.file.parent.clone(), &page.lang),
);
self.add_page(page, false)?;
}

pages_insert_anchors.insert(
page.file.path.clone(),
self.find_parent_section_insert_anchor(&page.file.parent.clone(), &page.lang),
);
self.add_page(page, false)?;
}
self.create_default_index_sections()?;

{
let library = self.library.read().unwrap();
Expand Down Expand Up @@ -446,6 +471,9 @@ 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 =
Expand Down
18 changes: 18 additions & 0 deletions components/site/tests/invalid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mod common;

use site::Site;
use std::env;

#[test]
fn errors_on_index_md_page_in_section() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_sites_invalid");
path.push("indexmd");
let config_file = path.join("config.toml");
let mut site = Site::new(&path, &config_file).unwrap();
let res = site.load();
assert!(res.is_err());
let err = res.unwrap_err();
assert!(format!("{:?}", err)
.contains("We can't have a page called `index.md` in the same folder as an index section"));
}
3 changes: 3 additions & 0 deletions test_sites_invalid/indexmd/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title = "My Integration Testing site"
base_url = "https://replace-this-with-your-url.com"

2 changes: 2 additions & 0 deletions test_sites_invalid/indexmd/content/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
+++
+++
2 changes: 2 additions & 0 deletions test_sites_invalid/indexmd/content/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
+++
+++

0 comments on commit ee4cbb6

Please sign in to comment.