You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt Content:如何正确渲染Markdown文件内容?

Nuxt Content渲染Markdown正文的解决方法
  • 要渲染Markdown正文,你需要使用 <ContentRenderer> 组件。
  • ContentDoc 组件仅负责获取文档数据(包括frontmatter和解析后的body结构),不会自动渲染正文,需配合 ContentRenderer 完成正文输出。

示例代码1:结合useAsyncData与ContentRenderer

<template>
  <article>
    <h1>{{ doc.title }}</h1>
    <p class="year">发布年份:{{ doc.year }}</p>
    <!-- 渲染Markdown正文 -->
    <ContentRenderer :value="doc.body" />
  </article>
</template>

<script setup>
const { data: doc } = await useAsyncData('article-design', () => {
  return queryContent('/article/design').findOne()
})
</script>

示例代码2:使用ContentDoc插槽配合ContentRenderer

<template>
  <ContentDoc path="/article/design">
    <template #default="{ doc }">
      <h1>{{ doc.title }}</h1>
      <p class="year">发布年份:{{ doc.year }}</p>
      <ContentRenderer :value="doc.body" />
    </template>
  </ContentDoc>
</template>

核心说明:doc.body 是Nuxt Content解析Markdown后生成的AST结构,ContentRenderer 会将该结构转换为对应的HTML内容输出,从而显示Markdown正文。

内容的提问来源于stack exchange,提问作者dev_dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:06:02