Raise Error with Invalid UnifiedEmbed URL (#16051)

* raise error with invalid embed url

* fix spec; update docs
This commit is contained in:
Arit Amana 2022-01-11 14:55:06 -05:00 committed by GitHub
parent 1a10bfd70d
commit 7dc59eb4bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View file

@ -21,22 +21,20 @@ module UnifiedEmbed
# @return [LiquidTagBase]
def self.new(tag_name, link, parse_context)
klass = UnifiedEmbed::Registry.find_liquid_tag_for(link: link)
# If we can't find a registered "embed" tag, let's raise an exception.
# This exception will give the user an opportunity to adjust their approach.
#
# In a prior implementation, we chose to render an A-tag using the given URL.
# With that prior implementation, a user expecting a "rich embed" might not
# notice that they didn't have a rich embed and instead published a basic
# A-tag. In addition, said A-tag would goes nowhere; which may confuse
# users and/or Forem readers.
raise StandardError, "Embed URL not valid" unless klass
if klass
# Why the __send__? Because a LiquidTagBase class "privatizes"
# the `.new` method. And we want to instantiate the specific
# liquid tag for the given link.
klass.__send__(:new, tag_name, link, parse_context)
else
# If we don't know how to handle the embed, let's just give the
# user an A-tag.
super
end
end
def render(_context)
link, _options = strip_tags(@markup)
%(<a href="#{link}">#{link}</a>)
# Why the __send__? Because a LiquidTagBase class "privatizes"
# the `.new` method. And we want to instantiate the specific
# liquid tag for the given link.
klass.__send__(:new, tag_name, link, parse_context)
end
end
end

View file

@ -10,9 +10,11 @@ RSpec.describe UnifiedEmbed::Tag, type: :liquid_tag do
expect(GistTag).to have_received(:new)
end
it "renders an A-tag when no link-matching class is found" do
it "raises an error when no link-matching class is found" do
link = "https://takeonrules.com/about"
parsed_tag = Liquid::Template.parse("{% embed #{link} %}")
expect(parsed_tag.render).to eq(%(<a href="#{link}">#{link}</a>))
expect do
Liquid::Template.parse("{% embed #{link} %}")
end.to raise_error(StandardError, "Embed URL not valid")
end
end