Style Rendered Markdown with Tailwind Typography
이 컨텐츠는 아직 번역되지 않았습니다.
You can use Tailwind’s Typography plugin to style rendered Markdown from sources such as Astro’s content collections.
This recipe will teach you how to create a reusable Astro component to style your Markdown content using Tailwind’s utility classes.
Prerequisites
Section titled PrerequisitesAn Astro project that:
- has Astro’s Tailwind integration installed.
- uses Astro’s content collections.
Setting Up @tailwindcss/typography
Section titled Setting Up @tailwindcss/typographyFirst, install @tailwindcss/typography
using your preferred package manager.
npm install -D @tailwindcss/typography
pnpm add -D @tailwindcss/typography
yarn add --dev @tailwindcss/typography
Then, add the package as a plugin in your Tailwind configuration file.
/** @type {import('tailwindcss').Config} */
export default { theme: { // ... }, plugins: [ require('@tailwindcss/typography'), // ... ],}
Recipe
Section titled Recipe-
Create a
<Prose />
component to provide a wrapping<div>
with a<slot />
for your rendered Markdown. Add the style classprose
alongside any desired Tailwind element modifiers in the parent element.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>The
@tailwindcss/typography
plugin uses element modifiers to style child components of a container with theprose
class.These modifiers follow the following general syntax:
prose-[element]:class-to-applyFor example,
prose-h1:font-bold
gives all<h1>
tags thefont-bold
Tailwind class. -
Query your collection entry on the page you want to render your Markdown. Pass the
<Content />
component fromawait entry.render()
to<Prose />
as a child to wrap your Markdown content in Tailwind styles.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>