英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论