惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

drew's dev blog

Hunting a production-only proxy bug in SvelteKit Artisanal Handcrafted Git Repositories Everything you need to know about Python 3.13 – JIT and GIL went up the hill How Postgres stores oversized values – let's raise a TOAST How Postgres stores data on disk – this one's a page turner Bumper Comtrade QuEST Rust Wrapper Dotfiles How long is a cucumber? 🥒 Jekyll KaTeX Block Using make and latexmk for easy LaTeX compilation Calculating the overlap of aerial photos Custom Jekyll plugins with GitHub Pages Calculating meters per pixel from aerial photographs Compiling zsh without root Coding cheatsheet
Proper line numbers with Jekyll
Drew Silcock · 2014-07-18 · via drew's dev blog

· 5 min read

By default, Jekyll uses the (excellent) Pygments syntax highlighter for code blocks. While this works well, the line numbers it produces are less than satisfactory.

Here’s the default lineno option, inline:

lineno=inline

This works, but has two main visual and practical problems:

  1. There is no visual separation between the line numbers and the code, causing them to visually become indistinct, and
  2. When trying to copy code from the codeblocks, the line numbers are included, annoyingly.

So what’s the alternative?

Well, Pygments has inbuilt the table option, which separates the code from the linenumbers, ostensibly fixing both of these problems. Let’s take a look:

lineno=table

Well, as you can see, this doesn’t really look good either. The main problems are:

  1. The size of the line number table is inconsistent between codeblocks, and
  2. The line numbers don’t align with the actual lines of code

So let’s get rid of the lineno option altogether, and get our beautiful but functional line numbers through CSS counters, as described in an article by Alex Peattie.

CSS File

Alex’s CSS is as follows:

1

pre {

2

counter-reset: line-numbering;

3

border: solid 1px #d9d9d9;

4

border-radius: 0;

5

background: #fff;

6

padding: 0;

7

line-height: 23px;

8

margin-bottom: 30px;

9

white-space: pre;

10

overflow-x: auto;

11

word-break: inherit;

12

word-wrap: inherit;

13

}

14

15

pre a::before {

16

content: counter(line-numbering);

17

counter-increment: line-numbering;

18

padding-right: 1em; /* space after numbers */

19

width: 25px;

20

text-align: right;

21

opacity: 0.7;

22

display: inline-block;

23

color: #aaa;

24

background: #eee;

25

margin-right: 16px;

26

padding: 2px 10px;

27

font-size: 13px;

28

-webkit-touch-callout: none;

29

-webkit-user-select: none;

30

-khtml-user-select: none;

31

-moz-user-select: none;

32

-ms-user-select: none;

33

user-select: none;

34

}

35

36

pre a:first-of-type::before {

37

padding-top: 10px;

38

}

39

40

pre a:last-of-type::before {

41

padding-bottom: 10px;

42

}

43

44

pre a:only-of-type::before {

45

padding: 10px;

46

}

Here’s what it produces after adding it to your syntax.css:

beautiful linenumbers

Note those important lines at the end of pre a::before:

1

-webkit-touch-callout: none;

2

-webkit-user-select: none;

3

-khtml-user-select: none;

4

-moz-user-select: none;

5

-ms-user-select: none;

6

user-select: none;

This tell the browser to ignore the line numbers when copying, solving one of our initial problems.

In addition, the background grey of #eee gives the visual distinction between line numbers and code that we were lacking from lineno=inline. And, of course, they align properly with the actual lines of code, unlike lineno=table.

On top of this, the padding gives the line numbers a consistent spacing and the solid border given by border: solid 1px #d9d9d9; gives the code a clear separation from the main text.

Lineanchors

It’s not quite that simple, though. These CSS counters need the lineanchors option to be given for each codeblock, or it ends up looking like this:

without lineanchors

To solve this, you can either just put lineanchors in every highlight Liquid tag, which is a bit annoying and can easily be forgotten, or you can use a global plugin to allow you to specify global Pygments options in your _config.yml. Such a plugin is available here, thanks to Dana Silver.

Using this plugin, you can simply specify as follows in your _config.yml:

1

pygments_options:

2

- lineanchors

Then you don’t need to put it in each codeblock tag and can forget about it!

Unfortuantely, GitHub Pages doesn’t allow for custom Jekyll plugins for security reasons, so unless you want to build the site locally and push the resulting html, you’re gonna have to stick to putting lineanchors in each tag. Up to you which you want to do.

Another problem I had, although I am unsure whether this problem is universal/reproducible, is that an annoying y-scroll bar appeared, even when there was no need for it. This is what it looked like:

annoying scroll bar

Now, I haven’t come all this way just to be bested by an annoying y-scroll bar, so I added in this bit of CSS to Alex’s code to get rid of it:

1

/* In pre { .. } */

2

overflow-y: hidden;

This is placed in pre { .. }, just after overflow-x: auto;.

After this, I finally had the beautiful line numbers that Pygments natively lacks.

Update: 5-8-14

Dana’s global configs Jekyll plugin is incompatible with the new Jekyll 2.2.0 release which both I and GitHub Pages are now using. So it looks like until I get round to sorting out why the plugin is incompatible, you’ll need to actually type highlight lang lineanchors for each individual code block. Annoying.

I always forget to do this, and could not be bothered to go through each codeblock in each blog post I’ve written, so here’s a simple bash script to replace all instances of the highlight lang Liquid tag with its lineanchors equivalent:

1

#!/bin/bash

2

3

# Adds lineanchors option to all codeblock Liquid tags

4

5

languages=( vim python perl ruby yaml css html bash cpp c )

6

posts="path/to/posts"

7

8

for lang in "${languages[@]}"

9

do

10

:

11

perl -pi -e "s/highlight $lang/highlight $lang lineanchors/g" $posts

12

done

Change $posts to correspond to the location of your blog posts, the contents of which you wish to replace. If you’re using more lexers, just add them into the languages array.