Selecting the programming language used in code blocks

:bookmark: This guide explains how to select the programming language used in code blocks within Discourse posts, along with how to suppress syntax highlighting.

:person_raising_hand: Required user level: All users

When you enclose code using triple backticks in your posts, Discourse uses highlight.js to automatically detect and apply syntax highlighting to the code. However, you can specify the programming language if the autodetection does not work as expected.

Specifying the programming language for code blocks

To specify the programming language for a code block, type the name of the language right after the first set of backticks:

``` your_language
# Your code here
```

For example, to specify Ruby:

``` ruby
3.times do |stuff|
  stuff.do
end
```

The result will look like this:

3.times do |stuff|
  stuff.do
end

The language must be one of the highlighted languages configured for syntax highlighting on your site. If a language is not working, consult your site administrator.

Suppressing highlighting

If you want to prevent any syntax highlighting, you can use β€œtext” as the language:

``` text
Your text to NOT highlight here
```

The result will appear as plain text without any highlighting:

Your text to NOT highlight here

Best practices

  • Always specify the language if the automatic detection is not recognizing your code correctly.
  • For admins: Ensure to verify that the chosen language is configured in the highlighted languages site setting.

Additional resources

Last edited by @hugh 2024-06-25T05:53:26Z

Check documentPerform check on document:
10 Likes