How can I number the code lines which are highlighted using pygments in Jekyll?
Asked
Active
Viewed 6,060 times
1 Answers
36
According to the Liquid Extensions wiki page of the Jekyll documentation, the highlight
Liquid tag has an optional second parameter, which may have the value linenos
to turn on line numbering:
{% highlight language linenos %}
your code here
{% endhighlight %}
Use it with caution. With linenos
the line numbers are actually inserted in the code's text, so will be impossible to copy the code block without them. (This could be solved by letting the visitor to $('.lineno').toggle()
the line numbers' visibility. Works in Firefox, not sure if is portable.)
Update: Better use linenos=table
:
{% highlight language linenos=table %}
your code here
{% endhighlight %}
That will place the code in a table
with two cells: first td
all the line numbers, second td
the code itself. This makes possible to select only the code, without the line numbers.

manatwork
- 1,689
- 1
- 28
- 31
-
1https://github.com/mojombo/jekyll/blob/master/lib/jekyll/tags/highlight.rb#L23 This makes me think there is a way to make them not inline if I can find the right keyword – Usman Ismail Jun 19 '12 at 13:39
-
Great question. It made me do some research on Pygments side. Updated the answer. – manatwork Jun 19 '12 at 14:05
-
Now just have to fix css and I am all set. Thanks for your help – Usman Ismail Jun 19 '12 at 15:07
-
I've tried that way but I get errors in Maruku due table not closed, you know anything about that @manatwork? – alex88 May 12 '13 at 18:29
-
Sorry @alex88, not met such error. But I experienced strange errors with Maruku, so I use [kramdown](http://kramdown.rubyforge.org/) instead as it seems to be much more stable. – manatwork May 13 '13 at 06:52
-
@manatwork thanks for the reply, btw yeah I've got it solved too changing markdown parser ;) Thanks again! – alex88 May 13 '13 at 07:42
-
You can prevent the user from selecting line numbers using CSS, rather than the table format, http://stackoverflow.com/a/4407335/85662. – WhatIsHeDoing May 15 '17 at 15:13