Slugify given slugs as well

This commit is contained in:
Vincent Prouillet 2019-08-04 16:13:07 +02:00
parent 4b43b75d22
commit 0199017d07
6 changed files with 39 additions and 23 deletions

View File

@ -160,7 +160,7 @@ impl Page {
page.slug = { page.slug = {
if let Some(ref slug) = page.meta.slug { if let Some(ref slug) = page.meta.slug {
slug.trim().to_string() slugify(&slug.trim())
} else if page.file.name == "index" { } else if page.file.name == "index" {
if let Some(parent) = page.file.path.parent() { if let Some(parent) = page.file.path.parent() {
if let Some(slug) = slug_from_dated_filename { if let Some(slug) = slug_from_dated_filename {
@ -437,6 +437,22 @@ Hello world"#;
assert_eq!(page.permalink, config.make_permalink("hello-world")); assert_eq!(page.permalink, config.make_permalink("hello-world"));
} }
#[test]
fn can_make_url_from_slug_only_with_no_special_chars() {
let content = r#"
+++
slug = "hello-&-world"
+++
Hello world"#;
let config = Config::default();
let res = Page::parse(Path::new("start.md"), content, &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.path, "hello-world/");
assert_eq!(page.components, vec!["hello-world"]);
assert_eq!(page.permalink, config.make_permalink("hello-world"));
}
#[test] #[test]
fn can_make_url_from_path() { fn can_make_url_from_path() {
let content = r#" let content = r#"

View File

@ -167,12 +167,12 @@ fn can_build_site_without_live_reload() {
assert!(file_contains!( assert!(file_contains!(
public, public,
"sitemap.xml", "sitemap.xml",
"<loc>https%3A//replace-this-with-your-url.com/posts/simple/</loc>" "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"
)); ));
assert!(file_contains!( assert!(file_contains!(
public, public,
"sitemap.xml", "sitemap.xml",
"<loc>https%3A//replace-this-with-your-url.com/posts/</loc>" "<loc>https://replace-this-with-your-url.com/posts/</loc>"
)); ));
// Drafts are not in the sitemap // Drafts are not in the sitemap
assert!(!file_contains!(public, "sitemap.xml", "draft")); assert!(!file_contains!(public, "sitemap.xml", "draft"));
@ -280,7 +280,7 @@ fn can_build_site_with_taxonomies() {
assert!(file_contains!( assert!(file_contains!(
public, public,
"categories/a/rss.xml", "categories/a/rss.xml",
"https%3A//replace-this-with-your-url.com/categories/a/rss.xml" "https://replace-this-with-your-url.com/categories/a/rss.xml"
)); ));
// Extending from a theme works // Extending from a theme works
assert!(file_contains!(public, "categories/a/index.html", "EXTENDED")); assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
@ -291,12 +291,12 @@ fn can_build_site_with_taxonomies() {
assert!(file_contains!( assert!(file_contains!(
public, public,
"sitemap.xml", "sitemap.xml",
"<loc>https%3A//replace-this-with-your-url.com/categories/</loc>" "<loc>https://replace-this-with-your-url.com/categories/</loc>"
)); ));
assert!(file_contains!( assert!(file_contains!(
public, public,
"sitemap.xml", "sitemap.xml",
"<loc>https%3A//replace-this-with-your-url.com/categories/a/</loc>" "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"
)); ));
} }
@ -425,7 +425,7 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!( assert!(file_contains!(
public, public,
"sitemap.xml", "sitemap.xml",
"<loc>https%3A//replace-this-with-your-url.com/posts/page/4/</loc>" "<loc>https://replace-this-with-your-url.com/posts/page/4/</loc>"
)); ));
} }
@ -478,7 +478,7 @@ fn can_build_site_with_pagination_for_index() {
assert!(file_contains!( assert!(file_contains!(
public, public,
"sitemap.xml", "sitemap.xml",
"<loc>https%3A//replace-this-with-your-url.com/page/1/</loc>" "<loc>https://replace-this-with-your-url.com/page/1/</loc>"
)) ))
} }
@ -559,7 +559,7 @@ fn can_build_site_with_pagination_for_taxonomy() {
assert!(file_contains!( assert!(file_contains!(
public, public,
"sitemap.xml", "sitemap.xml",
"<loc>https%3A//replace-this-with-your-url.com/tags/a/page/6/</loc>" "<loc>https://replace-this-with-your-url.com/tags/a/page/6/</loc>"
)) ))
} }
@ -643,7 +643,7 @@ fn can_apply_page_templates() {
assert_eq!(child.meta.title, Some("Local section override".into())); assert_eq!(child.meta.title, Some("Local section override".into()));
} }
// https%3A//github.com/getzola/zola/issues/571 // https://github.com/getzola/zola/issues/571
#[test] #[test]
fn can_build_site_custom_builtins_from_theme() { fn can_build_site_custom_builtins_from_theme() {
let (_, _tmp_dir, public) = build_site("test_site"); let (_, _tmp_dir, public) = build_site("test_site");

View File

@ -112,17 +112,17 @@ fn can_build_multilingual_site() {
// sitemap contains all languages // sitemap contains all languages
assert!(file_exists!(public, "sitemap.xml")); assert!(file_exists!(public, "sitemap.xml"));
assert!(file_contains!(public, "sitemap.xml", "https%3A//example.com/blog/something-else/")); assert!(file_contains!(public, "sitemap.xml", "https://example.com/blog/something-else/"));
assert!(file_contains!(public, "sitemap.xml", "https%3A//example.com/fr/blog/something-else/")); assert!(file_contains!(public, "sitemap.xml", "https://example.com/fr/blog/something-else/"));
assert!(file_contains!(public, "sitemap.xml", "https%3A//example.com/it/blog/something-else/")); assert!(file_contains!(public, "sitemap.xml", "https://example.com/it/blog/something-else/"));
// one rss per language // one rss per language
assert!(file_exists!(public, "rss.xml")); assert!(file_exists!(public, "rss.xml"));
assert!(file_contains!(public, "rss.xml", "https%3A//example.com/blog/something-else/")); assert!(file_contains!(public, "rss.xml", "https://example.com/blog/something-else/"));
assert!(!file_contains!(public, "rss.xml", "https%3A//example.com/fr/blog/something-else/")); assert!(!file_contains!(public, "rss.xml", "https://example.com/fr/blog/something-else/"));
assert!(file_exists!(public, "fr/rss.xml")); assert!(file_exists!(public, "fr/rss.xml"));
assert!(!file_contains!(public, "fr/rss.xml", "https%3A//example.com/blog/something-else/")); assert!(!file_contains!(public, "fr/rss.xml", "https://example.com/blog/something-else/"));
assert!(file_contains!(public, "fr/rss.xml", "https%3A//example.com/fr/blog/something-else/")); assert!(file_contains!(public, "fr/rss.xml", "https://example.com/fr/blog/something-else/"));
// Italian doesn't have RSS enabled // Italian doesn't have RSS enabled
assert!(!file_exists!(public, "it/rss.xml")); assert!(!file_exists!(public, "it/rss.xml"));

View File

@ -2,18 +2,18 @@
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"> <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel> <channel>
<title>{{ config.title }}</title> <title>{{ config.title }}</title>
<link>{{ config.base_url | urlencode | safe }}</link> <link>{{ config.base_url | safe }}</link>
<description>{{ config.description }}</description> <description>{{ config.description }}</description>
<generator>Zola</generator> <generator>Zola</generator>
<language>{{ config.default_language }}</language> <language>{{ config.default_language }}</language>
<atom:link href="{{ feed_url | safe | urlencode | safe }}" rel="self" type="application/rss+xml"/> <atom:link href="{{ feed_url | safe }}" rel="self" type="application/rss+xml"/>
<lastBuildDate>{{ last_build_date | date(format="%a, %d %b %Y %H:%M:%S %z") }}</lastBuildDate> <lastBuildDate>{{ last_build_date | date(format="%a, %d %b %Y %H:%M:%S %z") }}</lastBuildDate>
{% for page in pages %} {% for page in pages %}
<item> <item>
<title>{{ page.title }}</title> <title>{{ page.title }}</title>
<pubDate>{{ page.date | date(format="%a, %d %b %Y %H:%M:%S %z") }}</pubDate> <pubDate>{{ page.date | date(format="%a, %d %b %Y %H:%M:%S %z") }}</pubDate>
<link>{{ page.permalink | urlencode | safe }}</link> <link>{{ page.permalink | safe }}</link>
<guid>{{ page.permalink | urlencode | safe }}</guid> <guid>{{ page.permalink | safe }}</guid>
<description>{% if page.summary %}{{ page.summary }}{% else %}{{ page.content }}{% endif %}</description> <description>{% if page.summary %}{{ page.summary }}{% else %}{{ page.content }}{% endif %}</description>
</item> </item>
{% endfor %} {% endfor %}

View File

@ -2,7 +2,7 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for sitemap_entry in entries %} {% for sitemap_entry in entries %}
<url> <url>
<loc>{{ sitemap_entry.permalink | urlencode | safe }}</loc> <loc>{{ sitemap_entry.permalink | safe }}</loc>
{% if sitemap_entry.date %} {% if sitemap_entry.date %}
<lastmod>{{ sitemap_entry.date }}</lastmod> <lastmod>{{ sitemap_entry.date }}</lastmod>
{% endif %} {% endif %}

View File

@ -1,7 +1,7 @@
+++ +++
title = "Fixed slug" 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", "/an-old-url/an-old-alias.html"] aliases = ["/an-old-url/old-page", "/an-old-url/an-old-alias.html"]
+++ +++