Whether I call the plugin directly from my template section or mount it programmatically while passing to it a language prop value, the language I specify always ends up being ignored by the plugin who instead chooses himself a language.
From template section:
<highlightjs language='javascript' code="var x = 5;" />
From script section (programmatically):
<template>
<div class="myElement"></div>
</template>
<script setup>
// some code here...
nextTick(() => {
mount(HighlightPlugin.component, {
element: document.querySelector('.myElement'),
props: {
language: 'javascript',
code: codeSnippet
}
})
})
</script>
Here's how the plugin is imported in my main.js:
import 'highlight.js/styles/github-dark-dimmed.css'
import 'highlight.js/lib/common'
import hljsVuePlugin from '@highlightjs/vue-plugin'
app.use(hljsVuePlugin)
app.mount('#app')
EDIT - updated version:
import 'highlight.js/styles/github-dark-dimmed.css'
import 'highlight.js/lib/common'
import hljsVuePlugin from '@highlightjs/vue-plugin'
import hljs from 'highlight.js/lib/core'
import javascript from 'highlight.js/lib/languages/javascript'
hljs.registerLanguage('javascript', javascript)
app.use(hljsVuePlugin)
app.mount('#app')
Beside the current
highlightjs/vue-pluginissue 49, you have the 2021highlightjs/vue-pluginissue 8 "languagesetting is not effective": there might be problems when different versions of Highlight.js are present in the project's dependency tree. A workaround suggested involved manually installing a specific version of Highlight.js (version 10.7.2), which resolved the issue for a user. But there might be compatibility concerns with newer versions of Highlight.js.The a
console.log(hljs.version);.Make sure you import it explicitly in your
main.jsor wherever you configure Highlight.js:Double-check if there is any code that might be overwriting the
languageprop or if the plugin has default behavior when it fails to recognize the specified language. That could happen if the language module was not imported.Then try and implement a diagnostic script in your Vue project:
That way, you will see the currently used Highlight.js version. And you will check if the specified languages are correctly registered and available.
You can also test with this script if a simple JavaScript code snippet is correctly highlighted.
The goal is to see if the issue lies with language detection or plugin integration.