Forum Suggestion: Unformatted Code Warning

Forum Suggestion: Unformatted Code Warning


It seems like a very high percentage of newbie posts don’t format their code properly, leading to all sorts of weird formatting errors, lots of first replies being “I can’t answer until your code is readable”, and lots of mods having to go back and help people add in backticks.

Seems to me like the best way around this would be automatically detecting when a post has code that isn’t enclosed in backticks and warning the user.

I’ve thrown together a quick proof of concept here:

I’m sure the regex/conditional could be made smarter, though it should work for most cases as is. It will do the right thing for any of the following:

.someClass {
  font-family: "Comic Sans MS";
  color: fuchsia;
<h1 class="myHeader"></h1>
`function alreadyFormattedCode() {

…but not for something like the following:

`I love backticks`, but actually my code is <tag>over here</tag>!

Could something like this be implemented?


Solid. It wouldn’t take much to make the plugin catch almost all of the unformatted code here, but even if it only caught 50-60% this could be a huge boon.


I like this idea, but isn’t “[code]” and “[\code]” also a valid way of formatting code here?


Doesn’t work for me :confused:


I know that’s used by Phpbb forum.


It wouldn’t necessarily catch all languages (the most common ones in freeCodeCamp could) because they’re so many of them, but it would be beneficial. If I recall correctly this forum is run on Discourse though a plugin to do this may be possible. Maybe it could be an open source plugin for any Discourse forum to run :slight_smile:

var a=10;

Sorry the closing tag is [/code]

But yeah anything that helps the mods catch unformatted code would be a big plus. This just might be something to look out for (Although from the sound of it I may be the only one using this :slight_smile: )


Modified the regexes/conditional to account for this.

That’s a great idea! Unfortunately, looking at info I can find on creating Discourse plugins, it seems like the threshold is pretty high (this “beginner’s guide”, for example, starts by recommending you read another guide that tells you how to set up the environment, which itself looks to be a fairly extensive process).

From what I can glean from the front-end using Chrome dev tools, it looks like the Rails backend serves up Ember components with dynamically generated IDs, so anything as simple as document.querySelector('#id') wouldn’t even work for selecting DOM elements. Though I guess refactoring the static JavaScript would be a piece of cake for a seasoned Rails developer.

Wonder if we have any of those around here? :thinking:


This would be an interesting idea on freeCodeCamp’s open source for good.


I’ve done a few personal projects in Rails. Currently too busy to start something new, but I’ll keep it towards the top of my todo list unless someone else steps in.

How is the forum doing?