Skip to content

Commit

Permalink
Merge pull request #203 from badboy/empty-frontmatter
Browse files Browse the repository at this point in the history
fix(frontmatter): Support empty frontmatters
  • Loading branch information
epage committed Apr 24, 2017
2 parents 5c41f85 + 0072eab commit 5aa5813
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/document.rs
Expand Up @@ -177,17 +177,20 @@ impl Document {

let yaml_result = try!(YamlLoader::load_from_str(attribute_split));

let yaml_attributes = try!(yaml_result[0]
.as_hash()
.ok_or_else(|| format!("Incorrect front matter format in {:?}", file_path)));

for (key, value) in yaml_attributes {
if let Some(v) = yaml_to_liquid(value) {
let key = key.as_str()
.ok_or_else(|| format!("Invalid key {:?}", key))?
.to_owned();
attributes.insert(key, v);
if !yaml_result.is_empty() {
let yaml_attributes = try!(yaml_result[0]
.as_hash()
.ok_or_else(|| format!("Incorrect front matter format in {:?}", file_path)));

for (key, value) in yaml_attributes {
if let Some(v) = yaml_to_liquid(value) {
let key = key.as_str()
.ok_or_else(|| format!("Invalid key {:?}", key))?
.to_owned();
attributes.insert(key, v);
}
}

}

content_split
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/empty_frontmatter/_layouts/default.liquid
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>{{ path }}</h1>

{{ content }}
</body>
</html>

10 changes: 10 additions & 0 deletions tests/fixtures/empty_frontmatter/_layouts/posts.liquid
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>My blog - {{ title }}</title>
</head>
<body>
{{ content }}
</body>
</html>

7 changes: 7 additions & 0 deletions tests/fixtures/empty_frontmatter/index.liquid
@@ -0,0 +1,7 @@
extends: default.liquid
---
This is my Index page!

{% for post in posts %}
<a href="{{post.path}}">{{ post.title }}</a>
{% endfor %}
6 changes: 6 additions & 0 deletions tests/fixtures/empty_frontmatter/posts/my-first-blogpost.md
@@ -0,0 +1,6 @@
---
# {{ title }}

Hey there this is my first blogpost and this is super awesome.

My Blog is lorem ipsum like, yes it is..
5 changes: 5 additions & 0 deletions tests/mod.rs
Expand Up @@ -201,3 +201,8 @@ pub fn excerpts() {
pub fn posts_in_subfolder() {
run_test("posts_in_subfolder").unwrap();
}

#[test]
pub fn empty_frontmatter() {
run_test("empty_frontmatter").expect("Build error");
}
17 changes: 17 additions & 0 deletions tests/target/empty_frontmatter/index.html
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>index.html</h1>

This is my Index page!


<a href="posts/my-first-blogpost.html">My First Blogpost</a>


</body>
</html>

3 changes: 3 additions & 0 deletions tests/target/empty_frontmatter/posts/my-first-blogpost.html
@@ -0,0 +1,3 @@
<h1>My First Blogpost</h1>
<p>Hey there this is my first blogpost and this is super awesome.</p>
<p>My Blog is lorem ipsum like, yes it is..</p>

0 comments on commit 5aa5813

Please sign in to comment.