使用 Tailwind Typography 美化渲染后的 Markdown
你可以使用 Tailwind 的 Typography 插件来美化如 Astro 的 内容集合 等来源的渲染后的 Markdown。
本指南将教你如何使用 Tailwind 的实用类创建一个可复用的 Astro 组件,以便美化你的 Markdown 内容。
前提条件
段落标题 前提条件一个 Astro 项目:
- 已经安装了 Astro 的 Tailwind 集成。
- 使用了 Astro 的 内容集合。
设置 @tailwindcss/typography
段落标题 设置 @tailwindcss/typography首先,使用你喜欢的包管理器安装 @tailwindcss/typography
。
npm install -D @tailwindcss/typography
pnpm add -D @tailwindcss/typography
yarn add --dev @tailwindcss/typography
然后,在你的 Tailwind 配置文件中添加该包作为插件。
/** @type {import('tailwindcss').Config} */
export default { theme: { // ... }, plugins: [ require('@tailwindcss/typography'), // ... ],}
操作步骤
段落标题 操作步骤-
创建一个
<Prose />
组件,提供一个被<div>
包裹并包含你的渲染 Markdown 的<slot />
。在父元素中添加样式类prose
,并在其中添加任何你想要的 Tailwind 元素修饰符。src/components/Prose.astro ------<divclass="prose dark:prose-invertprose-h1:font-bold prose-h1:text-xlprose-a:text-blue-600 prose-p:text-justify prose-img:rounded-xlprose-headings:underline"><slot /></div> -
在你想要渲染 Markdown 的页面上查询你的集合条目。将
await entry.render()
的<Content />
组件作为子组件传递给<Prose />
,以便用 Tailwind 样式包裹你的 Markdown 内容。src/pages/index.astro ---import Prose from "../components/Prose.astro";import Layout from "../layouts/Layout.astro";import { getEntry } from 'astro:content';const entry = await getEntry('collection', 'entry');const { Content } = await entry.render();---<Layout><Prose><Content /></Prose></Layout>
资源
段落标题 资源更多操作指南
-
状态共享
学习如何使用 Nano Stores 在框架组件之间共享状态
-
添加 RSS 摘要
通过向你的 Astro 网站添加 RSS 摘要让用户来订阅你的内容。
-
安装一个 Vite 或 Rollup 插件
了解如何通过向项目添加 Rollup 插件来导入 YAML 数据。
-
构建自定义图像组件
了解如何使用 getImage 函数构建支持媒体查询的自定义图像组件
-
使用 API 路由构建表单
了解如何使用 JavaScript 发送表单到 API 路由
-
在 Astro 页面中构建 HTML 表单
了解如何在 Astro 页面中构建 HTML 表单并在你的 frontmatter 中处理提交请求
-
在 Astro 中使用 Bun
了解如何在 Astro 站点中使用 Bun。
-
调用服务器端点
了解如何在 Astro 中调用服务器端点
-
校验验证码
了解如何创建一个 API 路由并从客户端进行获取。
-
用 Docker 来构建你的 Astro 网站
了解如何使用 Docker 来构建你的 Astro 网站。
-
Dynamically Import Images
Learn how to dynamically import images using Vite's import.meta.glob function
-
为链接添加图标
了解如何安装 rehype 插件并为你 Markdown 文件中的链接添加图标
-
添加 i18n 功能
通过动态路由和内容集合来让你的 Astro 站点支持国际化。
-
添加最后修改时间
编写一个在你的 Markdown 和 MDX 文件里添加最后修改时间的 remark 插件。
-
添加阅读时间
编写一个在你的 Markdown 和 MDX 文件里添加阅读所需时间的 remark 插件。
-
在 Astro 组件中共享状态
了解如何通过 Nano Stores 在 Astro 组件中共享状态
-
使用流式处理来提升页面性能
了解如何使用流式处理来提升页面性能
-
使用 Tailwind Typography 美化渲染后的 Markdown
了解如何使用 @tailwind/typography 美化你渲染后的 Markdown