diff --git a/app/helpers/markdown_helper.rb b/app/helpers/markdown_helper.rb new file mode 100644 index 00000000..169900e5 --- /dev/null +++ b/app/helpers/markdown_helper.rb @@ -0,0 +1,21 @@ +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: true) + Sanitize.fragment(md.render(content), EVIL_TAGS).html_safe + end +end \ No newline at end of file diff --git a/app/services/flavored_markdown.rb b/app/services/flavored_markdown.rb new file mode 100644 index 00000000..e88f0453 --- /dev/null +++ b/app/services/flavored_markdown.rb @@ -0,0 +1,26 @@ +class FlavoredMarkdown < Redcarpet::Render::HTML + include Rails.application.routes.url_helpers + + def preprocess(text) + wrap_mentions(text) + end + + def wrap_mentions(text) + text.gsub! /(^|\s)(@\w+)/ do + "#{$1}[#{$2}](#{show_user_profile_path $2.tr('@', '')})" + end + text + end + + def header(text, _header_level) + paragraph text + end + + def paragraph(text) + "

#{text}

" + end + + def raw_html(raw_html) + Rack::Utils.escape_html raw_html + end +end \ No newline at end of file diff --git a/app/views/shared/_answerbox.html.haml b/app/views/shared/_answerbox.html.haml index 8a53cdaa..0fc42e3b 100644 --- a/app/views/shared/_answerbox.html.haml +++ b/app/views/shared/_answerbox.html.haml @@ -25,14 +25,14 @@ .panel-body %p - if @display_all.nil? - = a.content[0..255] + = markdown a.content[0..255] - if a.content.length > 255 [...] %p %a.btn.btn-primary{href: show_user_answer_path(a.user.screen_name, a.id)} Read the entire answer - else - = a.content + = markdown a.content - if @user.nil? .row .col-md-6.col-sm-4.col-xs-7.text-left.text-muted