Syntax highlighting markdown code blocks in Jekyll (without using liquid tags)

前端 未结 9 1912
灰色年华
灰色年华 2020-12-04 09:19

It seems like syntax highlighting in Jekyll is limited to using liquid tags and pygments like so:

{% highlight bash %}
cd ~
{% endhighlight %}
相关标签:
9条回答
  • 2020-12-04 10:01

    In latest jekyll support code blocks, but if you use older version, you need to hack.

    How about below? Try to add below's file as your _plugin/triple-backtick.rb

    module Jekyll
      class MarkdownConverter
        alias :old_convert :convert
        def convert(content)
          content.gsub!(/(?:^|\n)```(\w*)\n(.*\n)```\n/m) do |text|
            cls = $1.empty? ? "prettyprint" : "prettyprint lang-#{$1}"
            "<pre class=\"#{cls}\"><code>#{$2}</code></pre>"
          end
          old_convert(content)
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-04 10:04

    I ended up switching to kramdown to parse markdown which comes with coderay for syntax highlighting. This has the benefit of being a pure ruby solution which works on heroku.

    0 讨论(0)
  • 2020-12-04 10:08

    I've described 2 alternative solutions to adding properly formatted code snippets to your Jekyll driven site. http://demisx.github.io/jekyll/2014/01/13/improve-code-highlighting-in-jekyll.html. They do not rely on 3-rd party plugins and compatible with free GitHub Pages hosting.

    0 讨论(0)
提交回复
热议问题