Add a redirect_to parameter to section front matter

Close #103
This commit is contained in:
Vincent Prouillet 2017-07-25 16:56:13 +09:00
parent 26011e4c94
commit 2beb0621ca
6 changed files with 29 additions and 1 deletions

View file

@ -1,10 +1,14 @@
# Changelog # 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) ## 0.1.1 (2017-07-16)
- Fix RSS feed not behaving (https://github.com/Keats/gutenberg/issues/101) - Fix RSS feed not behaving (https://github.com/Keats/gutenberg/issues/101)
## 0.1.0 (2017-07-14) ## 0.1.0 (2017-07-14)
- Parallelize all the things - Parallelize all the things

View file

@ -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. 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 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 ### Table of contents
Each page/section will generate a table of content based on the title. It is accessible through `section.toc` and Each page/section will generate a table of content based on the title. It is accessible through `section.toc` and

View file

@ -38,6 +38,11 @@ pub struct SectionFrontMatter {
/// to be used directly, like a posts section in a personal site /// to be used directly, like a posts section in a personal site
#[serde(skip_serializing)] #[serde(skip_serializing)]
pub render: Option<bool>, pub render: Option<bool>,
/// 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<String>,
/// Any extra parameter present in the front matter /// Any extra parameter present in the front matter
pub extra: Option<HashMap<String, Value>>, pub extra: Option<HashMap<String, Value>>,
} }
@ -96,6 +101,7 @@ impl Default for SectionFrontMatter {
paginate_by: None, paginate_by: None,
paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()), paginate_path: Some(DEFAULT_PAGINATE_PATH.to_string()),
render: Some(true), render: Some(true),
redirect_to: None,
insert_anchor: Some(InsertAnchor::None), insert_anchor: Some(InsertAnchor::None),
extra: None, extra: None,
} }

View file

@ -647,6 +647,12 @@ impl Site {
return Ok(()); 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() { if section.meta.is_paginated() {
self.render_paginated(&output_path, section)?; self.render_paginated(&output_path, section)?;
} else { } else {

View file

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

View file

@ -122,6 +122,10 @@ fn can_build_site_without_live_reload() {
assert!(file_exists!(public, "an-old-url/old-page/index.html")); assert!(file_exists!(public, "an-old-url/old-page/index.html"));
assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else")); 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 // No tags or categories
assert_eq!(file_exists!(public, "categories/index.html"), false); assert_eq!(file_exists!(public, "categories/index.html"), false);
assert_eq!(file_exists!(public, "tags/index.html"), false); assert_eq!(file_exists!(public, "tags/index.html"), false);