Allow sorting subsections by weight

This commit is contained in:
Vincent Prouillet 2017-09-26 17:04:18 +09:00
parent 2d97786e67
commit b248b03045
6 changed files with 23 additions and 9 deletions

View file

@ -3,6 +3,7 @@
## 0.1.4 (unreleased)
- Fix `section.subsections` not being filled correctly
- `section.subsections` can now be sorted by a `weight` attribute on a section front-matter
- Do nothing on directory adding/removal in livereload
- Add back `draft` on pages that was wrongly removed

View file

@ -17,9 +17,13 @@ pub struct SectionFrontMatter {
pub title: Option<String>,
/// Description in <meta> that appears when linked, e.g. on twitter
pub description: Option<String>,
/// Whether to sort by "date", "order" or "none". Defaults to `none`.
/// Whether to sort by "date", "order", "weight" or "none". Defaults to `none`.
#[serde(skip_serializing)]
pub sort_by: Option<SortBy>,
/// The weight for this section. This is used by the parent section to order its subsections.
/// Higher values means it ends at the end.
#[serde(skip_serializing)]
pub weight: Option<usize>,
/// Optional template, if we want to specify which template to render for that page
#[serde(skip_serializing)]
pub template: Option<String>,
@ -70,6 +74,10 @@ impl SectionFrontMatter {
f.insert_anchor = Some(InsertAnchor::None);
}
if f.weight.is_none() {
f.weight = Some(0);
}
Ok(f)
}
@ -97,6 +105,7 @@ impl Default for SectionFrontMatter {
title: None,
description: None,
sort_by: Some(SortBy::None),
weight: Some(0),
template: None,
paginate_by: None,
paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()),

View file

@ -330,14 +330,14 @@ impl Site {
let sections = self.sections.clone();
for section in self.sections.values_mut() {
match grandparent_paths.get(&section.file.parent) {
Some(paths) => {
for p in paths {
section.subsections.push(sections[p].clone());
}
},
None => continue,
};
if let Some(paths) = grandparent_paths.get(&section.file.parent) {
section.subsections = paths
.iter()
.map(|p| sections[p].clone())
.collect::<Vec<_>>();
section.subsections
.sort_by(|a, b| a.meta.weight.unwrap().cmp(&b.meta.weight.unwrap()));
}
}
}

View file

@ -2,4 +2,5 @@
title = "DevOps"
sort_by = "order"
redirect_to = "posts/tutorials/devops/docker"
weight = 10
+++

View file

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

View file

@ -47,6 +47,8 @@ fn can_parse_site() {
let tutorials_section = &site.sections[&posts_path.join("tutorials").join("_index.md")];
assert_eq!(tutorials_section.subsections.len(), 2);
assert_eq!(tutorials_section.subsections[0].clone().meta.title.unwrap(), "Programming");
assert_eq!(tutorials_section.subsections[1].clone().meta.title.unwrap(), "DevOps");
assert_eq!(tutorials_section.pages.len(), 0);
let devops_section = &site.sections[&posts_path.join("tutorials").join("devops").join("_index.md")];