[How to] Post correctly formatted and colorized code on the forum?

Continuing the discussion from Ruby backtick returns nothing!:

Triple backtick delimiter lines.


  • image

Backquote character key on US keyboard.


If it is error message text (or some other plain text that contains <, >, #, or other characters that can confuse Discourse markdown,) then specify “text” as a lexing language:

```text
paste error message here
```

If it is Ruby, then:

```ruby
# code here
```

If it is Javascript, then:

```javascript
/* js code here */
```

If it is HTML, then:

```html
<!-- html here -->
```

If it is C, then:

```c
// C code here
```

… etc. (Discourse code lexer recognizes the major programming languages.)

# Test
module Eneroth3::TestSyntaxHighlight
  def self.method_name
    puts "String"
    5+12
    yield false
    return true
  end
end

I had no idea this forum supported syntax highlighting :open_mouth: .

2 Likes

Yep! Discourse processes your post’s text through a Markdown language formatter, and implements this feature of Markdown. The Markdown formatter is also why posts of error messages and code can get so screwed up if people don’t use the triple backtick syntax!

2 Likes

Welcome Dylan … the wikipedia page (linked below) explains what markdown is in detail.

But it is basically a simple, human-readable text code that can be used to nicely format posting on the web or documents.

The forum system should have sent you a message explaining the basics of using markdown on the forum.
(Click your avatar menu at the top right, see if the welcome message from the system is there.)

1 Like