Skip to content

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

FEATURE: customizable tag separator with value transformer in proper HTML (#31674)

Kris55a3a4e69ed7
diff --git a/app/assets/javascripts/discourse/app/lib/render-tags.js b/app/assets/javascripts/discourse/app/lib/render-tags.js
index 4ec6c029080..b168311926e 100644
--- a/app/assets/javascripts/discourse/app/lib/render-tags.js
+++ b/app/assets/javascripts/discourse/app/lib/render-tags.js
@@ -1,4 +1,5 @@
import renderTag from "discourse/lib/render-tag";
+import { applyValueTransformer } from "discourse/lib/transformer";
import { i18n } from "discourse-i18n";
let callbacks = null;
@@ -42,41 +43,71 @@ export default function (topic, params) {
}
}
- let customHtml = null;
+ const separator = (index) => {
+ return applyValueTransformer("tag-separator", ",", {
+ topic,
+ index,
+ });
+ };
+
+ const separatorSpan = (index) => {
+ return `<span class="discourse-tags__tag-separator">${separator(
+ index
+ )}</span>`;
+ };
+
+ const callbackResults = [];
if (callbacks) {
callbacks.forEach((c) => {
const html = c(topic, params);
if (html) {
- if (customHtml) {
- customHtml += html;
- } else {
- customHtml = html;
- }
+ callbackResults.push(html);
}
});
}
- if (customHtml || (tags && tags.length > 0)) {
- buffer = `<div class='discourse-tags' role='list'
- aria-label=${i18n("tagging.tags")}>`;
- if (tags) {
+ const hasContent = (tags && tags.length > 0) || callbackResults.length > 0;
+
+ if (hasContent) {
+ buffer = `<div class='discourse-tags'
+ role='list'
+ aria-label=${i18n("tagging.tags")}>`;
+
+ let currentIndex = 0;
+
+ if (tags && tags.length > 0) {
for (let i = 0; i < tags.length; i++) {
- buffer +=
- renderTag(tags[i], {
- description:
- topic.tags_descriptions && topic.tags_descriptions[tags[i]],
- isPrivateMessage,
- tagsForUser,
- tagName,
- }) + "";
+ buffer += renderTag(tags[i], {
+ description:
+ topic.tags_descriptions && topic.tags_descriptions[tags[i]],
+ isPrivateMessage,
+ tagsForUser,
+ tagName,
+ });
+
+ // separator after each tag
+ // except if it's the last tag
+ // and there are no customizations
+ if (i < tags.length - 1 || callbackResults.length > 0) {
+ buffer += separatorSpan(currentIndex);
+ currentIndex++;
+ }
}
}
- if (customHtml) {
- buffer += customHtml;
+ // add custom results with separator
+ for (let i = 0; i < callbackResults.length; i++) {
+ buffer += callbackResults[i];
+
+ // don't add separator to the last item
+ if (i < callbackResults.length - 1) {
+ buffer += separatorSpan(currentIndex);
+ currentIndex++;
+ }
}
buffer += "</div>";
}
+
return buffer;
}
diff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
index b1cfd1683bc..0d892f896c4 100644
--- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js
+++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
@@ -31,6 +31,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"parent-category-row-class-mobile",
"post-menu-buttons",
"small-user-attrs",
+ "tag-separator",
"topic-list-class",
"topic-list-columns",
"topic-list-header-sortable-column",
diff --git a/app/assets/javascripts/discourse/tests/acceptance/search-test.js b/app/assets/javascripts/discourse/tests/acceptance/search-test.js
index e559cee82fe..65363cf2c98 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/search-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/search-test.js
@@ -777,7 +777,7 @@ acceptance("Search - with tagging enabled", function (needs) {
assert
.dom(".search-menu .results ul li:nth-of-type(1) .discourse-tags")
- .hasText("devslow", "tags displayed in search results");
+ .hasText("dev,slow", "tags displayed in search results");
});
test("initial options - topic search scope - selecting a tag defaults to searching 'in all topics'", async function (assert) {
diff --git a/app/assets/javascripts/discourse/tests/acceptance/tags-test.js b/app/assets/javascripts/discourse/tests/acceptance/tags-test.js
index b77bb769c3b..ef2584810cb 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/tags-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/tags-test.js
@@ -1,5 +1,6 @@
import { click, currentURL, fillIn, visit } from "@ember/test-helpers";
import { test } from "qunit";
+import { withPluginApi } from "discourse/lib/plugin-api";
import {
acceptance,
queryAll,
@@ -710,3 +711,37 @@ acceptance("Tag show - topic list without `more_topics_url`", function (needs) {
assert.dom(".topic-list-bottom .footer-message").exists();
});
});
+
+acceptance("Tag separator customization", function (needs) {
+ needs.pretender((server, helper) => {
+ server.get("/latest.json", () => {
+ return helper.response({
+ topic_list: {
+ topics: [
+ {
+ id: 42,
+ tags: ["feature", "bug", "dev"],
+ posters: [],
+ },
+ ],
+ },
+ });
+ });
+ });
+
+ test("applying a value transformation for custom tag separator", async function (assert) {
+ withPluginApi("1.34.0", (api) => {
+ api.registerValueTransformer("tag-separator", () => {
+ return " | ";
+ });
+ });
+
+ await visit("/latest");
+
+ assert
+ .dom(
+ ".topic-list .topic-list-item:first-child .discourse-tags__tag-separator"
+ )
+ .hasText(" | ", "custom separator is displayed between tags");
+ });
+});
diff --git a/app/assets/stylesheets/common/base/tagging.scss b/app/assets/stylesheets/common/base/tagging.scss
index 04df862960e..63fafa1bac4 100644
--- a/app/assets/stylesheets/common/base/tagging.scss
+++ b/app/assets/stylesheets/common/base/tagging.scss
@@ -118,15 +118,6 @@
}
}
-.discourse-tags,
-.list-tags,
-.search-category {
- .discourse-tag.simple:not(:last-child)::after {
- content: ",\00a0";
- margin-left: 1px;
- }
-}
-
.d-header .topic-header-extra {
.discourse-tags {
font-size: var(--font-down-1);
@@ -136,6 +127,10 @@
.discourse-tags {
display: inline-flex;
flex-wrap: wrap;
+
+ &__tag-separator {
+ margin-right: 0.25em;
+ }
}
.fps-result .add-full-page-tags {