Skip to content

Commit

Permalink
Merge pull request #356 from alphagov/content-modelling/647-ignore-em…
Browse files Browse the repository at this point in the history
…bed-codes-when-argument-is-blank

Ignore embed codes when argument is blank
  • Loading branch information
pezholio authored Oct 23, 2024
2 parents a5c249b + 85441f4 commit 18ca993
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 8.5.1

* Rename embed-related code to `content block`
* Do not attempt to parse embed codes if `content_blocks` option is missing

### 8.5.0

* Support embeds in Govspeak
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ will output

### Content blocks

Authors can embed different types of [supported content](https://github.com/alphagov/govspeak/blob/main/lib/govspeak/embedded_content.rb#L3) created by the Content Block Manager
Authors can embed different types of [supported content](https://github.com/alphagov/govspeak/blob/main/lib/govspeak/content_block.rb#L3) created by the Content Block Manager

```
{{embed:content_block_email_address:d308f561-e5ee-45b5-90b2-3ac36a23fad9}}
Expand All @@ -620,7 +620,7 @@ with options provided

```
{
embeds: [
content_blocks: [
{
content_id: "d308f561-e5ee-45b5-90b2-3ac36a23fad9",
title: "Government Digital Service",
Expand Down
18 changes: 10 additions & 8 deletions lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
require "govspeak/html_validator"
require "govspeak/html_sanitizer"
require "govspeak/blockquote_extra_quote_remover"
require "govspeak/embed_extractor"
require "govspeak/embedded_content"
require "govspeak/content_block_extractor"
require "govspeak/content_block"
require "govspeak/post_processor"
require "govspeak/link_extractor"
require "govspeak/template_renderer"
require "govspeak/presenters/attachment_presenter"
require "govspeak/presenters/contact_presenter"
require "govspeak/presenters/embed_presenter"
require "govspeak/presenters/content_block_presenter"
require "govspeak/presenters/h_card_presenter"
require "govspeak/presenters/image_presenter"
require "govspeak/presenters/attachment_image_presenter"
Expand All @@ -40,7 +40,7 @@ class Document
@extensions = []

attr_accessor :images
attr_reader :attachments, :contacts, :links, :locale, :embeds
attr_reader :attachments, :contacts, :links, :locale, :content_blocks

def self.to_html(source, options = {})
new(source, options).to_html
Expand All @@ -60,7 +60,7 @@ def initialize(source, options = {})
@attachments = Array.wrap(options.delete(:attachments))
@links = Array.wrap(options.delete(:links))
@contacts = Array.wrap(options.delete(:contacts))
@embeds = Array.wrap(options.delete(:embeds))
@content_blocks = Array.wrap(options.delete(:content_blocks))
@locale = options.fetch(:locale, "en")
@options = { input: PARSER_CLASS_NAME,
sanitize: true,
Expand Down Expand Up @@ -259,11 +259,13 @@ def insert_strong_inside_p(body, parser = Govspeak::Document)
render_image(AttachmentImagePresenter.new(attachment))
end

extension("embeds", Govspeak::EmbeddedContent::EMBED_REGEX) do |_embed_code, _document_type, content_id|
embed = embeds.detect { |e| e[:content_id] == content_id }
extension("content blocks", Govspeak::ContentBlock::EMBED_REGEX) do |embed_code, _document_type, content_id|
next embed_code if content_blocks.empty?

embed = content_blocks.detect { |e| e[:content_id] == content_id }
next "" unless embed

EmbedPresenter.new(embed).render
ContentBlockPresenter.new(embed).render
end

# As of version 1.12.0 of Kramdown the block elements (div & figcaption)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Govspeak
class EmbeddedContent
class ContentBlock
SUPPORTED_DOCUMENT_TYPES = %w[contact content_block_email_address].freeze
UUID_REGEX = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/
EMBED_REGEX = /({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):#{UUID_REGEX}}})/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Govspeak
class EmbedExtractor
class ContentBlockExtractor
def initialize(document)
@document = document
end

def content_references
@content_references ||= @document.scan(EmbeddedContent::EMBED_REGEX).map { |match|
EmbeddedContent.new(document_type: match[1], content_id: match[2], embed_code: match[0])
@content_references ||= @document.scan(ContentBlock::EMBED_REGEX).map { |match|
ContentBlock.new(document_type: match[1], content_id: match[2], embed_code: match[0])
}.uniq
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "htmlentities"

module Govspeak
class EmbedPresenter
class ContentBlockPresenter
include ActionView::Helpers::TagHelper

attr_reader :embed
Expand Down
2 changes: 1 addition & 1 deletion lib/govspeak/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Govspeak
VERSION = "8.5.0".freeze
VERSION = "8.5.1".freeze
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "test_helper"

class EmbedExtractorTest < Minitest::Test
class ContentBlockExtractorTest < Minitest::Test
extend Minitest::Spec::DSL

describe "EmbedExtractor" do
subject { Govspeak::EmbedExtractor.new(document) }
describe "ContentBlockExtractor" do
subject { Govspeak::ContentBlockExtractor.new(document) }

describe "when there is no embedded content" do
let(:document) { "foo" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "test_helper"

class GovspeakEmbedsTest < Minitest::Test
class GovspeakContentBlocksTest < Minitest::Test
extend Minitest::Spec::DSL

def compress_html(html)
Expand All @@ -10,7 +10,7 @@ def compress_html(html)
let(:content_id) { SecureRandom.uuid }

it "renders an email address when present in options[:embeds]" do
embed = {
content_block = {
content_id:,
document_type: "content_block_email_address",
title: "foo",
Expand All @@ -20,38 +20,52 @@ def compress_html(html)
}
govspeak = "{{embed:content_block_email_address:#{content_id}}}"

rendered = Govspeak::Document.new(govspeak, embeds: [embed]).to_html
rendered = Govspeak::Document.new(govspeak, content_blocks: [content_block]).to_html

expected = "<p><span class=\"embed embed-content_block_email_address\" id=\"embed_#{content_id}\">#{embed[:details][:email_address]}</span></p>"
expected = "<p><span class=\"embed embed-content_block_email_address\" id=\"embed_#{content_id}\">#{content_block[:details][:email_address]}</span></p>"

assert_equal compress_html(expected), compress_html(rendered)
end

it "renders the title when the document type is a contact" do
embed = {
content_block = {
content_id:,
document_type: "contact",
title: "foo",
}
govspeak = "{{embed:contact:#{content_id}}}"

rendered = Govspeak::Document.new(govspeak, embeds: [embed]).to_html
rendered = Govspeak::Document.new(govspeak, content_blocks: [content_block]).to_html

expected = "<p><span class=\"embed embed-contact\" id=\"embed_#{content_id}\">#{embed[:title]}</span></p>"
expected = "<p><span class=\"embed embed-contact\" id=\"embed_#{content_id}\">#{content_block[:title]}</span></p>"

assert_equal compress_html(expected), compress_html(rendered)
end

it "ignores missing embeds" do
it "removes embed code if a content block cannot be found" do
content_block = {
content_id: SecureRandom.uuid,
document_type: "contact",
title: "foo",
}

govspeak = "{{embed:contact:#{content_id}}}"

rendered = Govspeak::Document.new(govspeak, embeds: []).to_html
rendered = Govspeak::Document.new(govspeak, content_blocks: [content_block]).to_html

assert_equal compress_html(""), compress_html(rendered)
end

it "retains an embed code if content_blocks are not specified" do
govspeak = "{{embed:contact:#{content_id}}}"

rendered = Govspeak::Document.new(govspeak).to_html

assert_equal compress_html("<p>#{govspeak}</p>"), compress_html(rendered)
end

it "supports multiple embeds" do
embeds = [
content_blocks = [
{
content_id: SecureRandom.uuid,
document_type: "contact",
Expand All @@ -67,16 +81,16 @@ def compress_html(html)
},
]

govspeak = %(Here is a contact: {{embed:contact:#{embeds[0][:content_id]}}}
govspeak = %(Here is a contact: {{embed:contact:#{content_blocks[0][:content_id]}}}
Here is an email address: {{embed:content_block_email_address:#{embeds[1][:content_id]}}}
Here is an email address: {{embed:content_block_email_address:#{content_blocks[1][:content_id]}}}
)

rendered = Govspeak::Document.new(govspeak, embeds:).to_html
rendered = Govspeak::Document.new(govspeak, content_blocks:).to_html

expected = """
<p>Here is a contact: <span class=\"embed embed-contact\" id=\"embed_#{embeds[0][:content_id]}\">#{embeds[0][:title]}</span></p>
<p>Here is an email address: <span class=\"embed embed-content_block_email_address\" id=\"embed_#{embeds[1][:content_id]}\">#{embeds[1][:details][:email_address]}</span></p>
<p>Here is a contact: <span class=\"embed embed-contact\" id=\"embed_#{content_blocks[0][:content_id]}\">#{content_blocks[0][:title]}</span></p>
<p>Here is an email address: <span class=\"embed embed-content_block_email_address\" id=\"embed_#{content_blocks[1][:content_id]}\">#{content_blocks[1][:details][:email_address]}</span></p>
"""

assert_equal compress_html(expected), compress_html(rendered)
Expand Down

0 comments on commit 18ca993

Please sign in to comment.