Skip to content

A platform for community discussion. Free, open, simple.

FIX: Render hashtag icons in category descriptions (#36867)

Régis Hanol8aa6e54df1b0
diff --git a/frontend/discourse/app/components/categories-boxes.gjs b/frontend/discourse/app/components/categories-boxes.gjs
index dfe37a413ba..e856737ba3e 100644
--- a/frontend/discourse/app/components/categories-boxes.gjs
+++ b/frontend/discourse/app/components/categories-boxes.gjs
@@ -6,12 +6,14 @@ import { classNameBindings, tagName } from "@ember-decorators/component";
import CategoryLogo from "discourse/components/category-logo";
import CategoryTitleBefore from "discourse/components/category-title-before";
import CategoryTitleLink from "discourse/components/category-title-link";
+import DecoratedHtml from "discourse/components/decorated-html";
import PluginOutlet from "discourse/components/plugin-outlet";
import borderColor from "discourse/helpers/border-color";
import categoryColorVariable from "discourse/helpers/category-color-variable";
import categoryLink, {
categoryBadgeHTML,
} from "discourse/helpers/category-link";
+import dirSpan from "discourse/helpers/dir-span";
import lazyHash from "discourse/helpers/lazy-hash";
@tagName("section")
@@ -78,7 +80,9 @@ export default class CategoriesBoxes extends Component {
{{#unless c.isMuted}}
<div class="description">
- {{htmlSafe c.description_excerpt}}
+ <DecoratedHtml
+ @html={{dirSpan c.description_excerpt htmlSafe="true"}}
+ />
</div>
{{#if c.isGrandParent}}
diff --git a/frontend/discourse/app/components/decorated-html.gjs b/frontend/discourse/app/components/decorated-html.gjs
index 2bdba1762f8..9b60d601249 100644
--- a/frontend/discourse/app/components/decorated-html.gjs
+++ b/frontend/discourse/app/components/decorated-html.gjs
@@ -95,6 +95,20 @@ export function resetHtmlDecorators() {
/**
* Reactively renders cooked HTML with decorations applied.
+ *
+ * When no custom `@decorate` function is provided, registered HTML decorators
+ * (via `api.decorateCookedElement`) are automatically applied. This handles
+ * common cases like hashtag icons, mentions, and other cooked content decorations.
+ *
+ * @component DecoratedHtml
+ * @param {SafeString} html - The HTML content to render (must be wrapped with htmlSafe)
+ * @param {Function} [decorate] - Custom decorator function receiving (element, helper, decorateArgs).
+ * When provided, you're responsible for calling applyHtmlDecorators if needed.
+ * @param {Object} [decorateArgs] - Additional arguments passed to the decorate function
+ * @param {Object} [model] - Model object (e.g., post) passed to decorators via helper.model
+ * @param {Object} [context] - Context object passed to decorators via helper.context
+ * @param {string} [className] - CSS class name for the wrapper div
+ * @param {string} [id] - ID attribute for the wrapper div
*/
export default class DecoratedHtml extends Component {
renderGlimmerInfos = new TrackedArray();
@@ -111,12 +125,20 @@ export default class DecoratedHtml extends Component {
const decorateFn = this.args.decorate;
- // force parameters explicity declarated in `decorateArgs` to be tracked despite the
+ // force parameters explicitly declared in `decorateArgs` to be tracked despite the
// use of `untrack` below
decorateArgs && Object.values(decorateArgs);
try {
- untrack(() => decorateFn?.(cookedDiv, helper, decorateArgs));
+ untrack(() => {
+ if (decorateFn) {
+ // Custom decorator provided - it's responsible for calling applyHtmlDecorators if needed
+ decorateFn(cookedDiv, helper, decorateArgs);
+ } else {
+ // No custom decorator - automatically apply registered HTML decorators
+ applyHtmlDecorators(cookedDiv, helper);
+ }
+ });
} catch (e) {
if (isRailsTesting() || isTesting()) {
throw e;
@@ -137,7 +159,7 @@ export default class DecoratedHtml extends Component {
get elementToDecorate() {
const cooked = this.args.html || htmlSafe("");
if (!isHTMLSafe(cooked)) {
- throw "@cooked must be an htmlSafe string";
+ throw "@html must be an htmlSafe string";
}
const cookedDiv = detachedDocument.createElement("div");
cookedDiv.innerHTML = cooked.toString();
diff --git a/frontend/discourse/app/components/parent-category-row.gjs b/frontend/discourse/app/components/parent-category-row.gjs
index d761591ccc0..d41e8e87a4e 100644
--- a/frontend/discourse/app/components/parent-category-row.gjs
+++ b/frontend/discourse/app/components/parent-category-row.gjs
@@ -4,6 +4,7 @@ import { htmlSafe } from "@ember/template";
import CategoryListItem from "discourse/components/category-list-item";
import CategoryTitleLink from "discourse/components/category-title-link";
import CategoryUnread from "discourse/components/category-unread";
+import DecoratedHtml from "discourse/components/decorated-html";
import MobileCategoryTopic from "discourse/components/mobile-category-topic";
import PluginOutlet from "discourse/components/plugin-outlet";
import SubCategoryItem from "discourse/components/sub-category-item";
@@ -53,7 +54,12 @@ export default class ParentCategoryRow extends CategoryListItem {
{{#if this.category.description_excerpt}}
<tr class="category-description">
<td colspan="3">
- {{htmlSafe this.category.description_excerpt}}
+ <DecoratedHtml
+ @html={{dirSpan
+ this.category.description_excerpt
+ htmlSafe="true"
+ }}
+ />
</td>
</tr>
{{/if}}
@@ -141,7 +147,12 @@ export default class ParentCategoryRow extends CategoryListItem {
{{#if this.category.description_excerpt}}
<div class="category-description">
- {{dirSpan this.category.description_excerpt htmlSafe="true"}}
+ <DecoratedHtml
+ @html={{dirSpan
+ this.category.description_excerpt
+ htmlSafe="true"
+ }}
+ />
</div>
{{/if}}
diff --git a/frontend/discourse/app/components/sub-category-row.gjs b/frontend/discourse/app/components/sub-category-row.gjs
index b88a6be822b..05973d8b92f 100644
--- a/frontend/discourse/app/components/sub-category-row.gjs
+++ b/frontend/discourse/app/components/sub-category-row.gjs
@@ -1,5 +1,6 @@
import CategoryListItem from "discourse/components/category-list-item";
import CategoryTitleLink from "discourse/components/category-title-link";
+import DecoratedHtml from "discourse/components/decorated-html";
import SubCategoryItem from "discourse/components/sub-category-item";
import borderColor from "discourse/helpers/border-color";
import dirSpan from "discourse/helpers/dir-span";
@@ -47,7 +48,12 @@ export default class SubCategoryRow extends CategoryListItem {
<CategoryTitleLink @tagName="h4" @category={{this.category}} />
{{#if this.category.description_excerpt}}
<div class="category-description subcategory-description">
- {{dirSpan this.category.description_excerpt htmlSafe="true"}}
+ <DecoratedHtml
+ @html={{dirSpan
+ this.category.description_excerpt
+ htmlSafe="true"
+ }}
+ />
</div>
{{/if}}
{{#if this.category.subcategories}}
diff --git a/frontend/discourse/tests/integration/components/decorated-html-test.gjs b/frontend/discourse/tests/integration/components/decorated-html-test.gjs
index a0c089d8e55..63b9cde6b31 100644
--- a/frontend/discourse/tests/integration/components/decorated-html-test.gjs
+++ b/frontend/discourse/tests/integration/components/decorated-html-test.gjs
@@ -5,7 +5,9 @@ import { render, settled } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import curryComponent from "ember-curry-component";
import { module, test } from "qunit";
-import DecoratedHtml from "discourse/components/decorated-html";
+import DecoratedHtml, {
+ registerHtmlDecorator,
+} from "discourse/components/decorated-html";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { WIDGET_DECOMMISSION_OPTIONS } from "discourse/widgets/widget";
@@ -167,4 +169,50 @@ module("Integration | Component | <DecoratedHtml />", function (hooks) {
"Glimmer component is not rendered when a template is passed as the component"
);
});
+
+ test("applies registered HTML decorators by default", async function (assert) {
+ let decoratorCalled = false;
+ registerHtmlDecorator((element) => {
+ decoratorCalled = true;
+ element.innerHTML += "<span id='auto-decorated'>Decorated</span>";
+ });
+
+ await render(
+ <template>
+ <DecoratedHtml @html={{htmlSafe "<div>Content</div>"}} />
+ </template>
+ );
+
+ assert.true(decoratorCalled, "registered decorator was called");
+ assert.dom("#auto-decorated").hasText("Decorated");
+ });
+
+ test("custom @decorate function replaces default decoration", async function (assert) {
+ let registeredDecoratorCalled = false;
+ registerHtmlDecorator(() => {
+ registeredDecoratorCalled = true;
+ });
+
+ let customDecoratorCalled = false;
+ const customDecorator = (element) => {
+ customDecoratorCalled = true;
+ element.innerHTML += "<span id='custom'>Custom</span>";
+ };
+
+ await render(
+ <template>
+ <DecoratedHtml
+ @html={{htmlSafe "<div>Content</div>"}}
+ @decorate={{customDecorator}}
+ />
+ </template>
+ );
+
+ assert.true(customDecoratorCalled, "custom decorator was called");
+ assert.false(
+ registeredDecoratorCalled,
+ "registered decorator was not called when custom decorator is provided"
+ );
+ assert.dom("#custom").hasText("Custom");
+ });
});
diff --git a/spec/system/categories_page_spec.rb b/spec/system/categories_page_spec.rb
index e345f924e6b..b13d281ce5b 100644
--- a/spec/system/categories_page_spec.rb
+++ b/spec/system/categories_page_spec.rb
@@ -8,6 +8,24 @@ RSpec.describe "Categories Page" do
let(:category_page) { PageObjects::Pages::Category.new }
before { CategoryFeaturedTopic.feature_topics }
+ describe "hashtag icons in category descriptions" do
+ fab!(:tag) { Fabricate(:tag, name: "important") }
+ fab!(:category_with_hashtag) do
+ Fabricate(:category_with_definition, description: "Check out #important for more info")
+ end
+
+ it "renders hashtag icons in category descriptions" do
+ category_page.visit_categories
+
+ within(
+ ".category-list tr[data-category-id='#{category_with_hashtag.id}'] .category-description",
+ ) do
+ expect(page).to have_css(".hashtag-cooked", text: "important")
+ expect(page).to have_css(".hashtag-cooked .d-icon")
+ end
+ end
+ end
+
describe "subcategories_with_featured_topics" do
before { SiteSetting.desktop_category_page_style = "subcategories_with_featured_topics" }
it "displays subcategories and topics" do