From 691efd1f7bc678772503be53450acaa1cf005f9d Mon Sep 17 00:00:00 2001 From: rhymes Date: Tue, 19 May 2020 10:57:35 +0200 Subject: [PATCH] Enable comment liquid tag and alias devcomment to it (#7912) * Enable comment liquid tag and alias devcomment to it * Update editor guide * Add explanation --- .../{DevCommentTag.scss => CommentTag.scss} | 0 app/assets/stylesheets/ltags/LiquidTags.scss | 2 +- .../{dev_comment_tag.rb => comment_tag.rb} | 6 ++- app/liquid_tags/null_tag.rb | 2 +- app/views/pages/_editor_guide_text.html.erb | 2 +- spec/liquid_tags/comment_tag_spec.rb | 54 +++++++++++++++++++ spec/liquid_tags/dev_comment_tag_spec.rb | 40 -------------- spec/liquid_tags/null_tag_spec.rb | 2 +- 8 files changed, 62 insertions(+), 46 deletions(-) rename app/assets/stylesheets/ltags/{DevCommentTag.scss => CommentTag.scss} (100%) rename app/liquid_tags/{dev_comment_tag.rb => comment_tag.rb} (69%) create mode 100644 spec/liquid_tags/comment_tag_spec.rb delete mode 100644 spec/liquid_tags/dev_comment_tag_spec.rb diff --git a/app/assets/stylesheets/ltags/DevCommentTag.scss b/app/assets/stylesheets/ltags/CommentTag.scss similarity index 100% rename from app/assets/stylesheets/ltags/DevCommentTag.scss rename to app/assets/stylesheets/ltags/CommentTag.scss diff --git a/app/assets/stylesheets/ltags/LiquidTags.scss b/app/assets/stylesheets/ltags/LiquidTags.scss index 529b1a487..e7794c477 100644 --- a/app/assets/stylesheets/ltags/LiquidTags.scss +++ b/app/assets/stylesheets/ltags/LiquidTags.scss @@ -2,7 +2,7 @@ @import 'TweetTag'; @import 'YoutubeTag'; @import 'GithubTag'; -@import 'DevCommentTag'; +@import 'CommentTag'; @import 'LinkTag'; @import 'PodcastTag'; @import 'UserTag'; diff --git a/app/liquid_tags/dev_comment_tag.rb b/app/liquid_tags/comment_tag.rb similarity index 69% rename from app/liquid_tags/dev_comment_tag.rb rename to app/liquid_tags/comment_tag.rb index 4579590c1..1f2865b0c 100644 --- a/app/liquid_tags/dev_comment_tag.rb +++ b/app/liquid_tags/comment_tag.rb @@ -1,4 +1,4 @@ -class DevCommentTag < LiquidTagBase +class CommentTag < LiquidTagBase PARTIAL = "comments/liquid".freeze def initialize(_tag_name, id_code, _tokens) @@ -19,4 +19,6 @@ class DevCommentTag < LiquidTagBase end end -Liquid::Template.register_tag("devcomment", DevCommentTag) +Liquid::Template.register_tag("comment", CommentTag) +# kept for compatibility with existing comments embeds on DEV +Liquid::Template.register_tag("devcomment", CommentTag) diff --git a/app/liquid_tags/null_tag.rb b/app/liquid_tags/null_tag.rb index bb7993f6c..7c1a64ca8 100644 --- a/app/liquid_tags/null_tag.rb +++ b/app/liquid_tags/null_tag.rb @@ -4,7 +4,7 @@ class NullTag < Liquid::Block end end -disabled_tags = %w[assign break capture case comment cycle decrement for if ifchanged include increment unless tablerow] +disabled_tags = %w[assign break capture case cycle decrement for if ifchanged include increment unless tablerow] disabled_tags.each do |tag| Liquid::Template.register_tag(tag, NullTag) diff --git a/app/views/pages/_editor_guide_text.html.erb b/app/views/pages/_editor_guide_text.html.erb index 181c76ef6..425c661f0 100644 --- a/app/views/pages/_editor_guide_text.html.erb +++ b/app/views/pages/_editor_guide_text.html.erb @@ -72,7 +72,7 @@

All you need is the ID at the end of a comment URL. To get the comment link, click either the timestamp or the menu button in the top right corner on a comment and then click "Permalink". Here's an example:

- {% devcomment 2d1a %} + {% comment 2d1a %}

<%= community_name %> Podcast Episode Embed

All you need is the full link of the podcast episode:

{% podcast https://dev.to/basecspodcast/s2e2--queues-irl %} diff --git a/spec/liquid_tags/comment_tag_spec.rb b/spec/liquid_tags/comment_tag_spec.rb new file mode 100644 index 000000000..023bd01a5 --- /dev/null +++ b/spec/liquid_tags/comment_tag_spec.rb @@ -0,0 +1,54 @@ +require "rails_helper" + +RSpec.describe CommentTag, type: :liquid_tag do + let(:user) { create(:user) } + let(:article) { create(:article) } + let(:comment) { create(:comment, commentable: article, user: user) } + + setup { Liquid::Template.register_tag("comment", described_class) } + + def generate_comment_tag(id_code) + Liquid::Template.parse("{% comment #{id_code} %}") + end + + context "when given valid id_code" do + it "fetches the target comment and render properly" do + liquid = generate_comment_tag(comment.id_code_generated) + + expect(liquid.render).to include(CGI.escapeHTML(comment.body_markdown)) + expect(liquid.render).to include(CGI.escapeHTML(user.name)) + end + + it "raise error if comment ID does not exist" do + expect do + liquid = generate_comment_tag("this will fail") + liquid.render + end.to raise_error(StandardError) + end + end + + context "when rendered" do + let(:rendered_tag) { generate_comment_tag(comment.id_code_generated).render } + + it "shows the comment date" do + expect(rendered_tag).to include(comment.readable_publish_date) + end + + it "embeds the comment published timestamp" do + expect(rendered_tag).to include(comment.decorate.published_timestamp) + end + end + + context "with the legacy 'devcomment'" do + before do + Liquid::Template.register_tag("devcomment", described_class) + end + + it "renders properly" do + liquid = Liquid::Template.parse("{% devcomment #{comment.id_code_generated} %}") + + expect(liquid.render).to include(CGI.escapeHTML(comment.body_markdown)) + expect(liquid.render).to include(CGI.escapeHTML(user.name)) + end + end +end diff --git a/spec/liquid_tags/dev_comment_tag_spec.rb b/spec/liquid_tags/dev_comment_tag_spec.rb deleted file mode 100644 index 95ad10b28..000000000 --- a/spec/liquid_tags/dev_comment_tag_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -require "rails_helper" - -RSpec.describe DevCommentTag, type: :liquid_tag do - let(:user) { create(:user, username: "DevCommentTagTest", name: "DevCommentTag Test") } - let(:article) { create(:article) } - let(:comment) { create(:comment, commentable: article, body_markdown: "DevCommentTagTest", user: user) } - - setup { Liquid::Template.register_tag("devcomment", described_class) } - - def generate_comment_tag(id_code) - Liquid::Template.parse("{% devcomment #{id_code} %}") - end - - context "when given valid id_code" do - it "fetches the target comment and render properly" do - liquid = generate_comment_tag(comment.id_code_generated) - expect(liquid.render).to include(comment.body_markdown) - expect(liquid.render).to include(user.name) - end - - it "raise error if comment does not exist" do - expect do - liquid = generate_comment_tag("this will fail") - liquid.render - end.to raise_error(StandardError) - end - end - - context "when rendered" do - let(:rendered_tag) { generate_comment_tag(comment.id_code_generated).render } - - it "shows the comment date" do - expect(rendered_tag).to include(comment.readable_publish_date) - end - - it "embeds the comment published timestamp" do - expect(rendered_tag).to include(comment.decorate.published_timestamp) - end - end -end diff --git a/spec/liquid_tags/null_tag_spec.rb b/spec/liquid_tags/null_tag_spec.rb index 4fcdd603b..3eddcfe3d 100644 --- a/spec/liquid_tags/null_tag_spec.rb +++ b/spec/liquid_tags/null_tag_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" RSpec.describe NullTag, type: :liquid_tag do describe "#initialize" do - tags = %w[assign capture case comment cycle for if ifchanged include unless] + tags = %w[assign capture case cycle for if ifchanged include unless] setup { tags.each { |tag| Liquid::Template.register_tag(tag, described_class) } }