Skip to content

Commit 8713cd7

Browse files
authored
Merge pull request #39 from DFE-Digital/add-front-matter-stripping-option
Add front matter stripping option to renderer
2 parents 2b217bd + f25d9d3 commit 8713cd7

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

lib/govuk_markdown.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
module GovukMarkdown
99
def self.render(markdown, govuk_options = {})
10-
renderer = GovukMarkdown::Renderer.new(govuk_options, { with_toc_data: true, link_attributes: { class: "govuk-link" } })
10+
renderer = GovukMarkdown::Renderer.new(govuk_options, { with_toc_data: true, strip_front_matter: true, link_attributes: { class: "govuk-link" } })
1111
Redcarpet::Markdown.new(renderer, tables: true, no_intra_emphasis: true).render(markdown).strip
1212
end
1313
end

lib/govuk_markdown/preprocessor.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ def inject_details
3737
self
3838
end
3939

40+
def strip_front_matter(enabled)
41+
return self unless enabled
42+
43+
@output = output.gsub(%r{^---\n.*\n---}m, "")
44+
45+
self
46+
end
47+
4048
private
4149

4250
# parse as markdown if there are multiple lines of content

lib/govuk_markdown/renderer.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module GovukMarkdown
22
class Renderer < ::Redcarpet::Render::HTML
33
def initialize(govuk_options, options = {})
44
@headings_start_with = govuk_options[:headings_start_with]
5+
@strip_front_matter = govuk_options[:strip_front_matter]
6+
57
super options
68
end
79

@@ -93,6 +95,7 @@ def preprocess(document)
9395
.new(document)
9496
.inject_inset_text
9597
.inject_details
98+
.strip_front_matter(@strip_front_matter)
9699
.output
97100
end
98101
end

spec/preprocessor_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,63 @@
320320
end
321321
end
322322

323+
describe "strip front matter" do
324+
let(:input) do
325+
<<~MD
326+
---
327+
title: Hi
328+
tags: hello, world
329+
---
330+
331+
Waffle waffle waffle waffle.
332+
333+
Waffle waffle waffle waffle.
334+
MD
335+
end
336+
337+
let(:actual_output) { render(input, { strip_front_matter: }) }
338+
339+
context "when front matter is stripped" do
340+
let(:strip_front_matter) { true }
341+
342+
let(:expected_output) do
343+
<<~EXPECTED.strip
344+
<p class="govuk-body-m">Waffle waffle waffle waffle.</p>
345+
<p class="govuk-body-m">Waffle waffle waffle waffle.</p>
346+
EXPECTED
347+
end
348+
349+
it "renders no front matter" do
350+
expect(actual_output).not_to include("title")
351+
end
352+
353+
it "renders without front matter" do
354+
expect(actual_output).to eql(expected_output)
355+
end
356+
end
357+
358+
context "when front matter is not stripped" do
359+
let(:strip_front_matter) { false }
360+
361+
let(:expected_output) do
362+
<<~EXPECTED.strip
363+
<p class="govuk-body-m">Waffle waffle waffle waffle.</p>
364+
<p class="govuk-body-m">Waffle waffle waffle waffle.</p>
365+
EXPECTED
366+
end
367+
368+
it "renders the front matter" do
369+
expect(actual_output).to include(%(<hr class="govuk-section-break govuk-section-break--xl govuk-section-break--visible">))
370+
expect(actual_output).to include(%(<p class="govuk-body-m">title: Hi</p>))
371+
expect(actual_output).to include(%(<h2 id="tags-hello-world" class="govuk-heading-l">tags: hello, world</h2>))
372+
end
373+
374+
it "renders the content too" do
375+
expect(actual_output).to include(expected_output)
376+
end
377+
end
378+
end
379+
323380
describe "multiple preprocessing steps" do
324381
let(:expected_output) do
325382
<<~HTML

spec/spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require_relative "./../lib/govuk_markdown"
22
require "pry"
33

4-
def render(content)
5-
GovukMarkdown.render(content)
4+
def render(content, govuk_options = {})
5+
GovukMarkdown.render(content, govuk_options)
66
end
77

88
def expect_equal_ignoring_ws(first, second)

0 commit comments

Comments
 (0)