Browse Source

do not highlight code blocks without lang specified by default

Le Tan 8 years ago
parent
commit
f6cf98c827

+ 10 - 5
src/resources/hoedown.js

@@ -3,10 +3,14 @@ var placeholder = document.getElementById('placeholder');
 // Use Marked to highlight code blocks in edit mode.
 marked.setOptions({
     highlight: function(code, lang) {
-        if (lang && hljs.getLanguage(lang)) {
-            return hljs.highlight(lang, code).value;
+        if (lang) {
+            if (hljs.getLanguage(lang)) {
+                return hljs.highlight(lang, code).value;
+            } else {
+                return hljs.highlightAuto(code).value;
+            }
         } else {
-            return hljs.highlightAuto(code).value;
+            return code;
         }
     }
 });
@@ -37,7 +41,9 @@ var updateHtml = function(html) {
                 }
             }
 
-            hljs.highlightBlock(code);
+            if (listContainsRegex(code.classList, /language-.*/)) {
+                hljs.highlightBlock(code);
+            }
         }
     }
 
@@ -63,4 +69,3 @@ var highlightText = function(text, id, timeStamp) {
     var html = marked(text);
     content.highlightTextCB(html, id, timeStamp);
 }
-

+ 7 - 3
src/resources/markdown-it.js

@@ -44,10 +44,14 @@ var mdit = window.markdownit({
     typographer: true,
     langPrefix: 'lang-',
     highlight: function(str, lang) {
-        if (lang && hljs.getLanguage(lang)) {
-            return hljs.highlight(lang, str).value;
+        if (lang) {
+            if (hljs.getLanguage(lang)) {
+                return hljs.highlight(lang, str).value;
+            } else {
+                return hljs.highlightAuto(str).value;
+            }
         } else {
-            return hljs.highlightAuto(str).value;
+            return str;
         }
     }
 });

+ 10 - 0
src/resources/markdown_template.js

@@ -844,3 +844,13 @@ var addClassToCodeBlock = function() {
         }
     }
 };
+
+var listContainsRegex = function(strs, exp) {
+    for (var i = 0, len = strs.length; i < len; ++i) {
+        if (exp.test(strs[i])) {
+            return true;
+        }
+    }
+
+    return false;
+}

+ 7 - 3
src/resources/marked.js

@@ -17,10 +17,14 @@ renderer.heading = function(text, level) {
 // Highlight.js to highlight code block
 marked.setOptions({
     highlight: function(code, lang) {
-        if (lang && hljs.getLanguage(lang)) {
-            return hljs.highlight(lang, code).value;
+        if (lang) {
+            if (hljs.getLanguage(lang)) {
+                return hljs.highlight(lang, code).value;
+            } else {
+                return hljs.highlightAuto(code).value;
+            }
         } else {
-            return hljs.highlightAuto(code).value;
+            return code;
         }
     }
 });

+ 3 - 1
src/resources/showdown.js

@@ -60,7 +60,9 @@ var highlightCodeBlocks = function(doc, enableMermaid, enableFlowchart) {
             } if (enableFlowchart && code.classList.contains('language-flowchart')) {
                 // Flowchart code block.
                 continue;
-            } else {
+            }
+
+            if (listContainsRegex(code.classList, /language-.*/)) {
                 hljs.highlightBlock(code);
             }
         }