From 66bd797df9aa45670ddb25a46075bb4b456c3e57 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Sun, 13 May 2018 15:41:35 +0800 Subject: [PATCH] fix: highlight lines are cut when sliding (#437) --- lib/default-theme/styles/code.styl | 18 ++++++++++++----- lib/markdown/highlightLines.js | 31 ++++++++++-------------------- lib/markdown/preWrapper.js | 10 +++++++++- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/default-theme/styles/code.styl b/lib/default-theme/styles/code.styl index b562307a82..54cced7e3e 100644 --- a/lib/default-theme/styles/code.styl +++ b/lib/default-theme/styles/code.styl @@ -21,18 +21,26 @@ padding 0 background-color transparent border-radius 0 - .highlighted-line - background-color rgba(0, 0, 0, 66%) - display block - margin 0 -1.5rem - padding 0 1.5rem div[class*="language-"] position relative background-color $codeBgColor border-radius 6px + .highlight-lines + user-select none + padding-top 1.3rem + position absolute + z-index 0 + width 100% + line-height 1.4 + .highlighted + background-color rgba(0, 0, 0, 66%) + pre + position relative + z-index 1 &::before position absolute + z-index 3 top 0.8em right 1em font-size 0.75rem diff --git a/lib/markdown/highlightLines.js b/lib/markdown/highlightLines.js index 7f3fcc4b8e..dd8334324b 100644 --- a/lib/markdown/highlightLines.js +++ b/lib/markdown/highlightLines.js @@ -1,4 +1,4 @@ -// forked from https://github.com/egoist/markdown-it-highlight-lines +// Modified from https://github.com/egoist/markdown-it-highlight-lines const RE = /{([\d,-]+)}/ const wrapperRE = /^
/
@@ -27,9 +27,7 @@ module.exports = md => {
       : token.content
 
     const rawCode = code.replace(wrapperRE, '')
-    const leadingWrapper = code.match(wrapperRE)[0]
-
-    const codeSplits = rawCode.split('\n').map((split, index) => {
+    const highlightLinesCode = rawCode.split('\n').map((split, index) => {
       const lineNumber = index + 1
       const inRange = lineNumbers.some(([start, end]) => {
         if (start && end) {
@@ -38,23 +36,14 @@ module.exports = md => {
         return lineNumber === start
       })
       if (inRange) {
-        return {
-          code: `${split || '\n'}`,
-          highlighted: true
-        }
-      }
-      return {
-        code: split
+        return `
 
` } - }) - let highlightedCode = leadingWrapper - codeSplits.forEach(split => { - if (split.highlighted) { - highlightedCode += split.code - } else { - highlightedCode += `${split.code}\n` - } - }) - return highlightedCode + return '
' + }).join('') + + const highlightLinesWrapperCode = + `
${highlightLinesCode}
` + + return highlightLinesWrapperCode + code } } diff --git a/lib/markdown/preWrapper.js b/lib/markdown/preWrapper.js index 01ef8574a7..1ade38753a 100644 --- a/lib/markdown/preWrapper.js +++ b/lib/markdown/preWrapper.js @@ -1,4 +1,11 @@ // markdown-it plugin for wrapping
 ... 
. +// +// If your plugin was chained before preWrapper, you can add additional eleemnt directly. +// If your plugin was chained after preWrapper, you can use these slots: +// 1. +// 2. +// 3. +// 4. module.exports = md => { const fence = md.renderer.rules.fence @@ -6,6 +13,7 @@ module.exports = md => { const [tokens, idx] = args const token = tokens[idx] const rawCode = fence(...args) - return `
${rawCode}
` + return `
` + + `${rawCode}
` } }