Skip to content

Commit 325b39f

Browse files
committed
Add govspeak translator component
- add basic component, documentation and tests - we do not demonstrate all possible govspeak options as this is handled by the main component already - update all components spec to ignore new component, as no stylesheet
1 parent 6932261 commit 325b39f

6 files changed

Lines changed: 87 additions & 2 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "govspeak"
2+
3+
module GovspeakTranslatorHelper
4+
def parse_govspeak(block)
5+
doc = Govspeak::Document.new(block.strip)
6+
doc.to_html.strip
7+
end
8+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<% content = capture do %>
2+
<%= yield %>
3+
<% end %>
4+
<% if content.strip.length > 0 %>
5+
<%= render "govuk_publishing_components/components/govspeak", {} do %>
6+
<%= raw parse_govspeak(content) %>
7+
<% end %>
8+
<% end %>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Govspeak translator
2+
description: Translates govspeak markdown into HTML.
3+
body: |
4+
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.
5+
6+
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.
7+
accessibility_criteria: |
8+
- headings must be part of a correct heading structure for the page
9+
- text should have a text contrast ratio higher than 4.5:1 against the background colour to meet WCAG AA
10+
shared_accessibility_criteria:
11+
- link
12+
examples:
13+
default:
14+
data:
15+
block: |
16+
This is a paragraph. See how it looks.
17+
18+
* This is a list item
19+
* This is another list item
20+
* One more list item
21+
22+
###A heading
23+
24+
And now an address.
25+
26+
$A
27+
HM Revenue and Customs
28+
Bradford
29+
BD98 1YY
30+
$A

spec/components/all_components_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
expect(yaml["accessibility_criteria"] || yaml["shared_accessibility_criteria"]).to be_truthy
1919
end
2020

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

37-
it "has a correctly named SCSS file" do
37+
it "has a correctly named SCSS file", skip: component_name.in?(%w[govspeak_translator]) do
3838
css_file = "#{__dir__}/../../app/assets/stylesheets/components/_#{component_name.tr('_', '-')}.scss"
3939
expect(File).to exist(css_file)
4040
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
RSpec.describe "GovspeakTranslatorComponent", type: :view do
2+
def component_name
3+
"govspeak_translator"
4+
end
5+
6+
it "renders nothing when required params are not passed" do
7+
expect(render_component({})).to be_empty
8+
end
9+
10+
it "transforms simple markdown into markup" do
11+
render "components/#{component_name}" do
12+
"##heading"
13+
end
14+
15+
expect(rendered).to include("<h2 id=\"heading\">heading</h2>")
16+
end
17+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
RSpec.describe GovspeakTranslatorHelper do
2+
describe "#parse_govspeak" do
3+
it "converts simple govspeak into markup" do
4+
str = "#heading"
5+
expect(parse_govspeak(str)).to eq("<h1 id=\"heading\">heading</h1>")
6+
end
7+
8+
it "converts multi line govspeak into markup" do
9+
str = "
10+
#heading
11+
12+
This is a paragraph
13+
"
14+
expect(parse_govspeak(str)).to eq("<h1 id=\"heading\">heading</h1>\n\n<p>This is a paragraph</p>")
15+
end
16+
17+
it "ignores formatting" do
18+
str = "\n\t\t\t#heading\n\n\t"
19+
expect(parse_govspeak(str)).to eq("<h1 id=\"heading\">heading</h1>")
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)