Skip to content

Commit

Permalink
Fixed the Broken Specific Line Issue (#50)
Browse files Browse the repository at this point in the history
myousuffazal authored Oct 27, 2023
1 parent fc8534f commit 56ef339
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions resources/code-specific-lines.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ Prism.hooks.add( 'complete', function( env ) {
if (showLineRanges) {
var showLines = showLines( showLineRanges );

var tokenList = tokenList( env.element.innerHTML, showLines );
var tokenList = tokenList( env.element, showLines );

var lineNumbersActive = Prism.util.isActive( env.element, 'line-numbers' );

@@ -23,14 +23,14 @@ Prism.hooks.add( 'complete', function( env ) {

showLineRanges.replace( /\s+/g, '' ).split( ',' ).filter( Boolean ).forEach( function( currentRange ) {
if (currentRange.includes( '-' )) {
var range = currentRange.split( '-' );
var range = currentRange.split( '-' ).map( Number );

if ( range[0] > range[1] ) {
[range[0], range[1]] = [range[1], range[0]];
}

for ( var counter = range[0]; counter <= range[1]; counter++ ) {
showLines.push( Number( counter ) );
showLines.push( counter );
}
}
else {
@@ -41,14 +41,25 @@ Prism.hooks.add( 'complete', function( env ) {
return showLines;
}

function tokenList( HTML, showLines ) {
var tokenList = HTML.split( /\n(?!$)/g );
function tokenList( element, showLines ) {
var tokenList = element.innerHTML.split( /\n(?!$)/g );
var tokenCount = tokenList.length;
const regex = /(<span aria-hidden="true" class="line-numbers-rows">(.*<\/span>))/gm;
var extraLanguageSpan = false;
const regexExtraLanguageSpan = /<span class="token [^"]*? language-[^"]*?">/gm;
const regexLineNumberRows = /(<span aria-hidden="true" class="line-numbers-rows">(.*<\/span>))/gm;

if ( regexExtraLanguageSpan.test( tokenList[0] ) ) {
tokenList[0] = tokenList[0].replace ( regexExtraLanguageSpan, '' );
extraLanguageSpan = true;
}

tokenList.forEach( function( value, index ) {
if ( index === tokenCount - 1 ) {
value = value.replace (regex, '' );
value = value.replace ( regexLineNumberRows, '' );

if ( extraLanguageSpan ) {
value = value.replace( '</span>', '' );
}
}

if ( showLines.includes ( index + 1 ) ) {

0 comments on commit 56ef339

Please sign in to comment.