From e9c5cef664447bc2111eb18b37cd057730ead1ff Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 1 Nov 2017 12:16:15 -0700 Subject: [PATCH] Add the ability to define a default language (#206) * Add the ability to define a default language I don't want to have to add the language i'm working on to every single block nor do I want to rely on a default heuristic to find the language. This adds the ability to write ``` highlight: { defaultLang: 'xxxx' } ``` in `siteConfig.js` to force the language when not specified. I tested it without a a highlight block, without a defaultLang attribute and with one and with a wrong one. All of them work as expected. * Move comment around --- lib/core/Remarkable.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/core/Remarkable.js b/lib/core/Remarkable.js index 92a8a950e0..710021a417 100644 --- a/lib/core/Remarkable.js +++ b/lib/core/Remarkable.js @@ -42,21 +42,24 @@ class Remarkable extends React.Component { renderMarkdown(source) { if (!this.md) { + const siteConfig = require(CWD + "/siteConfig.js"); + this.md = new Markdown({ // Highlight.js expects hljs css classes on the code element. // This results in

         langPrefix: 'hljs css ',
         highlight: function (str, lang) {
+          lang = lang || (siteConfig.highlight && siteConfig.highlight.defaultLang);
           if (lang && hljs.getLanguage(lang)) {
             try {
               return hljs.highlight(lang, str).value;
             } catch (err) {}
           }
-      
+
           try {
             return hljs.highlightAuto(str).value;
           } catch (err) {}
-      
+
           return '';
         }
       });
@@ -65,7 +68,6 @@ class Remarkable extends React.Component {
       this.md.use(anchors);
 
       // Allow client sites to register their own plugins
-      const siteConfig = require(CWD + "/siteConfig.js");
       if (siteConfig.markdownPlugins) {
         siteConfig.markdownPlugins.forEach(function(plugin) {
           this.md.use(plugin);
@@ -96,4 +98,4 @@ Remarkable.defaultProps = {
   options: {},
 };
 
-module.exports = Remarkable;
\ No newline at end of file
+module.exports = Remarkable;