mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-05-03 09:25:18 -04:00
Link hashtags in Twitter onebox
This commit is contained in:
parent
b84ee29c43
commit
4491b1f06f
2 changed files with 29 additions and 3 deletions
|
@ -24,15 +24,15 @@ module Oneboxer
|
||||||
result['created_at'] =
|
result['created_at'] =
|
||||||
Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y")
|
Time.parse(result['created_at']).strftime("%I:%M%p - %d %b %y")
|
||||||
|
|
||||||
result['text'] = link_urls_and_handles_in result['text']
|
result['text'] = link_all_the_things_in result['text']
|
||||||
|
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def link_urls_and_handles_in(text)
|
def link_all_the_things_in(text)
|
||||||
link_handles_in link_urls_in(text)
|
link_hashtags_in link_handles_in link_urls_in(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_urls_in(text)
|
def link_urls_in(text)
|
||||||
|
@ -55,6 +55,19 @@ module Oneboxer
|
||||||
text
|
text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def link_hashtags_in(text)
|
||||||
|
text.scan(/\s#(\w+)/).flatten.uniq.each do |hashtag|
|
||||||
|
text.gsub!("##{hashtag}", [
|
||||||
|
"<a href='https://twitter.com/search?q=%23#{hashtag}' ",
|
||||||
|
"target='_blank'>",
|
||||||
|
"##{hashtag}",
|
||||||
|
"</a>"
|
||||||
|
].join)
|
||||||
|
end
|
||||||
|
|
||||||
|
text
|
||||||
|
end
|
||||||
|
|
||||||
def tweet_for(id)
|
def tweet_for(id)
|
||||||
request = Net::HTTP::Get.new(tweet_uri_for id)
|
request = Net::HTTP::Get.new(tweet_uri_for id)
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,19 @@ describe Oneboxer::TwitterOnebox do
|
||||||
].join)
|
].join)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when the text contains a hashtag' do
|
||||||
|
let(:text) { 'No secrets. #NSA' }
|
||||||
|
|
||||||
|
it 'wraps each hashtag in a link' do
|
||||||
|
expect(subject.parse(data)['text']).to eq([
|
||||||
|
"No secrets. ",
|
||||||
|
"<a href='https://twitter.com/search?q=%23NSA' target='_blank'>",
|
||||||
|
"#NSA",
|
||||||
|
"</a>"
|
||||||
|
].join)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue