FIX: register plugin only if enabled

FIX: register plugin only if enabled

diff --git a/assets/javascripts/discourse-markdown/discourse-graphviz.js.es6 b/assets/javascripts/discourse-markdown/discourse-graphviz.js.es6
index 7938f3b..64451e2 100644
--- a/assets/javascripts/discourse-markdown/discourse-graphviz.js.es6
+++ b/assets/javascripts/discourse-markdown/discourse-graphviz.js.es6
@@ -3,31 +3,33 @@ export function setup(helper) {
     return;
   }
 
+  helper.whiteList(["div.graphviz", "div.graphviz.is-loading"]);
+
   helper.registerOptions((opts, siteSettings) => {
     opts.features.graphviz = siteSettings.discourse_graphviz_enabled;
   });
 
-  helper.whiteList(["div.graphviz", "div.graphviz.is-loading"]);
-
   helper.registerPlugin(md => {
-    md.inline.bbcode.ruler.push("graphviz", {
-      tag: "graphviz",
+    if (md.options.discourse.features.graphviz) {
+      md.inline.bbcode.ruler.push("graphviz", {
+        tag: "graphviz",
 
-      replace: function(state, tagInfo, content) {
-        const engines = ["dot", "neato", "circo", "fdp", "osage", "twopi"];
-        const token = state.push("html_raw", "", 0);
-        const escaped = state.md.utils.escapeHtml(
-          content.replace(/[\r\n\t]/g, "")
-        );
-        const inputEngine = state.md.utils.escapeHtml(tagInfo.attrs.engine);
-        const engine = engines.includes(inputEngine)
-          ? `data-engine='${inputEngine}'`
-          : "data-engine='dot'";
+        replace: function(state, tagInfo, content) {
+          const engines = ["dot", "neato", "circo", "fdp", "osage", "twopi"];
+          const token = state.push("html_raw", "", 0);
+          const escaped = state.md.utils.escapeHtml(
+            content.replace(/[\r\n\t]/g, "")
+          );
+          const inputEngine = state.md.utils.escapeHtml(tagInfo.attrs.engine);
+          const engine = engines.includes(inputEngine)
+            ? `data-engine='${inputEngine}'`
+            : "data-engine='dot'";
 
-        token.content = `<div class="graphviz is-loading" ${engine}>\n${escaped}\n</div>\n`;
+          token.content = `<div class="graphviz is-loading" ${engine}>\n${escaped}\n</div>\n`;
 
-        return true;
-      }
-    });
+          return true;
+        }
+      });
+    }
   });
 }

GitHub sha: b30324ef

1 Like