|
| 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, '&') |
| 11 | + .replace(/</g, '<') |
| 12 | + .replace(/>/g, '>') |
| 13 | + .replace(/"/g, '"') |
| 14 | + .replace(/'/g, '''); |
| 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