1 line
4.4 KiB
JavaScript
Raw Normal View History

2025-01-24 14:24:43 +08:00
(()=>{let i=!1;const a={initCodeBlock(){var e=$("*:not(figure) > pre > code");0!==e.length&&e.each(function(e){var i=$(this).parent();let t=$(this).attr("class"),n="",a=!1,o=!1;if(null!=t){var r=t.match(/[|<](.*)$/),s=t.match(/:select/);if(r||s){let e=0;s&&(e=s.index,r?r[1].endsWith(s[0])&&(r[1]=r[1].substring(0,r[1].length-s[0].length)):n=t.substring(9,s.index),a=!0),r&&(e=r.index<e?r.index:e,"<"===r[0][0]&&(o=!0),n=r[1]),$(this).attr("class",t.substring(0,e)),n=n||t.substring(9,e)}else n=t.substring(9)}let l=$(this).text().split("\n")||[],d=l.length-1,c=String(d).length,p=(1===c&&(c=2),"");for(var g=0;g<d;g++)p+=`<li ${a&&/^\s*\|\+\s+/.test(l[g])?'class="code-select"':""}>${String(g+1).padStart(c,0)}</li>`;""!==l[d].trim()&&(p+=`<li ${a&&/^\s*\|\+\s+/.test(l[g])?'class="code-select"':""}>${String(1+d).padStart(c,0)}</li>`),a&&$(this).text($(this).text().replace(/(^\s*)\|\+\s/gm,"$1"));let h=`codeBlock${e}-`+(new Date).getTime(),u="";o&&(u=" close",$(this).parent().hide());s=`<div><i class="ri-arrow-down-s-line${u}" data-code='#${h}'></i><i class="ri-file-copy-2-line btn-clipboard" title="复制代码" data-clipboard-target='#${h}'></i></div>`;$(this).attr("id",h),i.prepend(`<ul>${p}</ul>`),d>DreamConfig.code_fold_line?i.wrap('<figure class="fold hljs"></figure>').append('<div class="expand-done"><i class="ri-arrow-up-double-line"></i></div>'):i.wrap('<figure class="hljs"></figure>'),i.parent().prepend(`<figcaption>${n}${s}</figcaption>`)})},initLiterature(){$(".literature-content>p:not([class]),.literature-content>mew-hide>p:not([class])").each(function(){0===$(this).children(":not(code,a,strong,em,ins,b,s,br,span.pwd)").length&&$(this).addClass("note")})},initLike(){Utils.initLikeButton(".admire .agree.like","posts")},initHighlighting(){hljs.highlightAll()},initShare(){if(window.DShare){let e=$(".cover-image").css("background-image");e=e&&e.substring(5,e.length-2),DShare.create(".dshare",{image:e,imageSelector:".main-content"})}},initClipboard(){window.clipboard||(window.clipboard=new ClipboardJS(".btn-clipboard"),clipboard.on("error",function(e){e.clearSelection(),Qmsg.error("您的浏览器不支持复制")}),clipboard.on("success",function(){Qmsg.success("复制成功")}))},foldImage(){var e;DreamConfig.img_fold_height&&((e=$(".article .gallery-item>[data-fancybox]>img")).parent().addClass("fold"),e.each(function(){const e=$(this).parent();this.complete?this.scrollHeight>=DreamConfig.img_fold_height?e.append('<div class="expand-done"><i class="ri-arrow-up-double-line"></i></div>'):e.removeClass("fold"):this.onload=function(){this.scrollHeight>=DreamConfig.img_fold_height?e.append('<div class="expand-done"><i class="ri-arrow-up-double-line"></i></div>'):e.removeClass("fold")}}))},initEvent(){var e;i||((e=$("body")).on("click","figure>figcaption .ri-arrow-down-s-line",function(){var e=$(this);e.is(".close")?($(e.attr("data-code")).parent().slideDown(200),e.removeClass("close")):($(e.attr("data-code")).parent().slideUp(200),e.addClass("close"))}),e.on("click","figure > pre > .expand-done",function(){Utils.foldBlock($(this).parent().parent())}),e.on("click",".gallery-item .expand-done",function(e){e.stopPropagation(),Utils.foldBlock($(this).parent())}),Utils.initLikeEvent(".admire .agree.like","posts",e=>e.find("span").find("span")),window.onCommentSuccessEvent=(e,i)=>{var t=encrypt("mew-hide-"+i),n=(n=localStorage.getItem(t))?JSON.parse(decrypt(n)):[],e=String(e.postId);n.includes(e)||(n.push(e),$(`.main-content[data-target='${i}'][data-id='${e}'] mew-hide[hide]`).each(function(){$(this).before(decrypt(this.getAttribute("hide"))),$(this).remove(),commonContext.initGallery(),a.initCodeBlock(),a.initLiterature(),a.initHighlighting(),"true"===this.getAttribute("toc")&&commonContext.initTocAndNotice()}),localStorage.setItem(t,encrypt(JSON.stringify(n))))},i=!0)},initKatex(){var e=$(".main-content");!window.katex&&0!==e.length||(e.find(".katex--inline, [math-inline]").each(function(e,i){katex.render(i.innerText,i,{displayMode:!1})}),e.find(".katex--display, [math-display]").each(function(e,i){katex.render(i.innerText,i,{d