IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

Astro Markdown Component Utility for Any Framework

CSS-Tricks 2026-06-14 04:40:55 累计浏览 7 次
本机暂存

In the previous article, I spoke about the why and how to use a Markdown component in Astro.

Here, we’re going to expand on that and help you use Markdown everywhere — regardless of the framework you use. So, this works for React, Vue, and Svelte.

The entire process hinges on the Markdown utility I’ve built for Splendid Labz.

Why This Utility?

I hit a snag when using most Markdown libraries. I naturally write Markdown content like this:

<div>
  <div>
    <!-- prettier-ignore -->
    <Markdown>
      This is a paragraph

      This is a second paragraph
    </Markdown>
  </div>
</div>

But since most markdown libraries don’t account for whitespace indentation, they create an output with <pre> and <code> tags.

This is because Markdown treats the indentation beyond four spaces as a code block:

<div>
  <div>
    <pre><code>  This is a paragraph

      This is a second paragraph
    </code></pre>
  </div>
</div>

So you’re forced to strip all indentation and write it like this instead:

<div>
  <div>
    <!-- prettier-ignore -->
    <Markdown>
  This is a paragraph

  This is a second paragraph
    </Markdown>
  </div>
</div>

That’s hard to read and annoying to maintain.

My Markdown utility handles this whitespace issue and generates the correct HTML regardless of how your code is indented:

<div>
  <div>
    <p>This is a paragraph</p>
    <p>This is a second paragraph</p>
  </div>
</div>

Using This in Your Framework

It’s easy. You have to pass the Markdown text into the utility. If inline is true, then markdown will return an output without paragraph tags.

Here’s an example with Astro.

---
import { markdown } from '@splendidlabz/utils'
const { inline = false, content } = Astro.props
const slotContent = await Astro.slots.render('default')

// Process content
const html = markdown(content || slotContent, { inline })
---

<Fragment set:html={html} />

You can then use it like this:

<Markdown>
   <!-- Your content here -->
</Markdown>

Here’s another example for Svelte.

Svelte cannot read dynamic content from slots, so we can only pass it through a prop.

<script>
  import { markdown } from '@splendidlabz/utils'
  const { content, inline = false } = $props()
  const html = markdown(content, { inline })
</script>

<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html html}

And you can use it like this:

<Markdown content=`
  ### This is a header

  This is a paragraph
`/>

It’s rather simple to build the same for React and Vue so I’d leave that up to you.

Taking it Further

I’ve been building for the web — long enough to experience the frustration of doing the same things over and over again.

So I consolidated everything I use into a few simple libraries — like Splendid Utils, and a few others for layouts, Astro and Svelte components.

I write about all of them on my blog. Come by if you’re interested in better DX as you build your sites and apps!


Astro Markdown Component Utility for Any Framework originally handwritten and published with love on CSS-Tricks. You should really get the newsletter as well.

同分类推荐文章

  1. Why Isn’t My 3D View Transition Working? (2026-06-12 20:53:41)
  2. 全新的CSS border-shape属性简介 (2026-06-10 11:00:06)
  3. Scroll-Driven, Scroll-Triggered, Scroll States, and View Transitions (2026-06-08 21:00:34)

查看更多 前端 文章 →

建议继续学习

  1. facebook 的工程师文化 (累计阅读 7,253)
  2. 如何成为一名优秀的web前端工程师(前端攻城师)? (累计阅读 7,126)
  3. Pinterest:充分挖掘视觉的潜力 (累计阅读 6,797)
  4. 如何做好一份前端工程师的简历? (累计阅读 5,896)
  5. GitHub中的README.MD文件编写语法 (累计阅读 5,513)
  6. 关于恐惧的自白 (累计阅读 4,964)
  7. 关于前端开发 (累计阅读 4,916)
  8. 微博,将让新浪血尽而死 (累计阅读 3,870)
  9. 为什么创业公司需要写博客? (累计阅读 3,720)
  10. 建设一个网站的成本(之一) (累计阅读 3,660)