Enable comment liquid tag and alias devcomment to it (#7912)

* Enable comment liquid tag and alias devcomment to it

* Update editor guide

* Add explanation
This commit is contained in:
rhymes 2020-05-19 10:57:35 +02:00 committed by GitHub
parent 713a589f55
commit 691efd1f7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 46 deletions

View file

@ -2,7 +2,7 @@
@import 'TweetTag';
@import 'YoutubeTag';
@import 'GithubTag';
@import 'DevCommentTag';
@import 'CommentTag';
@import 'LinkTag';
@import 'PodcastTag';
@import 'UserTag';

View file

@ -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)

View file

@ -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)

View file

@ -72,7 +72,7 @@
<p>All you need is the
<code>ID</code> 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:
</p>
<code>{% devcomment 2d1a %}</code>
<code>{% comment 2d1a %}</code>
<h3><strong><%= community_name %> Podcast Episode Embed</strong></h3>
<p>All you need is the full link of the podcast episode:</p>
<code>{% podcast https://dev.to/basecspodcast/s2e2--queues-irl %}</code>

View file

@ -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

View file

@ -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

View file

@ -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) } }