# Content formatting when federating out

**URL:** <https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150>\
**Category:** Threadiverse Working Group\
**Created:** [April 27, 2024, 5:49pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150 "2024-04-27T17:49:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [April 27, 2024, 5:49pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/1 "2024-04-27T17:49:54Z")

</div>

I’ve mentioned this to you privately before but it wasn’t particularly important at this time.

… but it is now! 😅

When Discourse federates content out, it saves some simplified plaintext-first into `content`. Things like formatting and images are removed, block quotes are compressed into “author: text” lines, but most importantly, newlines are missing.

It leads to otherwise nicely formatted long posts to end up [quite unreadable](https://community.nodebb.org/post/https%3A%2F%2Fsocialhub.activitypub.rocks%2Fap%2Fobject%2Fdb75fd9d0320e0ba11dd6137b9c8ba77) (sorry a, I ended up coming back to Socialhub to read it 😅)

As far as I know right now there’s no recommendation to send simplified or sanitized content out, so all implementations I’ve seen just send raw html out as is and expect the remote end to sanitize if needed.

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 13, 2024, 2:17pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/2 "2024-05-13T14:17:25Z")

</div>

@angus gentle reminder about this issue 🙂

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 13, 2024, 2:18pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/3 "2024-05-13T14:18:36Z")

</div>

