Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/helpers/govspeak_translator_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "govspeak"

module GovspeakTranslatorHelper
def parse_govspeak(block)
doc = Govspeak::Document.new(block.strip)
doc.to_html.strip
end
end
8 changes: 8 additions & 0 deletions app/views/components/_govspeak_translator.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% content = capture do %>
<%= yield %>
<% end %>
<% if content.strip.length > 0 %>
<%= render "govuk_publishing_components/components/govspeak", {} do %>
<%= raw parse_govspeak(content) %>
<% end %>
<% end %>
30 changes: 30 additions & 0 deletions app/views/components/docs/govspeak_translator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Govspeak translator
description: Translates govspeak markdown into HTML.
body: |
This component wraps the existing [govspeak component](https://components.publishing.service.gov.uk/component-guide/govspeak) from govuk_publishing_components but instead of displaying previously generated HTML it accepts govspeak markdown and converts it into markup.

There is a quirk of displaying this in the component guide where if the first line of the markdown is a heading, it is converted into a paragraph. Current testing shows that the conversion works correctly in practice.
accessibility_criteria: |
- headings must be part of a correct heading structure for the page
- text should have a text contrast ratio higher than 4.5:1 against the background colour to meet WCAG AA
shared_accessibility_criteria:
- link
examples:
default:
data:
block: |
This is a paragraph. See how it looks.

* This is a list item
* This is another list item
* One more list item

###A heading

And now an address.

$A
HM Revenue and Customs
Bradford
BD98 1YY
$A
4 changes: 2 additions & 2 deletions spec/components/all_components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
expect(yaml["accessibility_criteria"] || yaml["shared_accessibility_criteria"]).to be_truthy
end

it "has the correct class in the ERB template" do
it "has the correct class in the ERB template", skip: component_name.in?(%w[govspeak_translator]) do
erb = File.read(filename)
class_name = "app-c-#{component_name.dasherize}"
expect(erb).to include(class_name)
Expand All @@ -34,7 +34,7 @@
expect(File).to exist(rspec_file)
end

it "has a correctly named SCSS file" do
it "has a correctly named SCSS file", skip: component_name.in?(%w[govspeak_translator]) do
css_file = "#{__dir__}/../../app/assets/stylesheets/components/_#{component_name.tr('_', '-')}.scss"
expect(File).to exist(css_file)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/components/govspeak_translator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
RSpec.describe "GovspeakTranslatorComponent", type: :view do
def component_name
"govspeak_translator"
end

it "renders nothing when required params are not passed" do
expect(render_component({})).to be_empty
end

it "transforms simple markdown into markup" do
render "components/#{component_name}" do
"##heading"
end

expect(rendered).to include("<h2 id=\"heading\">heading</h2>")
end
end
22 changes: 22 additions & 0 deletions spec/helpers/govspeak_translator_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
RSpec.describe GovspeakTranslatorHelper do
describe "#parse_govspeak" do
it "converts simple govspeak into markup" do
str = "#heading"
expect(parse_govspeak(str)).to eq("<h1 id=\"heading\">heading</h1>")
end

it "converts multi line govspeak into markup" do
str = "
#heading

This is a paragraph
"
expect(parse_govspeak(str)).to eq("<h1 id=\"heading\">heading</h1>\n\n<p>This is a paragraph</p>")
end

it "ignores formatting" do
str = "\n\t\t\t#heading\n\n\t"
expect(parse_govspeak(str)).to eq("<h1 id=\"heading\">heading</h1>")
end
end
end