Vue Prism.js Component

2.0.0 · active · verified Sun Apr 19

vue-prism-component is a Vue component wrapper for Prism.js, providing syntax highlighting capabilities within Vue applications. The current stable version is 2.0.0, which targets Vue 3. Version 1.x supports Vue 2. The package maintains a steady release cadence, with updates typically driven by new Vue major versions or dependency updates. Its key differentiator is its straightforward integration into Vue SFCs and JSX, allowing developers to easily display formatted code blocks or inline code snippets using the popular Prism.js library. Unlike direct Prism.js usage, this component handles reactivity and rendering within the Vue ecosystem, simplifying dynamic code display and theme management. It requires `prismjs` as a peer dependency for core highlighting functionality and themes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up and use `vue-prism-component` in a Vue 3 application. It shows importing Prism.js core, a theme, and specific language components, then renders both block and inline code snippets using the `<Prism>` component with `language` and `code` props.

import { createApp, h } from 'vue';
import Prism from 'vue-prism-component';
import 'prismjs'; // Core Prism.js
import 'prismjs/themes/prism-tomorrow.css'; // A dark theme
import 'prismjs/components/prism-javascript'; // Language support for JavaScript
import 'prismjs/components/prism-css'; // Language support for CSS

const App = {
  components: { Prism },
  data() {
    return {
      jsCode: 'function greet() {\n  console.log(\"Hello, Vue!\");\n}',
      cssCode: '.container {\n  margin: 0 auto;\n  padding: 20px;\n}',
      inlineCode: 'const x = 10;' 
    };
  },
  template: `
    <div>
      <h1>JavaScript Example:</h1>
      <prism language="javascript">{{ jsCode }}</prism>

      <h1>CSS Example:</h1>
      <prism language="css" :code="cssCode" />

      <h2>Inline Code:</h2>
      <p>Here's some inline code: <prism inline language="javascript">{{ inlineCode }}</prism></p>
    </div>
  `
};

createApp(App).mount('#app');

view raw JSON →