Skip to content

Instantly share code, notes, and snippets.

@erpe
Created October 22, 2015 17:53
Show Gist options
  • Save erpe/2b13ed827efa8a61bdae to your computer and use it in GitHub Desktop.
Save erpe/2b13ed827efa8a61bdae to your computer and use it in GitHub Desktop.
extend redcarpet with custom video tag
# create custom tags for redcarpet to insert youtube|vimeo iframes in
# your document.
# ::usage:: vid(youtube, 123456)
class VideoMarkdownRenderer < Redcarpet::Render::HTML
include ActionView::Helpers::AssetTagHelper
def preprocess(document)
# scans for: vid(provider, id)
# e.g. vid(youtube,12345)
vidregex = Regexp.compile(/\bvid\((\S+)\,\s*(\S+)\)\s*/)
document.to_enum(:scan, vidregex).map { Regexp.last_match }.each do |match|
document.gsub!(match.to_s, video_div(match[1],match[2]))
end
document
end
def video_div(provider, vid)
puts "video-div: looking for.."
content_tag(:div,
iframe_for(provider, vid), class: 'embed-responsive embed-responsive-16by9'
)
end
def iframe_for(provider, vid)
case provider
when 'youtube'
return content_tag(:iframe, nil,
width: '100%',
height: 'auto',
src: "//www.youtube.com/embed/#{vid}",
class: 'embed-responsive-item')
when 'vimeo'
return content_tag(:iframe, nil,
width: '100%',
height: 'auto',
src: "//player.vimeo.com/video/#{vid}",
class: 'embed-responsive-item')
end
end
end
# usage:
md = Redcarpet::Markdown.new(VideoMarkdownRenderer, hard_wrap: true)
md.render(document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment