Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert markdown to HTML in Astro

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 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading