2022-01-16 13:13:41 -08:00
|
|
|
# frozen_string_literal: true
|
2015-01-29 07:45:58 -08:00
|
|
|
|
2022-01-16 13:13:41 -08:00
|
|
|
class TwitteredMarkdown < Redcarpet::Render::StripDown
|
2015-01-29 07:45:58 -08:00
|
|
|
def preprocess(text)
|
|
|
|
wrap_mentions(text)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wrap_mentions(text)
|
2022-01-16 13:13:41 -08:00
|
|
|
text.gsub(/(^|\s)@([a-zA-Z0-9_]{1,16})/) do
|
|
|
|
local_user = User.find_by(screen_name: $2)
|
2015-01-29 07:45:58 -08:00
|
|
|
if local_user.nil?
|
|
|
|
"#{$1}#{$2}"
|
|
|
|
else
|
|
|
|
service = local_user.services.where(type: "Services::Twitter").first
|
|
|
|
if service.nil?
|
|
|
|
"#{$1}#{$2}"
|
|
|
|
else
|
|
|
|
"#{$1}@#{service.nickname}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|