This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 41
FIX: Ignore captions and quotes when detecting locale and update prompts #1483
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseAi | ||
| module Translation | ||
| class PostDetectionText | ||
| def self.get_text(post) | ||
| return if post.blank? | ||
| cooked = post.cooked | ||
| return if cooked.blank? | ||
|
|
||
| doc = Nokogiri::HTML5.fragment(cooked) | ||
| original = doc.text.strip | ||
|
|
||
| # quotes and blockquotes | ||
| doc.css("blockquote, aside.quote").remove | ||
| # image captions | ||
| doc.css(".lightbox-wrapper").remove | ||
|
|
||
| necessary = doc.text.strip | ||
|
|
||
| # oneboxes (external content) | ||
| doc.css("aside.onebox").remove | ||
| # code blocks | ||
| doc.css("code, pre").remove | ||
| # hashtags | ||
| doc.css("a.hashtag-cooked").remove | ||
| # emoji | ||
| doc.css("img.emoji").remove | ||
| # mentions | ||
| doc.css("a.mention").remove | ||
|
|
||
| preferred = doc.text.strip | ||
|
|
||
| return preferred if preferred.present? | ||
| return necessary if necessary.present? | ||
| original | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| describe DiscourseAi::Translation::PostDetectionText do | ||
| describe ".get_text" do | ||
| let(:post) { Fabricate.build(:post) } | ||
|
|
||
| it "returns nil when post is nil" do | ||
| expect(described_class.get_text(nil)).to be_nil | ||
| end | ||
|
|
||
| it "returns nil when post.cooked is nil" do | ||
| post.cooked = nil | ||
| expect(described_class.get_text(post)).to be_nil | ||
| end | ||
|
|
||
| it "handles simple text" do | ||
| post.cooked = "<p>Hello world</p>" | ||
| expect(described_class.get_text(post)).to eq("Hello world") | ||
| end | ||
|
|
||
| it "removes quotes" do | ||
| post.cooked = "<p>Hello </p><blockquote><p>Quote</p></blockquote><p>World</p>" | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "removes Discourse quotes" do | ||
| post.cooked = '<p>Hello </p><aside class="quote"><p>Quote</p></aside><p>World</p>' | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "removes image captions" do | ||
| post.cooked = '<p>Hello </p><div class="lightbox-wrapper">Caption text</div><p>World</p>' | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "removes oneboxes" do | ||
| post.cooked = '<p>Hello </p><aside class="onebox">Onebox content</aside><p>World</p>' | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "removes code blocks" do | ||
| post.cooked = "<p>Hello </p><pre><code>Code block</code></pre><p>World</p>" | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "removes hashtags" do | ||
| post.cooked = '<p>Hello </p><a class="hashtag-cooked">#hashtag</a><p>World</p>' | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "removes emoji" do | ||
| post.cooked = '<p>Hello </p><img class="emoji" alt=":smile:" title=":smile:"><p>World</p>' | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "removes mentions" do | ||
| post.cooked = '<p>Hello </p><a class="mention">@user</a><p>World</p>' | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
|
|
||
| it "falls back to necessary text when preferred is empty" do | ||
| post.cooked = '<aside class="quote">Quote</aside><a class="mention">@user</a>' | ||
| expect(described_class.get_text(post)).to eq("@user") | ||
| end | ||
|
|
||
| it "falls back to cooked when all filtering removes all content" do | ||
| post.cooked = "<blockquote>Quote</blockquote>" | ||
| expect(described_class.get_text(post)).to eq("Quote") | ||
| end | ||
|
|
||
| it "handles complex nested content correctly" do | ||
| post.cooked = | ||
| '<p>Hello </p><div class="lightbox-wrapper"><p>Image caption</p><img src="test.jpg"></div><blockquote><p>Quote text</p></blockquote><p>World</p><pre><code>Code block</code></pre><a class="mention">@user</a>' | ||
| expect(described_class.get_text(post)).to eq("Hello World") | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would group stuff in constants, to have only two remove calls.
I don't think the comments are adding much:
# emoji=>img.emoji. But if you still think they are important you can format like this:and then call:
doc.css(*NECESSARY_SELECTORS).removeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also add that necessary/preferred is not very clear. I don't really know why they are necessary or preferred, this is where we would need a comment IMO.