FIX: Replace Twitter handles one at a time (#15870)

Previously, all handles and hashtags were replaced in one go which could
result in a wrong result if a handle was a substring of another one.
This commit is contained in:
Dan Ungureanu
2022-02-09 13:54:02 +02:00
committed by GitHub
parent 5ff3a9c4bb
commit 1fb97f8bba
2 changed files with 29 additions and 19 deletions

View File

@ -95,28 +95,17 @@ class TwitterApi
protected
def link_handles_in(text)
text.scan(/(?:^|\s)@(\w+)/).flatten.uniq.each do |handle|
text.gsub!(/(?:^|\s)@#{handle}/, [
" <a href='https://twitter.com/#{handle}' target='_blank'>",
"@#{handle}",
"</a>"
].join)
end
text.strip
text.gsub(/(?:^|\s)@\w+/) do |match|
handle = match.strip[1..]
"<a href='https://twitter.com/#{handle}' target='_blank'>@#{handle}</a>"
end.strip
end
def link_hashtags_in(text)
text.scan(/(?:^|\s)#(\w+)/).flatten.uniq.each do |hashtag|
text.gsub!(/(?:^|\s)##{hashtag}/, [
" <a href='https://twitter.com/search?q=%23#{hashtag}' ",
"target='_blank'>",
"##{hashtag}",
"</a>"
].join)
end
text.strip
text.gsub(/(?:^|\s)#\w+/) do |match|
hashtag = match.strip[1..]
"<a href='https://twitter.com/search?q=%23#{hashtag}' target='_blank'>##{hashtag}</a>"
end.strip
end
def user_timeline_uri_for(screen_name)