Discourse Guide: Code Formatting


#1

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). This guide focuses on Markdown, but the forums 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!

Entire line code formatting

To format an entire line, add 4 spaces at the start of a line. All text until the next line break will be code.

Example:

Typing:

    All text after 4 spaces will be formatted.

Produces:

All text after 4 spaces will be formatted.

Block code formatting

While you can use the 4-space method to produce multiple lines of code, this can be tedious, especially if you are copying and pasting and need to type 4 spaces on multiple lines of text.

Instead, 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 forums 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.


Latest beta won't start (11.jun.2017)
'Invalid Manifest' my first mod
#2

Someone needs to get the Discourse coders to add Lua for us :smile:


#3

It should just be a site setting (no need to involve coders). Perhaps @Relyss could add lua to the list in site settings. (highlighted languages setting).


#4

It says including too many languages can impact performance. But I could always remove languages that we don’t use often here too :thinking:


#5

Looking at the list, I don’t know any reason there would be objectivec, coffeescript, or apache code shared. Also, on another site I admin we’ve added 3 languages and haven’t seen any performance difference. I’d like to assume that Discourse allows you to add at least a few non-default languages without tanking performance.


#6

Testing…

function Shop:get_name()
   return self._sv.name
end

It works! :jubilant:


#7

Does it? There’s not formatting on that, it’s just raw text.
It works!


#8

I had to refresh the browser to see it, was showing as plain text first.


#9

Thought I did that…sorry!