DEV: Allow plugins to add extra icons to category-link (#9743)

DEV: Allow plugins to add extra icons to category-link (#9743)

diff --git a/app/assets/javascripts/discourse/app/helpers/category-link.js b/app/assets/javascripts/discourse/app/helpers/category-link.js
index 7f566b5..2f4699b 100644
--- a/app/assets/javascripts/discourse/app/helpers/category-link.js
+++ b/app/assets/javascripts/discourse/app/helpers/category-link.js
@@ -18,6 +18,12 @@ function categoryStripe(color, classes) {
   return "<span class='" + classes + "' " + style + "></span>";
 }
 
+let _extraIconRenderers = [];
+
+export function addExtraIconRenderer(renderer) {
+  _extraIconRenderers.push(renderer);
+}
+
 /**
   Generates category badge HTML
 
@@ -149,12 +155,15 @@ function defaultCategoryLinkRenderer(category, opts) {
   }
 
   if (restricted) {
-    html += `${iconHTML(
-      "lock"
-    )}<span class="category-name" ${categoryDir}>${categoryName}</span>`;
-  } else {
-    html += `<span class="category-name" ${categoryDir}>${categoryName}</span>`;
+    html += iconHTML("lock");
   }
+  _extraIconRenderers.forEach(renderer => {
+    const iconName = renderer(category);
+    if (iconName) {
+      html += iconHTML(iconName);
+    }
+  });
+  html += `<span class="category-name" ${categoryDir}>${categoryName}</span>`;
   html += "</span>";
 
   if (opts.topicCount && categoryStyle !== "box") {
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index 06cecc2..a10e183 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -48,6 +48,7 @@ import {
   addComposerUploadMarkdownResolver
 } from "discourse/components/composer-editor";
 import { addCategorySortCriteria } from "discourse/components/edit-category-settings";
+import { addExtraIconRenderer } from "discourse/helpers/category-link";
 import { queryRegistry } from "discourse/widgets/widget";
 import Composer from "discourse/models/composer";
 import { on } from "@ember/object/evented";
@@ -1065,6 +1066,22 @@ class PluginApi {
   decorateTopicTitle(callback) {
     addTopicTitleDecorator(callback);
   }
+
+  /**
+   * Allows adding icons to the category-link html
+   *
+   * `‍``
+   * api.addCategoryLinkIcon((category) => {
+   *  if (category.someProperty) {
+        return "eye"
+      }
+   * });
+   * `‍``
+   *
+   **/
+  addCategoryLinkIcon(renderer) {
+    addExtraIconRenderer(renderer);
+  }
 }
 
 let _pluginv01;

GitHub sha: 9bce91f5

This commit appears in #9743 which was approved by eviltrout. It was merged by markvanlan.