@@ -4,6 +4,17 @@ import 'ldrs/react/Quantum.css';
44import InfiniteScroll from 'react-infinite-scroll-component' ;
55import { useNavigate , Link } from 'react-router-dom' ;
66
7+ // Add this helper function at the top of your component
8+ const highlightText = ( text , query ) => {
9+ if ( ! query || ! text ) return text ;
10+
11+ // Escape special regex characters in the query
12+ const escapedQuery = query . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
13+ // const escapedQuery = query; //for testing purposes
14+ const regex = new RegExp ( `(${ escapedQuery } )` , 'gi' ) ;
15+ return text . replace ( regex , '<u>$1</u>' ) ;
16+ } ;
17+
718function ListView ( props ) {
819 const [ displayedCourses , setDisplayedCourses ] = useState ( [ ] ) ;
920 const [ hasMore , setHasMore ] = useState ( true ) ;
@@ -169,17 +180,27 @@ function ListView(props) {
169180 >
170181 < div >
171182 < div className = "codeNameContainer" style = { { display : 'flex' } } >
172- < p className = "font-bold text-[#000061]" > { course . code } </ p >
173- < p className = "font-bold text-[#000061]" style = { { color : "GrayText" , opacity : 0.5 , marginLeft : "0.5em" } } > { handlePeriods ( course ?. periods ) } </ p >
183+ < p className = "font-bold text-[#000061]"
184+ dangerouslySetInnerHTML = { {
185+ __html : highlightText ( course . code , props . query )
186+ } }
187+ />
188+ < p className = "font-bold text-[#000061]"
189+ style = { { color : "GrayText" , opacity : 0.5 , marginLeft : "0.5em" } } >
190+ { handlePeriods ( course ?. periods ) }
191+ </ p >
174192 </ div >
175- < p className = "font-bold" > { course . name } </ p >
193+ < p className = "font-bold"
194+ dangerouslySetInnerHTML = { {
195+ __html : highlightText ( course . name , props . query )
196+ } }
197+ />
176198 < p
177199 className = "text-gray-600"
178200 dangerouslySetInnerHTML = { {
179201 __html : readMore [ course . code ]
180-
181- ? course ?. description
182- : ( course ?. description ?. slice ( 0 , 200 ) + "..." ) ,
202+ ? highlightText ( course ?. description , props . query )
203+ : highlightText ( course ?. description ?. slice ( 0 , 200 ) + "..." , props . query )
183204 } }
184205 />
185206 { course ?. description ?. length > 150 && (
0 commit comments