Browse Source

Allow sorting subsections by weight

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
b248b03045
6 changed files with 23 additions and 9 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +10
    -1
      components/front_matter/src/section.rs
  3. +8
    -8
      components/site/src/lib.rs
  4. +1
    -0
      components/site/test_site/content/posts/tutorials/devops/_index.md
  5. +1
    -0
      components/site/test_site/content/posts/tutorials/programming/_index.md
  6. +2
    -0
      components/site/tests/site.rs

+ 1
- 0
CHANGELOG.md 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



+ 10
- 1
components/front_matter/src/section.rs 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()),


+ 8
- 8
components/site/src/lib.rs 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()));
}
}
}



+ 1
- 0
components/site/test_site/content/posts/tutorials/devops/_index.md View File

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

+ 1
- 0
components/site/test_site/content/posts/tutorials/programming/_index.md View File

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

+ 2
- 0
components/site/tests/site.rs 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")];


Loading…
Cancel
Save