Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute canonical path before adjusting parent path #856

Merged
merged 3 commits into from
Nov 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions components/library/src/content/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl FileInfo {
let file_path = path.to_path_buf();
let mut parent = file_path.parent().expect("Get parent of page").to_path_buf();
let name = path.file_stem().unwrap().to_string_lossy().to_string();
let canonical = parent.join(&name);
let mut components =
find_content_components(&file_path.strip_prefix(base_path).unwrap_or(&file_path));
let relative = if !components.is_empty() {
Expand All @@ -78,7 +79,7 @@ impl FileInfo {
path: file_path,
// We don't care about grand parent for pages
grand_parent: None,
canonical: parent.join(&name),
canonical,
parent,
name,
components,
Expand Down Expand Up @@ -135,7 +136,7 @@ impl FileInfo {
}

self.name = parts.swap_remove(0);
self.canonical = self.parent.join(&self.name);
self.canonical = self.path.parent().expect("Get parent of page path").join(&self.name);
let lang = parts.swap_remove(0);

Ok(lang)
Expand Down Expand Up @@ -254,4 +255,28 @@ mod tests {
assert!(res.is_ok());
assert_eq!(res.unwrap(), "fr");
}

/// Regression test for https://github.com/getzola/zola/pull/856.
#[test]
fn correct_canonical_for_index() {
let file = FileInfo::new_page(
&Path::new("/home/vincent/code/site/content/posts/tutorials/python/index.md"),
&PathBuf::new(),
);
assert_eq!(file.canonical, Path::new("/home/vincent/code/site/content/posts/tutorials/python/index"));
}

/// Regression test for https://github.com/getzola/zola/pull/856.
#[test]
fn correct_canonical_after_find_language() {
let mut config = Config::default();
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
let mut file = FileInfo::new_page(
&Path::new("/home/vincent/code/site/content/posts/tutorials/python/index.fr.md"),
&PathBuf::new(),
);
let res = file.find_language(&config);
assert!(res.is_ok());
assert_eq!(file.canonical, Path::new("/home/vincent/code/site/content/posts/tutorials/python/index"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason for those bugs is that in my mind the canonical path was /home/vincent/code/site/content/posts/tutorials/python.
Anyway, thanks a lot!

}
}