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.

Edit: Discourse theme component

7 Likes

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.

2 Likes

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:

1 Like
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: )

1 Like

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.

2 Likes

Serendipitously enough, I learned Ember recently for unrelated reasons, and Discourse now has “theme components” as a lightweight, front-end only alternative to plugins, so I hacked something together. @QuincyLarson @raisedadead any chance we could get this added to the forum?

1 Like

Yes, as soon as we can get more confirmation that this would be help.

Definitely + 1 on this.

@lionel-rowe this may/may not be a good idea, and I haven’t looked at how this works, but would it be possible to target the code block button in the text box when that dialog is closed so that it could be highlighted, eg add click handler to confirm button -> onclick add inline style to code button that changes its colour to something obvious then transitions out after a second or so

I think it would be doable, though I’m not sure how easy without resorting to hacky document.querySelector stuff (which is definitely not “the Ember way”, haha). The other thing is that the message would need to change, as currently it only mentions the `backtick` method.

1 Like

Honestly, even if it only catches a relatively small portion of cases it would be a huge help as long as it doesn’t hit a bunch of false positives.

Seems like a reasonable idea, though I think Ariel has a good point, false positives may make the situation annoying for users.

Thanks for the feedback everyone.

I’ll have this plugin deployed in our next maintenance run first thing in morning my time around 11:00 am (UTC+5:30)

@lionel-rowe
Do I have to do anything additional apart from the installation instructions you already have?

@raisedadead Awesome! No, I think installation should be fairly simple. The only gotcha I can think of is that theme components have to be enabled individually for each theme (light and dark).

If anyone encounters any, please let me know in this thread or open an issue on the relevant GitHub repo.

2 Likes

Hi Everyone!

I have installed the component on the themes, can we have a few people test it out and let us know of the feedback.

/cc @moderators

Thanks.

2 Likes

I think we have set this to not trigger for users above trust level of 3 (which makes it less annoying).

To test it out, you probably need to sign up with a new account (throw away one)?