【问题标题】:Is it possible to extend Redcarpet to auto embed youtube videos from a link?是否可以将 Redcarpet 扩展为从链接自动嵌入 youtube 视频?
【发布时间】:2012-09-08 21:20:07
【问题描述】:

是否可以使用Redcarpet Markdown 库自动嵌入来自 youtube 共享链接的 youtube 视频?如果是这样,有人对如何去做有任何想法吗? (我使用的是 Ruby/Rails 3.2)

【问题讨论】:

    标签: ruby-on-rails ruby markdown redcarpet


    【解决方案1】:

    当然可以,你可以重写 .to_html 方法,但你可能甚至不需要扩展它,你可以这样做;

    # 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 ")
    

    这将用您的自定义 html 替换 [youtube YOUTUBE_ID] 的任何实例。

    【讨论】:

    • 谢谢亚当,虽然我更多地考虑像 auto_html github.com/dejan/auto_html 这样的东西,其中转换的是 url 而不是标签。因此,我正在考虑将 auto_html 与 Redcarpet 一起使用。
    • 太好了,那会奏效。您也可以将上面的正则表达式更改为 @html.gsub(/http:\/\/www\.youtube\.com\/watch\?v=(.*?)\s/, "....actual html $1")
    • 我在做类似的事情时发现了这个。我只是想指出我需要转义正则表达式中的括号。 /\[youtube\s+(.*?)\]/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2012-03-20
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    相关资源
    最近更新 更多