2020-07-06 09:04:30 -04:00
|
|
|
---
|
|
|
|
layout: default
|
2020-08-10 10:53:48 -04:00
|
|
|
title: Code with line numbers
|
|
|
|
parent: Code
|
|
|
|
grand_parent: UI Components
|
2020-09-14 12:43:59 -04:00
|
|
|
permalink: /docs/ui-components/code/line-numbers/
|
2020-07-06 09:04:30 -04:00
|
|
|
---
|
|
|
|
|
2020-09-14 12:46:26 -04:00
|
|
|
# Code snippets with line numbers
|
2020-07-07 13:34:35 -04:00
|
|
|
|
|
|
|
The default settings for HTML compression are incompatible with the HTML
|
|
|
|
produced by Jekyll (4.1.1 or earlier) for line numbers from highlighted code
|
|
|
|
-- both when using Kramdown code fences and when using Liquid highlight tags.
|
|
|
|
|
|
|
|
To avoid non-conforming HTML and unsatisfactory layout, HTML compression
|
|
|
|
can be turned off by using the following configuration option:
|
2020-07-06 09:04:30 -04:00
|
|
|
|
2020-07-07 13:54:31 -04:00
|
|
|
{% highlight yaml %}
|
2020-07-06 09:04:30 -04:00
|
|
|
compress_html:
|
|
|
|
ignore:
|
|
|
|
envs: all
|
2020-07-07 13:54:31 -04:00
|
|
|
{% endhighlight %}
|
2020-07-06 09:04:30 -04:00
|
|
|
|
2020-07-07 13:34:35 -04:00
|
|
|
When using Kramdown code fences, line numbers are turned on globally by the
|
|
|
|
following configuration option:
|
|
|
|
|
2020-07-07 13:54:31 -04:00
|
|
|
{% highlight yaml %}
|
2020-07-07 13:34:35 -04:00
|
|
|
kramdown:
|
|
|
|
syntax_highlighter_opts:
|
|
|
|
block:
|
2020-09-11 13:42:44 -04:00
|
|
|
line_numbers: true
|
2020-07-07 13:54:31 -04:00
|
|
|
{% endhighlight %}
|
2020-07-07 13:34:35 -04:00
|
|
|
|
|
|
|
Line numbers can then be suppressed locally using Liquid tags (_without_ the
|
|
|
|
`linenos` option) instead of fences:
|
|
|
|
|
2020-07-07 13:54:31 -04:00
|
|
|
{% highlight yaml %}
|
2020-07-07 13:34:35 -04:00
|
|
|
{% raw %}{% highlight some_language %}
|
|
|
|
Some code
|
|
|
|
{% endhighlight %}{% endraw %}
|
2020-07-07 13:54:31 -04:00
|
|
|
{% endhighlight %}
|
2020-07-07 13:34:35 -04:00
|
|
|
|
2020-09-14 12:46:26 -04:00
|
|
|
## Workarounds
|
2020-07-07 13:34:35 -04:00
|
|
|
|
|
|
|
To use HTML compression together with line numbers, all highlighted code
|
|
|
|
needs to be wrapped using one of the following workarounds.
|
|
|
|
(The variable name `some_var` can be changed to avoid clashes; it can also
|
|
|
|
be replaced by `code` -- but note that `code=code` cannot be removed).
|
|
|
|
|
2020-09-14 13:29:09 -04:00
|
|
|
### Code fences (three backticks)
|
2020-07-07 13:34:35 -04:00
|
|
|
|
2020-07-07 13:54:31 -04:00
|
|
|
{% highlight default %}
|
2020-07-07 13:34:35 -04:00
|
|
|
{% raw %}{% capture some_var %}
|
|
|
|
```some_language
|
|
|
|
Some code
|
|
|
|
```
|
|
|
|
{% endcapture %}
|
|
|
|
{% assign some_var = some_var | markdownify %}
|
|
|
|
{% include fix_linenos.html code=some_var %}{% endraw %}
|
2020-07-07 13:54:31 -04:00
|
|
|
{% endhighlight %}
|
2020-07-07 13:34:35 -04:00
|
|
|
|
2020-09-14 12:46:26 -04:00
|
|
|
### Liquid highlighting
|
2020-07-07 13:34:35 -04:00
|
|
|
|
2020-07-07 13:54:31 -04:00
|
|
|
{% highlight default %}
|
2020-07-07 13:34:35 -04:00
|
|
|
{% raw %}{% capture some_var %}
|
|
|
|
{% highlight some_language linenos %}
|
|
|
|
Some code
|
|
|
|
{% endhighlight %}
|
|
|
|
{% endcapture %}
|
|
|
|
{% include fix_linenos.html code=some_var %}{% endraw %}
|
2020-07-07 13:54:31 -04:00
|
|
|
{% endhighlight %}
|
2020-07-07 13:34:35 -04:00
|
|
|
|
|
|
|
_Credit:_ The original version of the above workaround was suggested by
|
|
|
|
Dmitry Hrabrov at
|
|
|
|
<https://github.com/penibelst/jekyll-compress-html/issues/71#issuecomment-188144901>.
|
|
|
|
|
2020-09-14 12:46:26 -04:00
|
|
|
## Examples
|
2020-07-07 13:34:35 -04:00
|
|
|
|
2020-09-14 13:29:09 -04:00
|
|
|
✅ Using code fences + workaround (will only show line numbers if enabled globally in `_config.yml`):
|
2020-07-07 13:34:35 -04:00
|
|
|
|
2020-09-14 13:24:10 -04:00
|
|
|
{% capture code_fence %}
|
2020-07-06 09:04:30 -04:00
|
|
|
```js
|
|
|
|
// Javascript code with syntax highlighting in fences
|
|
|
|
var fun = function lang(l) {
|
|
|
|
dateformat.i18n = require('./lang/' + l)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
2020-09-14 12:54:55 -04:00
|
|
|
{% endcapture %}
|
|
|
|
{% assign code_fence = code_fence | markdownify %}
|
2020-09-14 13:24:10 -04:00
|
|
|
{% include fix_linenos.html code=code_fence %}
|
2020-07-06 09:04:30 -04:00
|
|
|
|
2020-09-14 12:54:55 -04:00
|
|
|
✅ Using liquid highlighting + workaround:
|
|
|
|
|
2020-07-06 09:04:30 -04:00
|
|
|
{% capture code %}
|
|
|
|
{% highlight ruby linenos %}
|
|
|
|
# Ruby code with syntax highlighting and fixed line numbers using Liquid
|
|
|
|
GitHubPages::Dependencies.gems.each do |gem, version|
|
|
|
|
s.add_dependency(gem, "= #{version}")
|
|
|
|
end
|
|
|
|
{% endhighlight %}
|
|
|
|
{% endcapture %}
|
|
|
|
{% include fix_linenos.html code=code %}
|
|
|
|
{% assign code = nil %}
|
|
|
|
|
2020-09-14 12:54:55 -04:00
|
|
|
❌ With the default configuration options, the following example illustrates
|
|
|
|
the **incorrect** formatting arising from the incompatibility of HTML compression
|
2020-07-07 13:34:35 -04:00
|
|
|
and the non-conforming HTML produced by Jekyll for line numbers:
|
|
|
|
|
2020-07-06 09:04:30 -04:00
|
|
|
{% highlight ruby linenos %}
|
|
|
|
# Ruby code with syntax highlighting and unfixed line numbers using Liquid
|
|
|
|
GitHubPages::Dependencies.gems.each do |gem, version|
|
|
|
|
s.add_dependency(gem, "= #{version}")
|
|
|
|
end
|
|
|
|
{% endhighlight %}
|