1

How can I convert markdown to HTML within Astro? If you import an MD file within Astro, the content will automatically be converted to HTML in the background.

But I use Strapi as my CMS system. I fetch my posts with the fetch() method, and that works fine. However, the content is in markdown format, and I want to convert it to HTML.

Thank you :)

1 Answers1

1

The Astro documentation suggests using the marked package to render remotely fetched Markdown:

---
import { marked } from 'marked';
const response = await fetch('https://raw.githubusercontent.com/wiki/adam-p/markdown-here/Markdown-Cheatsheet.md');
const markdown = await response.text();
const content = marked.parse(markdown);
---
<article set:html={content} />

The astro-remote package also offers components that do something similar while the @astropub/md package will render Markdown for you using the remark ecosystem and can use your existing Astro Markdown configuration if you want it to.

swithinbank
  • 1,127
  • 1
  • 6
  • 15