|
| 1 | +import hljs from "highlight.js"; |
1 | 2 | import { Liquid, type Template } from "liquidjs";
|
2 | 3 | import type { LiquidOptions, RenderOptions } from "liquidjs/dist/liquid-options";
|
3 | 4 | import compact from "lodash-es/compact";
|
@@ -153,6 +154,31 @@ export class CustomLiquid extends Liquid {
|
153 | 154 | // Add charset to pages that forgot it
|
154 | 155 | if (!$("meta[charset]").length) $('<meta charset="UTF-8">').prependTo("head");
|
155 | 156 |
|
| 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(/</g, "<").replace(/>/g, ">").replace(/&/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 | + |
156 | 182 | const prependedIncludes = ["header"];
|
157 | 183 | const appendedIncludes = ["wai-site-footer", "site-footer"];
|
158 | 184 |
|
|
0 commit comments