Do not append non-sortables pages when not sortable

This commit is contained in:
Vincent Prouillet 2017-05-01 15:35:49 +09:00
parent 9669c3562c
commit b256aaf7d0
9 changed files with 46 additions and 13 deletions

View file

@ -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)

View file

@ -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<Page>, section: Option<&Section>) -> Vec<Page> {
let sort_by = if let Some(ref sec) = section {
sec.meta.sort_by()
@ -260,7 +262,7 @@ pub fn sort_pages(pages: Vec<Page>, section: Option<&Section>) -> Vec<Page> {
}
}
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<Page>, section: Option<&Section>) -> Vec<Page> {
}
}
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(&section));
// 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(&section));
assert_eq!(pages.len(), 2);
}
#[test]
fn test_populate_previous_and_next_pages() {
let input = vec![

View file

@ -1,4 +1,4 @@
+++
title = "DevOps"
description = ""
sort_by = "order"
+++

View file

@ -1,7 +1,6 @@
+++
title = "Docker"
description = ""
date = "2017-02-01"
order = 1
+++
A simple page

View file

@ -1,7 +1,6 @@
+++
title = "Nix"
description = ""
date = "2017-03-01"
order = 2
+++
A simple page

View file

@ -1,4 +1,4 @@
+++
title = "Programming"
description = ""
sort_by = "order"
+++

View file

@ -1,6 +1,6 @@
+++
title = "Python tutorial"
description = ""
order = 1
+++
A simple page

View file

@ -1,6 +1,6 @@
+++
title = "Rust"
description = ""
order = 2
+++
A simple page

View file

@ -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);