From 2af033fca9ff5ca2ffed24a10b995915fdd100ae Mon Sep 17 00:00:00 2001 From: Andrew Sverdrup Date: Tue, 10 Dec 2019 12:19:30 -0800 Subject: [PATCH] Add Next Tech Liquid Tag (#4958) [deploy] * Add liquid tag for Next Tech * Add liquid tag for Next Tech * Use project share URL in liquid tag. * Update docs and specs with share token URL. --- app/liquid_tags/nexttech_tag.rb | 37 +++++++++++++++++++ app/views/liquids/_nexttech.html.erb | 4 ++ app/views/pages/_editor_guide_text.html.erb | 5 +++ docs/frontend/liquid-tags.md | 1 + spec/liquid_tags/next_tech_tag_spec.rb | 30 +++++++++++++++ .../nexttech_liquid_tag.approved.html | 7 ++++ 6 files changed, 84 insertions(+) create mode 100644 app/liquid_tags/nexttech_tag.rb create mode 100644 app/views/liquids/_nexttech.html.erb create mode 100644 spec/liquid_tags/next_tech_tag_spec.rb create mode 100644 spec/support/fixtures/approvals/nexttech_liquid_tag.approved.html diff --git a/app/liquid_tags/nexttech_tag.rb b/app/liquid_tags/nexttech_tag.rb new file mode 100644 index 000000000..c4867f2e0 --- /dev/null +++ b/app/liquid_tags/nexttech_tag.rb @@ -0,0 +1,37 @@ +class NextTechTag < LiquidTagBase + PARTIAL = "liquids/nexttech".freeze + + def initialize(tag_name, share_url, tokens) + super + @token = parse_share_url(share_url) + end + + def render(_context) + ActionController::Base.new.render_to_string( + partial: PARTIAL, + locals: { + token: @token + }, + ) + end + + private + + # Returns the share token from the end of the share URL. + def parse_share_url(share_url) + clean_share_url = ActionController::Base.helpers.strip_tags(share_url).delete(" ").gsub(/\?.*/, "") + raise StandardError, "Invalid Next Tech share URL" unless valid_share_url?(clean_share_url) + + clean_share_url.split("/").last + end + + # Examples of valid share URLs: + # - https://nt.dev/s/123456abcdef + # - http://nt.dev/s/123456abcdef/ + # - nt.dev/s/123456abcdef + def valid_share_url?(share_url) + (share_url =~ /^(?:(?:http|https):\/\/)?nt\.dev\/s\/[a-z0-9]{12}\/{0,1}$/)&.zero? + end +end + +Liquid::Template.register_tag("nexttech", NextTechTag) diff --git a/app/views/liquids/_nexttech.html.erb b/app/views/liquids/_nexttech.html.erb new file mode 100644 index 000000000..907f6fec9 --- /dev/null +++ b/app/views/liquids/_nexttech.html.erb @@ -0,0 +1,4 @@ + diff --git a/app/views/pages/_editor_guide_text.html.erb b/app/views/pages/_editor_guide_text.html.erb index 70fd8aadc..61df85d4c 100644 --- a/app/views/pages/_editor_guide_text.html.erb +++ b/app/views/pages/_editor_guide_text.html.erb @@ -262,6 +262,11 @@

repl.it Embed

All you need is the URL after the domain name:

{% replit @WigWog/PositiveFineOpensource %} +

Next Tech Embed

+

All you need is the share URL for your sandbox. You can get the share URL by clicking + the "Share" button in the top right when the sandbox is open.

+

+ {% nexttech https://nt.dev/s/6ba1fffbd09e %}

Instagram Embed

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

{% instagram BXgGcAUjM39 %} diff --git a/docs/frontend/liquid-tags.md b/docs/frontend/liquid-tags.md index ce647f59e..fce6167e6 100644 --- a/docs/frontend/liquid-tags.md +++ b/docs/frontend/liquid-tags.md @@ -46,6 +46,7 @@ Here is a bunch of liquid tags supported on DEV: {% codesandbox ppxnl191zx %} {% jsfiddle https://jsfiddle.net/link2twenty/v2kx9jcd %} {% replit @WigWog/PositiveFineOpensource %} +{% nexttech https://nt.dev/s/6ba1fffbd09e %} {% instagram BXgGcAUjM39 %} {% speakerdeck 7e9f8c0fa0c949bd8025457181913fd0 %} {% soundcloud https://soundcloud.com/user-261265215/dev-to-review-episode-1 %} diff --git a/spec/liquid_tags/next_tech_tag_spec.rb b/spec/liquid_tags/next_tech_tag_spec.rb new file mode 100644 index 000000000..e033f6bae --- /dev/null +++ b/spec/liquid_tags/next_tech_tag_spec.rb @@ -0,0 +1,30 @@ +require "rails_helper" + +RSpec.describe NextTechTag, type: :liquid_template do + describe "#link" do + let(:nexttech_link) { "https://nt.dev/s/6ba1fffbd09e" } + + def generate_new_liquid(link) + Liquid::Template.register_tag("nexttech", NextTechTag) + Liquid::Template.parse("{% nexttech #{link} %}") + end + + it "accepts nexttech link" do + liquid = generate_new_liquid(nexttech_link) + rendered_nexttech_iframe = liquid.render + Approvals.verify(rendered_nexttech_iframe, name: "nexttech_liquid_tag", format: :html) + end + + it "accepts nexttech link with a / at the end" do + expect do + generate_new_liquid(nexttech_link + "/") + end.not_to raise_error + end + + it "rejects invalid nexttech link" do + expect do + generate_new_liquid("https://nt.dev/s/1234567890z*") + end.to raise_error(StandardError) + end + end +end diff --git a/spec/support/fixtures/approvals/nexttech_liquid_tag.approved.html b/spec/support/fixtures/approvals/nexttech_liquid_tag.approved.html new file mode 100644 index 000000000..58eb21c90 --- /dev/null +++ b/spec/support/fixtures/approvals/nexttech_liquid_tag.approved.html @@ -0,0 +1,7 @@ + + + + + +