Package Exports
- vue-highlight.js
- vue-highlight.js/lib/allLanguages
- vue-highlight.js/lib/allLanguages.js
- vue-highlight.js/lib/languages/vue
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-highlight.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Vue Highlight.js
đ Highlight.js syntax highlighter component for Vue.
âī¸ Installation
Via npm:
npm install highlight.js vue-highlight.js@next
Via Yarn:
yarn add highlight.js vue-highlight.js@next
Via pnpm:
pnpm add highlight.js vue-highlight.js@next
đŦ Demo
Go to https://gluons.github.io/vue-highlight.js
đ Usage
Main file:
There are 2 ways to import Highlight.js languages.
Import only languages that you want.
import Vue from 'vue'; import VueHighlightJS from 'vue-highlight.js'; // Highlight.js languages (Only required languages) import css from 'highlight.js/lib/languages/css'; import javascript from 'highlight.js/lib/languages/javascript'; import vue from 'vue-highlight.js/lib/languages/vue'; /* * Import Highlight.js theme * Find more: https://highlightjs.org/static/demo/ */ import 'highlight.js/styles/default.css'; import App from './App'; /* * Use Vue Highlight.js */ Vue.use(VueHighlightJS, { // Register only languages that you want languages: { css, javascript, vue } }); new Vue({ el: '#app', render: h => h(App) });
Import all languages.
import Vue from 'vue'; import VueHighlightJS from 'vue-highlight.js'; // Highlight.js languages (All languages) import 'vue-highlight.js/lib/allLanguages' /* * Import Highlight.js theme * Find more: https://highlightjs.org/static/demo/ */ import 'highlight.js/styles/default.css'; import App from './App'; /* * Use Vue Highlight.js */ Vue.use(VueHighlightJS); new Vue({ el: '#app', render: h => h(App) });
Vue file:
<template>
<div id="app">
<!-- Code Block -->
<highlight-code lang="javascript">
let str = 'Hello, World!';
console.log(str);
</highlight-code>
<!-- Inline Code Block -->
<highlight-code lang="javascript" inline>alert('Hello, World!');</highlight-code>
</div>
</template>
<script>
// JavaScript...
</script>
<style>
/* StyleSheet... */
</style>
â Plugin Options
languages
Type: { [name: string]: LanguageFn }
Default: {}
Highlight.js languages.
Add the languages that you want to use here.
name
is the name of language to register with Highlight.js' registerLanguage(languageName, languageDefinition)
API.
See https://github.com/isagalaev/highlight.js#nodejs about importing each language from highlight.js.
đ API
<highlight-code>
Highlight.js code block.
đ° Slots
Static code content.
đ° Properties
lang
Type: String
Highlight.js language.
inline
Type: Boolean
Default: false
Enable inline code block when set it to true
.
code
Type: String
Code content in code block.
You can use this prop if you want to bind code content to your data source.
It's useful for dynamic code content.
Component will ignore slot
static content if you use this.
auto
Type: Boolean
Enable auto detecting code language.
Code will be detected by highlight.js'
highlightAuto
function.
auto
will work well when you import all Highlight.js languages.
Component will ignore lang
prop if you use auto
.
â FAQ
How to write HTML code inside slot?
You have to escape all HTML tags before add it into slot.
If you didn't do that, browser will interpret those tags as HTML element.
âšī¸ See isagalaev/highlight.js#866If you use
code
property instead ofslot
, you don't need to worry about this.
Vue Highlight.js will automatically escape it for you.Why did I get
SyntaxError: Unterminated template literal
error when used<script></script>
incode
property?If you add
</script>
at anywhere insidescript
tag, although it's astring
inside quotes, it will always close thescript
tag.
âšī¸ See "Unterminated template literal" syntax error when literal contains script tag