For newly created labels, assign a random style

AI-generated summary

In a development discussion about automatically assigning styles to newly created tags, sheng_hualuo proposed using six predefined CSS classes (tag-style-1 through tag-style-6) and assigning one randomly upon tag creation — noting that manual assignment via the tag style component isn’t sufficient. Alteras suggested an alternative approach: instead of true randomness at creation time, use the plugin API’s replaceTagRenderer() to dynamically assign styles based on the tag name (e.g., encoding the tag name into a base-6 number and using modulo to pick a style 1–6). This method ensures consistent styling across users and avoids randomness, but achieves visual variety. The suggestion references the tag-icons plugin and points to the default tag renderer code in Discourse’s repository for implementation context.

I have created 6 css classes for tags: tag-style-1, tag-style-2, tag-style-3, tag-style-4, tag-style-5, tag-style-6. I want to randomly assign one of the above 6 classes to a new tag when I create a new tag, so that the newly created tag will have a different style. I know the tag style component is possible, but it needs to be added manually, not randomly assigned!

Not exactly random, and not at tag creation, but you can do something similar to the Tag Icons component and use the replaceTagRenderer() from the plugin-api to modify the html of the tags at view (see https://github.com/isabella232/discourse-tag-icons/blob/master/common/header.html).

A function like this would take the tag name, map the tag name to a number 1-6 (i.e. encode the string into base 6 and take the last digit/apply modulo), and add it to the HTML string. It wont be random since the style would be determined by tag name, but it would keep the style the same for all users

The original default render tag function is here: