Fix Liquid Tags (#590)

* Fix glitch tag

* Add custom solutions for default liquid tags

* Update liquid tags regex

* Add speakerdeck tag and format guide a bit
This commit is contained in:
Andy Zhao 2018-09-04 16:19:24 -04:00 committed by Mac Siri
parent d9b6941537
commit 832e892373
10 changed files with 57 additions and 13 deletions

View file

@ -0,0 +1,7 @@
class AssignTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError.new("Liquid's Assign tag is disabled")
end
end
Liquid::Template.register_tag("assign".freeze, AssignTag)

View file

@ -0,0 +1,7 @@
class CycleTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError.new("Liquid's Cycle tag is disabled")
end
end
Liquid::Template.register_tag("cycle".freeze, CycleTag)

View file

@ -20,7 +20,13 @@ class GlitchTag < LiquidTagBase
private
def parse_id(input)
input.delete(" ")
input_no_space = input.delete(" ")
raise StandardError, "Invalid Glitch ID" unless valid_id?(input_no_space)
input_no_space
end
def valid_id?(input)
(input =~ /^[a-zA-Z0-9\-]{1,110}$/)&.zero?
end
end

View file

@ -0,0 +1,7 @@
class IncludeTag < Liquid::Block
def initialize(_tag_name, _markup, _options)
raise StandardError.new("Liquid's Include tag is disabled")
end
end
Liquid::Template.register_tag("include".freeze, IncludeTag)

View file

@ -22,7 +22,7 @@ class ReplitTag < LiquidTagBase
end
def valid_id?(id)
id.length > 1 && id =~ /[a-zA-Z0-9\/]/
id =~ /\A\@[\w]{2,15}\/[a-zA-Z0-9\-]{0,60}\Z/
end
end

View file

@ -41,7 +41,7 @@ class SpeakerdeckTag < LiquidTagBase
end
def valid_id?(id)
!!(id =~ /\A[a-z\d]*\Z/i)
id =~ /\A[a-z\d]{32}\Z/i
end
end

View file

@ -35,16 +35,16 @@
<h3><strong>Inline Images</strong></h3>
<p>
<img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OsLaFSo9--/c_fill,f_auto,fl_progressive,h_220,q_auto,w_220/https://thepracticaldev.s3.amazonaws.com/uploads/user/profile_image/31047/af153cd6-9994-4a68-83f4-8ddf3e13f0bf.jpg" alt="example image, with sloan"></img>
<code>
<pre>
![Alt text of image](put-link-to-image-here)
<figcaption> You can even add a caption using the figcaption tag!</figcaption>
</code>
</pre>
<figcaption> You can even add a caption using the HTML <code>figcaption</code> tag!</figcaption>
</p>
<h3><strong>Headers</strong></h3>
<p>Add a header to your post with this syntax:</p>
<pre>
<code><br>#One '#' for a h1 header<br>##Two '#'s for a h2 header<br>...<br>######Six '#'s for a h6 header</code>
<br># One '#' for a h1 header<br>## Two '#'s for a h2 header<br>...<br>###### Six '#'s for a h6 header
</pre>
<h1>One '#' for a h1 header</h1>
<h2>Two '#'s for a h2 header</h2>
@ -74,12 +74,12 @@
<code>{% twitter 834439977220112384 %}</code>
<h3><strong>Glitch embed</strong></h3>
<p>Use the glitch project slug</p>
<p>All you need is the Glitch project slug</p>
<code>{% glitch earthy-course %}</code>
<h3><strong>GitHub Repo Embed</strong></h3>
<p>Use the GitHub username and repo name like this:</p>
<code>{% github thepracticaldev/dev.to_core %}</code>
<p>All you need is the GitHub username and repo:</p>
<code>{% github thepracticaldev/dev.to %}</code>
<h3><strong>GitHub Issue or Comment Embed</strong></h3>
<p>All you need is the GitHub issue or comment URL:</p>
@ -100,7 +100,7 @@
<h3><strong>RunKit Embed</strong></h3>
<p>Put executable code within a runkit liquid block, as follows:</p>
<pre><code>{% runkit %}<br>console.log("Place javascript here!"); <br>{% endrunkit %} <br></code></pre>
<pre>{% runkit %}<br>console.log("Place javascript here!"); <br>{% endrunkit %} <br></pre>
<h3><strong>repl.it Embed</strong></h3>
<p>All you need is the URL after the domain name:</p>
@ -110,6 +110,18 @@
<p>All you need is the Instagram post <code>id</code> from the URL.</p>
<code>{% instagram BXgGcAUjM39 %}</code>
<h3><strong>Speakerdeck Tag</strong></h3>
<p>All you need is the data-id code from the embed link:</p>
<pre>
<span style="color: gray;"># Given this embed link:</span>
< script async class="speakerdeck-embed"
data-id="<span style="color: orange;">7e9f8c0fa0c949bd8025457181913fd0</span>"
data-ratio="1.77777777777778" src="//speakerdeck.com/assets/embed.js">< /script >
</pre>
<pre>
{% speakerdeck <span style="color: orange;">7e9f8c0fa0c949bd8025457181913fd0</span> %}
</pre>
<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>

View file

@ -3,6 +3,7 @@ require "rails_helper"
RSpec.describe GlitchTag, type: :liquid_template do
describe "#id" do
let(:valid_id) { "BXgGcAUjM39" }
let(:id_with_quotes) { 'some-id" onload="alert(42)"' }
def generate_tag(id)
Liquid::Template.register_tag("glitch", GlitchTag)
@ -12,5 +13,9 @@ RSpec.describe GlitchTag, type: :liquid_template do
it "accepts a valid id" do
expect { generate_tag(valid_id) }.not_to raise_error
end
it "does not accept double quotes" do
expect { generate_tag(id_with_quotes) }.to raise_error(StandardError)
end
end
end

View file

@ -2,7 +2,7 @@ require "rails_helper"
RSpec.describe ReplitTag, type: :liquid_template do
describe "#id" do
let(:replit_id) { "dQw4w9WgXcQ" }
let(:replit_id) { "@WigWog/PositiveFineOpensource" }
def generate_new_liquid(id)
Liquid::Template.register_tag("replit", ReplitTag)

View file

@ -2,7 +2,7 @@ require "rails_helper"
RSpec.describe SpeakerdeckTag, type: :liquid_template do
describe "#id" do
let(:valid_id) { "BXgGcAUjM39" }
let(:valid_id) { "7e9f8c0fa0c949bd8025457181913fd0" }
let(:invalid_id) { "blahblahblahbl sdsdssd // dsdssd" }
def generate_tag(id)