diff --git a/lib/govuk_markdown.rb b/lib/govuk_markdown.rb index 0bc58c2..8c4cdaf 100644 --- a/lib/govuk_markdown.rb +++ b/lib/govuk_markdown.rb @@ -7,7 +7,7 @@ module GovukMarkdown def self.render(markdown, govuk_options = {}) - renderer = GovukMarkdown::Renderer.new(govuk_options, { with_toc_data: true, link_attributes: { class: "govuk-link" } }) + renderer = GovukMarkdown::Renderer.new(govuk_options, { with_toc_data: true, strip_front_matter: true, link_attributes: { class: "govuk-link" } }) Redcarpet::Markdown.new(renderer, tables: true, no_intra_emphasis: true).render(markdown).strip end end diff --git a/lib/govuk_markdown/preprocessor.rb b/lib/govuk_markdown/preprocessor.rb index a909bc8..8983024 100644 --- a/lib/govuk_markdown/preprocessor.rb +++ b/lib/govuk_markdown/preprocessor.rb @@ -37,6 +37,14 @@ def inject_details self end + def strip_front_matter(enabled) + return self unless enabled + + @output = output.gsub(%r{^---\n.*\n---}m, "") + + self + end + private # parse as markdown if there are multiple lines of content diff --git a/lib/govuk_markdown/renderer.rb b/lib/govuk_markdown/renderer.rb index 223df9e..07b2d0e 100644 --- a/lib/govuk_markdown/renderer.rb +++ b/lib/govuk_markdown/renderer.rb @@ -2,6 +2,8 @@ module GovukMarkdown class Renderer < ::Redcarpet::Render::HTML def initialize(govuk_options, options = {}) @headings_start_with = govuk_options[:headings_start_with] + @strip_front_matter = govuk_options[:strip_front_matter] + super options end @@ -93,6 +95,7 @@ def preprocess(document) .new(document) .inject_inset_text .inject_details + .strip_front_matter(@strip_front_matter) .output end end diff --git a/spec/preprocessor_spec.rb b/spec/preprocessor_spec.rb index e6f83a2..c466dd5 100644 --- a/spec/preprocessor_spec.rb +++ b/spec/preprocessor_spec.rb @@ -320,6 +320,63 @@ end end + describe "strip front matter" do + let(:input) do + <<~MD + --- + title: Hi + tags: hello, world + --- + + Waffle waffle waffle waffle. + + Waffle waffle waffle waffle. + MD + end + + let(:actual_output) { render(input, { strip_front_matter: }) } + + context "when front matter is stripped" do + let(:strip_front_matter) { true } + + let(:expected_output) do + <<~EXPECTED.strip +

Waffle waffle waffle waffle.

+

Waffle waffle waffle waffle.

+ EXPECTED + end + + it "renders no front matter" do + expect(actual_output).not_to include("title") + end + + it "renders without front matter" do + expect(actual_output).to eql(expected_output) + end + end + + context "when front matter is not stripped" do + let(:strip_front_matter) { false } + + let(:expected_output) do + <<~EXPECTED.strip +

Waffle waffle waffle waffle.

+

Waffle waffle waffle waffle.

+ EXPECTED + end + + it "renders the front matter" do + expect(actual_output).to include(%(
)) + expect(actual_output).to include(%(

title: Hi

)) + expect(actual_output).to include(%(

tags: hello, world

)) + end + + it "renders the content too" do + expect(actual_output).to include(expected_output) + end + end + end + describe "multiple preprocessing steps" do let(:expected_output) do <<~HTML diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d66825a..0b93337 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,8 @@ require_relative "./../lib/govuk_markdown" require "pry" -def render(content) - GovukMarkdown.render(content) +def render(content, govuk_options = {}) + GovukMarkdown.render(content, govuk_options) end def expect_equal_ignoring_ws(first, second)