SlideShare Liquid tag (#1485)

* Create slideshare_tag.rb

* Create slideshare_tag_spec.rb

* Add slide share

* rubocop compliance

* Still learning

https://ruby-doc.org/core-2.5.1/Regexp.html

* More secure match

Co-Authored-By: Link2Twenty <AndrewB05@gmail.com>

* Strip whitespace

Co-Authored-By: Link2Twenty <AndrewB05@gmail.com>

* Strip before regex

Co-Authored-By: Link2Twenty <AndrewB05@gmail.com>

* Remove latter strip

Co-Authored-By: Link2Twenty <AndrewB05@gmail.com>
This commit is contained in:
Andrew Bone 2019-01-10 17:58:16 +00:00 committed by Ben Halpern
parent 8f348f9831
commit 4673496830
3 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,33 @@
class SlideshareTag < LiquidTagBase
def initialize(tag_name, key, tokens)
super
@key = validate key.strip
@height = 450
end
def render(_context)
finalize_html <<-HTML
<iframe
src="//www.slideshare.net/slideshow/embed_code/key/#{@key}"
alt="#{@key} on slideshare.net"
width="100%"
height="#{@height}"
frameborder="0"
scrolling="no"
allowfullscreen>
</iframe>
HTML
end
private
def validate(key)
if key.match?(/\A[a-zA-Z0-9]{14}\Z/)
key
else
raise StandardError, "Invalid Slideshare Key"
end
end
end
Liquid::Template.register_tag("slideshare", SlideshareTag)

View file

@ -121,6 +121,10 @@
<li><strong>YouTube:</strong> <code>{% youtube dQw4w9WgXcQ %}</code></li>
<li><strong>Vimeo:</strong> <code>{% vimeo 193110695 %}</code></li>
</ul>
<h3><strong>SlideShare Embed</strong></h3>
<p>All you need is the SlideShare <code>key</code></p>
<code>{% slideshare rdOzN9kr1yK5eE %}</code>
<h3><strong>CodePen Embed</strong></h3>
<p>All you need is the full CodePen <code>link</code>, ending in the pen ID code, as follows:</p>

View file

@ -0,0 +1,16 @@
require "rails_helper"
RSpec.describe SlideshareTag, type: :liquid_template do
describe "#key" do
let(:valid_key) { "rdOzN9kr1yK5eE" }
def generate_tag(key)
Liquid::Template.register_tag("slideshare", SlideshareTag)
Liquid::Template.parse("{% slideshare #{key} %}")
end
it "accepts a valid key" do
expect { generate_tag(valid_key) }.not_to raise_error
end
end
end