Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mentions, hashtags and custom emojis being formatted with Markdown #2433

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions app/lib/advanced_text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
private

def format_markdown(html)
# 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('_', '\\_') }
Plastikmensch marked this conversation as resolved.
Show resolved Hide resolved
html = markdown_formatter.render(html)
html.delete("\r").delete("\n")
end
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/advanced_text_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
it 'creates a mention link' do
expect(subject).to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
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 '<a href="https://cb6e6126.ngrok.io/@_bob_" class="u-url mention">@<span>_bob_</span></a></span>'
end
end
end

context 'with text containing unlinkable mentions' do
Expand Down Expand Up @@ -272,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">#<span>_hashtag_</span></a>'
end
end

context 'with text with a stand-alone xmpp: URI' do
let(:text) { 'xmpp:user@instance.com' }

Expand Down