Skip to content

Commit

Permalink
wip(syntax-highlighting): catch recursive errors and prism errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMcA committed Sep 16, 2024
1 parent a027c5e commit 09cf5e1
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions client/src/document/code/syntax-highlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,21 @@ function highlightStringSync(
const resolvedLanguage = ALIASES.get(language) || language;
const prismLanguage = Prism.languages[resolvedLanguage];
if (prismLanguage) {
return Prism.highlight(text, prismLanguage, resolvedLanguage);
try {
return Prism.highlight(text, prismLanguage, resolvedLanguage);
} catch {
console.warn("Syntax highlighting: prism error");
}
}
return;
}

async function importLanguage(language: string) {
async function importLanguage(language: string, recursiveDepth = 0) {
if (recursiveDepth > 100) {
console.warn("Syntax highlighting: recursion error");
throw new Error("Syntax highlighting: recursion error");
}

const prismLanguage = Prism.languages[language];

if (!prismLanguage) {
Expand All @@ -113,7 +122,9 @@ async function importLanguage(language: string) {
"prism-svelte"
);
} catch (e) {
console.warn(`Failed to import ${language} prism language`);
console.warn(
`Syntax highlighting: failed to import ${language} prism language`
);
throw e;
}
} else {
Expand All @@ -124,7 +135,9 @@ async function importLanguage(language: string) {
(typeof config.require === "string"
? [config.require]
: config.require
).map((dependency) => importLanguage(dependency))
).map((dependency) =>
importLanguage(dependency, recursiveDepth + 1)
)
);
} catch {
return;
Expand All @@ -135,7 +148,7 @@ async function importLanguage(language: string) {
(typeof config.optional === "string"
? [config.optional]
: config.optional
).map((dependency) => importLanguage(dependency))
).map((dependency) => importLanguage(dependency, recursiveDepth + 1))
);
}
try {
Expand All @@ -145,7 +158,9 @@ async function importLanguage(language: string) {
`prismjs/components/prism-${language}.js`
);
} catch (e) {
console.warn(`Failed to import ${language} prism language`);
console.warn(
`Syntax highlighting: failed to import ${language} prism language`
);
throw e;
}
}
Expand Down

0 comments on commit 09cf5e1

Please sign in to comment.