Skip to content

Commit 17ac21c

Browse files
committed
initial commit
0 parents  commit 17ac21c

File tree

8 files changed

+789
-0
lines changed

8 files changed

+789
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Highlight.js plugin for Vue.js
2+
3+
This plugin provides a `highlightjs` component for use
4+
in your templates:
5+
6+
```html
7+
<div id="app">
8+
<!-- bind to a data property named `code` -->
9+
<highlightjs autodetect :code="code" />
10+
<!-- or literal code works as well -->
11+
<highlightjs language='javascript' code="var x = 5;" />
12+
</div>
13+
```
14+
15+
## Using the pre-built libraries
16+
17+
```html
18+
<link rel="stylesheet" href="/path/to/styles/default.css">
19+
<script src="/path/to/highlight.min.js"></script>
20+
<script src="/path/to/highlight-vue.min.js"></script>
21+
```
22+
23+
Then simply register the plugin with Vue:
24+
25+
```js
26+
Vue.use(hljsVuePlugin());
27+
```
28+
29+
30+
## Using ES6 modules / bundling
31+
32+
```js
33+
import hljs from 'highlight.js/lib/core';
34+
import javascript from 'highlight.js/lib/languages/javascript';
35+
import vuePlugin from "@highlightjs/vue-plugin";
36+
37+
hljs.registerLanguage('javascript', javascript);
38+
39+
Vue.use(vuePlugin());
40+
```

dist/highlightjs-vue.min.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
var hljsVuePlugin = (function (highlightjs) {
2+
'use strict';
3+
4+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
5+
6+
var highlightjs__default = /*#__PURE__*/_interopDefaultLegacy(highlightjs);
7+
8+
function escapeHTML(value) {
9+
return value
10+
.replace(/&/g, '&amp;')
11+
.replace(/</g, '&lt;')
12+
.replace(/>/g, '&gt;')
13+
.replace(/"/g, '&quot;')
14+
.replace(/'/g, '&#x27;');
15+
}
16+
17+
function hasValueOrEmptyAttribute(value) {
18+
return Boolean(value || value === "");
19+
}
20+
21+
function BuildVuePlugin(hljs=highlightjs__default['default']) {
22+
const Component = {
23+
props: ["language", "code", "autodetect"],
24+
data: function() {
25+
return {
26+
detectedLanguage: "",
27+
unknownLanguage: false
28+
};
29+
},
30+
computed: {
31+
className() {
32+
if (this.unknownLanguage) return "";
33+
34+
return "hljs " + this.detectedLanguage;
35+
},
36+
highlighted() {
37+
// no idea what language to use, return raw code
38+
if (!this.autoDetect && !hljs.getLanguage(this.language)) {
39+
console.warn(`The language "${this.language}" you specified could not be found.`);
40+
this.unknownLanguage = true;
41+
return escapeHTML(this.code);
42+
}
43+
44+
let result = {};
45+
if (this.autoDetect) {
46+
result = hljs.highlightAuto(this.code);
47+
this.detectedLanguage = result.language;
48+
} else {
49+
result = hljs.highlight(this.language, this.code, this.ignoreIllegals);
50+
this.detectedLanguage = this.language;
51+
}
52+
return result.value;
53+
},
54+
autoDetect() {
55+
return !this.language || hasValueOrEmptyAttribute(this.autodetect);
56+
},
57+
ignoreIllegals() {
58+
return true;
59+
}
60+
},
61+
// this avoids needing to use a whole Vue compilation pipeline just
62+
// to build Highlight.js
63+
render(createElement) {
64+
return createElement("pre", {}, [
65+
createElement("code", {
66+
class: this.className,
67+
domProps: { innerHTML: this.highlighted }
68+
})
69+
]);
70+
}
71+
// template: `<pre><code :class="className" v-html="highlighted"></code></pre>`
72+
};
73+
74+
const VuePlugin = {
75+
install(Vue) {
76+
Vue.component('highlightjs', Component);
77+
},
78+
component: Component
79+
};
80+
81+
return VuePlugin;
82+
}
83+
84+
return BuildVuePlugin;
85+
86+
}(hljs));

0 commit comments

Comments
 (0)