Skip to content

A platform for community discussion. Free, open, simple.

check for code-blocks before loading highlight-js

Joe20de49c8722f
diff --git a/app/assets/javascripts/discourse/app/lib/highlight-syntax.js b/app/assets/javascripts/discourse/app/lib/highlight-syntax.js
index 80cc830f443..b8d0abef447 100644
--- a/app/assets/javascripts/discourse/app/lib/highlight-syntax.js
+++ b/app/assets/javascripts/discourse/app/lib/highlight-syntax.js
@@ -11,6 +11,13 @@ export default function highlightSyntax(elem, siteSettings, session) {
const selector = siteSettings.autohighlight_all_code
? "pre code"
: "pre code[class]";
+
+ const codeblocks = elem.querySelectorAll(selector);
+
+ if (!codeblocks.length) {
+ return;
+ }
+
const path = session.highlightJsPath;
if (!path) {
@@ -20,7 +27,7 @@ export default function highlightSyntax(elem, siteSettings, session) {
return loadScript(path).then(() => {
customHighlightJSLanguages();
- elem.querySelectorAll(selector).forEach((e) => {
+ codeblocks.forEach((e) => {
// Large code blocks can cause crashes or slowdowns
if (e.innerHTML.length > 30000) {
return;