class CustomMarkdownRenderer < CommonMarker::HtmlRenderer YOUTUBE_REGEX = %r{https?://(?:www\.)?(?:youtube\.com/watch\?v=|youtu\.be/)([^&/]+)} VIMEO_REGEX = %r{https?://(?:www\.)?vimeo\.com/(\d+)} MP4_REGEX = %r{https?://(?:www\.)?.+\.(mp4)} def text(node) content = node.string_content if content.include?('^') split_content = parse_sup(content) out(split_content.join) else out(escape_html(content)) end end def link(node) render_embedded_content(node) || super end private def render_embedded_content(node) link_url = node.url youtube_match = link_url.match(YOUTUBE_REGEX) if youtube_match out(make_youtube_embed(youtube_match)) return true end vimeo_match = link_url.match(VIMEO_REGEX) if vimeo_match out(make_vimeo_embed(vimeo_match)) return true end mp4_match = link_url.match(MP4_REGEX) if mp4_match out(make_video_embed(link_url)) return true end false end def parse_sup(content) content.split(/(\^[^\^]+\^)/).map do |segment| if segment.start_with?('^') && segment.end_with?('^') "#{escape_html(segment[1..-2])}" else escape_html(segment) end end end def make_youtube_embed(youtube_match) video_id = youtube_match[1] %( ) end def make_vimeo_embed(vimeo_match) video_id = vimeo_match[1] %( ) end def make_video_embed(link_url) %( ) end end