Formatting posts using markdown, BBCode, and HTML

:bookmark: This reference guide details all the formatting options available in Discourse posts, including markdown, BBCode, and HTML. It specifies what is supported and provides resources where you can see examples.

:person_raising_hand: Required user level: All users

Understanding supported formatting in posts

While plain text is sufficient for most replies, Discourse allows users to enhance the formatting of their posts by using markdown, BBCode, and HTML. This guide explains what types of formatting are supported and how you can use them effectively.

You can also practice some of the basics in an interactive tutorial by sending a personal message to @discobot here or on any Discourse site.

Summary

This guide covers:

  • Supported markdown syntax
  • Supported BBCode tags
  • Supported and safe HTML tags and attributes

Best practices

  • Use markdown primarily for its simplicity and readability.
  • Mix markdown and BBCode only when necessary, and keep usage consistent.
  • Limit the use of HTML to simple, safe tags to avoid formatting issues and ensure posts are secure.

Supported markdown

Discourse uses markdown-it for its markdown implementation.

Some commonly used markdown features include:

  • Bold: **text**
  • Italic: *text*
  • Link: [title](https://example.com)
  • Inline code: `code`
  • Highlight: <mark></mark>
  • Code block:
    ```
    code block
    ```
    

In longer posts (like this one), it can also be helpful to add structure using headings:

# Heading 1
## Heading 2
### Heading 3

Note: when an image is uploaded, the markdown syntax to reference the image will automatically be generated.

![caption|500x500](upload://image.jpeg)

For a full list of markdown features, please refer to the interactive guide.

Supported BBCode

Discourse supports a subset of common BBCode tags, some of which are generated automatically to support special formatting, for example when you quote a post in your reply:

[quote="user"]quoted text[/quote]

You can also use BBCode to mark hide text as a spoiler or as details

spoiled text

[spoiler]spoiled text[/spoiler]
click to expand

hidden text

[details="click to expand"]hidden text[/details]

BBCode tags for simple formatting supported by markdown or are also supported:

  • [b]strong[/b]
  • [i]emphasis[/i]
  • [u]underlined[/u]
  • [s]strikethrough[/s]
  • [ul][li]option one[/li][/ul]
  • [img]http://example.com/image.png[/img]
  • [url]http://example.com[/url]
  • [code]code[/code]

For a complete list of supported BBCode tags and examples, check the Discourse BBCode tests.

Supported HTML

Discourse supports a safe subset of HTML. While you can mix HTML with markdown, only specific HTML tags and attributes are allowed to ensure security and consistent styling.

Some of the allowed HTML tags include:

  • <a href="https://example.com">link</a>
  • <strong>bold text</strong>
  • <em>italic text</em>
  • <ul><li>list item</li></ul>
  • <img src="https://example.com/image.png" alt="description">

For a detailed list of allowed HTML tags and attributes, refer to Discourseā€™s allow-list.

:information_source: Note: Inline styles, like <span style="color:red">colored text</span>, are not supported. For custom styles, see customize post contents.

Common issues and solutions

  • BBCode or HTML tags not working: Ensure the tags you are using are part of the supported subset.
  • Inline styles being stripped: Discourse does not support inline styles for security reasons. Use custom styles or plugins if necessary.
  • Formatting changes on pasting from other editors: Some rich-text formats may not render correctly. Use markdown to format text consistently.

FAQs

Q: Can I use tables in Discourse?
A: Yes, you can use markdown to create tables. For more information, see this guide on tables.

Q: Why isnā€™t my inline style working?
A: Discourse does not support inline styles in HTML for security reasons. Consider using custom BBCode or the Discourse custom styling guide.

Q: Can I add custom BBCode tags?
A: Yes, you can add custom BBCode tags. Refer to the Discourse BBCode plugin for more information.

Additional resources

Last edited by @MarkDoerr 2024-07-11T00:31:08Z

Check documentPerform check on document:
25 Likes

None of these references mention tables.

2 Likes

That is true but this other topic does. Add a table to your post using markdown

2 Likes

Text color and text background color doesnā€™t seem to work with <span> or bbcode, am I missing something?

  • <span style="color:red">some red markdown text</span>
    • some red markdown text
  • bbcode: [color=red]Red color text[/color]
    • [color=red]Red color text[/color]
1 Like

yes, looks like HTML inline style is not supported

You can make use of this guide:

(And see it in action here: šŸ“œ [Wiki] Schlumpf hub serial numbers reference - Unicycles and Equipment - Unicyclist.com)

Or use this plugin that extends BBcode support:

As for the allowed HTML tags, I believe this is the reference:

Though I donā€™t find table HTML tags (which are allowed) inside for some reason.

Maybe because they are referenced here:

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/pretty-text/engines/discourse-markdown/table.js#L31

4 Likes

Thanks a lot for the useful references!

1 Like

It looks like currently, the filter is set up so that the rowspan and colspan attributes on td and th elements are filtered out. My understanding is that these are safe (and I believe they are very useful): would the team be open to allowing these attributes?

As a sidebar, @Canapin talked about the location of the allowed list of table tags.

The .../discourse-markdown/table.js file, though, seems to be an implementation around specifically Markdown tables; would regular HTML tables need a separate extension to allowList?

I believe rowspan and colspan are pretty safe, but I also want to ask for one other piece of markup. Could we allow data-* attributes on table elements? I think this would be very useful for specifying different types of table contents, in particular to use as hooks for custom CSS.

1 Like

Hi :slight_smile:

I think this would be trickier than expected.
If you create an HTML table in your post, and someone quotes your table, itā€™s converted into Markdown which doesnā€™t support rowspan and colspan.

I suspect there would be other issues I didnā€™t think about.

Itā€™s not exactly the same, but you can wrap your table with [wrap] tags :slight_smile:
See: Customize posts' contents with your own styles
But you wonā€™t be able to have data- attributes inside an HTML table (for example, to customize specific rows or cells).

edit: if your table uses Markdown instead of HTML, you can insert spans with data- attributes like this:

|Column 1 | Column 2|
|--- | ---|
|<span data-thing>test</span> | test|
|test | test|

But they will be removed if someone quotes or copy-paste your table.

1 Like

It would be really helpful if some simple and safe inline CSS styles were obeyed in raw HTML added to Discourse posts. There could be converted HTML posts from a previous forum engine and/or users sometimes paste in rich text with things like text and/or image alignment.

The following works as expected:


<div align="center">
<p>Test</p>
</div>

Test

But this doesnā€™t:


<p style="text-align: center;">Test</p>

Test


The same goes for inline <span style="color: ... formatting.

Thereā€™s more likely to be inline styles like that in converted / pasted content from other WYSIWYG editors.

The style attribute isnā€™t allowed in Discourse because it could easily break things.

Also, Discourse has a certain philosophy on how information show be presented in posts, and limits tags and attributes on purpose.

The align attribute on <div> is explicitly allowed here: discourse/allow-lister.js at main Ā· discourse/discourse Ā· GitHub

If you were to migrate a forum and wanted to keep some special styling like colors, Iā€™d go with Discourse BBCode and edit the importer if needed :slight_smile:

1 Like