-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
var vueHighlightJS = {};
vueHighlightJS.install = function install(Vue, options) {
var hljs = options && options.hljs;
if (!hljs) throw new Error('vue-highlightjs requires options.hljs in Vue.use()');
Vue.directive('highlightjs', {
deep: true,
bind: function bind(el, binding) {
// on first bind, highlight all targets
var targets = el.querySelectorAll('code');
var target;
var i;
for (i = 0; i < targets.length; i += 1) {
target = targets[i];
if (typeof binding.value === 'string') {
// if a value is directly assigned to the directive, use this
// instead of the element content.
target.textContent = binding.value;
}
hljs.highlightBlock(target);
}
},
componentUpdated: function componentUpdated(el, binding) {
// after an update, re-fill the content and then highlight
var targets = el.querySelectorAll('code');
var target;
var i;
for (i = 0; i < targets.length; i += 1) {
target = targets[i];
if (typeof binding.value === 'string') {
target.textContent = binding.value;
}
hljs.highlightBlock(target);
}
}
});
};
module.exports = vueHighlightJS;