-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nuxt 3 Support ? #47
Comments
Got it working by creating this plugin. import { defineNuxtPlugin } from '#app';
import hljs from 'highlight.js';
import mdit from 'markdown-it';
import sub from 'markdown-it-sub';
import sup from 'markdown-it-sup';
import fn from 'markdown-it-footnote';
import emo from 'markdown-it-emoji';
import def from 'markdown-it-deflist';
import ins from 'markdown-it-ins';
import container from 'markdown-it-container';
const markdownit = new mdit({
html: true,
xhtmlOut: false,
breaks: false,
langPrefix: 'language-',
linkify: true,
typographer: true,
quotes: '“”‘’',
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return '<pre class="hljs"><code>' +
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
'</code></pre>';
} catch (error) {console.log(error)}
}
return '<pre class="hljs"><code>' + mdit.utils.escapeHtml(str) + '</code></pre>';
}
})
.use(sub)
.use(sup)
.use(fn)
.use(emo)
.use(def)
.use(ins)
.use(container,'codeblock',{marker:'@'});
markdownit.linkify.set({ fuzzyEmail: false });
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.provide('mdit',markdownit);
}); |
Hello, Can you provide a way to use the plugin in a .vue file ?
|
You can access it by |
Work for me ! Below, a working sample with an API call to strapi4 in nuxt3
|
@jyotirmaybarman Thanks for providing the plugin. It's works fine for me, except on page reload it weirdly instantly vanishes for some reason do you may have any pointers where i could look at? |
@nevotheless Sorry for this late reply, hope you have found a solution for your issue. |
Unfortunately not but I have found out that my issue is not tied to markdownit. |
@nevotheless maybe there is some problem with your data fetching, maybe you should try different ways to fetch data in different lifecycle hooks. |
ty for the advice, i came to the same conclusion but i didn't really fiddle with it ever since. It demotivated me too much 😅 |
Hi, Any update for this? @nuxtjs/markdownit still maintained? |
The solution that worked for me in Nuxt 3 RC 1- install package
3- in the component template, use it in a dev like
define
thats it, no edits to nuxt.config required it works in this example, the class is using tailwind; if you use tailwind, you can also use the typography plugin, which is recommended
credits: Mounir Younes |
Add markdown renderer as a nuxt plugin:
import md from "markdown-it";
export default defineNuxtPlugin(() => {
const renderer = md();
return {
provide: {
mdRenderer: renderer,
},
};
});
<script setup lang="ts">
const markdownString = "# Markdwon-it in Nuxt3"
</script>
<template>
...
<section
class="prose prose-gray text-sm"
v-html="$mdRenderer.render(markdownString)"
/>
...
</template> |
Hi I try your answer and get this Expected 1 arguments, but got 0.ts(2554) md need to pass some configuration,,, do you know what I should use? Thanks |
@wvovaw are you using a specific version of markdown-it ? When I add it to my modules in nuxt config upon running dev I get the error that |
Here is what I have in the Package.json: "@types/markdown-it": "^12.2.3",
"markdown-it": "^13.0.1",
"nuxt": "3.1.0", Markdown-it package and its declarations package hasn't been updating for ~10 months so I'm up to date. P.S. I just upgraded my nuxt version to [email protected] and everything is still ok. 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,
},
};
}); <!-- @/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.
Do not do that because markdown-it package is not a nuxt module. Just install the package, create a plugin file, provide md().renderer and that's it! |
@wvovaw you're a hero! Works like charm 👍 |
Thanks @wvovaw It put this here, probably someone need target="_blank" or other attributes:
|
@wvovaw super elegant solution doesn't work for me, inside RichText component I have this weird typescript error :
And in browser : Cannot read properties of undefined (reading 'render') Would anyone be able to point me in the right direction on what have I done wrong ? |
Thanks @wvovaw, this is much cleaner than what I had been using. This is how I got working syntax highlighting if anyone else lands at this page.
// @/plugins/markdownit.ts
import md from 'markdown-it'
import markdownItHighlightjs from 'markdown-it-highlightjs'
export default defineNuxtPlugin(() => {
const renderer = md()
renderer.use(markdownItHighlightjs)
return {
provide: {
mdRenderer: renderer,
},
}
}) <template>
...
</template>
<script setup>
import hljs from 'highlight.js'
import 'highlight.js/styles/nord.css'
onMounted(() => {
hljs.highlightAll()
})
</script> |
@wvovaw Been trying to get this to work for a while now. You're truly amazing! A million thanks! |
Thanks for the solutions, everyone! I'm going to track Nuxt 3 support here: #67 I've linked the solution in here as an alternative. |
@wvovaw Thanks for the solution, it still work for my nuxt project version: "nuxt": "^3.8.0", and current version of markdown-it "@types/markdown-it": "^13.0.7",
"markdown-it": "^13.0.2", |
May I ask how to add plugins to |
@xarthurx I'm not sure, but try this // @/plugins/markdownit.ts
import md from "markdown-it";
import { plugin } from "some-markdown-it-plugin";
export default defineNuxtPlugin(() => {
const renderer = md().use(plugin);
return {
provide: {
mdRenderer: renderer,
},
};
}); BTW I'd recommend you to go with nuxt content for now for markdown powered websites. |
I tried, very hard. But the system is not mature when I need some plugins to do a bit of customized stuff... |
It is incompatible with latest nuxt 3 beta ...!!
The text was updated successfully, but these errors were encountered: