added Markdown to answers

This commit is contained in:
nilsding 2014-12-28 19:06:49 +01:00
parent 443ca0e663
commit 0e4d744478
3 changed files with 49 additions and 2 deletions

View File

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

View File

@ -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)
"<p>#{text}</p>"
end
def raw_html(raw_html)
Rack::Utils.escape_html raw_html
end
end

View File

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