How to render text from GPT with styles

Hi,
I’ve build a front-end similar to the chat gpt website by using the OpenAI API’s.

However something I’ve not figured out how to do in a clean way is rendering the outputs from the LLM in a rich format.

The closest I got was by instructing the LLM to respond in markdown, then use a library like marked.js to convert the raw markdown into HTML. Then feed this HTML into DOMPurify to sanitize the HTML. Finally render out the sanitized HTML.
But this sort of feels hacky, so I was wondering if someone knows a better way to achive this.

In summary,
How does the ChatGPT website render the simple raw txt from the llms into styled text with

  • Italics, bold, sections
  • ordered and unordered lists
  • various code blocks with code highlighting