Skip to content

Commit dc61084

Browse files
authored
highlight.js: Add missing overrides, update version, and run at build time (#4627)
Changes in this PR: - Adds `language-*` or `no-highlight` classes to code elements inside pre elements where they were missing, to provide stability across highlight.js upgrades in the event of changes to auto-detection logic - This intentionally does not go through the trouble/noise of adding syntax highlighting to obsolete Flash and Silverlight techniques - Adds a build-time check which logs in dev / fails in CI if any code blocks have no class specified - Updates to use the latest version of highlight.js - Switches to invoking `highlight.js` at build time and including it as an npm dependency - This means no more `highlight.min.js` bundle taking up more space in the repo on each update - This also means syntax highlighting no longer involves running client-side JS on every visit to every page, better optimizing for sustainability
1 parent ad4542f commit dc61084

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+729
-568
lines changed

11ty/CustomLiquid.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hljs from "highlight.js";
12
import { Liquid, type Template } from "liquidjs";
23
import type { LiquidOptions, RenderOptions } from "liquidjs/dist/liquid-options";
34
import compact from "lodash-es/compact";
@@ -153,6 +154,31 @@ export class CustomLiquid extends Liquid {
153154
// Add charset to pages that forgot it
154155
if (!$("meta[charset]").length) $('<meta charset="UTF-8">').prependTo("head");
155156

157+
const $missingHljsOverrides = $("pre code:not([class])");
158+
if ($missingHljsOverrides.length) {
159+
$missingHljsOverrides.each((_, el) => {
160+
const code = $(el).html()!.replace(/\n/g, "\\n");
161+
const excerpt = `${code.slice(0, 40)}${code.length > 40 ? "..." : ""}`;
162+
console.log(
163+
`${filepath} missing "language-*" or "no-highlight" class on <pre><code> starting with ${excerpt}`
164+
);
165+
});
166+
if (process.env.ELEVENTY_RUN_MODE === "build") {
167+
throw new Error(
168+
"Please ensure all code blocks have a highlight.js class (language-* or no-highlight)."
169+
);
170+
}
171+
}
172+
173+
$("pre code[class*='language-']").each((_, el) => {
174+
const $el = $(el);
175+
// Unescape any HTML entities (which were originally needed for client-side highlight.js)
176+
const code = $el.html()!.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
177+
const language = $el.attr("class")!.replace(/^.*language-(\S+).*$/, "$1");
178+
$el.html(hljs.highlight(code, { language }).value);
179+
$el.addClass("hljs");
180+
});
181+
156182
const prependedIncludes = ["header"];
157183
const appendedIncludes = ["wai-site-footer", "site-footer"];
158184

_includes/waiscript.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
{% if isTechniques %}{% assign hljsPath = "../" %}{% endif %}
2-
<link rel="stylesheet" href="{{ hljsPath }}a11y-light.css">
3-
<script src="{{ hljsPath }}highlight.min.js"></script>
1+
<link rel="stylesheet" href="{% if isTechniques %}../{% endif %}a11y-light.css">
42
<script>
5-
hljs.highlightAll();
6-
var translationStrings = {}; /* fix WAI JS */
3+
var translationStrings = {}; // prevent errors in WAI main.js t() calls
74
</script>
85
<script src="https://www.w3.org/WAI/assets/scripts/main.js"></script>

eleventy.config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ export default async function (eleventyConfig: any) {
193193
eleventyConfig.addPassthroughCopy({
194194
"css/base.css": "techniques/base.css",
195195
"css/a11y-light.css": "techniques/a11y-light.css",
196-
"script/highlight.min.js": "techniques/highlight.min.js",
197196
});
198197

199198
eleventyConfig.addPassthroughCopy("understanding/*.css");
@@ -225,10 +224,6 @@ export default async function (eleventyConfig: any) {
225224
join(dir.input, "css", "a11y-light.css"),
226225
join(dir.output, "understanding", "a11y-light.css")
227226
);
228-
await copyFile(
229-
join(dir.input, "script", "highlight.min.js"),
230-
join(dir.output, "understanding", "highlight.min.js")
231-
);
232227

233228
// Output guidelines/index.html and dependencies for PR runs (not for GH Pages or W3C site)
234229
const sha = process.env.COMMIT_REF; // Read environment variable exposed by Netlify

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"cheerio": "^1.0.0",
2323
"glob": "^10.3.16",
2424
"gray-matter": "^4.0.3",
25+
"highlight.js": "^11.11.1",
2526
"liquidjs": "^10.21.0",
2627
"lodash-es": "^4.17.21",
2728
"mkdirp": "^3.0.1",

script/highlight.min.js

Lines changed: 625 additions & 493 deletions
Large diffs are not rendered by default.

techniques/aria/ARIA24.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ <h5><strong>- Instead of... -</strong></h5>
7979
</section>
8080
<section>
8181
<h5><strong>- Do... -</strong></h5>
82-
<pre xml:space="preserve"><code>&lt;p&gt;
82+
<pre><code class="language-html">&lt;p&gt;
8383
&lt;span class=&quot;icon icon-star-bg&quot; role=&quot;img&quot; aria-label=&quot;Favorite&quot;&gt;&lt;/span&gt;
8484
&lt;/p&gt;</code></pre>
8585
</section>

techniques/client-side-script/SCR37.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h3>An options button that opens a dialog</h3>
2222
<p>The triggering element is a button and the script is triggered from the onclick event. This sends the appropriate events to the operating system so that assistive technology is aware of the change in the DOM. </p>
2323
<p>In this example, the Submit and Reset buttons inside the dialog simply hide the <code>div</code>. </p>
2424

25-
<pre><code>...
25+
<pre><code class="language-html">...
2626
&lt;button onclick="TogglePopup(event,true)"
2727
name="pop0001"&gt;Options&lt;/button&gt;
2828

@@ -46,7 +46,7 @@ <h3>An options button that opens a dialog</h3>
4646

4747
<p>The <code>div</code>, heading and <code>form</code> elements are styled with CSS to look like a dialog. </p>
4848

49-
<pre><code>...
49+
<pre><code class="language-css">...
5050
a { color:blue; }
5151
a.clickPopup img { border:none; width:0; }
5252

@@ -63,7 +63,7 @@ <h3>An options button that opens a dialog</h3>
6363

6464
<p>The script toggles the display of the popup <code>div</code>, showing it and hiding it. </p>
6565

66-
<pre><code>...
66+
<pre><code class="language-javascript">...
6767
function TogglePopup(evt,show) {
6868
HarmonizeEvent(evt);
6969
var src = evt.target;

techniques/css/C15.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ <h3>Link elements</h3>
4848
<h3>Highlighting elements that receive focus</h3>
4949
<p>In this example, the :focus pseudo-class is used to change the style applied to input fields when they receive focus by changing the background color.</p>
5050

51-
<pre xml:space="preserve"><code class="language-">&lt;!doctype html&gt;
51+
<pre><code class="language-html">&lt;!doctype html&gt;
5252
&lt;html lang="en"&gt;
5353
&lt;head&gt;
5454
&lt;style&gt;

techniques/css/C43.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h3>Using CSS <code>scroll-padding</code> to un-obscure content</h3>
3030
<p>This example demonstrates a technique to un-obscure content underneath a fixed-position banner. To prevent the page becoming unusable at smaller screen sizes, the banner becomes un-fixed. To observe the <code>scroll-padding</code> effect you will need to use a viewport over 800px wide.</p>
3131
</aside>
3232
<p>Working example: <a href="../../working-examples/css-padding-focus-not-obscured/">Using CSS <code>scroll-padding</code> to un-obscure content</a>.</p>
33-
<pre xml:space="preserve"><code>&lt;!doctype html&gt;
33+
<pre><code class="language-html">&lt;!doctype html&gt;
3434
&lt;html lang="en"&gt;
3535
&lt;head&gt;
3636
&lt;meta charset="utf-8"&gt;

0 commit comments

Comments
 (0)