diff --git a/CHANGELOG.md b/CHANGELOG.md index f677c72..08d7ca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - Fix XML templates overriding and reloading - `title` and `description` are now optional in the front matter - Add GenericConfig, Vim syntax +- Add `_index.md` for homepage as well +- Allow sorting by `none`, `date` and `order` for sections ## 0.0.4 (2017-04-23) diff --git a/src/page.rs b/src/page.rs index 1296bf5..9530c3f 100644 --- a/src/page.rs +++ b/src/page.rs @@ -239,8 +239,10 @@ impl ser::Serialize for Page { } } -/// Sort pages -/// TODO: write doc and tests +/// Sort pages using the method for the given section +/// +/// Any pages that doesn't have a date when the sorting method is date or order +/// when the sorting method is order will be ignored. pub fn sort_pages(pages: Vec, section: Option<&Section>) -> Vec { let sort_by = if let Some(ref sec) = section { sec.meta.sort_by() @@ -260,7 +262,7 @@ pub fn sort_pages(pages: Vec, section: Option<&Section>) -> Vec { } } can_be_sorted.sort_by(|a, b| b.meta.date().unwrap().cmp(&a.meta.date().unwrap())); - can_be_sorted.append(&mut cannot_be_sorted); + // can_be_sorted.append(&mut cannot_be_sorted); can_be_sorted }, @@ -275,7 +277,7 @@ pub fn sort_pages(pages: Vec, section: Option<&Section>) -> Vec { } } can_be_sorted.sort_by(|a, b| b.meta.order().cmp(&a.meta.order())); - can_be_sorted.append(&mut cannot_be_sorted); + // can_be_sorted.append(&mut cannot_be_sorted); can_be_sorted }, @@ -429,6 +431,37 @@ mod tests { assert_eq!(pages[2].clone().meta.order.unwrap(), 1); } + #[test] + fn test_can_sort_none() { + let input = vec![ + create_page_with_order(2), + create_page_with_order(3), + create_page_with_order(1), + ]; + let mut front_matter = FrontMatter::default(); + front_matter.sort_by = Some(SortBy::None); + let section = Section::new(Path::new("hey"), front_matter); + let pages = sort_pages(input, Some(§ion)); + // Should be sorted by date + assert_eq!(pages[0].clone().meta.order.unwrap(), 2); + assert_eq!(pages[1].clone().meta.order.unwrap(), 3); + assert_eq!(pages[2].clone().meta.order.unwrap(), 1); + } + + #[test] + fn test_ignore_page_with_missing_field() { + let input = vec![ + create_page_with_order(2), + create_page_with_order(3), + create_page_with_date("2019-01-01"), + ]; + let mut front_matter = FrontMatter::default(); + front_matter.sort_by = Some(SortBy::Order); + let section = Section::new(Path::new("hey"), front_matter); + let pages = sort_pages(input, Some(§ion)); + assert_eq!(pages.len(), 2); + } + #[test] fn test_populate_previous_and_next_pages() { let input = vec![ diff --git a/test_site/content/posts/tutorials/devops/_index.md b/test_site/content/posts/tutorials/devops/_index.md index b4f4ab5..d2e9ea9 100644 --- a/test_site/content/posts/tutorials/devops/_index.md +++ b/test_site/content/posts/tutorials/devops/_index.md @@ -1,4 +1,4 @@ +++ title = "DevOps" -description = "" +sort_by = "order" +++ diff --git a/test_site/content/posts/tutorials/devops/docker.md b/test_site/content/posts/tutorials/devops/docker.md index 85796b1..5fa7749 100644 --- a/test_site/content/posts/tutorials/devops/docker.md +++ b/test_site/content/posts/tutorials/devops/docker.md @@ -1,7 +1,6 @@ +++ title = "Docker" -description = "" -date = "2017-02-01" +order = 1 +++ A simple page diff --git a/test_site/content/posts/tutorials/devops/nix.md b/test_site/content/posts/tutorials/devops/nix.md index 4fdf351..3b49d2a 100644 --- a/test_site/content/posts/tutorials/devops/nix.md +++ b/test_site/content/posts/tutorials/devops/nix.md @@ -1,7 +1,6 @@ +++ title = "Nix" -description = "" -date = "2017-03-01" +order = 2 +++ A simple page diff --git a/test_site/content/posts/tutorials/programming/_index.md b/test_site/content/posts/tutorials/programming/_index.md index d46aac2..5a6e2e3 100644 --- a/test_site/content/posts/tutorials/programming/_index.md +++ b/test_site/content/posts/tutorials/programming/_index.md @@ -1,4 +1,4 @@ +++ title = "Programming" -description = "" +sort_by = "order" +++ diff --git a/test_site/content/posts/tutorials/programming/python.md b/test_site/content/posts/tutorials/programming/python.md index 8a0a68d..39c5457 100644 --- a/test_site/content/posts/tutorials/programming/python.md +++ b/test_site/content/posts/tutorials/programming/python.md @@ -1,6 +1,6 @@ +++ title = "Python tutorial" -description = "" +order = 1 +++ A simple page diff --git a/test_site/content/posts/tutorials/programming/rust.md b/test_site/content/posts/tutorials/programming/rust.md index b217323..4d2cd15 100644 --- a/test_site/content/posts/tutorials/programming/rust.md +++ b/test_site/content/posts/tutorials/programming/rust.md @@ -1,6 +1,6 @@ +++ title = "Rust" -description = "" +order = 2 +++ A simple page diff --git a/tests/site.rs b/tests/site.rs index fbdbbac..b9f41cc 100644 --- a/tests/site.rs +++ b/tests/site.rs @@ -44,7 +44,7 @@ fn test_can_parse_site() { let posts_section = &site.sections[&posts_path]; assert_eq!(posts_section.subsections.len(), 1); //println!("{:#?}", posts_section.pages); - assert_eq!(posts_section.pages.len(), 5); + assert_eq!(posts_section.pages.len(), 4); let tutorials_section = &site.sections[&posts_path.join("tutorials")]; assert_eq!(tutorials_section.subsections.len(), 2);