diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index b385f696..590956ce 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -9,4 +9,9 @@ module MarkdownHelper md = Redcarpet::Markdown.new(Redcarpet::Render::StripDown, MARKDOWN_OPTS) CGI.unescape_html(Sanitize.fragment(md.render(content), EVIL_TAGS)).strip end + + def twitter_markdown(content) + md = Redcarpet::Markdown.new(TwitteredMarkdown, MARKDOWN_OPTS) + CGI.unescape_html(Sanitize.fragment(md.render(content), EVIL_TAGS)).strip + end end \ No newline at end of file diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index bd3b6ca8..243536c7 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -28,8 +28,8 @@ class Services::Twitter < Service def prepare_tweet(answer) # TODO: improve this. - question_content = strip_markdown answer.question.content - answer_content = strip_markdown answer.content + question_content = twitter_markdown answer.question.content + answer_content = twitter_markdown answer.content answer_url = show_user_answer_url( id: answer.id, username: answer.user.screen_name, diff --git a/app/services/twittered_markdown.rb b/app/services/twittered_markdown.rb new file mode 100644 index 00000000..aeadf428 --- /dev/null +++ b/app/services/twittered_markdown.rb @@ -0,0 +1,23 @@ +class TwitteredMarkdown < Redcarpet::Render::StripDown + + def preprocess(text) + wrap_mentions(text) + end + + def wrap_mentions(text) + text.gsub! /(^|\s)@([a-zA-Z0-9_]{1,16})/ do + local_user = User.find_by_screen_name($2) + 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 + text + end +end