From 97df239b4140714a3f5d905f3100e8319cf252db Mon Sep 17 00:00:00 2001 From: Robert George Date: Thu, 28 Sep 2023 14:09:50 -0700 Subject: [PATCH] Do not translate hashtags --- app/lib/text_formatter.rb | 2 +- app/services/translate_status_service.rb | 25 +++++++++++-------- .../services/translate_status_service_spec.rb | 16 ++++++------ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/lib/text_formatter.rb b/app/lib/text_formatter.rb index 581ee835b3f..55f695cc18e 100644 --- a/app/lib/text_formatter.rb +++ b/app/lib/text_formatter.rb @@ -98,7 +98,7 @@ class TextFormatter url = tag_url(hashtag) <<~HTML.squish - + HTML end diff --git a/app/services/translate_status_service.rb b/app/services/translate_status_service.rb index 9ad146ae7d3..74cbb59ce34 100644 --- a/app/services/translate_status_service.rb +++ b/app/services/translate_status_service.rb @@ -45,11 +45,11 @@ class TranslateStatusService < BaseService def source_texts texts = {} - texts[:content] = wrap_emoji_shortcodes(status_content_format(@status)) if @status.content.present? - texts[:spoiler_text] = wrap_emoji_shortcodes(html_escape(@status.spoiler_text)) if @status.spoiler_text.present? + texts[:content] = wrap_emoji_and_tags(status_content_format(@status)) if @status.content.present? + texts[:spoiler_text] = wrap_emoji_and_tags(html_escape(@status.spoiler_text)) if @status.spoiler_text.present? @status.preloadable_poll&.loaded_options&.each do |option| - texts[option] = wrap_emoji_shortcodes(html_escape(option.title)) + texts[option] = wrap_emoji_and_tags(html_escape(option.title)) end @status.media_attachments.each do |media_attachment| @@ -75,14 +75,14 @@ class TranslateStatusService < BaseService case source when :content - node = unwrap_emoji_shortcodes(translation.text) + node = unwrap_emoji_and_tags(translation.text) Sanitize.node!(node, Sanitize::Config::MASTODON_STRICT) status_translation.content = node.to_html when :spoiler_text - status_translation.spoiler_text = unwrap_emoji_shortcodes(translation.text).content + status_translation.spoiler_text = unwrap_emoji_and_tags(translation.text).content when Poll::Option status_translation.poll_options << Translation::Option.new( - title: unwrap_emoji_shortcodes(translation.text).content + title: unwrap_emoji_and_tags(translation.text).content ) when MediaAttachment status_translation.media_attachments << Translation::MediaAttachment.new( @@ -95,13 +95,18 @@ class TranslateStatusService < BaseService status_translation end - def wrap_emoji_shortcodes(text) - EmojiFormatter.new(text, @status.emojis, { raw_shortcode: true }).to_s + def wrap_emoji_and_tags(text) + html = EmojiFormatter.new(text, @status.emojis, { raw_shortcode: true }).to_s + fragment = Nokogiri::HTML.fragment(html) + fragment.css('.hashtag').each do |element| + element['translate'] = 'no' + end + fragment.to_s end - def unwrap_emoji_shortcodes(html) + def unwrap_emoji_and_tags(html) fragment = Nokogiri::HTML.fragment(html) - fragment.css('span[translate="no"]').each do |element| + fragment.css('span[translate="no"],.hashtag[translate="no"]').each do |element| element.remove_attribute('translate') element.replace(element.children) if element.attributes.empty? end diff --git a/spec/services/translate_status_service_spec.rb b/spec/services/translate_status_service_spec.rb index 5f6418f5d6f..b3b1284479c 100644 --- a/spec/services/translate_status_service_spec.rb +++ b/spec/services/translate_status_service_spec.rb @@ -203,7 +203,7 @@ RSpec.describe TranslateStatusService, type: :service do end end - describe '#wrap_emoji_shortcodes' do + describe '#wrap_emoji_and_tags' do before do service.instance_variable_set(:@status, status) end @@ -212,22 +212,22 @@ RSpec.describe TranslateStatusService, type: :service do let(:text) { ':highfive:' } it 'renders the emoji' do - html = service.send(:wrap_emoji_shortcodes, 'Hello :highfive:'.html_safe) - expect(html).to eq 'Hello :highfive:' + html = service.send(:wrap_emoji_and_tags, 'Hello :highfive: #hashtag'.html_safe) + expect(html).to eq 'Hello :highfive: #hashtag' end end end - describe '#unwrap_emoji_shortcodes' do + describe '#unwrap_emoji_and_tags' do describe 'string contains custom emoji' do it 'inserts the shortcode' do - fragment = service.send(:unwrap_emoji_shortcodes, '

Hello :highfive:!

') - expect(fragment.to_html).to eq '

Hello :highfive:!

' + fragment = service.send(:unwrap_emoji_and_tags, '

Hello :highfive:! #hashtag

') + expect(fragment.to_html).to eq '

Hello :highfive:! #hashtag

' end it 'preserves other attributes than translate=no' do - fragment = service.send(:unwrap_emoji_shortcodes, '

Hello :highfive:!

') - expect(fragment.to_html).to eq '

Hello :highfive:!

' + fragment = service.send(:unwrap_emoji_and_tags, '

Hello :highfive:! #hashtag

') + expect(fragment.to_html).to eq '

Hello :highfive:! #hashtag

' end end end