From 832e89237368b6a3c01bcbb6a88eaac79d5d3e1f Mon Sep 17 00:00:00 2001 From: Andy Zhao Date: Tue, 4 Sep 2018 16:19:24 -0400 Subject: [PATCH] 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 --- app/liquid_tags/assign_tag.rb | 7 ++++++ app/liquid_tags/cycle_tag.rb | 7 ++++++ app/liquid_tags/glitch_tag.rb | 8 +++++- app/liquid_tags/include_tag.rb | 7 ++++++ app/liquid_tags/replit_tag.rb | 2 +- app/liquid_tags/speakerdeck_tag.rb | 2 +- app/views/pages/_editor_guide_text.html.erb | 28 +++++++++++++++------ spec/liquid_tags/glitch_tag_spec.rb | 5 ++++ spec/liquid_tags/replit_tag_spec.rb | 2 +- spec/liquid_tags/speakerdeck_tag_spec.rb | 2 +- 10 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 app/liquid_tags/assign_tag.rb create mode 100644 app/liquid_tags/cycle_tag.rb create mode 100644 app/liquid_tags/include_tag.rb diff --git a/app/liquid_tags/assign_tag.rb b/app/liquid_tags/assign_tag.rb new file mode 100644 index 000000000..7a4271462 --- /dev/null +++ b/app/liquid_tags/assign_tag.rb @@ -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) diff --git a/app/liquid_tags/cycle_tag.rb b/app/liquid_tags/cycle_tag.rb new file mode 100644 index 000000000..61451a203 --- /dev/null +++ b/app/liquid_tags/cycle_tag.rb @@ -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) diff --git a/app/liquid_tags/glitch_tag.rb b/app/liquid_tags/glitch_tag.rb index 87b77b253..f32cf71ad 100644 --- a/app/liquid_tags/glitch_tag.rb +++ b/app/liquid_tags/glitch_tag.rb @@ -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 diff --git a/app/liquid_tags/include_tag.rb b/app/liquid_tags/include_tag.rb new file mode 100644 index 000000000..3416fbfae --- /dev/null +++ b/app/liquid_tags/include_tag.rb @@ -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) diff --git a/app/liquid_tags/replit_tag.rb b/app/liquid_tags/replit_tag.rb index 4c16597b5..828a24e67 100644 --- a/app/liquid_tags/replit_tag.rb +++ b/app/liquid_tags/replit_tag.rb @@ -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 diff --git a/app/liquid_tags/speakerdeck_tag.rb b/app/liquid_tags/speakerdeck_tag.rb index 47e092b38..71eab33f1 100644 --- a/app/liquid_tags/speakerdeck_tag.rb +++ b/app/liquid_tags/speakerdeck_tag.rb @@ -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 diff --git a/app/views/pages/_editor_guide_text.html.erb b/app/views/pages/_editor_guide_text.html.erb index 1f1f64424..aa9f5e809 100644 --- a/app/views/pages/_editor_guide_text.html.erb +++ b/app/views/pages/_editor_guide_text.html.erb @@ -35,16 +35,16 @@

Inline Images

example image, with sloan - +

         ![Alt text of image](put-link-to-image-here)
-        
You can even add a caption using the figcaption tag!
-
+
+
You can even add a caption using the HTML figcaption tag!

Headers

Add a header to your post with this syntax:

-      
#One '#' for a h1 header
##Two '#'s for a h2 header
...
######Six '#'s for a h6 header
+
# One '#' for a h1 header
## Two '#'s for a h2 header
...
###### Six '#'s for a h6 header

One '#' for a h1 header

Two '#'s for a h2 header

@@ -74,12 +74,12 @@ {% twitter 834439977220112384 %}

Glitch embed

-

Use the glitch project slug

+

All you need is the Glitch project slug

{% glitch earthy-course %}

GitHub Repo Embed

-

Use the GitHub username and repo name like this:

- {% github thepracticaldev/dev.to_core %} +

All you need is the GitHub username and repo:

+ {% github thepracticaldev/dev.to %}

GitHub Issue or Comment Embed

All you need is the GitHub issue or comment URL:

@@ -100,7 +100,7 @@

RunKit Embed

Put executable code within a runkit liquid block, as follows:

-
{% runkit %}
console.log("Place javascript here!");
{% endrunkit %}
+
{% runkit %}
console.log("Place javascript here!");
{% endrunkit %}

repl.it Embed

All you need is the URL after the domain name:

@@ -110,6 +110,18 @@

All you need is the Instagram post id from the URL.

{% instagram BXgGcAUjM39 %} +

Speakerdeck Tag

+

All you need is the data-id code from the embed link:

+
+      # Given this embed link:
+      < script async class="speakerdeck-embed"
+      data-id="7e9f8c0fa0c949bd8025457181913fd0"
+      data-ratio="1.77777777777778" src="//speakerdeck.com/assets/embed.js">< /script >
+    
+
+      {% speakerdeck 7e9f8c0fa0c949bd8025457181913fd0 %}
+    
+

Parsing Liquid Tags as a Code Example

To parse Liquid tags as code, simply wrap it with a single backtick or triple backticks.

`{% mytag %}{{ site.SOMETHING }}{% endmytag %}`

diff --git a/spec/liquid_tags/glitch_tag_spec.rb b/spec/liquid_tags/glitch_tag_spec.rb index e63b8c17e..a395b6bfc 100644 --- a/spec/liquid_tags/glitch_tag_spec.rb +++ b/spec/liquid_tags/glitch_tag_spec.rb @@ -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 diff --git a/spec/liquid_tags/replit_tag_spec.rb b/spec/liquid_tags/replit_tag_spec.rb index 0ea3ff60b..cdcc17473 100644 --- a/spec/liquid_tags/replit_tag_spec.rb +++ b/spec/liquid_tags/replit_tag_spec.rb @@ -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) diff --git a/spec/liquid_tags/speakerdeck_tag_spec.rb b/spec/liquid_tags/speakerdeck_tag_spec.rb index 21d6ea055..63d63dbb7 100644 --- a/spec/liquid_tags/speakerdeck_tag_spec.rb +++ b/spec/liquid_tags/speakerdeck_tag_spec.rb @@ -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)