I recently set up my own Discourse forum: https://crucible.hubbe.net/ for the most part I’ve been very happy with it. The community is for an arduino-type board used mostly for prop makers. As such, we end up using a lot of templated C++ code. In particular, we use this thing called a “style” which configures how lights show up. Styles can be complicated, so I wrote an online previewer/editor, then I used the discourse-linkify theme component to make it so that styles are automatically linked to the editor. I had to make some minor changes to the discorse-linkify theme component to make it quote URL characters and stuff, which was easy enough, and I can create a PR for those changes if there is interest.
However, there is a problem…
Some of the templated code ends up looking a bit like HTML because of all the < and > characters, and at some point Discourse removes some of those “tags”. Basically, it seems like any unknown word encased in < > gets removed. The next line in this post will be < foo > but without the spaces:
< - the foo is here
At first I thought it was the linkfy component that was doing something wrong, but after some digging, it seems that the missing stuff is already gone by the time linkify runs. So I guess the extra tags evaporated somewhere in the cooking process?
I noticed that in code blocks (triple-backtick and similar) the tags do survive, but for my purposes it would be better if they always survived.
For a while I thought that it would be sufficient to modify discourse/lib/utilities.cs:CODE_BLOCKS_REGEXP to make this work, but inCodeBlock isn’t used from a lot of places, so maybe that’s wrong? Also, I have not figured out how to actually modify CODE_BLOCKS_REGEXP from a plugin or theme component yet.
What code is actually responsible for removing these tags?
What is the best (most supported) way to disable it?
I should also point out that because people sometimes paste big blocks of code, it can be very difficult to tell that some parts in the middle went missing. At the very least, it would be better if unknown tags were turned into blinking warning signs or something that lets the user know that something unexpected happened.
You will have to educate your users, but this is your job as a moderator, plus it will help every user who has to deal with markdown later down the line.
You could do this by adding a new global pinned topic, something like “How to embed code on this site” or “How to type on this site”.
Because I have better things to do than to hurd cats?
If there is an easy way and a right way to do things, people will always pick the easy way. I suppose if I could actually prevent people from putting StylePtr<> templates outside of pre-formatted text, then people would be forced to do the right thing, but how do I do that? (Also, it feels like a very heavy-handed solution, as it could also prevent perfectly valid ways to talk about StylePtr<> templates.)
My current solution for linking StylePtr<> templates using linkify also doesn’t work on pre-formatted text because the DOM is very different, but that’s a minor problem that I can probably fix with a little coding.
Maybe. I think what I would do is to use this pattern to automatically add the backticks if they aren’t there, and then use a post-cooking callback for linking. Unless I’m mistaken, there is no way to add a link in the middle of some pre-formatted text otherwise.
Encouraging users to use backticks for inline code and 3 backticks for code blocks is the best solution. Maybe create a pinned topic about that in your forum?
The unformatted code detector theme component seems promising.
Then I just have to make a theme component that handles the linking inside preformatted code, which I was planning to do anyways. I will definitely give this a try.
Turns out that all I had to do was to enable multiline regexp support in the linkify module to make it work more like I wanted to. So I think I’m good, assuming people actually pay attention to the unformatted code detector.