import { GQLWordlistMatch } from "coral-framework/schema" ; interface Split { start : number ; end : number ; replace ? : boolean ; } export default function markPhrasesHTML ( text , words ) { const splits = [ ] ; if ( words . length === 0 ) { return text ; } if ( words . length === 1 ) { const word = words [ 0 ] ; splits . push ( { start : 0 , end : word . index , } ) ; splits . push ( { start : word . index , end : word . index + word . length , replace : true , } ) ; splits . push ( { start : word . index + word . length , end : text . length , } ) ; } else { const firstWord = words [ 0 ] ; splits . push ( { start : 0 , end : firstWord . index , } ) ; splits . push ( { start : firstWord . index , end : firstWord . index + firstWord . length , replace : true , } ) ; for ( let w = 1 ; w < words . length ; w ++ ) { const prevWord = words [ w - 1 ] ; const word = words [ w ] ; splits . push ( { start : prevWord . index + prevWord . length , end : word . index , } ) ; splits . push ( { start : word . index , end : word . index + word . length , replace : true , } ) ; } const lastWord = words [ words . length - 1 ] ; splits . push ( { start : lastWord . index + lastWord . length , end : text . length , } ) ; } const parts = [ ] ; for ( const split of splits ) { const part = text . substring ( split . start , split . end ) ; if ( split . replace ) { parts . push ( `<mark>${ part }</mark>` ) ; } else { parts . push ( part ) ; } } return parts . join ( "" ) ; }