diff --git a/CHANGELOG.md b/CHANGELOG.md index da0eb2e..caa320d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Add 1337 color scheme - Defaults to compressed Sass output - Fix regression wrt co-located assets slug detecting +- Rename `url` from page front-matter to `path` to be consistent ## 0.1.3 (2017-08-31) diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 0775eb6..4c12ccc 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -98,8 +98,9 @@ impl Page { } }; - if let Some(ref u) = page.meta.url { - page.path = u.trim().to_string(); + if let Some(ref p) = page.meta.path { + page.path = p.trim().trim_left_matches('/').to_string(); + } else { page.path = if page.file.components.is_empty() { page.slug.clone() @@ -110,6 +111,7 @@ impl Page { if !page.path.ends_with('/') { page.path = format!("{}/", page.path); } + page.permalink = config.make_permalink(&page.path); Ok(page) @@ -281,6 +283,36 @@ Hello world"#; assert_eq!(page.permalink, config.make_permalink("hello-world")); } + #[test] + fn can_make_url_from_path() { + let content = r#" + +++ + path = "hello-world" + +++ + Hello world"#; + let config = Config::default(); + let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config); + assert!(res.is_ok()); + let page = res.unwrap(); + assert_eq!(page.path, "hello-world/"); + assert_eq!(page.permalink, config.make_permalink("hello-world")); + } + + #[test] + fn can_make_url_from_path_starting_slash() { + let content = r#" + +++ + path = "/hello-world" + +++ + Hello world"#; + let config = Config::default(); + let res = Page::parse(Path::new("content/posts/intro/start.md"), content, &config); + assert!(res.is_ok()); + let page = res.unwrap(); + assert_eq!(page.path, "hello-world/"); + assert_eq!(page.permalink, config.make_permalink("hello-world")); + } + #[test] fn errors_on_invalid_front_matter_format() { // missing starting +++ diff --git a/components/front_matter/src/page.rs b/components/front_matter/src/page.rs index 1576531..6f20b58 100644 --- a/components/front_matter/src/page.rs +++ b/components/front_matter/src/page.rs @@ -20,10 +20,10 @@ pub struct PageFrontMatter { /// The page slug. Will be used instead of the filename if present /// Can't be an empty string if present pub slug: Option, - /// The url the page appears at, overrides the slug if set in the front-matter + /// The path the page appears at, overrides the slug if set in the front-matter /// otherwise is set after parsing front matter and sections /// Can't be an empty string if present - pub url: Option, + pub path: Option, /// Tags, not to be confused with categories pub tags: Option>, /// Only one category allowed. Can't be an empty string if present @@ -56,9 +56,9 @@ impl PageFrontMatter { } } - if let Some(ref url) = f.url { - if url == "" { - bail!("`url` can't be empty if present") + if let Some(ref path) = f.path { + if path == "" { + bail!("`path` can't be empty if present") } } @@ -109,7 +109,7 @@ impl Default for PageFrontMatter { date: None, draft: None, slug: None, - url: None, + path: None, tags: None, category: None, order: None, @@ -189,11 +189,11 @@ mod tests { } #[test] - fn errors_on_present_but_empty_url() { + fn errors_on_present_but_empty_path() { let content = r#" title = "Hello" description = "hey there" - url = """#; + path = """#; let res = PageFrontMatter::parse(content); assert!(res.is_err()); } diff --git a/components/site/test_site/content/posts/fixed-url.md b/components/site/test_site/content/posts/fixed-url.md index aed1d03..0718d52 100644 --- a/components/site/test_site/content/posts/fixed-url.md +++ b/components/site/test_site/content/posts/fixed-url.md @@ -1,7 +1,7 @@ +++ title = "Fixed URL" description = "" -url = "a-fixed-url" +path = "a-fixed-url" date = "2017-02-01" +++ diff --git a/docs/content/documentation/content/page.md b/docs/content/documentation/content/page.md index 177087b..ff26125 100644 --- a/docs/content/documentation/content/page.md +++ b/docs/content/documentation/content/page.md @@ -32,10 +32,11 @@ draft = false # It will still use the section path though slug = "" -# The URL the content will appear at -# If set, it cannot be an empty string and will override both `slug` and the filename -# and the sections' path won't be used -url = "" +# The path the content will appear at +# If set, it cannot be an empty string and will override both `slug` and the filename. +# The sections' path won't be used. +# It should not start with a `/` and the slash will be removed if it does +path = "" # An array of strings allowing you to group pages with them tags = []