Search code examples
jekyllpygmentskramdown

Use kramdown with pygments in Jekyll


I want to use kramdown (with features such as fenced code blocks, inline attribute lists, header IDs) together with pygments for syntax highlighting (e.g. for LaTeX support, which is not available with CodeRay used by kramdown). Jekyll supports both kramdown and pygments, but apparently not the two together (unless I use Liquid tags which I would prefer not to).

I've also found some plugin snippets of how to make kramdown fenced code blocks spit pygments highlighted code, but unfortunately I don't know how to make that work.

I tried dumping all of the code from that site on some _plugins/krampygs.rb file, but then jekyll build complains about:

Generating... error: undefined method `matches'

If I supply some trivial matches and output_ext as instructed by Jekyll plugin docs, but then I don't know how to select this new converter for my .md files. Adding something like

markdown: MarkdownConverter

on my _config.yml only complains that this is not a valid option.

So, well, I restate my question: How can I use kramdown with pygments in Jekyll?

Solution

With the help of Matthias (below), I was able to prepare this Kramdown+Pygments plugin for Jekyll 1.x.


Solution

  • Author of "that site" here.

    It depends on the Jekyll version. For the version when the post was written, that was enough. At least Jekyll 1.x requires that matches is defined in the MarkdownConverter, like so:

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

    Another problem that appears with Jekyll 1.x is that every custom Markdown converter is ignored. I worked around this by by stating the output extension explicitly

    def output_ext(ext)
      ".html"
    end
    

    and tell Jekyll that to look for a bogus Markdown extension by setting

    markdown_ext: foo
    

    in _config.yml.