From 2beb0621cae4e53bc4ba1142ccf825bb47f21839 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 25 Jul 2017 16:56:13 +0900 Subject: [PATCH] Add a redirect_to parameter to section front matter Close #103 --- CHANGELOG.md | 6 +++++- README.md | 7 +++++++ components/front_matter/src/section.rs | 6 ++++++ components/site/src/lib.rs | 6 ++++++ .../test_site/content/posts/tutorials/devops/_index.md | 1 + components/site/tests/site.rs | 4 ++++ 6 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb39424..b5ca4b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,14 @@ # Changelog +## 0.1.2 (unreleased) + +- Add `redirect_to` to section front matter to redirect when landing on section +root page + ## 0.1.1 (2017-07-16) - Fix RSS feed not behaving (https://github.com/Keats/gutenberg/issues/101) - ## 0.1.0 (2017-07-14) - Parallelize all the things diff --git a/README.md b/README.md index f4db349..5d9a741 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,13 @@ 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). +You can redirect a root section page to another url by using the `redirect_to` parameter of the front-matter followed +by a path: + +``` +redirect_to = "docs/docker" +``` + ### Table of contents Each page/section will generate a table of content based on the title. It is accessible through `section.toc` and diff --git a/components/front_matter/src/section.rs b/components/front_matter/src/section.rs index c728c1b..21063cd 100644 --- a/components/front_matter/src/section.rs +++ b/components/front_matter/src/section.rs @@ -38,6 +38,11 @@ pub struct SectionFrontMatter { /// to be used directly, like a posts section in a personal site #[serde(skip_serializing)] pub render: Option, + /// Whether to redirect when landing on that section. Defaults to `None`. + /// Useful for the same reason as `render` but when you don't want a 404 when + /// landing on the root section page + #[serde(skip_serializing)] + pub redirect_to: Option, /// Any extra parameter present in the front matter pub extra: Option>, } @@ -96,6 +101,7 @@ impl Default for SectionFrontMatter { paginate_by: None, paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()), render: Some(true), + redirect_to: None, insert_anchor: Some(InsertAnchor::None), extra: None, } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 89b713a..b66b677 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -647,6 +647,12 @@ impl Site { return Ok(()); } + if let Some(ref redirect_to) = section.meta.redirect_to { + let permalink = self.config.make_permalink(redirect_to); + create_file(&output_path.join("index.html"), &render_redirect_template(&permalink, &self.tera)?)?; + return Ok(()); + } + if section.meta.is_paginated() { self.render_paginated(&output_path, section)?; } else { 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 d2e9ea9..d8623ad 100644 --- a/components/site/test_site/content/posts/tutorials/devops/_index.md +++ b/components/site/test_site/content/posts/tutorials/devops/_index.md @@ -1,4 +1,5 @@ +++ title = "DevOps" sort_by = "order" +redirect_to = "posts/tutorials/devops/docker" +++ diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 4a52908..aa050ad 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -122,6 +122,10 @@ fn can_build_site_without_live_reload() { assert!(file_exists!(public, "an-old-url/old-page/index.html")); assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else")); + // redirect_to works + assert!(file_exists!(public, "posts/tutorials/devops/index.html")); + assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker")); + // No tags or categories assert_eq!(file_exists!(public, "categories/index.html"), false); assert_eq!(file_exists!(public, "tags/index.html"), false);