added a new Markdown renderer (TwitteredMarkdown) which will be used for sharing to Twitter

This commit is contained in:
nilsding 2015-01-29 16:45:58 +01:00
parent ca7789750a
commit 0bf46a888f
3 changed files with 30 additions and 2 deletions

View File

@ -9,4 +9,9 @@ module MarkdownHelper
md = Redcarpet::Markdown.new(Redcarpet::Render::StripDown, MARKDOWN_OPTS) md = Redcarpet::Markdown.new(Redcarpet::Render::StripDown, MARKDOWN_OPTS)
CGI.unescape_html(Sanitize.fragment(md.render(content), EVIL_TAGS)).strip CGI.unescape_html(Sanitize.fragment(md.render(content), EVIL_TAGS)).strip
end 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 end

View File

@ -28,8 +28,8 @@ class Services::Twitter < Service
def prepare_tweet(answer) def prepare_tweet(answer)
# TODO: improve this. # TODO: improve this.
question_content = strip_markdown answer.question.content question_content = twitter_markdown answer.question.content
answer_content = strip_markdown answer.content answer_content = twitter_markdown answer.content
answer_url = show_user_answer_url( answer_url = show_user_answer_url(
id: answer.id, id: answer.id,
username: answer.user.screen_name, username: answer.user.screen_name,

View File

@ -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