JSON frontmatter and allowing frontmatter to be located after a encoding value
This commit is contained in:
parent
f17b19d9de
commit
162622c453
|
@ -1,8 +1,11 @@
|
|||
Feature: YAML Front Matter
|
||||
In order to specific options and data inline
|
||||
|
||||
Scenario: Rendering html
|
||||
Scenario: Rendering html (yaml)
|
||||
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"
|
||||
Then I should see "<h1>This is the title</h1>"
|
||||
Then I should not see "---"
|
||||
|
@ -11,6 +14,19 @@ Feature: YAML Front Matter
|
|||
Then I should see "<?php"
|
||||
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
|
||||
Given the Server is running at "frontmatter-app"
|
||||
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
|
||||
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
|
||||
require "yaml"
|
||||
|
||||
# Parsing JSON frontmatter
|
||||
require "active_support/json"
|
||||
|
||||
app.after_configuration do
|
||||
::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods
|
||||
|
||||
|
@ -56,13 +59,13 @@ module Middleman::CoreExtensions
|
|||
@cache.delete(p)
|
||||
end
|
||||
|
||||
# Parse frontmatter out of a string
|
||||
# Parse YAML frontmatter out of a string
|
||||
# @param [String] content
|
||||
# @return [Array<Hash, String>]
|
||||
def parse_front_matter(content)
|
||||
def parse_yaml_front_matter(content)
|
||||
yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
||||
if content =~ yaml_regex
|
||||
content = content[($1.size + $2.size)..-1]
|
||||
content = content.sub(yaml_regex, "")
|
||||
|
||||
begin
|
||||
data = YAML.load($1)
|
||||
|
@ -80,6 +83,29 @@ module Middleman::CoreExtensions
|
|||
[{}, content]
|
||||
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
|
||||
# @param [String] path
|
||||
# @return [Array<Thor::CoreExt::HashWithIndifferentAccess, String>]
|
||||
|
@ -87,9 +113,10 @@ module Middleman::CoreExtensions
|
|||
full_path = File.expand_path(path, @app.source_dir)
|
||||
content = File.read(full_path)
|
||||
|
||||
result = parse_front_matter(content)
|
||||
|
||||
if result
|
||||
if result = parse_yaml_front_matter(content)
|
||||
data, content = result
|
||||
data = ::Middleman::Util.recursively_enhance(data).freeze
|
||||
elsif result = parse_json_front_matter(content)
|
||||
data, content = result
|
||||
data = ::Middleman::Util.recursively_enhance(data).freeze
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue