3

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)

A4J
  • 879
  • 10
  • 24

1 Answers1

5

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.

ADAM
  • 3,903
  • 4
  • 29
  • 45
  • Thanks Adam, tho I was thinking more along the lines of something like auto_html https://github.com/dejan/auto_html where a url is converted rather than a tag. Hence I'm considering using auto_html alongside Redcarpet. – A4J Sep 09 '12 at 21:11
  • Great, that would work. You could probably also just change the regex above to something like `@html.gsub(/http:\/\/www\.youtube\.com\/watch\?v=(.*?)\s/, "....actual html $1")` – ADAM Sep 11 '12 at 08:33
  • 1
    I just found this while working on something similar. I just wanted to point out that I needed to escape the brackets in my regexp. `/\[youtube\s+(.*?)\]/` – Scott Martin Sep 13 '13 at 21:19