JSON frontmatter and allowing frontmatter to be located after a encoding value
This commit is contained in:
parent
f17b19d9de
commit
162622c453
7 changed files with 77 additions and 7 deletions
|
@ -1,8 +1,11 @@
|
||||||
Feature: YAML Front Matter
|
Feature: YAML Front Matter
|
||||||
In order to specific options and data inline
|
In order to specific options and data inline
|
||||||
|
|
||||||
Scenario: Rendering html
|
Scenario: Rendering html (yaml)
|
||||||
Given the Server is running at "frontmatter-app"
|
Given the Server is running at "frontmatter-app"
|
||||||
|
When I go to "/front-matter-auto.html"
|
||||||
|
Then I should see "<h1>This is the title</h1>"
|
||||||
|
Then I should not see "---"
|
||||||
When I go to "/front-matter.html"
|
When I go to "/front-matter.html"
|
||||||
Then I should see "<h1>This is the title</h1>"
|
Then I should see "<h1>This is the title</h1>"
|
||||||
Then I should not see "---"
|
Then I should not see "---"
|
||||||
|
@ -11,6 +14,19 @@ Feature: YAML Front Matter
|
||||||
Then I should see "<?php"
|
Then I should see "<?php"
|
||||||
Then I should not see "---"
|
Then I should not see "---"
|
||||||
|
|
||||||
|
Scenario: Rendering html (json)
|
||||||
|
Given the Server is running at "frontmatter-app"
|
||||||
|
When I go to "/json-front-matter-auto.html"
|
||||||
|
Then I should see "<h1>This is the title</h1>"
|
||||||
|
Then I should not see "{"
|
||||||
|
When I go to "/json-front-matter.html"
|
||||||
|
Then I should see "<h1>This is the title</h1>"
|
||||||
|
Then I should not see "{"
|
||||||
|
When I go to "/json-front-matter-2.php"
|
||||||
|
Then I should see "<h1>This is the title</h1>"
|
||||||
|
Then I should see "<?php"
|
||||||
|
Then I should not see "{"
|
||||||
|
|
||||||
Scenario: A template changes frontmatter during preview
|
Scenario: A template changes frontmatter during preview
|
||||||
Given the Server is running at "frontmatter-app"
|
Given the Server is running at "frontmatter-app"
|
||||||
And the file "source/front-matter-change.html.erb" has the contents
|
And the file "source/front-matter-change.html.erb" has the contents
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
layout: false
|
||||||
|
title: This is the title
|
||||||
|
---
|
||||||
|
|
||||||
|
<h1><%= data.page.title %></h1>
|
|
@ -1,3 +1,5 @@
|
||||||
|
<h2> Test</h2>
|
||||||
|
|
||||||
---
|
---
|
||||||
layout: false
|
layout: false
|
||||||
title: This is the title
|
title: This is the title
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
{{{
|
||||||
|
"layout": false,
|
||||||
|
"title": "This is the title"
|
||||||
|
}}}
|
||||||
|
|
||||||
|
<h1><%= data.page.title %></h1>
|
||||||
|
<?php echo "sup"; ?>
|
|
@ -0,0 +1,6 @@
|
||||||
|
{{{
|
||||||
|
"layout": false,
|
||||||
|
"title": "This is the title"
|
||||||
|
}}}
|
||||||
|
|
||||||
|
<h1><%= data.page.title %></h1>
|
|
@ -0,0 +1,6 @@
|
||||||
|
{{{
|
||||||
|
"layout": false,
|
||||||
|
"title": "This is the title"
|
||||||
|
}}}
|
||||||
|
|
||||||
|
<h1><%= data.page.title %></h1>
|
|
@ -12,6 +12,9 @@ module Middleman::CoreExtensions
|
||||||
# Parsing YAML frontmatter
|
# Parsing YAML frontmatter
|
||||||
require "yaml"
|
require "yaml"
|
||||||
|
|
||||||
|
# Parsing JSON frontmatter
|
||||||
|
require "active_support/json"
|
||||||
|
|
||||||
app.after_configuration do
|
app.after_configuration do
|
||||||
::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
|
::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
|
||||||
|
|
||||||
|
@ -56,13 +59,13 @@ module Middleman::CoreExtensions
|
||||||
@cache.delete(p)
|
@cache.delete(p)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse frontmatter out of a string
|
# Parse YAML frontmatter out of a string
|
||||||
# @param [String] content
|
# @param [String] content
|
||||||
# @return [Array<Hash, String>]
|
# @return [Array<Hash, String>]
|
||||||
def parse_front_matter(content)
|
def parse_yaml_front_matter(content)
|
||||||
yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
||||||
if content =~ yaml_regex
|
if content =~ yaml_regex
|
||||||
content = content[($1.size + $2.size)..-1]
|
content = content.sub(yaml_regex, "")
|
||||||
|
|
||||||
begin
|
begin
|
||||||
data = YAML.load($1)
|
data = YAML.load($1)
|
||||||
|
@ -80,6 +83,29 @@ module Middleman::CoreExtensions
|
||||||
[{}, content]
|
[{}, content]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parse_json_front_matter(content)
|
||||||
|
json_regex = /^(\{\{\{\s*\n.*?\n?)^(\}\}\}\s*$\n?)/m
|
||||||
|
|
||||||
|
if content =~ json_regex
|
||||||
|
content = content.sub(json_regex, "")
|
||||||
|
|
||||||
|
begin
|
||||||
|
json = ($1+$2).sub("{{{", "{").sub("}}}", "}")
|
||||||
|
data = ActiveSupport::JSON.decode(json)
|
||||||
|
rescue => e
|
||||||
|
puts "JSON Exception: #{e.message}"
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
[data, content]
|
||||||
|
rescue
|
||||||
|
[{}, content]
|
||||||
|
end
|
||||||
|
|
||||||
# Get the frontmatter and plain content from a file
|
# Get the frontmatter and plain content from a file
|
||||||
# @param [String] path
|
# @param [String] path
|
||||||
# @return [Array<Thor::CoreExt::HashWithIndifferentAccess, String>]
|
# @return [Array<Thor::CoreExt::HashWithIndifferentAccess, String>]
|
||||||
|
@ -87,9 +113,10 @@ module Middleman::CoreExtensions
|
||||||
full_path = File.expand_path(path, @app.source_dir)
|
full_path = File.expand_path(path, @app.source_dir)
|
||||||
content = File.read(full_path)
|
content = File.read(full_path)
|
||||||
|
|
||||||
result = parse_front_matter(content)
|
if result = parse_yaml_front_matter(content)
|
||||||
|
data, content = result
|
||||||
if result
|
data = ::Middleman::Util.recursively_enhance(data).freeze
|
||||||
|
elsif result = parse_json_front_matter(content)
|
||||||
data, content = result
|
data, content = result
|
||||||
data = ::Middleman::Util.recursively_enhance(data).freeze
|
data = ::Middleman::Util.recursively_enhance(data).freeze
|
||||||
else
|
else
|
||||||
|
|
Loading…
Add table
Reference in a new issue