Supported formatting in posts (markdown, BBCode, and HTML)

There are three things to consider.

  1. What Markdown is allowed?

  2. What BBCode is allowed?

  3. What HTML is allowed?

All 3 can be mixed and merged to some extent.

Markdown

Our implementation uses Markdown-it.

You can see a complete interactive list of the formatting syntax here: https://markdown-it.github.io/

BBCode

We support a subset of “common” BBCode.

As for what BBCode is supported, the best place to look is the tests (app/assets/javascripts/discourse/tests/unit/lib/pretty-text-test.js), and here’s what I see there:

[b]strong[/b]
[i]emphasis[/i]
[u]underlined[/u]
[s]strikethrough[/s]
[ul][li]option one[/li][/ul]

[quote="eviltrout"][/quote]

[img]http://eviltrout.com/eviltrout.png[/img]
[url]http://bettercallsaul.com[/url]
[email]eviltrout@mailinator.com[/email]
[code]\nx++\n[/code]

Custom BBCode specific to Discourse, needed for functions that don’t fit into Markdown, but do fit into BBCode.

[spoiler]it's a sled[/spoiler]
[quote="eviltrout, post:1, topic:2"]

HTML

Markdown specifies that it works seamlessly with HTML, so we do.

However, we only support a “safe” subset of HTML.
See the list of supported tags and attributes here: discourse/allow-lister.js at main · discourse/discourse · GitHub

22 Likes

None of these references mention tables.

1 Like

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:

4 Likes

Thanks a lot for the useful references!

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