Browse Source

Add aliases to pages. Fix #86

index-subcmd
Vincent Prouillet 6 years ago
parent
commit
6a059d86d0
6 changed files with 33 additions and 0 deletions
  1. +2
    -0
      CHANGELOG.md
  2. +1
    -0
      README.md
  3. +4
    -0
      src/front_matter/page.rs
  4. +21
    -0
      src/site.rs
  5. +1
    -0
      test_site/content/posts/fixed-slug.md
  6. +4
    -0
      tests/site.rs

+ 2
- 0
CHANGELOG.md View File

@@ -9,6 +9,8 @@
- 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
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)



+ 1
- 0
README.md View File

@@ -91,6 +91,7 @@ A front-matter has only optional variables:
- category: only one category is allowed
- draft: whether the post is a draft or not
- 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 `+++`.
You can also, like in the config, add your own variables in a `[extra]` table.


+ 4
- 0
src/front_matter/page.rs View File

@@ -28,6 +28,9 @@ pub struct PageFrontMatter {
pub category: Option<String>,
/// Integer to use to order content. Lowest is at the bottom, highest first
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
#[serde(skip_serializing)]
pub template: Option<String>,
@@ -100,6 +103,7 @@ impl Default for PageFrontMatter {
tags: None,
category: None,
order: None,
aliases: None,
template: None,
extra: None,
}


+ 21
- 0
src/site.rs View File

@@ -337,6 +337,8 @@ impl Site {
/// Deletes the `public` directory and builds the site
pub fn build(&self) -> Result<()> {
self.clean()?;
// Render aliases first to allow overwriting
self.render_aliases()?;
self.render_sections()?;
self.render_orphan_pages()?;
self.render_sitemap()?;
@@ -352,6 +354,25 @@ impl Site {
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
pub fn render_robots(&self) -> Result<()> {
ensure_directory_exists(&self.output_path)?;


+ 1
- 0
test_site/content/posts/fixed-slug.md View File

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

A simple page with a slug defined


+ 4
- 0
tests/site.rs View File

@@ -118,6 +118,10 @@ fn can_build_site_without_live_reload() {
assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
// 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
assert_eq!(file_exists!(public, "categories/index.html"), false);
assert_eq!(file_exists!(public, "tags/index.html"), false);


Loading…
Cancel
Save