From b248b030455bdbe81aac00961cbd65a61cd7d03b Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 26 Sep 2017 17:04:18 +0900 Subject: [PATCH] Allow sorting subsections by weight --- CHANGELOG.md | 1 + components/front_matter/src/section.rs | 11 ++++++++++- components/site/src/lib.rs | 16 ++++++++-------- .../content/posts/tutorials/devops/_index.md | 1 + .../posts/tutorials/programming/_index.md | 1 + components/site/tests/site.rs | 2 ++ 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f81c42f..05974b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/components/front_matter/src/section.rs b/components/front_matter/src/section.rs index 21063cd..6de4c1b 100644 --- a/components/front_matter/src/section.rs +++ b/components/front_matter/src/section.rs @@ -17,9 +17,13 @@ pub struct SectionFrontMatter { pub title: Option, /// Description in that appears when linked, e.g. on twitter pub description: Option, - /// 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, + /// 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, /// Optional template, if we want to specify which template to render for that page #[serde(skip_serializing)] pub template: Option, @@ -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()), diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 257799e..786029a 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -330,14 +330,14 @@ impl Site { let sections = self.sections.clone(); for section in self.sections.values_mut() { - match grandparent_paths.get(§ion.file.parent) { - Some(paths) => { - for p in paths { - section.subsections.push(sections[p].clone()); - } - }, - None => continue, - }; + if let Some(paths) = grandparent_paths.get(§ion.file.parent) { + section.subsections = paths + .iter() + .map(|p| sections[p].clone()) + .collect::>(); + section.subsections + .sort_by(|a, b| a.meta.weight.unwrap().cmp(&b.meta.weight.unwrap())); + } } } diff --git a/components/site/test_site/content/posts/tutorials/devops/_index.md b/components/site/test_site/content/posts/tutorials/devops/_index.md index d8623ad..8e99674 100644 --- a/components/site/test_site/content/posts/tutorials/devops/_index.md +++ b/components/site/test_site/content/posts/tutorials/devops/_index.md @@ -2,4 +2,5 @@ title = "DevOps" sort_by = "order" redirect_to = "posts/tutorials/devops/docker" +weight = 10 +++ diff --git a/components/site/test_site/content/posts/tutorials/programming/_index.md b/components/site/test_site/content/posts/tutorials/programming/_index.md index 5a6e2e3..4a2c76f 100644 --- a/components/site/test_site/content/posts/tutorials/programming/_index.md +++ b/components/site/test_site/content/posts/tutorials/programming/_index.md @@ -1,4 +1,5 @@ +++ title = "Programming" sort_by = "order" +weight = 1 +++ diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 695c75f..cf7cfe2 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -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")];