diff --git a/lib/govspeak.rb b/lib/govspeak.rb index a9e4b203..fdd29b02 100644 --- a/lib/govspeak.rb +++ b/lib/govspeak.rb @@ -136,7 +136,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document) end extension('button', %r{ - ^ # Match start of line only, allows for indenting code examples + (?:\r|\n|^) # non-capturing match to make sure start of line and linebreak {button(.*?)} # match opening bracket and capture attributes \s* # any whitespace between opening bracket and link \[ # match start of link markdown @@ -147,7 +147,7 @@ def insert_strong_inside_p(body, parser=Govspeak::Document) \) # match end of link text markdown \s* # any whitespace between opening bracket and link {\/button} # match ending bracket - $ # Match end of line only, allows for indenting code examples + (?:\r|\n|$) # non-capturing match to make sure end of line and linebreak }x) { |attributes, text, href| button_classes = "button" button_classes << " button-start" if attributes =~ /start/ diff --git a/test/govspeak_button_test.rb b/test/govspeak_button_test.rb index eeca5e24..34807889 100644 --- a/test/govspeak_button_test.rb +++ b/test/govspeak_button_test.rb @@ -79,4 +79,53 @@ class GovspeakTest < Minitest::Test } end + + # Make sure button renders when typical linebreaks are before it, seen in publishing applications + test_given_govspeak "{button}[Line breaks](https://gov.uk/random){/button}\r\n\r\n{button}[Continue](https://gov.uk/random){/button}\r\n\r\n{button}[Continue](https://gov.uk/random){/button}" do + assert_html_output %{ +
+ + + + + } + end + + test_given_govspeak "{button}[More line breaks](https://gov.uk/random){/button}\n\n{button}[Continue](https://gov.uk/random){/button}\n\n{button}[Continue](https://gov.uk/random){/button}" do + assert_html_output %{ + + + + + + } + end + + test_given_govspeak %{ + ## Register to vote + + Introduction text about the service. + + lorem lorem lorem + lorem lorem lorem + + {button start}[Start Now](https://gov.uk/random){/button} + + lorem lorem lorem + lorem lorem lorem + } do + assert_html_output %{ +Introduction text about the service.
+ +lorem lorem lorem + lorem lorem lorem
+ + + +lorem lorem lorem + lorem lorem lorem
+ } + end end