forked from metachris/vue-highlightjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (30 loc) · 833 Bytes
/
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
var hljs = require('highlight.js')
var vueHighlightJS = {}
vueHighlightJS.install = function (Vue) {
Vue.directive('highlightjs', {
deep: true,
bind: function (el, binding) {
// on first bind, highlight all targets
var targets = el.querySelectorAll('code')
targets.forEach(function(target) {
// if a value is directly assigned to the directive, use this
// instead of the element content.
if (binding.value) {
target.innerHTML = binding.value
}
hljs.highlightBlock(target)
})
},
componentUpdated: function (el, binding) {
// after an update, re-fill the content and then highlight
var targets = el.querySelectorAll('code')
targets.forEach(function(target) {
if (binding.value) {
target.innerHTML = binding.value
hljs.highlightBlock(target)
}
})
}
})
}
module.exports = vueHighlightJS