DEV: add replaceTagRenderer to pluginApi

DEV: add replaceTagRenderer to pluginApi

diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
index e174b58..910b55f 100644
--- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6
+++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
@@ -31,6 +31,7 @@ import {
   replaceIcon
 } from "discourse-common/lib/icon-library";
 import { replaceCategoryLinkRenderer } from "discourse/helpers/category-link";
+import { replaceTagRenderer } from "discourse/lib/render-tag";
 import { addNavItem } from "discourse/models/nav-item";
 import { replaceFormatter } from "discourse/lib/utilities";
 import { modifySelectKit } from "select-kit/mixins/plugin-api";
@@ -42,7 +43,7 @@ import Sharing from "discourse/lib/sharing";
 import { addComposerUploadHandler } from "discourse/components/composer-editor";
 
 // If you add any methods to the API ensure you bump up this number
-const PLUGIN_API_VERSION = "0.8.28";
+const PLUGIN_API_VERSION = "0.8.29";
 
 class PluginApi {
   constructor(version, container) {
@@ -831,6 +832,21 @@ class PluginApi {
   }
 
   /**
+   * Registers a renderer that overrides the display of a tag.
+   *
+   * Example:
+   *
+   * function testTagRenderer(tag, params) {
+   *   const visibleName = Handlebars.Utils.escapeExpression(tag);
+   *   return `testing: ${visibleName}`;
+   * }
+   * api.replaceTagRenderer(testTagRenderer);
+   **/
+  replaceTagRenderer(fn) {
+    replaceTagRenderer(fn);
+  }
+
+  /**
    * Registers custom languages for use with HighlightJS.
    *
    * See https://highlightjs.readthedocs.io/en/latest/language-guide.html
diff --git a/app/assets/javascripts/discourse/lib/render-tag.js.es6 b/app/assets/javascripts/discourse/lib/render-tag.js.es6
index 8420638..4e0597d 100644
--- a/app/assets/javascripts/discourse/lib/render-tag.js.es6
+++ b/app/assets/javascripts/discourse/lib/render-tag.js.es6
@@ -1,4 +1,10 @@
-export default function renderTag(tag, params) {
+let _renderer = defaultRenderTag;
+
+export function replaceTagRenderer(fn) {
+  _renderer = fn;
+}
+
+function defaultRenderTag(tag, params) {
   params = params || {};
   const visibleName = Handlebars.Utils.escapeExpression(tag);
   tag = visibleName.toLowerCase();
@@ -41,3 +47,7 @@ export default function renderTag(tag, params) {
 
   return val;
 }
+
+export default function renderTag(tag, params) {
+  return _renderer(tag, params);
+}

GitHub sha: 4955ab16

1 Like

Forgot to mention in the commit: this will be used by themes/components (for example, to add icons per tag).

1 Like