时光与我

Back

Astro MDX 组件使用完全指南Blur image

mdx 组件使用实例#

在 Astro 中使用 .mdx 格式写文章,最大的好处是可以引入主题内置的交互组件,丰富排版。

引用示例#

用到哪个组件就引用那个组件,按需选择,注意区别导入来源from 'astro-pure/user'from 'astro-pure/advanced'

import { Aside, Tabs, TabItem, Steps, Timeline, Spoiler, Icon } from 'astro-pure/user'
import { GithubCard, LinkPreview } from 'astro-pure/advanced'
plaintext

组件总览表#

组件名导入来源功能典型场景
Asideastro-pure/user提示框(note/tip/danger 等类型)文中加提示、警告
Cardastro-pure/user卡片容器,可放列表/任意内容信息分组展示
Collapseastro-pure/user折叠展开块FAQ、隐藏细节
Tabs / TabItemastro-pure/user选项卡切换多语言/多方案对比
Timelineastro-pure/user时间线履历、版本更新记录
Stepsastro-pure/user步骤列表教程操作步骤
CardListastro-pure/user卡片列表资源/链接汇总
Buttonastro-pure/user按钮(支持 link / pill 样式)行动召唤按钮
Spoilerastro-pure/user剧透遮挡(hover 显示)隐藏答案、剧透内容
FormattedDateastro-pure/user格式化日期统一日期展示
Labelastro-pure/user小标签徽章状态/分类标记
Svgastro-pure/userSVG 加载器内联矢量图
Iconastro-pure/user主题内置图标(github/bilibili/rss 等 40+)社交链接、装饰图标
MdxReplastro-pure/userMDX 代码演示器实时预览代码渲染结果
Quoteastro-pure/advanced随机名言(支持 Quotable / 一言 API)页脚随机语录
GithubCardastro-pure/advancedGitHub 仓库卡片展示开源项目
LinkPreviewastro-pure/advanced链接预览卡片(自动抓取 OG 信息)友链、推荐文章
QRCodeastro-pure/advanced二维码生成扫码跳转
Toastastro-pure/advanced轻提示复制成功等反馈
Medium Zoomastro-pure/advanced图片灯箱放大文中图片点击放大

1. 提示框#

Aside 组件用于在文中插入醒目的提示,支持 note(默认)、tipdanger 等类型。

示例,可直接复制使用:

<Aside type='tip' title='小技巧'>
写代码前先喝口水。 
</Aside>

<Aside type='danger' title='警告'>
不要在生产服务器上跑 `rm -rf /`。
</Aside>
plaintext

实际渲染效果:

2. 选项卡#

TabsTabItem 组合使用,适合展示多语言或多方案的对比。

示例,可直接复制使用:

实际渲染效果:

bash npm install astro

3. Steps —— 步骤列表#

自动识别有序列表并渲染为步骤样式 示例,可直接复制使用:

<Steps>

1. 安装依赖
2. 启动开发服务器
3. 编写第一篇文章
4. 构建并部署

</Steps>
plaintext

实际渲染效果:

  1. 安装依赖
  2. 启动开发服务器
  3. 编写第一篇文章
  4. 构建并部署

4. Timeline —— 时间线#

示例,可直接复制使用:


<Timeline events={
  [
    { date: 'August 2021', content: 'Hello' },
    { date: 'August 2022', content: '<i>World!</i>' },
  ]
} />
plaintext

实际渲染效果:

  • August 2021
    Hello
  • August 2022
    World!

更多请移步 Docs文档

💡 更多请移步 Docs文档···

Astro MDX 组件使用完全指南
https://timingme.com/blog/mdx-components
Author 李富贵大人
Published at 2026年6月18日
Comment seems to stuck. Try to refresh?✨