Add Asciinema Liquid tag (#4152)
* Add Asciinema Liquid tag Closes #4135 * Fix Iframe URL * Remove unnecessary regex modifier * Re-add accidentally deleted closing tag and set iframe height * Improve error message for invalid ID Co-Authored-By: Andy Zhao <andyzhao.zhao@gmail.com>
This commit is contained in:
parent
757b1a8b57
commit
a9e1e71e1f
4 changed files with 64 additions and 0 deletions
32
app/liquid_tags/asciinema_tag.rb
Normal file
32
app/liquid_tags/asciinema_tag.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
class AsciinemaTag < LiquidTagBase
|
||||
PARTIAL = "liquids/asciinema".freeze
|
||||
|
||||
def initialize(tag_name, id, tokens)
|
||||
super
|
||||
@id = parse_id(id)
|
||||
end
|
||||
|
||||
def render(_context)
|
||||
ActionController::Base.new.render_to_string(
|
||||
partial: PARTIAL,
|
||||
locals: {
|
||||
id: @id
|
||||
},
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def parse_id(input)
|
||||
input_no_space = input.delete(" ")
|
||||
raise StandardError, "Invalid Asciinema ID: {% asciinema #{input_no_space} %}" unless valid_id?(input_no_space)
|
||||
|
||||
input_no_space
|
||||
end
|
||||
|
||||
def valid_id?(id)
|
||||
id.match?(/\A\d+\Z/)
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_tag("asciinema", AsciinemaTag)
|
||||
8
app/views/liquids/_asciinema.html.erb
Normal file
8
app/views/liquids/_asciinema.html.erb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<div class="ltag_asciinema">
|
||||
<iframe frameborder="0"
|
||||
scrolling="no"
|
||||
id="asciinema_<%= id %>"
|
||||
src="https://asciinema.org/a/<%= id %>/iframe"
|
||||
style="width:100%;height:50vh;">
|
||||
</iframe>
|
||||
</div>
|
||||
|
|
@ -321,6 +321,9 @@
|
|||
{% stackexchange <span style="color: aquamarine;">170185</span> <span style="color: orange;">diy</span> %}
|
||||
</pre>
|
||||
</p>
|
||||
<h3><strong>Asciinema Embed</strong></h3>
|
||||
<p>All you need is the Asciinema id:</p>
|
||||
<code>{% asciinema 239367 %}</code>
|
||||
<h3><strong>Parsing Liquid Tags as a Code Example</strong></h3>
|
||||
<p>To parse Liquid tags as code, simply wrap it with a single backtick or triple backticks.</p>
|
||||
<p><code>`{% mytag %}{{ site.SOMETHING }}{% endmytag %}`</code></p>
|
||||
|
|
|
|||
21
spec/liquid_tags/asciinema_tag_spec.rb
Normal file
21
spec/liquid_tags/asciinema_tag_spec.rb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe AsciinemaTag, type: :liquid_template do
|
||||
describe "#id" do
|
||||
let(:valid_id) { "1234" }
|
||||
let(:invalid_id) { "inv@lid" }
|
||||
|
||||
def generate_tag(id)
|
||||
Liquid::Template.register_tag("asciinema", AsciinemaTag)
|
||||
Liquid::Template.parse("{% asciinema #{id} %}")
|
||||
end
|
||||
|
||||
it "rejects invalid ids" do
|
||||
expect { generate_tag(invalid_id) }.to raise_error(StandardError)
|
||||
end
|
||||
|
||||
it "accepts a valid id" do
|
||||
expect { generate_tag(valid_id) }.not_to raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue