NUXT3如何将Markdown内容显示为HTML标记?

huangapple go评论49阅读模式
英文:

NUXT3 How to display markdown content as html tags?

问题

我正在尝试在我的NUXT3应用程序中从REST API显示我的Markdown内容。我可以使用以下代码获取原始数据:

<h2 class="bg-slate-100 p-5 mt-5 border rounded">{{ Services.data.Test[0].Title}}</h2>
<p class="bg-slate-100 p-5 mt-5 border rounded" >{{ Services.data.Test[0].Test_Data}}</p>

使用DirectUS -

<script setup>
const { $directus } = useNuxtApp()
const { data: Services } = await useAsyncData('Services', () => {
	return $directus.items('Services').readByQuery()
})
// console.log($directus);
</script>

我得到的内容没有正确的P和H标签,这与我在Markdown内容中设置的不符。它看起来像是原始文本。我想将其显示为HTML格式。在这里如何使用Markdown?在NUXT2中,我们可以使用markdownit模块... 但在NUXT3中,它不兼容。我阅读了NUXT内容模块的文档,但找不到我要找的内容。

英文:

I am trying to display my markdown content from REST API to my NUXT3 application. I can get raw data with this

<h2 class="bg-slate-100 p-5 mt-5 border rounded">{{ Services.data.Test[0].Title}}</h2>
<p class="bg-slate-100 p-5 mt-5 border rounded" >{{ Services.data.Test[0].Test_Data}}</p>

Using DirectUS -

<script setup>
const { $directus } = useNuxtApp()
const { data: Services } = await useAsyncData('Services', () => {
	return $directus.items('Services').readByQuery()
})
// console.log($directus);
</script>

I am getting this without proper P and H tags which I set in my markdown content. Its looking raw text.. I want to make it html format like it should look.
How can I use markdown here ? In NUXT2, we can use markdownit module... but in NUXT3, its not compatible.
I read NUXT content module docs but can not find what I am looking for.

答案1

得分: 6

In Nuxt 3 it's possible to use markdownit, i'm use in my application, i will share my files

Package.json:
"@types/markdown-it": "^12.2.3",
"markdown-it": "^13.0.1",
"nuxt": "3.1.0",

Here is the code snippet from the project:

// @/plugins/markdownit.ts
import md from "markdown-it";

export default defineNuxtPlugin(() => {
const renderer = md();
return {
provide: {
mdRenderer: renderer,
},
};
});

RichText.vue



There's no need to register the plugin, nuxt does it for you.

英文:

In Nuxt 3 it's possible to use markdownit, i'm use in my application, i will share my files

Package.json:
"@types/markdown-it": "^12.2.3",
"markdown-it": "^13.0.1",
"nuxt": "3.1.0",

Here is the code snippet from the project:

// @/plugins/markdownit.ts
import md from "markdown-it";

export default defineNuxtPlugin(() => {
  const renderer = md();
  return {
    provide: {
      mdRenderer: renderer,
    },
  };
});

RichText.vue

<!-- @/components/strapi_components/blocks/RichText.vue -->
    <script lang="ts" setup>
    const props = defineProps({
      block: {
        type: Object,
        required: true,
      },
    });
    onBeforeMount(() => {
      !["content"].every((propName) => props.block.hasOwnProperty(propName))
        ? console.error(`RichText.vue has not pass the block property validation`)
        : null;
    });
    </script>
    
    <template>
      <div class="container mx-auto grid place-content-center">
        <div
          class="prose-xl border border-gray-300 prose-blue prose-pre:bg-zinc-300 prose-pre:text-gray-800 text-sm m-5 p-5 bg-gray-100 shadow-xl rounded-2xl"
          v-html="$mdRenderer.render(block.content)"
        />
      </div>
    </template>    

There's no need to register the plugin, nuxt does it for you.v

huangapple
  • 本文由 发表于 2023年3月15日 18:09:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定