how to pass content to jekyll default converter after custom conversion?

邮差的信 提交于 2019-11-30 18:27:14

问题


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

For example,

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?


回答1:


Here's how to do it:

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

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.



来源:https://stackoverflow.com/questions/22761691/how-to-pass-content-to-jekyll-default-converter-after-custom-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!