Index page range script issue InDesign
I have a script for generating page ranges for an index document in InDesign. While the script executes successfully and creates the page ranges, there is an issue with hyperlinks. For example, if the input is 10, 29, 30, 31, 32, 33, 34, the script converts it to.10, 29-34 However, after the hyphen, the first digit (3 in this case) does not retain its hyperlink, while the last digit (4) does. I have verified this issue both in InDesign and in the ePub output.
Below is my script. If anyone can assist in resolving this, it would be greatly appreciated.
if (app.selection.length > 0 && app.selection[0].hasOwnProperty ('baseline'))
{
var s = app.selection[0].parentStory;
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = '\\d+, ([\\d+, ])+';
var r = s.findGrep();
for ( aLine = r.length-1; aLine >= 0; aLine-- )
r[aLine].contents= checkOneLine ( r[aLine].contents );
}
else
alert('Achtung!\nBitte einen Textabschnitt auswählen.');
function checkOneLine ( aString )
{
var a = aString.split( ', ');
for ( var i = 0; i < aString.length-1; i++ )
{
var k = i +1;
while ( k < aString.length && a[k] == Number( a[k-1] ) + 1)
{
k++;
}
if ( Number(a[k-1]) > Number( a[i] ) )
{
a[i] = a[i] + '–' + a[k-1];
a.splice( i+1, k-1-i );
}
}
return a.join( ', ');
}

