From 41bc94adf85957a9536984e114b6ff3d43da02f3 Mon Sep 17 00:00:00 2001 From: Plastikmensch Date: Fri, 6 Oct 2023 22:31:23 +0200 Subject: [PATCH 1/2] 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 cdf1e2d9cd9394..ab80f9453fff9e 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 5930fb7323bf981c7484705274b17a5d3f005e7d Mon Sep 17 00:00:00 2001 From: Plastikmensch Date: Fri, 6 Oct 2023 22:32:06 +0200 Subject: [PATCH 2/2] 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