Implement GitPitchTag (#4942)
This commit is contained in:
parent
1b2cdfb0c4
commit
07fd2281cd
4 changed files with 95 additions and 0 deletions
38
app/liquid_tags/gitpitch_tag.rb
Normal file
38
app/liquid_tags/gitpitch_tag.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
class GitPitchTag < LiquidTagBase
|
||||
PARTIAL = "liquids/gitpitch".freeze
|
||||
URL_REGEXP = /(http|https):\/\/gitpitch.com\/[a-zA-Z0-9\-\/]*/.freeze
|
||||
|
||||
def initialize(tag_name, link, tokens)
|
||||
super
|
||||
@link = parse_link(link)
|
||||
end
|
||||
|
||||
def render(_context)
|
||||
ActionController::Base.new.render_to_string(
|
||||
partial: PARTIAL,
|
||||
locals: {
|
||||
link: @link
|
||||
},
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_link(link)
|
||||
stripped_link = ActionController::Base.helpers.strip_tags(link)
|
||||
the_link = stripped_link.split(" ").first
|
||||
raise_error unless valid_link?(the_link)
|
||||
the_link
|
||||
end
|
||||
|
||||
def valid_link?(link)
|
||||
link_no_space = link.delete(" ")
|
||||
(link_no_space =~ URL_REGEXP)&.zero?
|
||||
end
|
||||
|
||||
def raise_error
|
||||
raise StandardError, "Invalid GitPitch URL"
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_tag("gitpitch", GitPitchTag)
|
||||
5
app/views/liquids/_gitpitch.html.erb
Normal file
5
app/views/liquids/_gitpitch.html.erb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<iframe
|
||||
height="450"
|
||||
src="<%= link %>"
|
||||
loading="lazy">
|
||||
</iframe>
|
||||
|
|
@ -167,6 +167,11 @@
|
|||
</code></p>
|
||||
</dd>
|
||||
</dl>
|
||||
<h3><strong>GitPitch Embed</strong></h3>
|
||||
<p>All you need is the GitPitch link:</p>
|
||||
<code>
|
||||
{% gitpitch https://gitpitch.com/gitpitch/in-60-seconds %}
|
||||
</code>
|
||||
<h3><strong>Video Embed</strong></h3>
|
||||
<p>All you need is the <code>id</code> from the URL.
|
||||
<ul>
|
||||
|
|
|
|||
47
spec/liquid_tags/gitpitch_tag_spec.rb
Normal file
47
spec/liquid_tags/gitpitch_tag_spec.rb
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe GitPitchTag, type: :liquid_tag do
|
||||
describe "#link" do
|
||||
let(:valid_links) do
|
||||
[
|
||||
"https://gitpitch.com/gitpitch/in-60-seconds",
|
||||
"https://gitpitch.com/gitpitch/what-is-gitpitch",
|
||||
"https://gitpitch.com/gitpitch/demo-deck",
|
||||
]
|
||||
end
|
||||
|
||||
let(:bad_links) do
|
||||
[
|
||||
"//pastebin.com/raw/b77FrXUA#gist.github.com",
|
||||
"https://gitpitch.com/gitpitch/github.com@evil.com",
|
||||
"https://gitpitch.github.com.evil.com",
|
||||
"https://github.com/string/string/raw/string/file",
|
||||
]
|
||||
end
|
||||
|
||||
def generate_tag(link)
|
||||
Liquid::Template.register_tag("gitpitch", GitPitchTag)
|
||||
Liquid::Template.parse("{% gitpitch #{link} %}")
|
||||
end
|
||||
|
||||
def generate_script(link)
|
||||
html = <<~HTML
|
||||
<iframe height="450" src="#{link}" loading="lazy"></iframe>
|
||||
HTML
|
||||
html.tr("\n", " ").delete(" ")
|
||||
end
|
||||
|
||||
it "rejects invalid gitpitch url" do
|
||||
expect do
|
||||
generate_new_liquid("really_long_invalid_link")
|
||||
end.to raise_error(StandardError)
|
||||
end
|
||||
|
||||
it "accepts valid gitpitch url" do
|
||||
valid_links.each do |link|
|
||||
liquid = generate_tag(link)
|
||||
expect(liquid.render.tr("\n", " ").delete(" ")).to eq(generate_script(link))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue