mirror of
https://github.com/snachodog/just-the-docs.git
synced 2025-09-13 05:13:33 -06:00
Support for the linenos option on highlighted code
The added examples in `docs/index-test.md` extend the previous examplees of highlighting, documenting the required inout.
This commit is contained in:
33
_includes/fix_linenos.html
Normal file
33
_includes/fix_linenos.html
Normal file
@@ -0,0 +1,33 @@
|
||||
{% comment -%}
|
||||
Fixes the HTML for highlighted code with linenos.
|
||||
|
||||
Derived from the workaround provided by Dmitry Hrabrov (DeXP) at
|
||||
https://github.com/penibelst/jekyll-compress-html/issues/71#issuecomment-188144901
|
||||
|
||||
Explanation:
|
||||
|
||||
The HTML produced by Rouge with the linenos option matches CSS `code table`.
|
||||
Jekyll (<= 4.1.1) always wraps the highlighted HTML with `pre`, which is
|
||||
unnecessary and non-conforming, and leads to validation error reports.
|
||||
The fix removes the `pre` tags around `_code` whenever it contains the pattern
|
||||
`<table class="rouge-table">`. This change avoids validation errors, and
|
||||
allows the use of [Jekyll layout for compressing HTML](http://jch.penibelst.de),
|
||||
which relies on `pre` tags not being nested.
|
||||
|
||||
Usage:
|
||||
|
||||
{% capture fix_linenos_code %}{% highlight AnyLanguage linenos %}
|
||||
Some code
|
||||
{% endhighlight %}{% endcapture %}{% include fix_linenos.html %}{{ fix_linenos_code }}
|
||||
|
||||
Caveats:
|
||||
|
||||
The above does not work when `Some code` happens to contain the matched string
|
||||
`<table class="rouge-table">`.
|
||||
|
||||
{%- endcomment %}
|
||||
|
||||
{% if fix_linenos_code contains '<table class="rouge-table">' %}
|
||||
{% assign fix_linenos_code = fix_linenos_code | replace: "<pre><code", "<code" %}
|
||||
{% assign fix_linenos_code = fix_linenos_code | replace: "</code></pre>", "</code>" %}
|
||||
{% endif %}
|
Reference in New Issue
Block a user