Astro MDX Basics
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Astro MDX Basics
Section titled “Astro MDX Basics”MDX (Markdown for the component era) combines the simplicity of Markdown with the power of React components. In Astro 7.x, MDX is fully supported and provides a powerful way to create dynamic content.
MDX vs Markdown
Section titled “MDX vs Markdown”Markdown (.md)
Section titled “Markdown (.md)”- Pure text formatting
- Limited to basic elements (headers, lists, links, images)
- No component integration
- Static content only
MDX (.mdx)
Section titled “MDX (.mdx)”- Markdown syntax + JSX components
- Can import and use React components
- Dynamic content generation
- Access to Astro’s
astro:assetsand other modules
Astro 7.x MDX Configuration
Section titled “Astro 7.x MDX Configuration”Astro 7.x has built-in MDX support through @astrojs/mdx. The MDX integration is automatically enabled when you use MDX files in your content collections.
Content Configuration
Section titled “Content Configuration”In site/src/content.config.ts:
import { defineCollection } from 'astro:content';import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';import { docsLoader, i18nLoader } from '@astrojs/starlight/loaders';import { blogSchema } from 'starlight-blog/schema';
export const collections = { docs: defineCollection({ loader: docsLoader(), schema: docsSchema({ extend: (context) => blogSchema(context) }), }), i18n: defineCollection({ loader: i18nLoader(), schema: i18nSchema, }),};This configuration automatically supports both .md and .mdx files in the docs collection.
MDX Component Integration
Section titled “MDX Component Integration”In MDX files, you can import and use components:
---title: My MDX Page---
import { MyComponent } from '../../components/MyComponent.astro';
# My Page
This is a markdown paragraph with an Astro component:
<MyComponent prop="value" />Astro Assets in MDX
Section titled “Astro Assets in MDX”Astro 7.x provides the astro:assets module for image optimization:
---title: Image Optimization---
import { Image, getImage } from 'astro:assets';import myImage from '../../assets/images/my-image.png';
const optimizedImage = await getImage({ src: myImage, width: 1200, quality: 75, formats: ['webp', 'avif', 'jpeg'],});
<Image src={optimizedImage} alt="Optimized image" loading="lazy" />Benefits of MDX
Section titled “Benefits of MDX”- Component Integration: Use React/Astro components in content
- Dynamic Content: Generate content dynamically with JavaScript
- Image Optimization: Use
astro:assetsfor optimized images - Consistent Format: Single content format for all documentation