Fix bug with colocated folders

This commit is contained in:
Vincent Prouillet 2017-10-04 09:23:25 +09:00
parent c348648b02
commit a24851790c
3 changed files with 45 additions and 7 deletions

View file

@ -20,6 +20,7 @@
- Remove deprecated `link` param of `get_url`
- Add 1337 color scheme
- Defaults to compressed Sass output
- Fix regression wrt co-located assets slug detecting
## 0.1.3 (2017-08-31)

View file

@ -86,7 +86,15 @@ impl Page {
if let Some(ref slug) = page.meta.slug {
slug.trim().to_string()
} else {
slugify(page.file.name.clone())
if page.file.name == "index" {
if let Some(parent) = page.file.path.parent() {
slugify(parent.file_name().unwrap().to_str().unwrap())
} else {
slugify(page.file.name.clone())
}
} else {
slugify(page.file.name.clone())
}
}
};
@ -210,6 +218,7 @@ impl ser::Serialize for Page {
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::io::Write;
use std::fs::{File, create_dir};
use std::path::Path;
@ -311,25 +320,54 @@ Hello world
}
#[test]
fn page_with_assets_gets_right_parent_path() {
fn page_with_assets_gets_right_info() {
let tmp_dir = TempDir::new("example").expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("assets");
let nested_path = path.join("content").join("posts").join("with-assets");
create_dir(&nested_path).expect("create nested temp dir");
File::create(nested_path.join("index.md")).unwrap();
let mut f = File::create(nested_path.join("index.md")).unwrap();
f.write_all(b"+++\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let res = Page::parse(
let res = Page::from_file(
nested_path.join("index.md").as_path(),
"+++\nurl=\"hey\"+++\n",
&Config::default()
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.file.parent, path.join("content").join("posts"));
assert_eq!(page.slug, "with-assets");
assert_eq!(page.assets.len(), 3);
assert_eq!(page.permalink, "http://a-website.com/posts/with-assets/");
}
#[test]
fn page_with_assets_and_slug_overrides_path() {
let tmp_dir = TempDir::new("example").expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("with-assets");
create_dir(&nested_path).expect("create nested temp dir");
let mut f = File::create(nested_path.join("index.md")).unwrap();
f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
let res = Page::from_file(
nested_path.join("index.md").as_path(),
&Config::default()
);
assert!(res.is_ok());
let page = res.unwrap();
assert_eq!(page.file.parent, path.join("content").join("posts"));
assert_eq!(page.slug, "hey");
assert_eq!(page.assets.len(), 3);
assert_eq!(page.permalink, "http://a-website.com/posts/hey/");
}
}

View file

@ -1,7 +1,6 @@
+++
title = "With assets"
description = "hey there"
slug = "with-assets"
+++
Hello world