diff --git a/app/labor/markdown_parser.rb b/app/labor/markdown_parser.rb
index b7b4eb773..677aaab09 100644
--- a/app/labor/markdown_parser.rb
+++ b/app/labor/markdown_parser.rb
@@ -203,23 +203,22 @@ class MarkdownParser
def wrap_mentions_with_links!(html)
html_doc = Nokogiri::HTML(html)
- # looks for node that isn't , , , and contains "@"
- html_doc.xpath('//*[not (self::code) and not(self::a) and contains(text(), "@")]').each do |node|
- # if the target node is a , or
, , and contains "@"
+ targets = html_doc.xpath('//html/body/*[not (self::code) and not(self::a) and contains(., "@")]').to_a
+
+ # A Queue system to look for and replace possible usernames
+ until targets.empty?
+ node = targets.shift
+
+ # only focus on portion of text with "@"
+ node.xpath("text()[contains(.,'@')]").each do |el|
+ el.replace(el.text.gsub(/\B@[a-z0-9_-]+/i) { |text| user_link_if_exists(text) })
end
+
+ # enqueue children that has @ in it's text
+ children = node.xpath('*[not(self::code) and not(self::a) and contains(., "@")]').to_a
+ targets.concat(children)
end
if html_doc.at_css("body")
diff --git a/spec/labor/markdown_parser_spec.rb b/spec/labor/markdown_parser_spec.rb
index 168527232..82a09f23d 100644
--- a/spec/labor/markdown_parser_spec.rb
+++ b/spec/labor/markdown_parser_spec.rb
@@ -81,12 +81,27 @@ RSpec.describe MarkdownParser do
expect(result).to include "@#{user.username} one two, @#{user.username}\n three four:
@#{user.username}