Vue Highlight Component
Vue Highlight Component is an older Vue 2 component designed to integrate code highlighting into Vue applications using the `highlight.js` library. It specifically targets `highlight.js` v9.x, which is now several major versions behind the current stable `highlight.js` (v11+). This package provides a simple wrapper component, `<highlight>`, allowing developers to display syntax-highlighted code blocks within their Vue templates, either by passing the code as slot content or via a `code` prop. Due to its age and lack of recent updates, it is largely superseded by more actively maintained and official solutions like `@highlightjs/vue-plugin`, which supports both Vue 2 and Vue 3 and integrates with modern `highlight.js` versions. There is no active release cadence, and it is primarily useful for legacy projects still on Vue 2 and an older `highlight.js` version.
Common errors
-
Error: Unknown language: <language-name>
cause The specific highlighting language required was not registered with `highlight.js`.fixImport the language module and register it using `hljs.registerLanguage('<language-name>', require('highlight.js/lib/languages/<language-name>'))`. -
Highlighted code appears unstyled (no colors, just plain text).
cause No `highlight.js` CSS theme has been imported into the project.fixAdd an `import 'highlight.js/styles/default.css'` (or your preferred theme) statement in your main application entry file (e.g., `main.js`) or a global style sheet. -
[Vue warn]: Failed to mount component: template or render function not defined.
cause This error often occurs when a Vue 2 component is used in a Vue 3 application or when a component's definition is incorrectly imported or registered.fixVerify that your project is indeed Vue 2. If it's Vue 3, switch to `@highlightjs/vue-plugin` (v2.x) or another Vue 3-compatible highlighting library.
Warnings
- breaking This package (`vue-highlight-component`) is abandoned and has not been updated in over seven years. It is not compatible with Vue 3 and relies on an outdated version of `highlight.js` (v9.x).
- gotcha The `highlight.js` library (v9.x) used by this component often requires manual registration of specific languages if they are not part of the default bundle. Importing `highlight.js/lib/languages/` for each needed language is necessary.
- gotcha Visual styling for the highlighted code is not included by the `vue-highlight-component` itself. A `highlight.js` CSS theme file must be imported separately into your project.
- gotcha This component is designed for Vue 2. Attempting to use it directly in a Vue 3 application will likely result in compatibility issues or errors due to changes in Vue's API and component instantiation.
Install
-
npm install vue-highlight-component -
yarn add vue-highlight-component -
pnpm add vue-highlight-component
Imports
- Highlight
import { Highlight } from 'vue-highlight-component'import Highlight from 'vue-highlight-component'
- hljs
const hljs = require('highlight.js')import hljs from 'highlight.js'
- CSS Theme
import 'highlight.js/styles/default.css'
Quickstart
<template>
<div id="app">
<h1>Code Example: Swift</h1>
<highlight language="swift">{{ codeSnippet }}</highlight>
<h1>Code Example: JavaScript (via prop)</h1>
<highlight language="javascript" :code="jsCodeSnippet"></highlight>
</div>
</template>
<script>
import hljs from 'highlight.js';
import Highlight from 'vue-highlight-component';
// Import a highlight.js theme for styling
import 'highlight.js/styles/atom-one-dark.css'; // Or any other theme from highlight.js/styles
// Register Swift language if it's not supported by default
// This typically requires installing language-specific modules for highlight.js v9.x
try {
hljs.registerLanguage('swift', require('highlight.js/lib/languages/swift'));
hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript'));
} catch (e) {
console.warn('Could not register language, ensure highlight.js language packages are installed:', e);
}
export default {
name: 'App',
data() {
return {
codeSnippet: `func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
}`,
jsCodeSnippet: `function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial(n - 1);
}`
};
},
components: {
Highlight
}
};
</script>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
margin: 20px;
background-color: #282c34;
color: #abb2bf;
}
h1 {
color: #61afef;
margin-top: 40px;
}
pre {
background-color: #21252b;
padding: 15px;
border-radius: 8px;
overflow-x: auto;
}
</style>