Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll: Fenced code blocks with line numbers

I'm trying to add line numbers to fenced code blocks in markdown with Jekyll.

I have tried using both kramdown and redcarpet and adding line_numbers: true to the _config.yml file (under the proper renderer config-block) but I can't seem to get line numbers on code blocks.

Solution: It turned out all I had to do was to switch back to kramdown (default markdown renderer) and start the code blocks with {% highlight <language> linenos %} and the code blocks gets line numbers.

like image 756
Michael Banzon Avatar asked Aug 05 '14 07:08

Michael Banzon


2 Answers

I used this great post. It worked for me. As for the line numbers use,

{% highlight <language> linenos %}
     <code>
 {% endhighlight %}

Check an example on my blog and see if this is what you want.

like image 76
ma08 Avatar answered Oct 15 '22 14:10

ma08


As I'm in love with pandoc I would recommend you to use the pandoc markdown compiler:

Add Pandoc

Install the pandoc compiler, e.g. on debian via sudo apt install pandoc.

Add this to your _config.yaml:

markdown: Pandoc
gems:
  - jekyll-pandoc

Then add the jekyll-pandoc gem to your Gemfile:

group :jekyll_plugins do
    # here are your other gems
    gem "jekyll-pandoc"
end

If you're having trouble refer to the official resources that I included at the end.

Use Pandoc linenumbers

When you have successfully set up jekyll-pandoc you can use it as easy as:

~~~~ {.java .numberLines startFrom="1"}
class MyClassPresentedWithLineNumbersViaPandoc {
  void lineNumberedFunction() {};
}
~~~~

This would enable java code highlighting and linenumbers that start counting at 1.

For an example of the default appearance have a look at this listing in my blog.

Official resources

jekyll-pandoc

pandoc

like image 41
Jan Avatar answered Oct 15 '22 16:10

Jan