> [@devnull](#):
>
> newlines are missing

This was incorrect. Newlines are not missing, as evidenced by Mastodon properly showing them.

NodeBB strips out newlines because in markdown you need two to create a new paragraph.

Everything else is still problematic though.

---

<div class="post-metadata">

**Author:** ![angus](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/angus/32/2295_2.png) [@angus](https://socialhub.activitypub.rocks/u/angus)\
**Post date:** [May 13, 2024, 3:52pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/4 "2024-05-13T15:52:19Z")

</div>

Thanks for the bump! I’ll work on this one this week.

---

<div class="post-metadata">

**Author:** ![angus](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/angus/32/2295_2.png) [@angus](https://socialhub.activitypub.rocks/u/angus)\
**Post date:** [May 13, 2024, 5:30pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/5 "2024-05-13T17:30:43Z")

</div>

@devnull We should probably go through this with a bit more specificity. I intentionally stripped back a lot from standard Discourse content as I figured it would be good to start from a clean base and work up from there to ensure maximum compatibility. For reference, in case it’s helpful, our specs on this, which give various examples of what I’m mentioning below [are here](https://github.com/discourse/discourse-activity-pub/blob/16b443e32b8779d481b52ce3fffa72eaadae99d7/spec/lib/discourse_activity_pub/content_parser_spec.rb).

Give this markdown block.

```md
# First Header

## Second Header

### Third Header

#### Fourth Header

Paragraph

[Link](https://discourse.org)

```

This is the HTML we currently support:

```HTML
<h1>First Header</h1>
<h2>Second Header</h2>
<h3>Third Header</h3>
<h4>Fourth Header</h4>
Paragraph
<a href="https://discourse.org">Link</a>

```

We also add in `\n` for line breaks as you say.

First thing I’m going to do is simply add `<p>` tag support (which will solve the line break issue I think?). So

```HTML
<h1>First Header</h1>
<h2>Second Header</h2>
<h3>Third Header</h3>
<h4>Fourth Header</h4>
<p>Paragraph</p>
<p><a href="https://discourse.org">Link</a></p>

```

What would be the other HTML tags do you think we should add support for at this stage? Here’s some ideas:

```HTML
ul
ol
li
blockquote
b
code

```

\*\*edit

Ok, so I’ve added the above into the content parser, so for this markdown

```md
# First Header

## Second Header

### Third Header

#### Fourth Header

Paragraph

[Link](https://discourse.org)

> This is a quote

- This is an unordered list item

1. This is an ordered list item

``
This is a code block
``

```

(note there’s actually three code ticks, I just can’t get it to escape inside of another code block)

We’ll now have

```auto
<h1>First Header</h1>
<h2>Second Header</h2>
<h3>Third Header</h3>
<h4>Fourth Header</h4>
<p>Paragraph</p>
<p><a href="https://discourse.org">Link</a></p>
<blockquote>
<p>This is a quote</p>
</blockquote>
<ul>
<li>This is an unordered list item</li>
</ul>
<ol>
<li>This is an ordered list item</li>
</ol>
<code class="lang-auto">This is a code block
</code>

```

I’ve pushed a draft PR where you can see this in the example in the spec

> <https://github.com/discourse/discourse-activity-pub/pull/102/files>

Lmk if this works for you!

---

<div class="post-metadata">

**Author:** ![silverpill](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/silverpill/32/1770_2.png) [@silverpill](https://socialhub.activitypub.rocks/u/silverpill)\
**Post date:** [May 13, 2024, 5:58pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/6 "2024-05-13T17:58:38Z")

</div>

In the past Mastodon was removing almost all tags, but in version 4.2 they made their sanitizer less aggressive: [ActivityPub - Mastodon documentation](https://docs.joinmastodon.org/spec/activitypub/#sanitization)

Your HTML snippet looks mostly compatible with it. Only headings are not supported, but they should degrade gracefully.

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 13, 2024, 10:25pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/7 "2024-05-13T22:25:16Z")

</div>

That’s a good point… initially I was advocating for you to just send everything just as you would when rendering to the Discourse frontend, but that is a bit messy at times (e.g. extra tags, useless attributes, etc.) — I see this already with content from Mastodon.

It even comes with problems for us… e.g. link preview HTML is sent when it ought to just be the naked link, emoji are sent as images, etc.

It would be better to send a slightly stripped down version for things like federation. 👍

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 13, 2024, 10:40pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/8 "2024-05-13T22:40:33Z")

</div>

We use sanitize-html ourselves. It comes with a list of default tags that are safe to allow:

> **[GitHub - apostrophecms/sanitize-html: Clean up user-submitted HTML,...](https://github.com/apostrophecms/sanitize-html?tab=readme-ov-file#default-options)**
>
> Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis. Built on htmlparser2 for speed and tolerance - apostrophecms/sanitize-html

Perhaps you could follow suit? We’d probably run our content through something similar on the way out (we currently don’t do anything)

---

<div class="post-metadata">

**Author:** ![angus](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/angus/32/2295_2.png) [@angus](https://socialhub.activitypub.rocks/u/angus)\
**Post date:** [May 14, 2024, 6:40am UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/9 "2024-05-14T06:40:05Z")

</div>

Yeah, as with other quetsions, we’ll probably need some convergence on this question too. Partly due to the list of tags supported (e.g. compare Mastodon’s with yours), but also because of things like content and styling expectations.

For example `discourse/discourse` often wraps `blockquote` in an `aside`.

```auto
<aside class="quote">
<blockquote>
<p>I'm quoted text</p>
</blockquote>
</aside>

```

Which you can see for yourself here:

> [@devnull](#):
>
> Perhaps you could follow suit?

(i.e. a “quote” is often more than than the `blockquote` _per se_; it often also includes a user’s avatar, sometimes a topic link etc)

But even though your parser supports `aside` I’m not sure if you handle `aside` like that. In fact, I note that Mastodon doesn’t handle `aside` at all, so on that front at least, I’m going to always strip `blockquote` from its `aside` wrapper, as Mastodon’s parser would probably hit the `aside` wrapper and discard the whole block.

At this stage, before we do more testing, I think this is a good list to support? Any additions (or subtractions)?

```html
h1
h2
h3
h4
p
a
ul
ol
li
blockquote
strong
em
code

```

The above should work with both your parser and with Mastodon’s (post 4.2).

\*\*edit the only slight question marks in my mind about the above is that `discourse/discourse`

- effectively treats `em` as italicised text, and doesn’t use `i` tags.
- uses `strong` instead of `b`.

For example:

_I’m emphasised_

**I’m strong**

- @devnull how do you treat `em`, `i`, `b` and `strong`?
- @silverpill any thoughts on that front?

\*\*second edit I’ve actually removed `br` from the above list as even though it should be there, getting line breaks right is often harder than it first seems, and I think we should first establish a “non-controversial” baseline.

The above is live in this draft PR. When we agree on this list, I’ll push this up for review

> <https://github.com/discourse/discourse-activity-pub/pull/102/files>

\*\* third edit @pfefferle curious if you have any initial take on the above?

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 14, 2024, 8:56am UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/10 "2024-05-14T08:56:00Z")

</div>

We don’t handle `i` or `em` differently. Same with `strong`.

I believe both are allowed by the parser so we just let it through and let the browser render it as-is…

I believe our markdown parser wraps code blocks in both `pre` and `code`

---

<div class="post-metadata">

**Author:** ![silverpill](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/silverpill/32/1770_2.png) [@silverpill](https://socialhub.activitypub.rocks/u/silverpill)\
**Post date:** [May 14, 2024, 11:21am UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/11 "2024-05-14T11:21:20Z")

</div>

> [@angus](#):
>
> @silverpill any thoughts on that front?

I don’t expect `em` and `strong` to cause any issues.

Your set of allowed tags should work well with all popular micro-blogging services.

---

<div class="post-metadata">

**Author:** ![angus](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/angus/32/2295_2.png) [@angus](https://socialhub.activitypub.rocks/u/angus)\
**Post date:** [May 14, 2024, 12:20pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/12 "2024-05-14T12:20:23Z")

</div>

Thanks guys, this is now up for review. I’ll let and know when its merged and I’ve updated this site

> <https://github.com/discourse/discourse-activity-pub/pull/102>
>
> @pmusaraj See further https://socialhub.activitypub.rocks/t/content-formatting-w…hen-federating-out/4150/9

---

<div class="post-metadata">

**Author:** ![angus](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/angus/32/2295_2.png) [@angus](https://socialhub.activitypub.rocks/u/angus)\
**Post date:** [May 17, 2024, 8:07am UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/13 "2024-05-17T08:07:11Z")

</div>

@devnull Thanks for your patience on this! I’ve pushed this PR to the top of the internal priority list.

---

<div class="post-metadata">

**Author:** ![angus](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/angus/32/2295_2.png) [@angus](https://socialhub.activitypub.rocks/u/angus)\
**Post date:** [May 17, 2024, 1:23pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/14 "2024-05-17T13:23:42Z")

</div>

> [@angus](#):
>
> I’ll let and know when its merged and I’ve updated this site
> 
> [Add additional html support by angusmcleod · Pull Request #102 · discourse/discourse-activity-pub · GitHub](https://github.com/discourse/discourse-activity-pub/pull/102)

This is now merged and this site has been updated! The Discourse plugin will now federate these permitted html tags (i.e. converting their markdown equivalents to html).

> <https://github.com/discourse/discourse-activity-pub/blob/37b8ce17995e577567bd499e836742882d9dd9a3/lib/discourse_activity_pub/content_parser.rb#L38>

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 17, 2024, 2:05pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/15 "2024-05-17T14:05:50Z")

</div>

Woohoo! Can’t wait 🙂

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 17, 2024, 3:56pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/16 "2024-05-17T15:56:53Z")

</div>

Hi @angus, looking good so far, although `<pre>` is still missing.

e.g. [How do you use `context` (if at all)? | NodeBB Community](https://community.nodebb.org/post/https%3A%2F%2Fsocialhub.activitypub.rocks%2Fap%2Fobject%2F5bf44f7e1014a52bf81308f1c86de3ed)

---

<div class="post-metadata">

**Author:** ![angus](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/angus/32/2295_2.png) [@angus](https://socialhub.activitypub.rocks/u/angus)\
**Post date:** [May 17, 2024, 4:27pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/17 "2024-05-17T16:27:50Z")

</div>

In which contexts do you use `pre`? I believe Discourse only uses it to wrap multiline code blocks. But I wasn’t 100% on that (or how other platforms use it) so I didn’t include it. We can add it, I just wanted to deal with it separately.

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 17, 2024, 6:21pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/18 "2024-05-17T18:21:47Z")

</div>

> [@angus](#):
>
> I believe Discourse only uses it to wrap multiline code blocks.

Exactly that ☝

NodeBB (via markdown-it) renders code blocks like so:

```html
<pre class="markdown-highlight"><code class="lang-js hljs language-javascript" data-lines="1"><span class="hljs-keyword">const</span> a = <span class="hljs-string">'test'</span>;
</code></pre>

```

---

<div class="post-metadata">

**Author:** ![devnull](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/devnull/32/2379_2.png) [@devnull](https://socialhub.activitypub.rocks/u/devnull)\
**Post date:** [May 30, 2024, 8:41pm UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/19 "2024-05-30T20:41:24Z")

</div>

Closing the loop a bit —

NodeBB now performs additional sanitization step to remove all css classes on the way out and on the way in, so the content you see coming from NodeBB should be a little cleaner now.

The issue here isn’t that the classes are cruft, but that there is a small chance that an identically named class has a different set of style rules and thus unpredictable behaviour could result.

e.g. Mastodon sends their content with no sanitization, so things like Microformats in class names are present, along with other class names that mean something in Mastodon (`mention`, `invisible`, etc.)

---

<div class="post-metadata">

**Author:** ![bumblefudge](https://socialhub.activitypub.rocks/user_avatar/socialhub.activitypub.rocks/bumblefudge/32/1970_2.png) [@bumblefudge](https://socialhub.activitypub.rocks/u/bumblefudge)\
**Post date:** [May 31, 2024, 11:29am UTC](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150/20 "2024-05-31T11:29:14Z")

</div>

> [@angus](#):
>
> Yeah, as with other quetsions, we’ll probably need some convergence on this question too.

Is it worth summarizing this thread/process once it’s completed with a FEP describing what “clean” looks like in 2024? It might help people trying to achieve smooth federation with Mastodon as well as smooth federation with your implementations…

[Next page](https://socialhub.activitypub.rocks/t/content-formatting-when-federating-out/4150.md?page=2)
