自定义转换后如何将内容传递给jekyll默认转换器? [英] how to pass content to jekyll default converter after custom conversion?

查看:61
本文介绍了自定义转换后如何将内容传递给jekyll默认转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a jekyll plugin which do something on markdown files first and passing the content back to the default converter

例如,

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

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

Right now, the closest thing that I could get it

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

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

But all the highlighting codes are losing color...and I suspect there are other problems...

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

My question is: what is a correct way to invoke the default converter?

推荐答案

方法如下:

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中选择.

Basically, you were right in using Jekyll::Converters::Markdown, but you need not specify KramdownParser, as your chosen parser will be automatically chosen from Jekyll::Site when you pass @config into the constructor.

这篇关于自定义转换后如何将内容传递给jekyll默认转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