Only load languages that are actually used
Firefox doesn't like 429 Too Many Requests at all
This commit is contained in:
+22
-56
@@ -2,69 +2,19 @@
|
|||||||
// Highlight.JS: https://highlightjs.org/
|
// Highlight.JS: https://highlightjs.org/
|
||||||
|
|
||||||
async function initializeHighlightJS() {
|
async function initializeHighlightJS() {
|
||||||
|
|
||||||
const languageRE = /\blanguage-([a-zA-Z0-9_\-]+)\b/gi;
|
const languageRE = /\blanguage-([a-zA-Z0-9_\-]+)\b/gi;
|
||||||
const noLanguageRE = /\bno-language\b/gi;
|
const noLanguageRE = /\bno-language\b/gi;
|
||||||
const languages = [
|
const languages = new Set();
|
||||||
"apache",
|
|
||||||
"asciidoc",
|
|
||||||
"autohotkey",
|
|
||||||
"bash",
|
|
||||||
"basic",
|
|
||||||
"c",
|
|
||||||
"cmake",
|
|
||||||
"cpp",
|
|
||||||
"csharp",
|
|
||||||
"css",
|
|
||||||
"d",
|
|
||||||
"diff",
|
|
||||||
"gcode",
|
|
||||||
"glsl",
|
|
||||||
"go",
|
|
||||||
"gradle",
|
|
||||||
"graphql",
|
|
||||||
"http",
|
|
||||||
"ini",
|
|
||||||
"java",
|
|
||||||
"javascript",
|
|
||||||
"json",
|
|
||||||
"kotlin",
|
|
||||||
"latex",
|
|
||||||
"less",
|
|
||||||
"llvm",
|
|
||||||
"lua",
|
|
||||||
"makefile",
|
|
||||||
"markdown",
|
|
||||||
"nginx",
|
|
||||||
"objectivec",
|
|
||||||
"oxygene",
|
|
||||||
"perl",
|
|
||||||
"php",
|
|
||||||
"php-template",
|
|
||||||
"plaintext",
|
|
||||||
"protobuf",
|
|
||||||
"python",
|
|
||||||
"python-repl",
|
|
||||||
"r",
|
|
||||||
"ruby",
|
|
||||||
"rust",
|
|
||||||
"scss",
|
|
||||||
"shell",
|
|
||||||
"sql",
|
|
||||||
"swift",
|
|
||||||
"typescript",
|
|
||||||
"vbnet",
|
|
||||||
"wasm",
|
|
||||||
"xml",
|
|
||||||
"yaml",
|
|
||||||
];
|
|
||||||
const selector = "code.block[class*=\"language\"]";
|
const selector = "code.block[class*=\"language\"]";
|
||||||
|
const blocks = document.querySelectorAll(selector);
|
||||||
|
|
||||||
if (document.querySelectorAll(selector).length == 0) {
|
// Early-Exit if there's nothing to highlight anyway to reduce waste.
|
||||||
|
if (blocks.length == 0) {
|
||||||
console.log("highlight.JS: Skipping, as it is not needed here.");
|
console.log("highlight.JS: Skipping, as it is not needed here.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load and configure highlight.JS
|
||||||
console.log("highlight.JS: Loading...");
|
console.log("highlight.JS: Loading...");
|
||||||
let highlightJS = await import("./highlightjs/highlight.mjs");
|
let highlightJS = await import("./highlightjs/highlight.mjs");
|
||||||
window.highlightJS = highlightJS.default;
|
window.highlightJS = highlightJS.default;
|
||||||
@@ -77,6 +27,17 @@ async function initializeHighlightJS() {
|
|||||||
"throwUnescapedHTML": true,
|
"throwUnescapedHTML": true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Find all languages used.
|
||||||
|
for (let block of blocks) {
|
||||||
|
for (let classname of block.classList) {
|
||||||
|
for (let match of classname.matchAll(languageRE)) {
|
||||||
|
if (match.length > 0) {
|
||||||
|
languages.add(match[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Import all languages.
|
// Import all languages.
|
||||||
let langs = [];
|
let langs = [];
|
||||||
for (let lang of languages) {
|
for (let lang of languages) {
|
||||||
@@ -90,6 +51,7 @@ async function initializeHighlightJS() {
|
|||||||
let err = null;
|
let err = null;
|
||||||
for (let attempt = 1; attempt <= 5; attempt++) {
|
for (let attempt = 1; attempt <= 5; attempt++) {
|
||||||
try {
|
try {
|
||||||
|
// Firefox breaks here because it hates 429 Too Many Requests. All other browsers work.
|
||||||
mod = await import(`./highlightjs/languages/${lang}.min.js`);
|
mod = await import(`./highlightjs/languages/${lang}.min.js`);
|
||||||
break;
|
break;
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
@@ -114,7 +76,11 @@ async function initializeHighlightJS() {
|
|||||||
// Highlight everything.
|
// Highlight everything.
|
||||||
function highlightAllCode() {
|
function highlightAllCode() {
|
||||||
console.log(`highlight.JS: Applying highlighting all valid code blocks...`);
|
console.log(`highlight.JS: Applying highlighting all valid code blocks...`);
|
||||||
window.highlightJS.highlightAll();
|
try {
|
||||||
|
window.highlightJS.highlightAll();
|
||||||
|
} catch (ex) {
|
||||||
|
console.error(`highlight.JS: ${ex}`);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
if (document.readyState === "complete"
|
if (document.readyState === "complete"
|
||||||
|| document.readyState === "loaded"
|
|| document.readyState === "loaded"
|
||||||
|
|||||||
Reference in New Issue
Block a user