Add aliases to pages. Fix #86

This commit is contained in:
Vincent Prouillet 2017-06-16 23:09:01 +09:00
parent 26e119479c
commit 6a059d86d0
6 changed files with 33 additions and 0 deletions

View file

@ -9,6 +9,8 @@
- Add `section` to a page Tera context if there is one - Add `section` to a page Tera context if there is one
- Reverse `order` sorting to be more intuitive: they are now desc, think of them - Reverse `order` sorting to be more intuitive: they are now desc, think of them
as 1st, 2nd in the list as 1st, 2nd in the list
- Add `aliases` to pages for when you are changing urls but want to redirect
to the new one
## 0.0.6 (2017-05-24) ## 0.0.6 (2017-05-24)

View file

@ -91,6 +91,7 @@ A front-matter has only optional variables:
- category: only one category is allowed - category: only one category is allowed
- draft: whether the post is a draft or not - draft: whether the post is a draft or not
- template: if you want to change the template used to render that specific page - template: if you want to change the template used to render that specific page
- aliases: which URL to redirect to the new: useful when you changed a page URL and don't want to 404
Even if your front-matter is empty, you will need to put the `+++`. Even if your front-matter is empty, you will need to put the `+++`.
You can also, like in the config, add your own variables in a `[extra]` table. You can also, like in the config, add your own variables in a `[extra]` table.

View file

@ -28,6 +28,9 @@ pub struct PageFrontMatter {
pub category: Option<String>, pub category: Option<String>,
/// Integer to use to order content. Lowest is at the bottom, highest first /// Integer to use to order content. Lowest is at the bottom, highest first
pub order: Option<usize>, pub order: Option<usize>,
/// All aliases for that page. Gutenberg will create HTML templates that will
#[serde(skip_serializing)]
pub aliases: Option<Vec<String>>,
/// Specify a template different from `page.html` to use for that page /// Specify a template different from `page.html` to use for that page
#[serde(skip_serializing)] #[serde(skip_serializing)]
pub template: Option<String>, pub template: Option<String>,
@ -100,6 +103,7 @@ impl Default for PageFrontMatter {
tags: None, tags: None,
category: None, category: None,
order: None, order: None,
aliases: None,
template: None, template: None,
extra: None, extra: None,
} }

View file

@ -337,6 +337,8 @@ impl Site {
/// Deletes the `public` directory and builds the site /// Deletes the `public` directory and builds the site
pub fn build(&self) -> Result<()> { pub fn build(&self) -> Result<()> {
self.clean()?; self.clean()?;
// Render aliases first to allow overwriting
self.render_aliases()?;
self.render_sections()?; self.render_sections()?;
self.render_orphan_pages()?; self.render_orphan_pages()?;
self.render_sitemap()?; self.render_sitemap()?;
@ -352,6 +354,25 @@ impl Site {
self.copy_static_directory() self.copy_static_directory()
} }
pub fn render_aliases(&self) -> Result<()> {
for page in self.pages.values() {
if let Some(ref aliases) = page.meta.aliases {
for alias in aliases {
let mut output_path = self.output_path.to_path_buf();
for component in alias.split("/") {
output_path.push(&component);
if !output_path.exists() {
create_directory(&output_path)?;
}
}
create_file(&output_path.join("index.html"), &render_redirect_template(&page.permalink, &self.tera)?)?;
}
}
}
Ok(())
}
/// Renders robots.txt /// Renders robots.txt
pub fn render_robots(&self) -> Result<()> { pub fn render_robots(&self) -> Result<()> {
ensure_directory_exists(&self.output_path)?; ensure_directory_exists(&self.output_path)?;

View file

@ -3,6 +3,7 @@ title = "Fixed slug"
description = "" description = ""
slug = "something-else" slug = "something-else"
date = "2017-01-01" date = "2017-01-01"
aliases = ["/an-old-url/old-page"]
+++ +++
A simple page with a slug defined A simple page with a slug defined

View file

@ -118,6 +118,10 @@ fn can_build_site_without_live_reload() {
assert!(file_exists!(public, "posts/tutorials/programming/index.html")); assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
// TODO: add assertion for syntax highlighting // TODO: add assertion for syntax highlighting
// aliases work
assert!(file_exists!(public, "an-old-url/old-page/index.html"));
assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
// 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);