Code fence warning

In order to reduce the number of posts made by newcomers with un-fenced code, is it possible to add some type of warning like:

  • a pinned thread about how to include code
  • scanning posts for keywords like def, print(, or import before posting, and suggesting to add code fences
  • A default post template that includes code fences and a snippet + explanation
  • ???

Sorry Terry, what’s “fenced code”?

Do you mean code that is indented by four spaces?

def func(arg):
    print(arg)

or is there some other way I should be marking code?

Aside from that, I would support the idea of a pinned thread etc. Could
it also reference supplying a minimal working example?

http://sscce.org/

http://www.catb.org/~esr/faqs/smart-questions.html

Using three “`” characters to mark the beginning and end of code blocks. Comes from Markdown (or some extension of) I believe

```python
def func(arg):
    print(arg)
```

Is this (only/mostly) an issue on the “users” category (which I have muted)? I don’t see the same problem anywhere else…

It seems to me like it’s a subset of the general problem of “people don’t know how to / aren’t willing to take the time to format their messages properly” - possibly combined with a little bit of not knowing Markdown (although the toolbar should limit the impact of the latter).

I’m not sure about that… GitHub has a toolbar in the comment text box, yet we see issues filed without using code blocks all the time. I think most people neither know that’s a thing, nor care enough to ask when the format obviously looks wrong after submission.

In my experience, yes.

Is it possible to make custom bots? If so, maybe we could create a bot that sends a private message to a user if they post what looks like code without formatting it.

A potentially perfect solution: Someone over at Discourse has developed a Code in Post Detector. It pops up a message saying “It looks like your post may contain formatted code” and gives the markdown for block code. The message content can presumably be customized.

The announcement contains links to its GitHub repo and some installation instruction. The most recent GitHub repo activity was last month, the March 2019 thread announcing the plug-in was active up to this past January 2022. The plug-in is endorsed/sponsored by Sam Saffron, a Discourse co-founder.

You can access the demo HERE.

3 Likes