UNPKG

vue-highlight.js

Version:

Highlight.js syntax highlighter component for Vue.

3 lines (2 loc) 10.2 kB
var VueHighlightJS=function(hljs,Vue){'use strict';function __extends(d,b){function __(){this.constructor=d}extendStatics(d,b),d.prototype=null===b?Object.create(b):(__.prototype=b.prototype,new __)}function __decorate(decorators,target,key,desc){var d,c=arguments.length,r=3>c?target:null===desc?desc=Object.getOwnPropertyDescriptor(target,key):desc;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(decorators,target,key,desc);else for(var i=decorators.length-1;0<=i;i--)(d=decorators[i])&&(r=(3>c?d(r):3<c?d(target,key,r):d(target,key))||r);return 3<c&&r&&Object.defineProperty(target,key,r),r}function Prop(options){return void 0===options&&(options={}),vueClassComponent_common_1(function(componentOptions,k){(componentOptions.props||(componentOptions.props={}))[k]=options})}function escape(code){return"string"==typeof code?code.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&apos;").replace(/</g,"&lt;").replace(/>/g,"&gt;"):code}function getSlotText(slot){return Array.isArray(slot)?slot.map(function(node){return Array.isArray(node.children)&&0<node.children.length?getSlotText(node.children):node.text}).join(""):""}function getMostUsed(indents){let result=0,maxUsed=0,maxWeight=0;for(const entry of indents){const key=entry[0],val=entry[1],u=val[0],w=val[1];(u>maxUsed||u===maxUsed&&w>maxWeight)&&(maxUsed=u,maxWeight=w,result=+key)}return result}function indentCode(code){if("string"==typeof code){var indent=detectIndent(code).indent||"\t";return code=redent(code,0,indent),code.trim()}return code}hljs=hljs&&hljs.hasOwnProperty("default")?hljs["default"]:hljs,Vue=Vue&&Vue.hasOwnProperty("default")?Vue["default"]:Vue;var extendStatics=function(d,b){return extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)b.hasOwnProperty(p)&&(d[p]=b[p])},extendStatics(d,b)},vueClassComponent_common=function(fn,module){return module={exports:{}},fn(module,module.exports),module.exports}(function(module,exports){function _interopDefault(ex){return ex&&"object"==typeof ex&&"default"in ex?ex["default"]:ex}function copyReflectionMetadata(to,from){forwardMetadata(to,from),Object.getOwnPropertyNames(from.prototype).forEach(function(key){forwardMetadata(to.prototype,from.prototype,key)}),Object.getOwnPropertyNames(from).forEach(function(key){forwardMetadata(to,from,key)})}function forwardMetadata(to,from,propertyKey){var metaKeys=propertyKey?Reflect.getOwnMetadataKeys(from,propertyKey):Reflect.getOwnMetadataKeys(from);metaKeys.forEach(function(metaKey){var metadata=propertyKey?Reflect.getOwnMetadata(metaKey,from,propertyKey):Reflect.getOwnMetadata(metaKey,from);propertyKey?Reflect.defineMetadata(metaKey,metadata,to,propertyKey):Reflect.defineMetadata(metaKey,metadata,to)})}function isPrimitive(value){var type=typeof value;return null==value||"object"!=type&&"function"!=type}function collectDataFromConstructor(vm,Component){var originalInit=Component.prototype._init;Component.prototype._init=function(){var _this=this,keys=Object.getOwnPropertyNames(vm);if(vm.$options.props)for(var key in vm.$options.props)vm.hasOwnProperty(key)||keys.push(key);keys.forEach(function(key){"_"!==key.charAt(0)&&Object.defineProperty(_this,key,{get:function(){return vm[key]},set:function(value){vm[key]=value},configurable:!0})})};var data=new Component;Component.prototype._init=originalInit;var plainData={};return Object.keys(data).forEach(function(key){void 0!==data[key]&&(plainData[key]=data[key])}),plainData}function componentFactory(Component,options){void 0===options&&(options={}),options.name=options.name||Component._componentTag||Component.name;var proto=Component.prototype;Object.getOwnPropertyNames(proto).forEach(function(key){if("constructor"!==key){if(-1<$internalHooks.indexOf(key))return void(options[key]=proto[key]);var descriptor=Object.getOwnPropertyDescriptor(proto,key);void 0===descriptor.value?(descriptor.get||descriptor.set)&&((options.computed||(options.computed={}))[key]={get:descriptor.get,set:descriptor.set}):"function"==typeof descriptor.value?(options.methods||(options.methods={}))[key]=descriptor.value:(options.mixins||(options.mixins=[])).push({data:function(){var _a;return _a={},_a[key]=descriptor.value,_a}})}}),(options.mixins||(options.mixins=[])).push({data:function(){return collectDataFromConstructor(this,Component)}});var decorators=Component.__decorators__;decorators&&(decorators.forEach(function(fn){return fn(options)}),delete Component.__decorators__);var superProto=Object.getPrototypeOf(Component.prototype),Super=superProto instanceof Vue$1?superProto.constructor:Vue$1,Extended=Super.extend(options);return forwardStaticMembers(Extended,Component,Super),reflectionIsSupported&&copyReflectionMetadata(Extended,Component),Extended}function forwardStaticMembers(Extended,Original,Super){Object.getOwnPropertyNames(Original).forEach(function(key){if("prototype"!==key){var extendedDescriptor=Object.getOwnPropertyDescriptor(Extended,key);if(!extendedDescriptor||extendedDescriptor.configurable){var descriptor=Object.getOwnPropertyDescriptor(Original,key);if(!hasProto){if("cid"===key)return;var superDescriptor=Object.getOwnPropertyDescriptor(Super,key);if(!isPrimitive(descriptor.value)&&superDescriptor&&superDescriptor.value===descriptor.value)return}Object.defineProperty(Extended,key,descriptor)}}})}function Component(options){return"function"==typeof options?componentFactory(options):function(Component){return componentFactory(Component,options)}}Object.defineProperty(exports,"__esModule",{value:!0});var Vue$1=_interopDefault(Vue),reflectionIsSupported="undefined"!=typeof Reflect&&Reflect.defineMetadata,hasProto={__proto__:[]}instanceof Array,$internalHooks=["data","beforeCreate","created","beforeMount","mounted","beforeDestroy","destroyed","beforeUpdate","updated","activated","deactivated","render","errorCaptured"];Component.registerHooks=function(keys){$internalHooks.push.apply($internalHooks,keys)},exports.default=Component,exports.createDecorator=function(factory){return function(target,key,index){var Ctor="function"==typeof target?target:target.constructor;Ctor.__decorators__||(Ctor.__decorators__=[]),"number"!=typeof index&&(index=void 0),Ctor.__decorators__.push(function(options){return factory(options,key,index)})}},exports.mixins=function(){for(var Ctors=[],_i=0;_i<arguments.length;_i++)Ctors[_i]=arguments[_i];return Vue$1.extend({mixins:Ctors})}}),Component=function(x){return x&&x.__esModule&&Object.prototype.hasOwnProperty.call(x,"default")?x.default:x}(vueClassComponent_common),vueClassComponent_common_1=vueClassComponent_common.createDecorator,vueClassComponent_common_2=vueClassComponent_common.mixins;const INDENT_RE=/^(?:( )+|\t+)/;var detectIndent=str=>{if("string"!=typeof str)throw new TypeError("Expected a string");let tabs=0,spaces=0,prev=0;const indents=new Map;let current,isIndent;for(const line of str.split(/\n/g)){if(!line)continue;let indent;const matches=line.match(INDENT_RE);matches?(indent=matches[0].length,matches[1]?spaces++:tabs++):indent=0;const diff=indent-prev;prev=indent,diff?(isIndent=0<diff,current=indents.get(isIndent?diff:-diff),current?current[0]++:(current=[1,0],indents.set(diff,current))):current&&(current[1]+=+isIndent)}const amount=getMostUsed(indents);let type,indent;return amount?spaces>=tabs?(type="space",indent=" ".repeat(amount)):(type="tab",indent="\t".repeat(amount)):(type=null,indent=""),{amount,type,indent}},stripIndent=str=>{const match=str.match(/^[ \t]*(?=\S)/gm);if(!match)return str;const indent=Math.min.apply(Math,match.map(x=>x.length)),re=new RegExp(`^[ \\t]{${indent}}`,"gm");return 0<indent?str.replace(re,""):str},indentString=(str,count,opts)=>{const options="object"==typeof opts?Object.assign({indent:" "},opts):{indent:opts||" "};if(count=void 0===count?1:count,"string"!=typeof str)throw new TypeError(`Expected \`input\` to be a \`string\`, got \`${typeof str}\``);if("number"!=typeof count)throw new TypeError(`Expected \`count\` to be a \`number\`, got \`${typeof count}\``);if("string"!=typeof options.indent)throw new TypeError(`Expected \`options.indent\` to be a \`string\`, got \`${typeof options.indent}\``);if(0===count)return str;const regex=options.includeEmptyLines?/^/mg:/^(?!\s*$)/mg;return str.replace(regex,options.indent.repeat(count))},redent=(str,count,indent)=>indentString(stripIndent(str),count||0,indent),inlineStyles={display:"inline !important","vertical-align":"middle"},HighlightCode=function(_super){function HighlightCode(){return null!==_super&&_super.apply(this,arguments)||this}return __extends(HighlightCode,_super),Object.defineProperty(HighlightCode.prototype,"hasCode",{get:function(){return"string"==typeof this.code&&0<this.code.length},enumerable:!0,configurable:!0}),HighlightCode.prototype.render=function(h){var _a,_b=this,hasCode=_b.hasCode,inline=_b.inline,auto=_b.auto,lang=this.lang,code=hasCode?this.code:getSlotText(this.$slots.default);inline||(code=indentCode(code));var highlightedCode;try{auto?(_a=hljs.highlightAuto(code),lang=_a.language,highlightedCode=_a.value):highlightedCode=lang?hljs.highlight(lang,code).value:escape(code)}catch(err){highlightedCode=escape(code),console.error(err)}return h(inline?"span":"pre",[h("code",{class:["hljs"].concat(lang?[lang]:[]),style:inline?inlineStyles:{},domProps:{innerHTML:highlightedCode}})])},__decorate([Prop(String)],HighlightCode.prototype,"lang",void 0),__decorate([Prop({type:Boolean,default:!1})],HighlightCode.prototype,"inline",void 0),__decorate([Prop(String)],HighlightCode.prototype,"code",void 0),__decorate([Prop(Boolean)],HighlightCode.prototype,"auto",void 0),HighlightCode=__decorate([Component({name:"HighlightCode"})],HighlightCode),HighlightCode}(Vue),install=function(vue,options){void 0===options&&(options={languages:{}});options.languages;{var xml_1=window.hljs.getLanguage("xml");window.hljs.registerLanguage("vue",function(){return xml_1})}vue.component("highlight-code",HighlightCode)};"undefined"!=typeof window&&window.Vue&&install(window.Vue);return{install:install}}(hljs,Vue); //# sourceMappingURL=vue-highlight.web.min.js.map