Vue Highlight Component

1.0.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and use the `Highlight` component in a Vue 2 application, including registering a custom language and applying a visual theme from `highlight.js`.

<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>

view raw JSON →