Implement Unified Embeds for Stackery URLs (#16320)

* complete implementation and add specs

* nudge Travis

* optimization
This commit is contained in:
Arit Amana 2022-01-26 16:19:27 -05:00 committed by GitHub
parent 355328cd4a
commit feebae6ae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 20 deletions

View file

@ -1,44 +1,55 @@
class StackeryTag < LiquidTagBase
PARTIAL = "liquids/stackery".freeze
REGISTRY_REGEXP = %r{https://app.stackery.io/editor/design(?<params>\?.*)?}
PARAM_REGEXP = /\A(owner=[\w\-]+)|(repo=[\w\-]+)|(file=.*)|(ref=.*)\Z/
def initialize(_tag_name, input, _parse_context)
super
@data = get_data(input.strip)
stripped_input = strip_tags(input)
unescaped_input = CGI.unescape_html(stripped_input)
@params = parsed_input(unescaped_input)
end
def render(_context)
ApplicationController.render(
partial: PARTIAL,
locals: {
owner: @data[:owner],
repo: @data[:repo],
ref: @data[:ref]
params: @params
},
)
end
private
def get_data(input)
items = input.split
owner = items.first
repo = items.second
ref = items.third || "master"
def parsed_input(input)
if input.split.length > 1
params = input.split
owner = params.first
repo = params.second
ref = params.third || "master"
validate_items(owner, repo)
raise StandardError, I18n.t("liquid_tags.stackery_tag.missing_argument") unless owner && repo
{
owner: owner,
repo: repo,
ref: ref
}
"owner=#{owner}&repo=#{repo}&ref=#{ref}"
else
match = pattern_match_for(input, [REGISTRY_REGEXP])
raise StandardError, I18n.t("liquid_tags.stackery_tag.missing_argument") unless match
extract_params(match[:params])
end
end
def validate_items(owner, repo)
return unless owner.blank? || repo.blank?
def extract_params(params)
params = params.delete("?").split("&")
params.select { |param| valid_param(param) }.join("&")
end
raise StandardError, I18n.t("liquid_tags.stackery_tag.missing_argument")
def valid_param(param)
(param =~ PARAM_REGEXP)&.zero?
end
end
Liquid::Template.register_tag("stackery", StackeryTag)
UnifiedEmbed.register(StackeryTag, regexp: StackeryTag::REGISTRY_REGEXP)

View file

@ -1,6 +1,6 @@
<iframe
title="stackery editor"
src="//app.stackery.io/editor/design?owner=<%= owner %>&repo=<%= repo %>&ref=<%= ref %>" width="100%"
src="//app.stackery.io/editor/design?<%= params %>" width="100%"
height="400"
frameBorder="0">
</iframe>

View file

@ -17,7 +17,9 @@ RSpec.describe StackeryTag, type: :liquid_tag do
it "renders valid input" do
template = generate_tag(valid_input)
expected = 'src="//app.stackery.io/editor/design?owner=deeheber&repo=lambda-layer-example&ref=layer-resource'
# rubocop:disable Layout/LineLength
expected = "src=\"//app.stackery.io/editor/design?owner=deeheber&amp;repo=lambda-layer-example&amp;ref=layer-resource\""
# rubocop:enable Layout/LineLength
expect(template.render(nil)).to include(expected)
end

View file

@ -195,6 +195,11 @@ RSpec.describe UnifiedEmbed::Registry do
end
end
it "returns StackeryTag for a stackery url" do
expect(described_class.find_liquid_tag_for(link: "https://app.stackery.io/editor/design?owner=stackery&repo=quickstart-ruby&file=template.yaml"))
.to eq(StackeryTag)
end
it "returns TweetTag for a tweet url" do
expect(described_class.find_liquid_tag_for(link: "https://twitter.com/aritdeveloper/status/1483614684884484099"))
.to eq(TweetTag)