forked from RickStrahl/highlightjs-badge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlightjs-badgejs.min.js
1 lines (1 loc) · 5.07 KB
/
highlightjs-badgejs.min.js
1
"use strict";!function(e,t){"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e):function(e){if(!e.document)throw new Error("A window with a document is required");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(d,e){var t;function o(e){var t,b={templateSelector:"#CodeBadgeTemplate",contentSelector:"body",loadDelay:0,copyIconClass:"fa fa-copy",copyIconContent:"",checkIconClass:"fa fa-check text-success",checkIconContent:"",hasLineNumber:"false",title:"Copy to clipboard",label:"",badgeClickable:"false",xmlBeautifier:"false"};function o(){b.loadDelay?setTimeout(n,loadDelay):n()}function n(){var e,t,o;document.querySelector(b.templateSelector)||((e=document.createElement("div")).innerHTML=function(){for(var e=["<style>","@media print {"," .code-badge { display: none; }","}"," .code-badge-pre {"," position: relative;"," }"," .code-badge {"," display: flex;"," flex-direction: row;"," white-space: normal;"," background: transparent;"," background: #333;"," color: white;"," font-size: 0.875em;"," opacity: 0.5;"," transition: opacity linear 0.5s;"," border-radius: 0 0 0 7px;"," padding: 5px 8px 5px 8px;"," position: absolute;"," right: 0;",Boolean(b.badgeClickable)?"cursor: pointer;":""," top: 0;"," }"," .code-badge.active {"," opacity: 0.8;"," }",""," .code-badge:hover {"," opacity: .95;"," }",""," .code-badge a,"," .code-badge a:hover {"," text-decoration: none;"," }",""," .code-badge-language {"," margin-right: 10px;"," font-weight: 600;"," color: goldenrod;"," }"," .code-badge-copy-icon {"," font-size: 1.2em;"," cursor: pointer;"," padding: 0 7px;"," margin-top:2;"," }"," .fa.text-success:{ color: limegreen !important }","</style>",'<div id="CodeBadgeTemplate" style="display:none">',' <div class="code-badge">',' <div class="code-badge-language" >{{label}} {{language}}</div>',' <div title="{{title}}">',' <i class="{{copyIconClass}} code-badge-copy-icon"></i></i></a>'," </div>"," </div>","</div>"],t="",o=0;o<e.length;o++)t+=e[o]+"\n";return t}(),t=e.querySelector("style"),o=e.querySelector(b.templateSelector),document.body.appendChild(t),document.body.appendChild(o));var n=document.querySelector(b.templateSelector).innerHTML,a=document.querySelectorAll("pre>code.hljs");Boolean(b.hasLineNumber)&&(a=document.querySelectorAll("pre>code.hljs:not(.hljs-line-numbers)"));for(var c=0;c<a.length;c++){var l=a[c];if(!l.querySelector(".code-badge")){for(var r="",i=0;i<l.classList.length;i++){var s=l.classList[i];if("language-"===s.substr(0,9)){r=l.classList[i].replace("language-","");break}if("lang-"===s.substr(0,5)){r=l.classList[i].replace("lang-","");break}if(!r)for(var d=0;d<l.classList.length;d++)if("hljs"!=l.classList[d]){r=l.classList[d];break}}"ps"==(r=r?r.toLowerCase():"text")?r="powershell":"cs"==r?r="csharp":"js"==r?r="javascript":"ts"==r?r="typescript":"fox"==r&&(r="foxpro");var p=n.replace("{{language}}",r).replace("{{copyIconClass}}",b.copyIconClass).replace("{{title}}",b.title).replace("{{label}}",b.label).trim(),u=document.createElement("div");u.innerHTML=p,u=u.querySelector(".code-badge");var g=l.parentElement;g.classList.add("code-badge-pre"),b.copyIconContent&&(u.querySelector(".code-badge-copy-icon").innerText=b.copyIconContent),g.insertBefore(u,l)}}var m=document.querySelectorAll(b.contentSelector);null!=m&&$(m).each(function(e){this.addEventListener("click",function(e){var t=e.srcElement;return t.classList.contains("code-badge-copy-icon")&&(e.preventDefault(),e.cancelBubble=!0,y(e,r)),Boolean(b.badgeClickable)&&-1!=t.className.indexOf("code-badge")&&(e.preventDefault(),e.cancelBubble=!0,y(e,r)),!1})})}function y(e,t){var o=e.srcElement.parentElement.parentElement.parentElement,n=o.querySelector("pre>code");Boolean(b.hasLineNumber)&&(n=o.querySelector("pre>code:not(.hljs-line-numbers)"));var a,c,l,r,i=n.textContent||n.innerText,s=document.createElement("textarea");s.value=i.trim(),Boolean(b.xmlBeautifier)&&t.toUpperCase()=="xml".toUpperCase()&&(s.value=(a=s.value,r=l="",c=c||"\t",a.split(/>\s*</).forEach(function(e){e.match(/^\/\w/)&&(r=r.substring(c.length)),l+=r+"<"+e+">\r\n",e.match(/^<?\w[^>]*[^\/]$/)&&(r+=c)}),l.substring(1,l.length-3))),document.body.appendChild(s),s.style.display="block",d.document.documentMode?s.setSelectionRange(0,s.value.length):s.select(),document.execCommand("copy"),document.body.removeChild(s),function(e){var t=b.copyIconClass.split(" "),o=b.checkIconClass.split(" "),n=e.querySelector(".code-badge-copy-icon");n.innerText=b.checkIconContent;for(var a=0;a<t.length;a++)n.classList.remove(t[a]);for(a=0;a<o.length;a++)n.classList.add(o[a]);setTimeout(function(){n.innerText=b.copyIconContent;for(var e=0;e<o.length;e++)n.classList.remove(o[e]);for(e=0;e<t.length;e++)n.classList.add(t[e])},2e3)}(o)}t=e,Object.assign(b,t),"loading"==document.readyState?document.addEventListener("DOMContentLoaded",o):o()}"boolean"!=typeof t&&(t=!1),d.highlightJsBadge=o,t&&o()});