Markdown, BBCode, and Emoji Not Rendering In API Post

I’m using the Discourse API to update a post via PUT; the updates are made, but only accepted HTML tags are parsed. Markdown, BBCode, and emojis are not rendered correctly. I’m passing the PUT request with a content type of application/json because it both is actually a JSON payload and is the only content type that seems to be accepted for the PUT request despite the documentation saying that it was one of three options.

Does anyone have any idea why the rendering engine isn’t parsing things when updates are made via the API?

1 Like

I’d look at the logs and also see what ended up in the raw markdown field.

3 Likes

I see no errors in the logs and the raw payload is exactly what I’m specifying it to be in the cURL request that does the PUT. When I pull the JSON view of the topic, I see this:

	`<p><img src="https://global.discourse-cdn.com/standard10/uploads/wysterialane/original/1X/284fb6069edededf16b0830d468eaf03bc261616.webp" alt="mother of god shades GIF" data-base62-sha1="5KBSuBIpIj3xlACzeZINBKw7wEu" width="262" height="200" class="animated"></p><br>Why won't this work? :face_with_symbols_over_mouth: <br>`

The gif in there was something I put in the OP and the rendering engine translated it to HTML and all is well. When I add the <br>Why won't this work? :face_with_symbols_over_mouth: <br> bit via the API, the raw text is posted but the rendering engine doesn’t do anything with it. Is this expected behavior? I guess a workaround is for me to have my service formulate the full HTML payload and pass that which is kinda crappy.

It’s also worth noting that I have a separate service that posts to a live chat thread via incoming webhook and emojis render correctly in the chat pane. This seems to be specific to the posts API.

Emojis and HTML

I think the issue you are running into with emoji shortcodes that are wrapped in HTML tags is the expected behavior. It can be reproduced in a post that’s created within the Discourse post editor. For example :face_with_symbols_over_mouth: will render as :face_with_symbols_over_mouth: within this paragraph.

If I use p tags to create a paragraph instead if relying on the markdown engine to do it, the shortcode won’t be rendered. For example:

<p>This sentence contains an emoji shortcode :face_with_symbols_over_mouth:</p>

This sentence contains an emoji shortcode :face_with_symbols_over_mouth:

Markdown

The issue with posting markdown via the API seems solvable, but maybe tricky to work with. Using curl from the terminal, something like this should work:

m="### this is a heading
                                                                                                                                                           
This is a very short paragraph with some _emphasis_ on a word.

This is another paragraph with a  :slight_smile: emoji

- this
- is
- an
- unordered
- list

Followed by another paragraph.

1) and
2) an
3) ordered
4) list"

curl -X POST "http://localhost:4200/posts.json" -H "Api-Key: $api_key" -H "Api-Username: simon" -F "title=Markdown test one" -F "category=6" -F "skip_validations=true" -F "raw=$m"

The skip_validations parameter in the above request is just there to prevent “body too short” or “duplicate title” types of errors when testing. It bypasses a bunch of site settings (min post length, body min entropy, min topic title length, etc.)

I’m unsure about the BBCode issue. Do the BBCode tags that you are trying to render work when added to your Discourse site’s post editor?

1 Like