Render Content in Astro

From the Astro cheat sheet · Content Collections · verified Jul 2026

Render Content

astro
---
// pages/blog/[id].astro
import { getCollection, render } from 'astro:content';

export async function getStaticPaths() {
  const posts = await getCollection('blog');
  return posts.map(post => ({
    params: { id: post.id },
    props: { post },
  }));
}

const { post } = Astro.props;
const { Content, headings } = await render(post);
---
<article>
  <h1>{post.data.title}</h1>
  <time>{post.data.pubDate.toLocaleDateString()}</time>

  <!-- Table of contents -->
  <nav>
    <ul>
      {headings.map(h => (
        <li style={\`margin-left: \${h.depth * 10}px\`}>
          <a href={\`#\${h.slug}\`}>{h.text}</a>
        </li>
      ))}
    </ul>
  </nav>

  <Content />
</article>
✅ render() processes Markdown/MDX into renderable Content component
💡 Returns headings array for table of contents generation
🔍 Content component renders the processed HTML
⚡ Works with both Markdown and MDX files
rendercontentmarkdown

Continue with Astro

Save the full cheat sheet or work through every related task.

More Astro tasks

Back to the full Astro cheat sheet