fix(highlight.js): occasional crash when multiple code blocks in read-only mode

This commit is contained in:
Elian Doran 2025-05-18 20:11:07 +03:00
parent a3c39bbf5e
commit 9642f209a0
No known key found for this signature in database

View File

@ -2,6 +2,8 @@ import { ensureMimeTypes, highlight, highlightAuto, loadTheme, Themes } from "@t
import mime_types from "./mime_types.js"; import mime_types from "./mime_types.js";
import options from "./options.js"; import options from "./options.js";
let highlightingLoaded = false;
export function getStylesheetUrl(theme: string) { export function getStylesheetUrl(theme: string) {
if (!theme) { if (!theme) {
return null; return null;
@ -25,6 +27,8 @@ export async function applySyntaxHighlight($container: JQuery<HTMLElement>) {
return; return;
} }
await ensureMimeTypesForHighlighting();
const codeBlocks = $container.find("pre code"); const codeBlocks = $container.find("pre code");
for (const codeBlock of codeBlocks) { for (const codeBlock of codeBlocks) {
const normalizedMimeType = extractLanguageFromClassList(codeBlock); const normalizedMimeType = extractLanguageFromClassList(codeBlock);
@ -58,6 +62,10 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLEle
} }
export async function ensureMimeTypesForHighlighting() { export async function ensureMimeTypesForHighlighting() {
if (highlightingLoaded) {
return;
}
// Load theme. // Load theme.
const currentThemeName = String(options.get("codeBlockTheme")); const currentThemeName = String(options.get("codeBlockTheme"));
loadHighlightingTheme(currentThemeName); loadHighlightingTheme(currentThemeName);
@ -65,6 +73,8 @@ export async function ensureMimeTypesForHighlighting() {
// Load mime types. // Load mime types.
const mimeTypes = mime_types.getMimeTypes(); const mimeTypes = mime_types.getMimeTypes();
await ensureMimeTypes(mimeTypes); await ensureMimeTypes(mimeTypes);
highlightingLoaded = true;
} }
export function loadHighlightingTheme(themeName: string) { export function loadHighlightingTheme(themeName: string) {