【问题标题】:how to pass content to jekyll default converter after custom conversion?自定义转换后如何将内容传递给 jekyll 默认转换器?
【发布时间】:2014-03-31 12:35:09
【问题描述】:

我正在尝试编写一个 jekyll 插件,它首先对 markdown 文件执行某些操作并将内容传递回默认转换器

例如,

module Jekyll
    class RMarkdownConverter < Converter
        safe :false
        priority :high

        def matches(ext)
            ext =~ /^\.(md|markdown)$/i
        end

        def output_ext(ext)
            ".html"
        end

        def convert(content)
            # do something with content
            # then pass it back to default converter
        end
    end
end

现在,我能得到的最接近的东西

converter = Jekyll::Converters::Markdown::KramdownParser.new(@config)
converter.convert(content)

但是所有突出显示的代码都在失去颜色......我怀疑还有其他问题......

我的问题是: 调用默认转换器的正确方法是什么?

【问题讨论】:

    标签: ruby markdown jekyll kramdown


    【解决方案1】:

    这是怎么做的:

    module Jekyll
        class MyConverter < Converter
            safe :false
            priority :high
    
            def matches(ext)
                ext =~ /^\.(md|markdown)$/i
            end
    
            def output_ext(ext)
                ".html"
            end
    
            def convert(content)
                # do your own thing with the content
                content = my_own_thing(content)
    
                # Now call the standard Markdown converter
                site = Jekyll::Site.new(@config)
                mkconverter = site.getConverterImpl(Jekyll::Converters::Markdown)
                mkconverter.convert(content)
            end
        end
    end
    

    基本上,您使用Jekyll::Converters::Markdown 是正确的,但您无需指定KramdownParser,因为当您将@config 传递给构造函数时,您选择的解析器将自动从Jekyll::Site 中选择。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2021-07-11
    相关资源
    最近更新 更多