Skip to content

Commit 66bd797

Browse files
authored
fix: highlight lines are cut when sliding (#437)
1 parent bc15841 commit 66bd797

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

lib/default-theme/styles/code.styl

+13-5
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@
2121
padding 0
2222
background-color transparent
2323
border-radius 0
24-
.highlighted-line
25-
background-color rgba(0, 0, 0, 66%)
26-
display block
27-
margin 0 -1.5rem
28-
padding 0 1.5rem
2924

3025
div[class*="language-"]
3126
position relative
3227
background-color $codeBgColor
3328
border-radius 6px
29+
.highlight-lines
30+
user-select none
31+
padding-top 1.3rem
32+
position absolute
33+
z-index 0
34+
width 100%
35+
line-height 1.4
36+
.highlighted
37+
background-color rgba(0, 0, 0, 66%)
38+
pre
39+
position relative
40+
z-index 1
3441
&::before
3542
position absolute
43+
z-index 3
3644
top 0.8em
3745
right 1em
3846
font-size 0.75rem

lib/markdown/highlightLines.js

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// forked from https://github.com/egoist/markdown-it-highlight-lines
1+
// Modified from https://github.com/egoist/markdown-it-highlight-lines
22

33
const RE = /{([\d,-]+)}/
44
const wrapperRE = /^<pre .*?><code>/
@@ -27,9 +27,7 @@ module.exports = md => {
2727
: token.content
2828

2929
const rawCode = code.replace(wrapperRE, '')
30-
const leadingWrapper = code.match(wrapperRE)[0]
31-
32-
const codeSplits = rawCode.split('\n').map((split, index) => {
30+
const highlightLinesCode = rawCode.split('\n').map((split, index) => {
3331
const lineNumber = index + 1
3432
const inRange = lineNumbers.some(([start, end]) => {
3533
if (start && end) {
@@ -38,23 +36,14 @@ module.exports = md => {
3836
return lineNumber === start
3937
})
4038
if (inRange) {
41-
return {
42-
code: `<span class="highlighted-line">${split || '\n'}</span>`,
43-
highlighted: true
44-
}
45-
}
46-
return {
47-
code: split
39+
return `<div class="highlighted">&nbsp;</div>`
4840
}
49-
})
50-
let highlightedCode = leadingWrapper
51-
codeSplits.forEach(split => {
52-
if (split.highlighted) {
53-
highlightedCode += split.code
54-
} else {
55-
highlightedCode += `${split.code}\n`
56-
}
57-
})
58-
return highlightedCode
41+
return '<br>'
42+
}).join('')
43+
44+
const highlightLinesWrapperCode =
45+
`<div class="highlight-lines">${highlightLinesCode}</div>`
46+
47+
return highlightLinesWrapperCode + code
5948
}
6049
}

lib/markdown/preWrapper.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
// markdown-it plugin for wrapping <pre> ... </pre>.
2+
//
3+
// If your plugin was chained before preWrapper, you can add additional eleemnt directly.
4+
// If your plugin was chained after preWrapper, you can use these slots:
5+
// 1. <!--beforebegin-->
6+
// 2. <!--afterbegin-->
7+
// 3. <!--beforeend-->
8+
// 4. <!--afterend-->
29

310
module.exports = md => {
411
const fence = md.renderer.rules.fence
512
md.renderer.rules.fence = (...args) => {
613
const [tokens, idx] = args
714
const token = tokens[idx]
815
const rawCode = fence(...args)
9-
return `<!--beforebegin--><div class="language-${token.info.trim()}"><!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
16+
return `<!--beforebegin--><div class="language-${token.info.trim()}">` +
17+
`<!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
1018
}
1119
}

0 commit comments

Comments
 (0)