diff --git a/src/js/post.js b/src/js/post.js index 1bd32b8..61554e9 100644 --- a/src/js/post.js +++ b/src/js/post.js @@ -42,16 +42,16 @@ const postContext = { } // 生成行号 let codes = $(this).text().split('\n') || [] - let nums = codes.length - 1 + let nums = codes.length let lineDigit = String(nums).length if (lineDigit === 1) lineDigit = 2 let lis = '' for (var i = 0; i < nums; i++) { + if (i === nums - 1 && codes[i].trim() === '') { + continue + } lis += `
  • ${String(i + 1).padStart(lineDigit, 0)}
  • ` } - if (codes[nums].trim() !== '') { - lis += `
  • ${String(nums + 1).padStart(lineDigit, 0)}
  • ` - } if (lines) { $(this).text($(this).text().replace(/(^\s*)\|\+\s/gm, '$1')) } diff --git a/templates/assets/js/post.min.js b/templates/assets/js/post.min.js index ea29833..e42bba5 100644 --- a/templates/assets/js/post.min.js +++ b/templates/assets/js/post.min.js @@ -1 +1 @@ -(()=>{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(/[|<](.*)$/),l=t.match(/:select/);if(r||l){let e=0;l&&(e=l.index,r?r[1].endsWith(l[0])&&(r[1]=r[1].substring(0,r[1].length-l[0].length)):n=t.substring(9,l.index),a=!0),r&&(e=r.index${String(g+1).padStart(c,0)}`;""!==s[d].trim()&&(h+=`
  • ${String(1+d).padStart(c,0)}
  • `),a&&$(this).text($(this).text().replace(/(^\s*)\|\+\s/gm,"$1"));let p=`codeBlock${e}-`+(new Date).getTime(),u="";o&&(u=" close",$(this).parent().hide());l=`
    `;$(this).attr("id",p),i.prepend(``),d>DreamConfig.code_fold_line?i.wrap('
    ').append('
    '):i.wrap('
    '),i.parent().prepend(`
    ${n}${l}
    `)})},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(){"undefined"!=typeof hljs&&null!==hljs&&(document.querySelectorAll('code[data-highlighted="yes"]').forEach(e=>{e.removeAttribute("data-highlighted")}),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('
    '):e.removeClass("fold"):this.onload=function(){this.scrollHeight>=DreamConfig.img_fold_height?e.append('
    '):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)}};window.initKatex=function(){var e=$(".main-content");"undefined"!=typeof katex&&null!==katex&&0!==e.length?(e.find("[math-inline], .math-inline, .katex--inline").each(function(e,i){katex.render(i.innerText,i,{displayMode:!1})}),e.find("[math-display], .math-display, .katex--display").each(function(e,i){katex.render(i.innerText,i,{displayMode:!0})})):console.log("katex is not defined")},window.initMermaid=function(){"undefined"!=typeof mermaid&&null!==mermaid&&(document.body,mermaid.initialize({startOnLoad:!0}),mermaid.run({querySelector:"text-diagram[data-type=mermaid]"}))},window.postPjax=function(i){0!==$(".main-content").length&&Object.keys(a).forEach(e=>window.pjaxSerialNumber===i&&a[e]())};{const t=["initEvent","initCodeBlock","initLiterature","initLike","foldImage"];Object.keys(a).forEach(e=>!window.pjaxSerialNumber&&t.includes(e)&&a[e]()),document.addEventListener("DOMContentLoaded",function(){Object.keys(a).forEach(e=>!window.pjaxSerialNumber&&!t.includes(e)&&a[e]())})}})(); \ No newline at end of file +(()=>{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(/[|<](.*)$/),l=t.match(/:select/);if(r||l){let e=0;l&&(e=l.index,r?r[1].endsWith(l[0])&&(r[1]=r[1].substring(0,r[1].length-l[0].length)):n=t.substring(9,l.index),a=!0),r&&(e=r.index${String(g+1).padStart(c,0)}`);a&&$(this).text($(this).text().replace(/(^\s*)\|\+\s/gm,"$1"));let p=`codeBlock${e}-`+(new Date).getTime(),u="";o&&(u=" close",$(this).parent().hide());l=`
    `;$(this).attr("id",p),i.prepend(``),s>DreamConfig.code_fold_line?i.wrap('
    ').append('
    '):i.wrap('
    '),i.parent().prepend(`
    ${n}${l}
    `)})},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(){"undefined"!=typeof hljs&&null!==hljs&&(document.querySelectorAll('code[data-highlighted="yes"]').forEach(e=>{e.removeAttribute("data-highlighted")}),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('
    '):e.removeClass("fold"):this.onload=function(){this.scrollHeight>=DreamConfig.img_fold_height?e.append('
    '):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)}};window.initKatex=function(){var e=$(".main-content");"undefined"!=typeof katex&&null!==katex&&0!==e.length?(e.find("[math-inline], .math-inline, .katex--inline").each(function(e,i){katex.render(i.innerText,i,{displayMode:!1})}),e.find("[math-display], .math-display, .katex--display").each(function(e,i){katex.render(i.innerText,i,{displayMode:!0})})):console.log("katex is not defined")},window.initMermaid=function(){"undefined"!=typeof mermaid&&null!==mermaid&&(document.body,mermaid.initialize({startOnLoad:!0}),mermaid.run({querySelector:"text-diagram[data-type=mermaid]"}))},window.postPjax=function(i){0!==$(".main-content").length&&Object.keys(a).forEach(e=>window.pjaxSerialNumber===i&&a[e]())};{const t=["initEvent","initCodeBlock","initLiterature","initLike","foldImage"];Object.keys(a).forEach(e=>!window.pjaxSerialNumber&&t.includes(e)&&a[e]()),document.addEventListener("DOMContentLoaded",function(){Object.keys(a).forEach(e=>!window.pjaxSerialNumber&&!t.includes(e)&&a[e]())})}})(); \ No newline at end of file