Skip to content

Commit 0f31306

Browse files
committed
Run highlight.js at build time instead of client-side
1 parent 21b9c7c commit 0f31306

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

11ty/CustomLiquid.ts

Lines changed: 10 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";
@@ -169,6 +170,15 @@ export class CustomLiquid extends Liquid {
169170
}
170171
}
171172

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+
172182
const prependedIncludes = ["header"];
173183
const appendedIncludes = ["wai-site-footer", "site-footer"];
174184

_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",

0 commit comments

Comments
 (0)