Users pasting unformatted code results in unwanted styles


we have a lot of this Bounce not Working - Support - phpList Discuss where people post code in from some config file and all the commented out lines beginning with a hash become #bold - I guess due to the markup

is there some way to change this? It’s driving me nuts :confused:

Use an actual code tag?

Here's some code
# with comments
1 Like

ok, so I will personally do that, but other people are not doing that so I have to keep going in and editing.

I guess that leaves me with two possible actions:

  1. if discourse could differentiate between code and normal text it could suggest they use the code tag OR
  2. if we can change the markup for bold to something that is not a hash

Users will have bad results if they paste in raw code without trying to format it as code. There is no way to work around this, no magical AI that can “detect” code and format it for the user.

Two ways:

  1. Use the ``` triple backticks at the top and bottom to mark a code block

  2. Select the code then press ctrl+k (or use the toolbar button) to indent it all by four spaces


While it’s true that it would be very hard to impossible to actually detect something like code in pasted content and educating a user about that kind of content-handling would probably suffice it could still be possible to add some kind of “weird content paste”-detection. Let my try to explain:

Something like code-blocks is typically pasted into a response. Since discourse is automatically doing a markdown transformation for the live-preview it should be possible to verify what kind of content has been pasted. If it for example consists of mostly headlines (e.g. 10 headlines and like only 2 paragraphs) it could be possible that a user pasted text which should actually be formatted in a different way (in this case as a code-block). The editor could then show a small notification telling the user that some more work should be done, before actually saving the message.

Still this kind of detection would be very hard to implement and might even use lots of computing resources on the client, which especially for mobile devices might just be too much.

I will just have to see how this evolves. So far everyone pasting code is not using code tag and they don’t take any action despite the massive bold headlines showing up in the preview window. I might just have to hack in some kind or note some where in the post window, or make a popup notice or something. Am surprised if it is just the phpList community that have an issue with this however. Guess most people are pasting from the config.php though, which has a lot of commented out sections by nature.

For your particular site possibly an interesting approach would be to run all “text chunks” pasted in via 4-5 regexes. For PHPs case you could quite easily come up with a few regexes that would be very accurate at predicting that a chunk pasted in is PHP. Then the editor could auto wrap. This would not have any performance impact, but hooking it in would be a bit tricky.

A more general approach as proposed by @tDo may be a bit harder to come up with. Clearly if we ever shipped something like this it would have to be opt in.

1 Like

Another very interesting thing @gingerling that you can try is giving users a “silver” badge first time they add a code block. It can create some incentive to learn how to use the system. (this can be easily automated with the current badge system)


I know we have discussed this before with @cpradio … we did eventually implement something like this on Stack Overflow but it is kind of tricky. And that was a site dedicated to code in a way that Discourse isn’t, necessarily.


interesting - i will discuss this with our developers.

I think for now, I can edit, select, control K, and this will do.

Part of our issue, which is why:

might not work, is that we have many people who only ever ask one question and very few returning visitors.

Good news! We have an official component for this now