Unformatted Code Detector

I had a false positive on this component. I think it may be from the bullet point items? I made all the text lorem ipsum but the structure is the same:

Hi,

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

https://www.discourse.org

Lorem ipsum dolor sit amet, consectetur adipiscing elit:

* pull_request
* pull_request_review
* pull_request_review_comment

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis `nostrud` exercitation ullamco `laboris` nisi ut aliquip ex ea commodo consequat.

https://www.discourse.org

Lorem ipsum dolor sit amet:

* push
* commit_comment

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Thanks,
Martin
1 Like

That looks like a valid code request format to me? Is there any reason it wouldn’t be

  • item_with_underscores

e.g. as code?

2 Likes

I just wouldn’t expect it to detect things inside an ordered or unordered list as code, but if this is expected I guess it’s fine?

But isn’t pull_request, pull_request_review, etc actually code? I guess I need more specifics on exactly what was in that list… I’d view it as a correct positive, not a false positive?

1 Like

I would argue it’s not a correct positive because it is not a block of code, it is in a list. And those are GitHub webhook “actions” that I have listed. But not a huge deal I guess, just seemed odd to me and I thought I would report it in case.

It could be; it’s borderline in my view due to the underscores :man_shrugging:

Minor nitpick: The default for the warning_modal.content is asking for the “code” toolbar button, but this button is called the “Preformatted text” button in the editor when you hover over it with your mouse.

grafik

grafik

To make it easier for new users to find this button (they will not find any code button), the warning_modal.content should be changed from “code button” → “Preformatted text button”.

3 Likes

How can I add a link to a topic with further examples and instructions?

I tried simply adding it at the end of warning_modal.content, but that resulted in this (my additions marked yellow):

grafik

How can I add some text and a link below the the current content?

Edit:

I just noticed that changing even a single character in warning_modal.content breaks the formatting.

grafik

Even worse: Just clicking into the warning_modal.content input field and then moving the cursor by the arrow keys makes the input field highlighted. After clicking the green checkmark to save the “edited” warning_modal.content (no change at all has been done, just moving the cursor one character), the formatting is broken as shown above.

Edit # 2

Solved with the help of How do I escape a backtick ` within in-line code in Markdown? - Meta Stack Exchange

@codinghorror @lionel-rowe Maybe you should look into this and adjust the default warning_modal.content accordingly regarding spaces and backticks (missing spaces within the heavily with backticks equipped “multiplelines” section were causing the problems described above).

2 Likes

Is there any way we can make it more clear to the user which key to chose for the code fences if he is doing it manually (i.e. not via the button)?

Today I have seen this:

The user obviously tried to follow the instructions, but chose the wrong key for the code fences ( ' instead of `). In the past I have also seen ... instead of ```. Both cases indicate that users need more explicit instructions which key to choose.

Alternatively: Don’t confuse users with those keys and just say: Use the “Preformatted text” button and you are done.


@lionel-rowe How can I customize the detection behaviour?

Currently the shebang is not detected as code, and I would like to change this.

Expected behaviour: #! indicates the beginning of a script and therefore should be detected as code.

Example for non-detection:


#!/bin/sh

echo “test”

. /lib/upgrade/common.sh

firmware="/tmp/firmware.img"
tmpdir="/tmp/_upgrade"
output="/dev/ttyS0"
before=“before-upgrade.sh”
after=“after-upgrade.sh”


In addition to this, it would be useful for us if root@ would be detected as code.

root@OpenWrt:~# ip link add link eth0 name eth0.9 type vlan id 9
root@OpenWrt:~# brctl addbr br-foo
root@OpenWrt:~# brctl addif br-foo eth0.9
root@OpenWrt:~# ip link set eth0.9 up
root@OpenWrt:~# ip link set br-foo up

4 Likes

@david is there a way to customize the detection?

There isn’t any way to have per-site customizations at the moment, no. We could certainly look at adding shebang and shell prompt detection to the ‘code energy’ system though.

3 Likes

Thanks for raising this - looks like a core bug with multiline theme translations. I’ve made a PR to fix:

3 Likes

Most of Discourse sites use discourse as a troubleshoting tools, Is this plug-in is suitable for the following tasks?, not only for code:

Linux:

  • Logs
  • Linux Command CLI
  • Terminal Output

For Example:

Sensors:
  System Temperatures: cpu: 78.0 C mobo: 36.0 C gpu: nouveau temp: 56.0 C 
  Fan Speeds (RPM): cpu: 0 fan-1: 3139 fan-3: 0 fan-5: 0 
  Power: 12v: N/A 5v: 2.90 3.3v: N/A vbat: 3.34 

Regards.,

4 Likes

It depends how much the rules can be edited; I’m unclear how the rules are edited. Can you clarify a bit @david in the first post?

1 Like

I’m afraid at the moment the rules can’t be edited. The sensitivity can be adjusted, and html detection can be turned on/off.

There are some built-in rules which should help with these. If you’d like to try it out, try posting some code in a PM to yourself here on Meta.

However, I think the example you posted is unlikely to be detected, since it doesn’t contain any special characters or timestamps which match our detection patterns.

4 Likes

The original vision I had was for a component where you could specify how much “energy” a given character (or complete string, such as C:\) adds, especially as that character (or string) begins repeating in the same nearby area over and over, multiplying the code energy score.

Sensors:
  System Temperatures: cpu: 78.0 C mobo: 36.0 C gpu: nouveau temp: 56.0 C 
  Fan Speeds (RPM): cpu: 0 fan-1: 3139 fan-3: 0 fan-5: 0 
  Power: 12v: N/A 5v: 2.90 3.3v: N/A vbat: 3.34 

This is rather colon heavy. So ideally if you wanted to tweak this, you’d set an energy level for the colon, and a repetition bonus.

I guess that is not possible at the moment, it is unfortunate all the rules are hard-coded.

5 Likes

We make use of the unformatted code detector in the OpenWrt forum, where we have lots of logs and CLI stuff. Working pretty well for us!

2 Likes

Just to clarify @tmomas’s note - We use a custom version of this plugin with some additional code-matching patterns not in the stock version. We also added a bit of user-interface to add (albeit unchecked and with much caution), custom regex patterns. Refs:

3 Likes

Does this also check for unformatted JSON? I’ve installed this plugin and added it to my theme, then tested with code examples (like the example in the gif) as well as JSON and I cannot get it to detect and warn me.

1 Like

I’ve just encountered an issue with trying to edit a post with an indented codeblock. It appears this theme component does not like that style anymore and insisted on backticks.

2 Likes