From a72894e81c435caeaae1a1564fe4f2b1a6ed0fc5 Mon Sep 17 00:00:00 2001 From: Plastikmensch Date: Fri, 6 Oct 2023 22:31:23 +0200 Subject: [PATCH 1/4] Fix mentions being formatted with Markdown Prevents usernames with underscores being formatted with underline. Signed-off-by: Plastikmensch --- app/lib/advanced_text_formatter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/lib/advanced_text_formatter.rb b/app/lib/advanced_text_formatter.rb index 3ba4c92be37585..8aa4a01c11066e 100644 --- a/app/lib/advanced_text_formatter.rb +++ b/app/lib/advanced_text_formatter.rb @@ -98,7 +98,8 @@ def rewrite private def format_markdown(html) - html = markdown_formatter.render(html) + # Force escape usernames in mentions before formatting + html = markdown_formatter.render(html.gsub(Account::MENTION_RE) { |re| re.gsub('_', '\\_') }) html.delete("\r").delete("\n") end From 4eb83233b6b714aac3863e5afec6f5b2e312a78f Mon Sep 17 00:00:00 2001 From: Plastikmensch Date: Fri, 6 Oct 2023 22:32:06 +0200 Subject: [PATCH 2/4] Add spec Test that usernames with underscores get linked correctly Signed-off-by: Plastikmensch --- spec/lib/advanced_text_formatter_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/lib/advanced_text_formatter_spec.rb b/spec/lib/advanced_text_formatter_spec.rb index f92385219615fe..03cd229bf94551 100644 --- a/spec/lib/advanced_text_formatter_spec.rb +++ b/spec/lib/advanced_text_formatter_spec.rb @@ -75,6 +75,15 @@ it 'creates a mention link' do expect(subject).to include '@alice' end + + context 'when username contains underscores' do + let(:preloaded_accounts) { [Fabricate(:account, username: '_bob_')] } + let(:text) { '@_bob_' } + + it 'creates a mention link' do + expect(subject).to include '@_bob_' + end + end end context 'with text containing unlinkable mentions' do From 518a76a55385fd4bef1fff58d569dac0900b4a86 Mon Sep 17 00:00:00 2001 From: Plastikmensch Date: Mon, 25 Nov 2024 16:20:08 +0100 Subject: [PATCH 3/4] Fix custom emojis and hashtags being formatted with Markdown Prevents custom emojis and hashtags from being underlined and therefore broken. Signed-off-by: Plastikmensch --- app/lib/advanced_text_formatter.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/lib/advanced_text_formatter.rb b/app/lib/advanced_text_formatter.rb index 8aa4a01c11066e..0d5ddf9731f281 100644 --- a/app/lib/advanced_text_formatter.rb +++ b/app/lib/advanced_text_formatter.rb @@ -98,8 +98,9 @@ def rewrite private def format_markdown(html) - # Force escape usernames in mentions before formatting - html = markdown_formatter.render(html.gsub(Account::MENTION_RE) { |re| re.gsub('_', '\\_') }) + # Force escape underscores in mentions, custom emojis and hashtags before formatting + html = html.gsub(/#{Account::MENTION_RE}|#{CustomEmoji::SCAN_RE}|#{Tag::HASHTAG_RE}/o) { |re| re.gsub('_', '\\_') } + html = markdown_formatter.render(html) html.delete("\r").delete("\n") end From d1acc6e6bc1664e81c71ae9862244d6ce266300e Mon Sep 17 00:00:00 2001 From: Plastikmensch Date: Mon, 25 Nov 2024 16:21:05 +0100 Subject: [PATCH 4/4] Adjust spec Test for hashtags with underscores. Custom emojis are not tested for in this file and I'm not sure how best to add that. Signed-off-by: Plastikmensch --- spec/lib/advanced_text_formatter_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/lib/advanced_text_formatter_spec.rb b/spec/lib/advanced_text_formatter_spec.rb index 03cd229bf94551..465f508b9dd427 100644 --- a/spec/lib/advanced_text_formatter_spec.rb +++ b/spec/lib/advanced_text_formatter_spec.rb @@ -281,6 +281,14 @@ end end + context 'with text containing a hashtag with underscores' do + let(:text) { '#_hashtag_' } + + it 'creates a hashtag link' do + expect(subject).to include '/tags/_hashtag_" class="mention hashtag" rel="tag">#_hashtag_' + end + end + context 'with text with a stand-alone xmpp: URI' do let(:text) { 'xmpp:user@instance.com' }