Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/services/discourse_translator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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").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)
Expand Down
5 changes: 5 additions & 0 deletions spec/services/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class EmptyTranslator < DiscourseTranslator::Base
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
end

it "strips anchor tags" do
post.cooked = "<a href='http://cat.com/image.png' />"
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
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)
Expand Down
Loading