import React from "react" ; import { tokenize } from "./course-search" ; import Lang from "@bit/wasedatime.core.ts.constants.langs" ; export const highlight = function ( searchTerm , searchLang , text ) : string | any [ ] { if ( searchTerm . length > 0 ) { const termUnion = tokenize ( searchTerm ) . join ( "|" ) ; let regex ; switch ( searchLang ) { case Lang . JA : regex = new RegExp ( `(${ termUnion })` , "i" ) ; break ; case Lang . EN : regex = new RegExp ( `\\b(${ termUnion })` , "i" ) ; break ; default : alert ( "Unsupported language: " + searchLang ) ; } const textParts = text . split ( regex ) ; return textParts . map ( ( part , i ) => { if ( i % 2 === 0 ) { return part ; } return < mark key = { i } > { part } < /mark>; } ) ; } else { return text ; } } ; export default highlight ;