From 9d5bc74ff6a598f9dc3fc2538da4d0e7cb0d4623 Mon Sep 17 00:00:00 2001 From: nineya <361654768@qq.com> Date: Tue, 28 Mar 2023 14:07:16 +0800 Subject: [PATCH] =?UTF-8?q?perf(code):=20=E4=BF=AE=E6=94=B9=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=9D=97=E4=B8=AD=E4=BB=A3=E7=A0=81=E8=A1=8C=E9=80=89?= =?UTF-8?q?=E4=B8=AD=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/js/post.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/js/post.js b/src/js/post.js index 4970ab1..3cc29a7 100644 --- a/src/js/post.js +++ b/src/js/post.js @@ -9,11 +9,11 @@ const postContext = { let clazz = $(this).attr('class') // 通过class初始化代码块标题和是否默认关闭 let title = '' - let lines = [0, 0] + let lines = false let isClose = false if (clazz != null) { let str1 = clazz.match(/[|<](.*)$/) - let str2 = clazz.match(/:(\d+)-(\d+)/) + let str2 = clazz.match(/:select/) if (str1 || str2) { let num = 0 if (str2) { @@ -25,8 +25,7 @@ const postContext = { } else { title = clazz.substring(9, str2.index) } - lines[0] = Number(str2[1]) - lines[1] = Number(str2[2]) + lines = true } if (str1) { num = str1.index < num ? str1.index : num @@ -41,12 +40,16 @@ const postContext = { } } // 生成行号 - let nums = $(this).text().split('\n').length - 1 || 1 + let codes = $(this).text().split('\n') || [] + let nums = codes.length - 1 let lineDigit = String(nums).length if (lineDigit === 1) lineDigit = 2 let lis = '' - for (var i = 1; i <= nums; i++) { - lis += `
  • = lines[0] && i <= lines[1]? 'class="code-select"' : ''}>${String(i).padStart(lineDigit, 0)}
  • ` + for (var i = 0; i < nums; i++) { + lis += `
  • ${String(i + 1).padStart(lineDigit, 0)}
  • ` + } + if (lines) { + $(this).text($(this).text().replace(/(^\s*)\|\+\s/gm,'$1')) } // 代码块的id,用于代码块复制和折叠 let id = `codeBlock${index}-${new Date().getTime()}`