Skip to content

Commit

Permalink
Merge pull request #355 from alphagov/content-modelling/647-support-e…
Browse files Browse the repository at this point in the history
…mbeds-in-govspeak

 (647) Support Embeds in Govspeak
  • Loading branch information
pezholio authored Oct 21, 2024
2 parents 6a36feb + 10f37aa commit a5c249b
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 8.5.0

* Support embeds in Govspeak

## 8.4.1
* Do not pin version of govuk_publishing_components

Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,34 @@ will output
</div>
```

### 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

```
{{embed:content_block_email_address:d308f561-e5ee-45b5-90b2-3ac36a23fad9}}
```

with options provided

```
{
embeds: [
{
content_id: "d308f561-e5ee-45b5-90b2-3ac36a23fad9",
title: "Government Digital Service",
details: { email_address: "test@example.com" },
}
]
}
```

will output

```html
<span class="embed_content_block_email_address" id="embed_d308f561-e5ee-45b5-90b2-3ac36a23fad9">test@example.com</span>
```

### Button

An accessible way to add button links into content, that can also allow cross domain tracking with [Google Analytics](https://support.google.com/analytics/answer/7372977?hl=en)
Expand Down
13 changes: 12 additions & 1 deletion lib/govspeak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +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/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/h_card_presenter"
require "govspeak/presenters/image_presenter"
require "govspeak/presenters/attachment_image_presenter"
Expand All @@ -37,7 +40,7 @@ class Document
@extensions = []

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

def self.to_html(source, options = {})
new(source, options).to_html
Expand All @@ -57,6 +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))
@locale = options.fetch(:locale, "en")
@options = { input: PARSER_CLASS_NAME,
sanitize: true,
Expand Down Expand Up @@ -255,6 +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 }
next "" unless embed

EmbedPresenter.new(embed).render
end

# As of version 1.12.0 of Kramdown the block elements (div & figcaption)
# inside this html block will have it's < > converted into HTML Entities
# when ever this code is used inside block level elements.
Expand Down
17 changes: 17 additions & 0 deletions lib/govspeak/embed_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Govspeak
class EmbedExtractor
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])
}.uniq
end

def content_ids
@content_ids ||= content_references.map(&:content_id)
end
end
end
15 changes: 15 additions & 0 deletions lib/govspeak/embedded_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Govspeak
class EmbeddedContent
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}}})/

attr_reader :document_type, :content_id, :embed_code

def initialize(document_type:, content_id:, embed_code:)
@document_type = document_type
@content_id = content_id
@embed_code = embed_code
end
end
end
32 changes: 32 additions & 0 deletions lib/govspeak/presenters/embed_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "action_view"
require "htmlentities"

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

attr_reader :embed

def initialize(embed)
@embed = ActiveSupport::HashWithIndifferentAccess.new(embed)
end

def content_id
embed[:content_id]
end

def document_type
embed[:document_type]
end

def render
body = if document_type == "content_block_email_address"
embed.dig(:details, :email_address)
else
embed[:title]
end

content_tag(:span, body, class: "embed embed-#{document_type}", id: "embed_#{content_id}")
end
end
end
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.4.1".freeze
VERSION = "8.5.0".freeze
end
59 changes: 59 additions & 0 deletions test/embed_extractor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "test_helper"

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

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

describe "when there is no embedded content" do
let(:document) { "foo" }

describe "#content_references" do
it "returns an empty array" do
assert_equal [], subject.content_references
end
end

describe "#content_ids" do
it "returns an empty array" do
assert_equal [], subject.content_ids
end
end
end

describe "when there is embedded content" do
let(:contact_uuid) { SecureRandom.uuid }
let(:content_block_email_address_uuid) { SecureRandom.uuid }

let(:document) do
"""
{{embed:contact:#{contact_uuid}}}
{{embed:content_block_email_address:#{content_block_email_address_uuid}}}
"""
end

describe "#content_references" do
it "returns all references" do
result = subject.content_references

assert_equal 2, result.count

assert_equal "contact", result[0].document_type
assert_equal contact_uuid, result[0].content_id
assert_equal "{{embed:contact:#{contact_uuid}}}", result[0].embed_code

assert_equal "content_block_email_address", result[1].document_type
assert_equal content_block_email_address_uuid, result[1].content_id
assert_equal "{{embed:content_block_email_address:#{content_block_email_address_uuid}}}", result[1].embed_code
end
end

describe "#content_ids" do
it "returns all uuids as an array" do
assert_equal [contact_uuid, content_block_email_address_uuid], subject.content_ids
end
end
end
end
end
84 changes: 84 additions & 0 deletions test/govspeak_embeds_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require "test_helper"

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

def compress_html(html)
html.gsub(/[\n\r]+\s*/, "")
end

let(:content_id) { SecureRandom.uuid }

it "renders an email address when present in options[:embeds]" do
embed = {
content_id:,
document_type: "content_block_email_address",
title: "foo",
details: {
email_address: "foo@example.com",
},
}
govspeak = "{{embed:content_block_email_address:#{content_id}}}"

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

expected = "<p><span class=\"embed embed-content_block_email_address\" id=\"embed_#{content_id}\">#{embed[: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_id:,
document_type: "contact",
title: "foo",
}
govspeak = "{{embed:contact:#{content_id}}}"

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

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

assert_equal compress_html(expected), compress_html(rendered)
end

it "ignores missing embeds" do
govspeak = "{{embed:contact:#{content_id}}}"

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

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

it "supports multiple embeds" do
embeds = [
{
content_id: SecureRandom.uuid,
document_type: "contact",
title: "foo",
},
{
content_id: SecureRandom.uuid,
document_type: "content_block_email_address",
title: "foo",
details: {
email_address: "foo@example.com",
},
},
]

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

rendered = Govspeak::Document.new(govspeak, embeds:).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>
"""

assert_equal compress_html(expected), compress_html(rendered)
end
end

0 comments on commit a5c249b

Please sign in to comment.