-
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
markdownit how to use highlight #8
Comments
This issue as been imported as question since it does not respect modules issue template. Only bug reports and feature requests stays open to reduce maintainers workload. |
@bookyo did you find a solution? |
@bookyo @mmintel I got it working in 3 steps: After installing // nuxt.config.js
export default {
....
// 1.) add markdown-it to modules:
modules: [
'@nuxtjs/markdownit',
],
css: [
// 2.) use a style for syntax highlighting
'highlight.js/styles/github.css'
]
....
markdownit: {
// 3.) use syntax highlighting:
highlight: function(str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, str).value
} catch (__) {}
return '' // use external default escaping
}
}
},
...
} You can use different styles, check out this list. It's been working for me so far, so I hope it'll help you too! That weird |
@fulopkovacs I follow the code method you provided and it still doesn't work. How can I solve the problem so that markdown-it can highlight the syntax |
You can try this, it works for me in nuxt.js
Reference - https://markdown-it.github.io/markdown-it/ |
I also have issues making highlight.js work with @nuxt/markdown-it. I want to display formatted code in my article pages whose content comes from an API. I get the following error when my article page loads: nuxt.config.js: const hljs = require('highlight.js');
module.exports = async () => {
return {
modules: [
'@nuxtjs/markdownit',
],
markdownit: {
html: true,
preset: 'default',
linkify: true,
breaks: true,
injected: true,
highlight: function (str, lang) {
// ➡️ hljs is undefined here ⬅️ 😢
if (lang && hljs.getLanguage(lang)) {
try {
return '<pre class="hljs"><code>' +
hljs.highlight(lang, str, true).value +
'</code></pre>';
} catch (__) {}
}
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
}
},
}
} _slug.vue: <template>
<div>
<main-header />
<main>
<sp-section>
<div
v-html="articleContent"
/>
</sp-section>
</main>
</div>
</template>
<script>
import hljs from 'highlight.js';
export default {
async asyncData({ app, params }) {
try {
const [ article ] = await app.$axios.$get(`http://localhost:1337/articles/?slug=${params.slug}`);
return {
article,
};
} catch (err) {
return false;
}
},
computed: {
articleContent() {
return this.$md.render(this.article.content);
},
},
};
</script> |
@ChucKN0risK |
@fulopkovacs No I'm not sure I should import it inside |
@fulopkovacs Did you have any chance of checking my issue? Thanks in advance ;) |
@ChucKN0risK I did look into it, but couldn't find anything in my local setup. I am definitely not an expert (not event a front-end dev by now), but the only thing I can think of is that maybe the Again, I'm not an expert, and I might be wrong about the |
@fulopkovacs Thanks for taking the time to answer me ;) I managed to directly use export default {
async asyncData({ app, params }) {
try {
const [ article ] = await app.$axios.$get(`http://localhost:1337/articles/?slug=${params.slug}`);
const content = markdownit({
html: true,
preset: 'default',
linkify: true,
breaks: true,
highlight: function (str, lang) {
return `<pre class="hljs language-${lang}"><code class="language-${lang} ${lang}">${hljs.highlightAuto(str).value}</code></pre>`;
}
}).render(article.content);
return {
article,
content,
};
} catch (err) {
return false;
}
},
} My content is fetched and rendered server side ;) |
I know this is closed, but in case anyone searches for it, I found Samuel Coe's article very useful 🎉 |
(opened until adding to docs or built-in feature) |
i write markdown content.
after $md.render() :
highlight is not work,.
my nuxt.config.js:
The text was updated successfully, but these errors were encountered: