Search code examples
ruby-on-railsrubymarkdownredcarpet

Is it possible to extend Redcarpet to auto embed youtube videos from a link?


Is it possible to use the Redcarpet Markdown library to automatically embed youtube videos from youtube share links? If so anyone got any ideas on how to go about it? (I'm using Ruby/Rails 3.2)


Solution

  • Sure it is, you can override the method .to_html, but you probably dont need to even extend it you could instead do something like this;

    # my regex is a little rusty
    @html = Redcarpet.new(@article.content).to_html
    @html = @html.gsub(/\[youtube\s+(.*?)\]/, "....actual html you want in here with youtube id \\1 ")
    

    That would replace any instace of [youtube YOUTUBE_ID] with your custom html.