how to pass content to jekyll default converter af

2020-03-05 21:12发布

问题:

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.