Skip to content

Instantly share code, notes, and snippets.

@adrianvalenz
Last active April 30, 2017 08:06
Show Gist options
  • Save adrianvalenz/c870fd19c984dbe768a754c2cc0c4351 to your computer and use it in GitHub Desktop.
Save adrianvalenz/c870fd19c984dbe768a754c2cc0c4351 to your computer and use it in GitHub Desktop.
[Rails] Markdown support with Code Pygmentation and Embeddable Videos

Some notes

When embedding a YouTube video, just paste the link, but you need to remove a few characters.

https://www.youtube.com/watch?v=NvX9Dmxixaw

  • Remove the 's' from the http protocal.
  • Remove the 'www.'
  • Remove the '.com'
  • Remove the 'watch?v='

So you are just left with:

`http://youtube/NvX9Dmxixaw

# ./app/helper/application_helper.rb
module ApplicationHelper
require './lib/helpers/markdown_renderer_with_special_links'
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, lexer: language)
end
end
def markdown(content)
renderer = HTMLwithPygments.new
options = {
autolink: true,
filter_html: true,
hard_wrap: true,
link_attributes: { rel: 'nofollow', target: "_blank" },
no_intra_emphasis: true,
disable_indented_code_blocks: true,
fenced_code_blocks: true,
lax_html_blocks: true,
strikethrough: true,
superscript: true
}
Redcarpet::Markdown.new(renderer, options).render(content).html_safe
@markdown ||= Redcarpet::Markdown.new(MarkdownRendererWithSpecialLinks, autolink: true, space_after_headers: true, fenced_code_blocks: true)
@markdown.render(content).html_safe
end
end
# example file of index displaying posts, written in HAML
- @posts.each do |post|
%article.a-blog-post
%h1= link_to post.title, post
%p= markdown truncate post.content, length:300
# Just add 'markdown' before your 'post.content' or whatever you want formatted in Markdown
# allows code pygmentation
gem 'pygments.rb'
# allows markdown functionality
gem 'redcarpet'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment