summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2016-02-21 19:22:02 -0500
committerRobert Speicher <rspeicher@gmail.com>2016-03-02 22:44:02 -0500
commit37c0b5cb32bcfdfdb2513528948fbafb036aadc7 (patch)
tree5fe41045888b37467835891adc5d74a2521cff78
parent74751791a8bf27c5576832f73a57a5b110b423ad (diff)
downloadgitlab-ce-rs-frontmatter-pre.tar.gz
Add YamlFrontMatterFilter to the PreProcessPipeliners-frontmatter-pre
This filter will detect YAML Front Matter and convert it to an HTML table for prettier formatting.
-rw-r--r--lib/banzai/filter/yaml_front_matter_filter.rb28
-rw-r--r--lib/banzai/pipeline/pre_process_pipeline.rb1
-rw-r--r--spec/lib/banzai/filter/yaml_front_matter_filter_spec.rb53
3 files changed, 82 insertions, 0 deletions
diff --git a/lib/banzai/filter/yaml_front_matter_filter.rb b/lib/banzai/filter/yaml_front_matter_filter.rb
new file mode 100644
index 00000000000..71da880b5b2
--- /dev/null
+++ b/lib/banzai/filter/yaml_front_matter_filter.rb
@@ -0,0 +1,28 @@
+require 'html/pipeline/filter'
+require 'yaml'
+
+module Banzai
+ module Filter
+ class YamlFrontMatterFilter < HTML::Pipeline::Filter
+ DELIM = '---'.freeze
+
+ # Hat-tip to Middleman: https://git.io/v2e0z
+ PATTERN = %r{
+ \A(?:[^\r\n]*coding:[^\r\n]*\r?\n)?
+ (?<start>#{DELIM})[ ]*\r?\n
+ (?<frontmatter>.*?)[ ]*\r?\n?
+ ^(?<stop>#{DELIM})[ ]*\r?\n?
+ \r?\n?
+ (?<content>.*)
+ }mx.freeze
+
+ def call
+ match = PATTERN.match(html)
+
+ return html unless match
+
+ "```yaml\n#{match['frontmatter']}\n```\n\n" << match['content']
+ end
+ end
+ end
+end
diff --git a/lib/banzai/pipeline/pre_process_pipeline.rb b/lib/banzai/pipeline/pre_process_pipeline.rb
index c174f0b862d..b3d1e9b661e 100644
--- a/lib/banzai/pipeline/pre_process_pipeline.rb
+++ b/lib/banzai/pipeline/pre_process_pipeline.rb
@@ -3,6 +3,7 @@ module Banzai
class PreProcessPipeline < BasePipeline
def self.filters
[
+ Filter::YamlFrontMatterFilter
]
end
diff --git a/spec/lib/banzai/filter/yaml_front_matter_filter_spec.rb b/spec/lib/banzai/filter/yaml_front_matter_filter_spec.rb
new file mode 100644
index 00000000000..fe70eada7eb
--- /dev/null
+++ b/spec/lib/banzai/filter/yaml_front_matter_filter_spec.rb
@@ -0,0 +1,53 @@
+require 'rails_helper'
+
+describe Banzai::Filter::YamlFrontMatterFilter, lib: true do
+ include FilterSpecHelper
+
+ it 'allows for `encoding:` before the frontmatter' do
+ content = <<-MD.strip_heredoc
+ # encoding: UTF-8
+ ---
+ foo: foo
+ ---
+
+ # Header
+
+ Content
+ MD
+
+ output = filter(content)
+
+ expect(output).not_to match 'encoding'
+ end
+
+ it 'converts YAML frontmatter to a fenced code block' do
+ content = <<-MD.strip_heredoc
+ ---
+ bar: :bar_symbol
+ ---
+
+ # Header
+
+ Content
+ MD
+
+ output = filter(content)
+
+ aggregate_failures do
+ expect(output).not_to include '---'
+ expect(output).to include "```yaml\nbar: :bar_symbol\n```"
+ end
+ end
+
+ context 'on content without frontmatter' do
+ it 'returns the content unmodified' do
+ content = <<-MD.strip_heredoc
+ # This is some Markdown
+
+ It has no YAML frontmatter to parse.
+ MD
+
+ expect(filter(content)).to eq content
+ end
+ end
+end