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

前端 未结 9 1911
灰色年华
灰色年华 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 09:49

    Fenced blocks were introduced with Redcarpet 2. Jekyll now supports Redcarpet 2.

    As an aside I am using Redcarpet with Rouge until Kramdown support is available.

    In addition some people prefer Nanoc to Jekyll.

    0 讨论(0)
  • 2020-12-04 09:50

    So I ran into this problem as well and after banging my head around a lot of places finally realised with official redcarpet2 support in Jekyll this is pretty simple. Write this in your _config.yml

    # Conversion
    markdown: redcarpet
    highlighter: pygments
    redcarpet:
      extensions: ["no_intra_emphasis", "fenced_code_blocks", "autolink", "strikethrough", "superscript"]
    

    Make sure that you have pygments css file and it is included. THIS STEP IS IMPORTANT.

    You can read my blog post http://blog.championswimmer.in/2015/10/jekyllsyntax-highlighting-in-github-favoured-markdown-codeblocks/ for details.

    0 讨论(0)
  • 2020-12-04 09:54

    Step 1. Install Redcarpet.

    gem install redcarpet
    

    Step 2. Update the build settings in your _config.yaml like this.

    # Build settings
    #markdown: kramdown
    markdown: redcarpet
    
    0 讨论(0)
  • 2020-12-04 09:54

    You can also use the triple-tilde syntax:

    ~~~ruby
    class Base
      def two
        1 + 1
      end
    end
    ~~~
    

    which is supported by Kramdown (Jekyll).

    0 讨论(0)
  • 2020-12-04 09:55

    Redcarpet is integrated integrated into Jekyll by default and code highlighting will function as expected.

    For older Jekyll blogs:

    1. Install redcarpet gem:

      gem install redcarpet

    2. Update _config.yaml

      markdown: redcarpet
      

    For reference and further info see:

    Closed Github Issue

    Updated Jekyll Codebase

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

    Alternate solution

    Markdown allows HTML, so if you don't mind adding a bit of JS, you could do this:

    ## A section
    
    Here is some Ruby code.
    
    <pre>
      <code class="ruby">
        puts "hello"
      </code>
    </pre>
    

    Then you could use Highlight.js (documentation here) to add highlighting based on that class.

    It's not an ideal solution, but it should work with any Markdown parser.

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