dm-vue-highlight v2.2.0
Vue Highlight.js
š Highlight.js syntax highlighter component for Vue.
āļø Installation
Via NPM:
Install highlight.js:
npm install highlight.jsInstall Vue Highlight.js:
npm install vue-highlight.js
Or Yarn:
Install highlight.js:
yarn add highlight.jsInstall Vue Highlight.js:
yarn add vue-highlight.js
š¬ Demo
Go to https://gluons.github.io/vue-highlight.js
š Usage
Main file:
import Vue from 'vue';
import VueHighlightJS from 'vue-highlight.js';
import App from './App';
/*
* Use Vue Highlight.js
*/
Vue.use(VueHighlightJS);
/*
* Import Highlight.js theme
* Find more: https://highlightjs.org/static/demo/
*/
import 'highlight.js/styles/default.css';
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>š 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'
highlightAutofunction.
Component will ignore lang prop if you use this.
ā 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
codeproperty 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 literalerror when used<script></script>incodeproperty?If you add
</script>at anywhere insidescripttag, although it's astringinside quotes, it will always close thescripttag.
ā¹ļø See "Unterminated template literal" syntax error when literal contains script tag
