đź’» </> How to format code in posts and articles

Posting code or preformatted text on the forums

Have a line of code you want to share on the forums? Perhaps you need to share an error log or file in your mod? Reading code without formatting can be tough, if not impossible if the code depends on spacing (looking at you, Python). So here your best friend is the code formatting button in the toolbar editor:


Simply write or copy/paste the code in the editor and click that button.

  • Inline code will look like:
    Here’s some inline code <script>Hello world</script>
  • And blocks of code:
  <p>Before the script...</p>

  <script>
    alert( 'Hello, world!' );
  </script>

  <p>...After the script.</p>

Make sure you leave line breaks before and after the block of code!

How does that work?

If using the button is annoying you can use Markdown, BBCode or HTML. The below guide focuses on Markdown, but the editor also support a subset of BBCode and HTML, so pick whichever style you like best.

Inline code formatting

To format a specific section of text, use a single back-tick on each side of the code. This can be done within regular text, or when the code is on its own line. Inline formatting does not apply any automatic code formatting (see below), and only applies the “code style” to the text between back-ticks.

Example 1:

Typing:

`This is a line of code`

Produces:

This is a line of code

Example 2:

Typing:

Here’s some text with `a bit of code` inside!

Produces:

Here’s some text with a bit of code inside!

Block code formatting

To format a block of code use 3 backticks (```) on their own line above and below your code. Be sure there are no spaces on the lines with the backticks, or this will not work.

Example:

Typing:

  
```
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
```
  

Produces:

public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}

Automatic code styling

The editor will try to automatically detect what language you are typing and style your code accordingly. This might be nice for sharing code, but tends to produce strange results when sharing other preformatted text. To avoid the automatic styling, declare your code to be “raw text” by typing text after the first 3 back ticks.

Example:

Typing:

  
```text
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
```
  

Produces:

public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}

In addition to text , you can manually choose the formatting of a code block with the following selectors: apache , bash , coffeescript , cpp , cs , css , diff , handlebars , http , ini , java , javascript , json , makefile , markdown , nginx , objectivec , perl , php , python , ruby , sql , xml.

See also: