Skip to content

Commit

Permalink
fix: highlight lines are cut when sliding (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz authored May 13, 2018
1 parent bc15841 commit 66bd797
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
18 changes: 13 additions & 5 deletions lib/default-theme/styles/code.styl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 10 additions & 21 deletions lib/markdown/highlightLines.js
Original file line number Diff line number Diff line change
@@ -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 = /^<pre .*?><code>/
Expand Down Expand Up @@ -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) {
Expand All @@ -38,23 +36,14 @@ module.exports = md => {
return lineNumber === start
})
if (inRange) {
return {
code: `<span class="highlighted-line">${split || '\n'}</span>`,
highlighted: true
}
}
return {
code: split
return `<div class="highlighted">&nbsp;</div>`
}
})
let highlightedCode = leadingWrapper
codeSplits.forEach(split => {
if (split.highlighted) {
highlightedCode += split.code
} else {
highlightedCode += `${split.code}\n`
}
})
return highlightedCode
return '<br>'
}).join('')

const highlightLinesWrapperCode =
`<div class="highlight-lines">${highlightLinesCode}</div>`

return highlightLinesWrapperCode + code
}
}
10 changes: 9 additions & 1 deletion lib/markdown/preWrapper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
// markdown-it plugin for wrapping <pre> ... </pre>.
//
// 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. <!--beforebegin-->
// 2. <!--afterbegin-->
// 3. <!--beforeend-->
// 4. <!--afterend-->

module.exports = md => {
const fence = md.renderer.rules.fence
md.renderer.rules.fence = (...args) => {
const [tokens, idx] = args
const token = tokens[idx]
const rawCode = fence(...args)
return `<!--beforebegin--><div class="language-${token.info.trim()}"><!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
return `<!--beforebegin--><div class="language-${token.info.trim()}">` +
`<!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
}
}

0 comments on commit 66bd797

Please sign in to comment.