From ffe8a243334c5eab28516e460912a626cbf22843 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 30 May 2017 19:23:07 +0900 Subject: [PATCH] Sort tag/category page by date Fix #75 --- CHANGELOG.md | 4 ++++ README.md | 5 +++++ src/content/taxonomies.rs | 17 ++++++++++++----- src/site.rs | 4 ++++ test_site/config.toml | 1 - 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0011ef9..6d65854 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.0.7 (unreleased) + +- Sort individual tag/category pages by date + ## 0.0.6 (2017-05-24) - Fix missing serialized data for sections diff --git a/README.md b/README.md index 20d7530..f82e372 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,10 @@ You can also paginate section, including the index by setting the `paginate_by` This represents the number of pages for each pager of the paginator. You will need to access pages through the `paginator` object. (TODO: document that). +### Taxonomies: tags and categories + +Individual tag/category pages are only supported for pages having a date. + ### Code highlighting themes Code highlighting can be turned on by setting `highlight_code = true` in `config.toml`. @@ -223,6 +227,7 @@ In case of shortcodes with a body, the body will be passed as the `body` variabl ## Adding syntax highlighting languages and themes + ### Adding a syntax Syntax highlighting depends on submodules so ensure you load them first: ```bash diff --git a/src/content/taxonomies.rs b/src/content/taxonomies.rs index 1f507ae..efef01f 100644 --- a/src/content/taxonomies.rs +++ b/src/content/taxonomies.rs @@ -6,6 +6,7 @@ use tera::{Context, Tera}; use config::Config; use errors::{Result, ResultExt}; use content::Page; +use content::sorting::{SortBy, sort_pages}; #[derive(Debug, Copy, Clone, PartialEq)] @@ -24,10 +25,12 @@ pub struct TaxonomyItem { impl TaxonomyItem { pub fn new(name: &str, pages: Vec) -> TaxonomyItem { + // We shouldn't have any pages without dates there + let (sorted_pages, _) = sort_pages(pages, SortBy::Date); TaxonomyItem { name: name.to_string(), slug: slugify(name), - pages, + pages: sorted_pages, } } } @@ -48,6 +51,14 @@ impl Taxonomy { // Find all the tags/categories first for page in all_pages { + // Don't consider pages without pages for tags/categories as that's the only thing + // we can sort pages with across sections + // If anyone sees that comment and wonder wtf, please open an issue as I can't think of + // usecases other than blog posts for built-in taxonomies + if page.meta.date.is_none() { + continue; + } + if let Some(ref category) = page.meta.category { categories .entry(category.to_string()) @@ -109,10 +120,6 @@ impl Taxonomy { let name = self.get_single_item_name(); let mut context = Context::new(); context.add("config", config); - // TODO: how to sort categories and tag content? - // Have a setting in config.toml or a _category.md and _tag.md - // The latter is more in line with the rest of Gutenberg but order ordering - // doesn't really work across sections. context.add(&name, item); context.add("current_url", &config.make_permalink(&format!("{}/{}", name, item.slug))); context.add("current_path", &format!("/{}/{}", name, item.slug)); diff --git a/src/site.rs b/src/site.rs index 132cbf9..07013d8 100644 --- a/src/site.rs +++ b/src/site.rs @@ -380,6 +380,10 @@ impl Site { } fn render_taxonomy(&self, taxonomy: &Taxonomy) -> Result<()> { + if taxonomy.items.is_empty() { + return Ok(()) + } + ensure_directory_exists(&self.output_path)?; let output_path = self.output_path.join(&taxonomy.get_list_name()); diff --git a/test_site/config.toml b/test_site/config.toml index ba0629c..4d058c3 100644 --- a/test_site/config.toml +++ b/test_site/config.toml @@ -2,6 +2,5 @@ title = "My site" base_url = "https://replace-this-with-your-url.com" highlight_code = true - [extra.author] name = "Vincent Prouillet"