Matt Wang d7e4a808b5
Fix incorrect HTML in theme & docs; validate HTML in CI (#1305)
This PR is motivated from https://github.com/just-the-docs/just-the-docs/pull/1259#issuecomment-1655899503. It adds a new workflow (`CI / Validate HTML (3.1)`) that validates the output of `bundle exec jekyll build`. It does this with two separate tools:

1. The [`html5validator-action`](https://github.com/Cyb3r-Jak3/html5validator-action), which is a wrapper (Docker image + argument forwarding) around the [Nu HTML checker](https://github.com/validator/validator), which is what is used by the [W3C markup validation service](https://validator.w3.org/)
2. [`html-proofer`](https://github.com/gjtorikian/html-proofer), which performs auxiliary checks on the validity of script, image, and link *values*, but not the markup itself
    - note: prior versions of `html-proofer` did use nokogiri to also validate HTML, but the author has elected to remove that feature in versions 4+

I then fix a few issues that are flagged by these tools. I'll split this into,

**changes affecting users**:
- strictly incorrect: in `_layouts/minimal.html`, a `<div>` had duplicate `id`s. I've removed the incorrect one, which is related to...
- semantically wrong (but not technically incorrect): in both `minimal` and `default` layouts, we had two `<div>` tags with `id="main-content-wrap"`. These don't do anything; the associated styling is with the *class* `main-content-wrap`. I've elected to remove these `id`s to avoid confusion and keep the layouts in sync; however, **this is technically a breaking change**
    - observe that `#main-content` is used for the "skip to main content" feature, which I missed in an earlier iteration of this PR

**changes affecting only our documentation**
- a broken link to mermaid docs (I've changed it to a valid one)
- an incorrectly-specified `aux_link` to our own repository
- various links that point to the bare URL `another-page`, which is clearly invalid; I've changed these to point to our homepage
- an incorrect header link
- various links to `http://example.com`, which I've changed to point to our homepage
- an incorrect link to `@flyx`'s profile for the AsciiDoctor gist
- a handful of (otherwise-valid) `http` links that should be `https`: the lunr docs, and patrick's personal website

The commit history shows the Nu validator flagging issues in CI properly in commits [4128b23](4128b23ef2) and [3527220](35272203ba).

## relevant configuration

- I exclude `github.com` URLs from external link checks with `html-proofer`. This is because GitHub does not like it when we ping too frequently, and rate limits us, which in turn provides many false positives. This is aligned with their documentation, which uses this ignore.
- I've pinned the hash for the 3rd-party action that wraps the W3C markup validation service. This aligns with #1148, but means that we'll have to keep an eye on it for updates.
2023-08-19 21:17:26 -04:00

3.5 KiB

layout, title, parent, grand_parent, permalink
layout title parent grand_parent permalink
default Code with line numbers Code UI Components /docs/ui-components/code/line-numbers/

Code snippets with line numbers

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:

{% highlight yaml %} compress_html: ignore: envs: all {% endhighlight %}

When using Kramdown code fences, line numbers are turned on globally by the following configuration option:

{% highlight yaml %} kramdown: syntax_highlighter_opts: block: line_numbers: true {% endhighlight %}

Line numbers can then be suppressed locally using Liquid tags (without the linenos option) instead of fences:

{% highlight yaml %} {% raw %}{% highlight some_language %} Some code {% endhighlight %}{% endraw %} {% endhighlight %}

Workarounds

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).

Code fences (three backticks)

{% highlight default %} {% raw %}{% capture some_var %}

Some code

{% endcapture %} {% assign some_var = some_var | markdownify %} {% include fix_linenos.html code=some_var %}{% endraw %} {% endhighlight %}

Liquid highlighting

{% highlight default %} {% raw %}{% capture some_var %} {% highlight some_language linenos %} Some code {% endhighlight %} {% endcapture %} {% include fix_linenos.html code=some_var %}{% endraw %} {% endhighlight %}

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.

Examples

Using code fences + workaround (will only show line numbers if enabled globally in _config.yml):

{% capture code_fence %}

// Javascript code with syntax highlighting in fences
var fun = function lang(l) {
  dateformat.i18n = require('./lang/' + l)
  return true;
}

{% endcapture %} {% assign code_fence = code_fence | markdownify %} {% include fix_linenos.html code=code_fence %}

Using liquid highlighting + workaround:

{% 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 %}

Narrow code stays close to the line numbers:

{% capture code %} {% highlight ruby linenos %} def foo puts 'foo' end {% endhighlight %} {% endcapture %} {% include fix_linenos.html code=code %} {% assign code = nil %}

{: .warning } The following generates incorrect and invalid HTML. It should not be used as a positive example; the improper layout (with the broken HTML tags) is intentional.

With the compression options used for the theme docs, the following example illustrates the incorrect formatting arising from the incompatibility of HTML compression and the non-conforming HTML produced by Jekyll for line numbers:

{% highlight ruby linenos %} def foo puts 'foo' end {% endhighlight %}