Implementing Markdown Support in Forums
Hi Chris,
I watched all of the videos in the Forum Series -- nice work!. I'm wondering if you're still planning on getting around to showing us how to implement support for Markdown within the forum posts. If not, can you point me in the direction of the gem you used?
Thanks, and happy holidays!
I ended up using Redcarpet to provide Markdown support in my forums. Here's my markdown()
helper function, if you're also using Redcarpet let me know if there are any configurations/extensions I should consider using:
def markdown(text)
render_options = {
# will remove from the output HTML tags inputted by user
filter_html: true,
# will insert <br /> tags in paragraphs where are newlines
# (ignored by default)
hard_wrap: true,
# hash for extra link options, for example 'nofollow'
link_attributes: { rel: 'nofollow', target: '_blank' }
# more
# will remove <img> tags from output
# no_images: true
# will remove <a> tags from output
# no_links: true
# will remove <style> tags from output
# no_styles: true
# generate links for only safe protocols
# safe_links_only: true
# and more ... (prettify, with_toc_data, xhtml)
}
renderer = Redcarpet::Render::HTML.new(render_options)
extensions = {
#will parse links without need of enclosing them
autolink: true,
# blocks delimited with 3 ` or ~ will be considered as code block.
# No need to indent. You can provide language name too.
# ```ruby
# block of code
# ```
fenced_code_blocks: true,
# will ignore standard require for empty lines surrounding HTML blocks
lax_spacing: true,
# will not generate emphasis inside of words, for example no_emph_no
no_intra_emphasis: true,
# will parse strikethrough from ~~, for example: ~~bad~~
strikethrough: true,
# will parse superscript after ^, you can wrap superscript in ()
superscript: true,
# will require a space after # in defining headers
space_after_headers: true
}
Redcarpet::Markdown.new(renderer, extensions).render(text).html_safe
end
Great post. Redcarpet is great and I like that you've got notes for all the different options.
Also check out this episode: https://gorails.com/episodes/markdown-emoji-with-html-pipeline-gem
I really like Redcarpet, but html-pipeline lets you add in your own filters so you can add things like Emoji and build your own for custom things (like maybe you want to detect Youtube URLs and embed them).
Ahh, I didn't notice it because I was looking for the title of the video to be prefixed with "Forum Series Part X". I'll watch it now, thanks!