Skip to content

Svelte Components trong MDX - Lỗi và Cách Fix

Svelte Components trong MDX - Lỗi và Cách Fix

Section titled “Svelte Components trong MDX - Lỗi và Cách Fix”

Quan trọng: Bài viết này nói về việc dùng Svelte trong MDX files của Starlight. Nếu bạn muốn dùng Svelte trong .astro files, hãy xem hướng dẫn chính thức bên dưới.

Cách đúng để thêm Svelte vào Astro (cho .astro files)

Section titled “Cách đúng để thêm Svelte vào Astro (cho .astro files)”

Theo hướng dẫn chính thức 2026:

Terminal window
# Cách khuyến nghị
npx astro add svelte
---
import Button from '../components/Button.svelte';
---
<!-- Render tĩnh (không JS) -->
<Button />
<!-- Có tương tác -->
<Button client:load />
<!-- Chỉ render trên client -->
<Button client:only="svelte" />

Cách này KHÔNG gây xung đột vì:

  • Astro tự nhận diện .svelte → không va chạm với .jsx, .vue
  • Mỗi component là island độc lập
  • Có thể mix: <ButtonSvelte /> + <CardReact /> + <ModalVue /> đều ổn

Vấn đề thực sự: Svelte trong MDX Files của Starlight

Section titled “Vấn đề thực sự: Svelte trong MDX Files của Starlight”

Khi sử dụng Astro Starlight (một documentation framework cho Astro), việc import và sử dụng Svelte components trực tiếp trong MDX files gây ra nhiều lỗi build.

[@mdx-js/rollup] Could not parse import/exports with acorn

hoặc

MDXError: Unexpected end of file in expression, expected a corresponding closing brace for {

1. Import Svelte Components trực tiếp ❌

Section titled “1. Import Svelte Components trực tiếp ❌”

Không hoạt động:

---
title: My Page
---
import Alert from '../../../components/svelte/Alert.svelte';
import GlassCard from '../../../components/svelte/GlassCard.svelte';
# Hello
<Alert type="info">Message</Alert>

Lý do: Starlight/MDX không hỗ trợ import Svelte components trực tiếp trong frontmatter.

2. Sử dụng JSX braces { trong Tables ❌

Section titled “2. Sử dụng JSX braces { trong Tables ❌”

Không hoạt động:

| ChatGPT | Claude Code |
|---------|-------------|
| function ContactForm() { | ✓ Tạo file |

Lý do: MDX hiểu { là bắt đầu JavaScript expression.

Cách fix: Sử dụng HTML entities:

| ChatGPT | Claude Code |
|---------|-------------|
| function ContactForm&#40;&#41; &#123; | ✓ Tạo file |

Hoạt động tốt:

---
title: My Page
---
import { Aside } from '@astrojs/starlight/components';
:::tip
Đây là tip!
:::
:::caution
Cẩn thận!
:::
:::danger
Nguy hiểm!
:::
---
title: My Page
---
import { Steps } from '@astrojs/starlight/components';
<Steps>
1. Bước đầu tiên
2. Bước thứ hai
3. Bước thứ ba
</Steps>
```javascript
function hello() {
console.log('Hello');
}
### 4. Tables thông thường ✅
```mdx
| Cột 1 | Cột 2 |
|-------|-------|
| Dữ liệu | Dữ liệu |
> Đây là trích dẫn

Dưới đây là các files đã được sửa lỗi:

FileVấn đềCách fix
markdown/gioi-thieu-markdown.mdxImport SvelteRemove imports, dùng Starlight :::
markdown/markdown-co-ban.mdxImport SvelteRemove imports, dùng Starlight :::
code/svelte-counter-example.mdxImport SvelteRemove imports
code/component-examples.mdxImport SvelteRemove imports, dùng code blocks
web-dev/cai-dat-astro-local.mdxImport SvelteRemove imports, dùng Starlight :::
markdown/components-alert-boxes.mdxImport Astro componentRemove imports, dùng Starlight :::
nhap/lap-trinh-cho-nha-tiep-thi.mdxBraces trong tableEscape thành &#123;

Cách 1: Sử dụng Starlight Aside (khuyến nghị)

Section titled “Cách 1: Sử dụng Starlight Aside (khuyến nghị)”
:::tip[Tiêu đề]
Nội dung tip...
:::
:::caution[Chú ý]
Nội dung cảnh báo...
:::
:::danger[Lỗi]
Nội dung lỗi...
:::

Cách 2: Sử dụng CSS thay vì Components

Section titled “Cách 2: Sử dụng CSS thay vì Components”

Thay vì:

<GlassCard>
Nội dung
</GlassCard>

Dùng:

> **Tiêu đề**
> Nội dung bên trong

Cách 3: Đặt code trong fenced code blocks

Section titled “Cách 3: Đặt code trong fenced code blocks”

Thay vì để { trực tiếp trong table, dùng:

| Code | Output |
|------|--------|
| `function() {` | ✓ |

  • Không nên import Svelte components trực tiếp trong MDX files của Starlight
  • Nên sử dụng Starlight built-in components như :::tip, :::caution, :::danger
  • Cẩn thận với braces {} trong tables - phải escape
  • Thay thế bằng CSS styling hoặc markdown thuần khi cần custom components