@@ -6,13 +6,11 @@ require("highlightjs-line-numbers.js/dist/highlightjs-line-numbers.min.js");
6
6
const kLoadingMessage = "Loading ..." ;
7
7
8
8
function removeTags ( str ) {
9
- if ( ( str === null ) || ( str === "" ) ) {
9
+ if ( str === null || str === "" ) {
10
10
return false ;
11
11
}
12
- // eslint-disable-next-line no-param-reassign
13
- str = str . toString ( ) ;
14
12
15
- return str . replace ( / < \/ ? [ ^ > ] + ( > | $ ) / ig, "" ) ;
13
+ return str . toString ( ) . replace ( / ( < ( [ ^ > ] + ) > ) / ig, "" ) ;
16
14
}
17
15
18
16
export class CodeFetcher {
@@ -96,20 +94,21 @@ export class CodeFetcher {
96
94
if ( withoutTags === false ) {
97
95
return line ;
98
96
}
99
- const incriminedCodeSingleLine = code . split ( "\n" ) . slice ( location [ 0 ] [ 0 ] - 1 , location [ 0 ] [ 0 ] ) ;
100
- const isMultiLine = location [ 0 ] [ 0 ] < location [ 1 ] [ 0 ] ;
101
- const [ [ startLine ] ] = location ;
97
+ const [ [ startLine ] , [ endLine , endColumn ] ] = location ;
98
+ const isMultiLine = startLine < endLine ;
102
99
const lineIndex = startLine >= 10 ? 9 : startLine - 1 ;
103
- const isRelevantLine = isMultiLine ?
104
- lineIndex <= index && index <= location [ 1 ] [ 0 ] - 1 :
105
- // eslint-disable-next-line max-len
106
- incriminedCodeSingleLine . includes ( value ) || ( ! isMultiLine && lineIndex === index && withoutTags . includes ( value ) ) || ( ! value && lineIndex === index ) ;
100
+ const startFrom = startLine >= 10 ? startLine - 9 : 1 ;
107
101
108
- if ( isRelevantLine ) {
109
- if ( ! isMultiLine && value && line . includes ( value ) ) {
110
- return line . replace ( value , `<span class="relevant-line">${ value } </span>` ) ;
111
- }
102
+ if ( isMultiLine && lineIndex <= index && endLine >= startFrom + index ) {
103
+ return `<span class="relevant-line">${ line } </span>` ;
104
+ }
105
+ else if ( ! isMultiLine && value && line . includes ( value ) ) {
106
+ const indexStart = line . indexOf ( value ) ;
112
107
108
+ // eslint-disable-next-line max-len
109
+ return `${ line . slice ( 0 , indexStart ) } <span class="relevant-line">${ line . slice ( indexStart , indexStart + endColumn ) } </span>${ line . slice ( indexStart + endColumn ) } ` ;
110
+ }
111
+ else if ( ! isMultiLine && startFrom + index === startLine ) {
113
112
return `<span class="relevant-line">${ line } </span>` ;
114
113
}
115
114
0 commit comments