diff --git a/app/services/discourse_translator/base.rb b/app/services/discourse_translator/base.rb index d9d9bcc4..1b5ca5f9 100644 --- a/app/services/discourse_translator/base.rb +++ b/app/services/discourse_translator/base.rb @@ -72,14 +72,17 @@ def self.language_supported?(detected_lang) private - def self.strip_img_for_detection(detection_text) + def self.strip_tags_for_detection(detection_text) html_doc = Nokogiri::HTML::DocumentFragment.parse(detection_text) html_doc.css("img").remove + html_doc.css("a.mention,a.lightbox").remove html_doc.to_html end def self.text_for_detection(topic_or_post) - strip_img_for_detection(get_text(topic_or_post).truncate(DETECTION_CHAR_LIMIT, omission: nil)) + strip_tags_for_detection( + get_text(topic_or_post).truncate(DETECTION_CHAR_LIMIT, omission: nil), + ) end def self.text_for_translation(topic_or_post) diff --git a/spec/services/base_spec.rb b/spec/services/base_spec.rb index bb806edb..2dc1f24e 100644 --- a/spec/services/base_spec.rb +++ b/spec/services/base_spec.rb @@ -41,6 +41,27 @@ class EmptyTranslator < DiscourseTranslator::Base expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("") end + it "strips @ mention anchor tags" do + post.cooked = "cat" + expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("") + end + + it "strips lightbox anchor tags" do + post.cooked = "" + expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("") + end + + it "leaves other anchor tags alone" do + cooked = <<~HTML +

+ + +

+ HTML + post.cooked = cooked + expect(DiscourseTranslator::Base.text_for_detection(post)).to eq(cooked) + end + it "truncates to DETECTION_CHAR_LIMIT of 1000" do post.cooked = "a" * 1001 expect(DiscourseTranslator::Base.text_for_detection(post).length).to eq(1000)