This article explains how to use Markdown, BBCode, and HTML to post formatted text in Discourse.
Required user level: All users can use it
Original official link: Posting code or preformatted text
When you want to share code in Discourse, or post error logs on Discourse. If we don’t format the code, it will be very difficult to read, and many codes contain spaces and other characters, which will deviate from their original meaning during content conversion.
Summary
In this guide, you will learn:
- How to format inline code.
- How to format single-line code.
- How to format multi-line code.
- How to use BBCode and HTML for code formatting.
- How to disable automatic code formatting.
Inline Code Formatting
To format inline code or special text, use single quotes before and after the content to be formatted.
This can be within a text, or the code can be on another line.
Inline code formatting will not apply any formatting to the code to be formatted (see examples below), only the code style will be applied to the code to be formatted.
Example 1:
Input:
This is a line of code
Output:
This is a line of code
Example 2:
Input:
Here’s some text with a bit of code inside!
Output:
Here’s some text with a bit of code inside!
Single-Line Code Formatting
If you need to format code on a single line, just add 4 spaces before the code on that line.
All content on a single line will be formatted as code until the beginning of the next line.
Example:
Input:
All text after 4 spaces will be formatted.
Output:
All text after 4 spaces will be formatted.
Multi-Line Code Formatting
For multi-line code, if you still use single-line code formatting, the whole process will be very troublesome.
Especially many codes use spaces for indentation.
Using single-line space code formatting will affect the indentation of the entire code block.
At this time, you can use 3 single quotes (backticks (```)) as the beginning of the code, and these 3 single quotes need to be on a separate line.
Also, there should be no spaces before these 3 single quotes, otherwise the entire code block may not be formatted.
Example:
Input:
public class CodeFormatting {
public static void main(String args) {
System.out.println(“I can format code now!”);
}
}
Output:
public class CodeFormatting {
public static void main(String[] args) {
System.out.println("I can format code now!");
}
}
BBCode and HTML
In addition to Markdown formatting, Discourse also supports BBCode and HTML.
For HTML, use <pre> and <code> tags:
<pre><code>
public class CodeFormatting {
public static void main(String[] args) {
System.out.println("I can format code now!");
}
}
</code></pre>
For BBCode, use the [code] tag:
[code]
public class CodeFormatting {
public static void main(String[] args) {
System.out.println("I can format code now!");
}
}
[/code]
For Specific Programming Languages
By default, Discourse automatically detects the language used based on your input format.
Of course, you can also manually specify the language for the code block by entering the language you want to format after the 3 single quotes.
``` your_language
# Your code here
Example:
For the Ruby language:
Input:
``` ruby
3.times do |stuff|
stuff.do
end
**Output:**
``` ruby
3.times do |stuff|
stuff.do
end
Supported languages must be among the highlighted languages supported below.
The list of supported languages can be accessed on GitHub.
Of course, you can use the default code lang setting to configure the default language.
No Syntax Highlighting
If you do not want the input text or code block to use any syntax highlighting, use text instead of the language selection.
Example:
Input:
``` text
Your text to NOT highlight here
**Output:**
``` text
Your text to NOT highlight here
Best Practices
- For inline code, always use single quotes to wrap the text you want to format.
- If you want to share multiple lines of code, format the code.
- For text and logs, avoid using automatic code styles, as these texts are mostly plain text, and use text formatting instead.
- When you find that the automatic style does not correctly recognize the code, remember to specify the correct language for your code.