mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 11:11:13 +08:00
FEATURE: Add quote-modified
class if a quote has been modified
This commit is contained in:
@ -145,7 +145,7 @@ export function setup(helper) {
|
|||||||
opts.emojiSet = siteSettings.emoji_set;
|
opts.emojiSet = siteSettings.emoji_set;
|
||||||
});
|
});
|
||||||
|
|
||||||
helper.registerPlugin(md=>{
|
helper.registerPlugin(md => {
|
||||||
md.block.bbcode.ruler.push('quotes', rule);
|
md.block.bbcode.ruler.push('quotes', rule);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
require_dependency 'url_helper'
|
require_dependency 'url_helper'
|
||||||
require_dependency 'pretty_text'
|
require_dependency 'pretty_text'
|
||||||
|
require_dependency 'quote_comparer'
|
||||||
|
|
||||||
class CookedPostProcessor
|
class CookedPostProcessor
|
||||||
include ActionView::Helpers::NumberHelper
|
include ActionView::Helpers::NumberHelper
|
||||||
@ -33,6 +34,7 @@ class CookedPostProcessor
|
|||||||
DiscourseEvent.trigger(:before_post_process_cooked, @doc, @post)
|
DiscourseEvent.trigger(:before_post_process_cooked, @doc, @post)
|
||||||
post_process_oneboxes
|
post_process_oneboxes
|
||||||
post_process_images
|
post_process_images
|
||||||
|
post_process_quotes
|
||||||
keep_reverse_index_up_to_date
|
keep_reverse_index_up_to_date
|
||||||
optimize_urls
|
optimize_urls
|
||||||
update_post_image
|
update_post_image
|
||||||
@ -90,6 +92,24 @@ class CookedPostProcessor
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def post_process_quotes
|
||||||
|
@doc.css("aside.quote").each do |q|
|
||||||
|
post_number = q['data-post']
|
||||||
|
topic_id = q['data-topic']
|
||||||
|
if topic_id && post_number
|
||||||
|
comparer = QuoteComparer.new(
|
||||||
|
topic_id.to_i,
|
||||||
|
post_number.to_i,
|
||||||
|
q.css('blockquote').text
|
||||||
|
)
|
||||||
|
|
||||||
|
if comparer.modified?
|
||||||
|
q['class'] = ((q['class'] || '') + " quote-modified").strip
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def add_large_image_placeholder!(img)
|
def add_large_image_placeholder!(img)
|
||||||
url = img["src"]
|
url = img["src"]
|
||||||
|
|
||||||
|
@ -73,12 +73,12 @@ function __lookupAvatar(p) {
|
|||||||
return __utils.avatarImg({size: "tiny", avatarTemplate: __helpers.avatar_template(p) }, __getURL);
|
return __utils.avatarImg({size: "tiny", avatarTemplate: __helpers.avatar_template(p) }, __getURL);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __formatUsername(u) {
|
function __formatUsername(username) {
|
||||||
return __helpers.format_username(u);
|
return __helpers.format_username(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __lookupPrimaryUserGroup(p) {
|
function __lookupPrimaryUserGroup(username) {
|
||||||
return __helpers.lookup_primary_user_group(p);
|
return __helpers.lookup_primary_user_group(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __getCurrentUser(userId) {
|
function __getCurrentUser(userId) {
|
||||||
|
24
lib/quote_comparer.rb
Normal file
24
lib/quote_comparer.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
class QuoteComparer
|
||||||
|
def self.whitespace
|
||||||
|
" \t\r\n".freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(topic_id, post_number, text)
|
||||||
|
@topic_id = topic_id
|
||||||
|
@post_number = post_number
|
||||||
|
@text = text
|
||||||
|
@parent_post = Post.where(topic_id: @topic_id, post_number: @post_number).first
|
||||||
|
end
|
||||||
|
|
||||||
|
# This algorithm is far from perfect, but it follows the Discourse
|
||||||
|
# philosophy of "catch the obvious cases, leave moderation for the
|
||||||
|
# complicated ones"
|
||||||
|
def modified?
|
||||||
|
return true if @text.blank? || @parent_post.blank?
|
||||||
|
|
||||||
|
parent_text = Nokogiri::HTML::fragment(@parent_post.cooked).text.delete(QuoteComparer.whitespace)
|
||||||
|
text = @text.delete(QuoteComparer.whitespace)
|
||||||
|
|
||||||
|
!parent_text.include?(text)
|
||||||
|
end
|
||||||
|
end
|
@ -778,4 +778,38 @@ describe CookedPostProcessor do
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "quote processing" do
|
||||||
|
let(:cpp) { CookedPostProcessor.new(cp) }
|
||||||
|
let(:pp) { Fabricate(:post, raw: "This post is ripe for quoting!") }
|
||||||
|
|
||||||
|
context "with an unmodified quote" do
|
||||||
|
let(:cp) do
|
||||||
|
Fabricate(
|
||||||
|
:post,
|
||||||
|
raw: "[quote=\"#{pp.user.username}, post: #{pp.post_number}, topic:#{pp.topic_id}]\nripe for quoting\n[/quote]\ntest"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not be marked as modified" do
|
||||||
|
cpp.post_process_quotes
|
||||||
|
expect(cpp.doc.css('aside.quote.quote-modified')).to be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a modified quote" do
|
||||||
|
let(:cp) do
|
||||||
|
Fabricate(
|
||||||
|
:post,
|
||||||
|
raw: "[quote=\"#{pp.user.username}, post: #{pp.post_number}, topic:#{pp.topic_id}]\nmodified\n[/quote]\ntest"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be marked as modified" do
|
||||||
|
cpp.post_process_quotes
|
||||||
|
expect(cpp.doc.css('aside.quote.quote-modified')).to be_present
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
41
spec/components/quote_comparer_spec.rb
Normal file
41
spec/components/quote_comparer_spec.rb
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
require 'quote_comparer'
|
||||||
|
|
||||||
|
describe QuoteComparer do
|
||||||
|
|
||||||
|
describe "#modified?" do
|
||||||
|
let(:post) { Fabricate(:post, raw: "This has **text** we _are_ matching") }
|
||||||
|
|
||||||
|
def qc(text)
|
||||||
|
QuoteComparer.new(post.topic_id, post.post_number, text)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for no post" do
|
||||||
|
expect(QuoteComparer.new(nil, nil, "test")).to be_modified
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for nil text" do
|
||||||
|
expect(qc(nil)).to be_modified
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for empty text" do
|
||||||
|
expect(qc("")).to be_modified
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns true for modified text" do
|
||||||
|
expect(qc("text is modified")).to be_modified
|
||||||
|
end
|
||||||
|
|
||||||
|
it "return false when the text matches exactly" do
|
||||||
|
expect(qc("This has text we are matching")).not_to be_modified
|
||||||
|
end
|
||||||
|
|
||||||
|
it "return false when there's a substring" do
|
||||||
|
expect(qc("text we are")).not_to be_modified
|
||||||
|
end
|
||||||
|
|
||||||
|
it "return false when there's extra space" do
|
||||||
|
expect(qc("\n\ntext we are \t")).not_to be_modified
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Reference in New Issue
Block a user