diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb index 8d69fa7c..8a164411 100644 --- a/app/helpers/markdown_helper.rb +++ b/app/helpers/markdown_helper.rb @@ -1,21 +1,12 @@ module MarkdownHelper def markdown(content) - md = Redcarpet::Markdown.new(FlavoredMarkdown, - filter_html: true, - escape_html: true, - no_images: true, - no_styles: true, - safe_links_only: true, - xhtml: false, - hard_wrap: true, - no_intra_emphasis: true, - tables: true, - fenced_code_blocks: true, - autolink: true, - disable_indented_code_blocks: true, - strikethrough: true, - superscript: false) + md = Redcarpet::Markdown.new(FlavoredMarkdown, MARKDOWN_OPTS) Sanitize.fragment(md.render(content), EVIL_TAGS).html_safe end + + def strip_markdown(content) + md = Redcarpet::Markdown.new(Redcarpet::Render::StripDown, MARKDOWN_OPTS) + CGI.unescape_html Sanitize.fragment(md.render(content), EVIL_TAGS) + end end \ No newline at end of file diff --git a/app/models/services/twitter.rb b/app/models/services/twitter.rb index b8b33d47..bd3b6ca8 100644 --- a/app/models/services/twitter.rb +++ b/app/models/services/twitter.rb @@ -1,5 +1,6 @@ class Services::Twitter < Service include Rails.application.routes.url_helpers + include MarkdownHelper def provider "twitter" @@ -27,8 +28,8 @@ class Services::Twitter < Service def prepare_tweet(answer) # TODO: improve this. - question_content = answer.question.content - answer_content = answer.content + question_content = strip_markdown answer.question.content + answer_content = strip_markdown answer.content answer_url = show_user_answer_url( id: answer.id, username: answer.user.screen_name, diff --git a/config/initializers/redcarpet.rb b/config/initializers/redcarpet.rb new file mode 100644 index 00000000..86df114b --- /dev/null +++ b/config/initializers/redcarpet.rb @@ -0,0 +1,18 @@ +require 'redcarpet/render_strip' + +MARKDOWN_OPTS = { + filter_html: true, + escape_html: true, + no_images: true, + no_styles: true, + safe_links_only: true, + xhtml: false, + hard_wrap: true, + no_intra_emphasis: true, + tables: true, + fenced_code_blocks: true, + autolink: true, + disable_indented_code_blocks: true, + strikethrough: true, + superscript: false +} \ No newline at end of file