The discussion explores methods to customize content within Discourse posts, focusing on using data-* attributes and the [wrap] BBCode for styling. Users inquire about adding custom classes to elements, with confirmation that only whitelisted HTML is allowed and that adding new classes requires a plugin. The allowList() function is highlighted as the current method for whitelisting attributes, replacing the deprecated whiteList(). Examples include using span with custom classes and creating a custom BBCode plugin for nolinkify. The conversation also references relevant GitHub files and the Discourse plugin system for extending functionality. Canapin, jimkleiber, SaraDev, and RGJ contribute insights on implementation and best practices.
[wrap="indent"]some text[/wrap]
It will create either a div or a span (if there is other content on the same line), containing the following attributes: class="d-wrap" and data-wrap="custom"
CSS
[data-wrap="custom"] {
color: pink;
}
Are there other ways to customize some chosen content in a post I’m not aware of?
Also, is there a way to add a custom class to an element? Couldn’t find anything in the search. I guess this is not possible?
I’d also be curious to see which HTML tags work in the composer. I think I had seen it somewhere before but couldn’t find it earlier today.
For example, I was trying to add a <button class="success">Click me</button> to the composer, it showed in the preview but failed when I posted it. I think some HTML tags work, just not sure which ones.
It is possible to uses some HTML classes in Discourse, however, most HTML in a post is sanitized for security reasons, and only very strict whitelisted HTML is allowed in markdown. To add classes to the white-list you must use a plugin, the sanitization happens server side as well as client side. Check out
Whitelisting HTML tags / attributes for some details on where to add the whitelisted attributes, and I believe it would look similar to something like this: Discourse HTML Whitelist. and as @RGJ has mentioned, please note that the whiteList() function is deprecated and it is calledallowList() nowadays.
Regarding other ways that you could customize content, posts are widgets, and Discourse themes have the ability to decorate widgets so you could leverage on that.
There are only a few lines of code in my plugin and I don’t really know what it’s doing and if all is needed, but at least I can add spans with a nolinkify class.
In my case, the purpose was to easily “unlinkify” words in posts with Auto-Linkify Words (it only accepts tags and classes to prevent linkification), and especially words in titles HTML tags while using DiscoTOC - automatic table of contents
I also tried a bbcode syntax based solution like this: