To render a Markdown string in Astro, convert it to HTML and output it with set:html. For content from an API or CMS, the best place is a content loader, whose renderMarkdown() uses Astro’s own pipeline so you can render it with <Content />. For one-off strings at runtime, a parser such as marked is the simplest option.
Key Takeaways
- Astro doesn’t render Markdown strings in templates on its own; you convert first.
- In a loader,
renderMarkdown()applies your Markdown config, Shiki and heading IDs.markedworks anywhere, including on-demand pages, but skips Astro’s plugins.- Sanitize Markdown that comes from users before using
set:html.
Option 1: renderMarkdown() in a content loader
When Markdown arrives from an API, store the rendered result with each entry:
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { z } from 'astro/zod';
const changelog = defineCollection({
loader: {
name: 'changelog-loader',
async load({ store, parseData, renderMarkdown }) {
const res = await fetch('https://api.example.com/releases');
const releases: { version: string; date: string; notes: string }[] = await res.json();
store.clear();
for (const r of releases) {
const data = await parseData({ id: r.version, data: { version: r.version, date: r.date } });
store.set({ id: r.version, data, rendered: await renderMarkdown(r.notes) });
}
},
},
schema: z.object({ version: z.string(), date: z.coerce.date() }),
});
export const collections = { changelog };
---
// src/pages/changelog.astro
import { getCollection, render } from 'astro:content';
const releases = await Promise.all(
(await getCollection('changelog')).map(async (release) => ({
version: release.data.version,
Content: (await render(release)).Content,
})),
);
---
{releases.map(({ version, Content }) => (
<section><h2>{version}</h2><Content /></section>
))}
renderMarkdown() runs your configured processor, so headings get IDs, code blocks get Shiki highlighting, and your Markdown plugins apply, exactly like local .md files. It only exists inside loaders. Our content loaders guide covers inline and reusable loaders.
Option 2: marked anywhere
For a string you have in a component, such as a CMS field fetched on an on-demand page:
npm install marked
---
// src/components/Markdown.astro
import { marked } from 'marked';
interface Props { source: string }
const html = await marked.parse(Astro.props.source);
---
<div class="prose" set:html={html} />
<Markdown source={product.longDescription} />
marked supports GitHub-flavored Markdown by default and is fast. It doesn’t know about Astro, so there’s no Shiki or heading IDs unless you add marked extensions. The Fragment and set:html tip explains how set:html inserts raw HTML.
Option 3: Astro’s own processor
@astrojs/markdown-remark exports createMarkdownProcessor(), the function Astro uses internally:
// src/lib/markdown.ts
import { createMarkdownProcessor } from '@astrojs/markdown-remark';
const processor = await createMarkdownProcessor({
shikiConfig: { theme: 'github-dark' },
});
export async function renderMd(source: string) {
const { code, metadata } = await processor.render(source);
return { html: code, headings: metadata.headings };
}
It returns HTML plus headings, and it’s useful in scripts or endpoints outside a loader. Because it’s a lower-level API, it takes its own options rather than reading astro.config.mjs, and its signature can change between releases. Prefer Option 1 when you can.
Sanitize untrusted Markdown
Markdown can contain raw HTML, including <script> and event handlers. If the source is your own CMS, that’s usually fine. If users write it (comments, profiles), sanitize the HTML before output:
import { marked } from 'marked';
import sanitizeHtml from 'sanitize-html';
const safe = sanitizeHtml(await marked.parse(userInput));
Never pass unsanitized user content to set:html. Our Markdown pipeline explainer covers what each stage of Astro’s renderer does, and the Content Collections docs page explains render(). If you build on a documentation theme, Option 1 keeps remote content styled like local pages.
FAQ
How do I render a Markdown string in an Astro component?
Convert it to HTML with a library like marked, then output it with <div set:html={html} />. For CMS or API content, use renderMarkdown() in a content loader instead, so it goes through Astro’s Markdown pipeline.
Is there a Markdown component in Astro?
Not built in. Older Astro versions had a <Markdown /> component that was removed. Use renderMarkdown() in a loader, a Markdown library with set:html, or a community component.
Is set:html safe for Markdown content?
It’s safe for Markdown you control. For anything users can write, sanitize the HTML first with a library such as sanitize-html, because Markdown allows raw HTML that could run scripts.