@@ -5,6 +5,7 @@ import { Highlight, Prism } from "prism-react-renderer";
55import { forwardRef , ReactNode , useCallback , useEffect , useState } from "react" ;
66import { TextWrapIcon } from "~/assets/icons/TextWrapIcon" ;
77import { cn } from "~/utils/cn" ;
8+ import { highlightSearchText } from "~/utils/logUtils" ;
89import { Button } from "../primitives/Buttons" ;
910import { Dialog , DialogContent , DialogHeader , DialogTitle } from "../primitives/Dialog" ;
1011import { Paragraph } from "../primitives/Paragraph" ;
@@ -72,12 +73,6 @@ type CodeBlockProps = {
7273const dimAmount = 0.5 ;
7374const extraLinesWhenClipping = 0.35 ;
7475
75- const SEARCH_HIGHLIGHT_STYLES = {
76- backgroundColor : "#facc15" ,
77- color : "#000000" ,
78- fontWeight : "500" ,
79- } as const ;
80-
8176const defaultTheme : PrismTheme = {
8277 plain : {
8378 color : "#9C9AF2" ,
@@ -371,7 +366,7 @@ export const CodeBlock = forwardRef<HTMLDivElement, CodeBlockProps>(
371366 ) }
372367 dir = "ltr"
373368 >
374- { highlightSearchInText ( code , searchTerm ) }
369+ { highlightSearchText ( code , searchTerm ) }
375370 </ pre >
376371 </ div >
377372 ) }
@@ -413,7 +408,7 @@ export const CodeBlock = forwardRef<HTMLDivElement, CodeBlockProps>(
413408 className = "overflow-auto px-3 py-3 scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600"
414409 >
415410 < pre className = "relative mr-2 p-2 font-mono text-base leading-relaxed" dir = "ltr" >
416- { highlightSearchInText ( code , searchTerm ) }
411+ { highlightSearchText ( code , searchTerm ) }
417412 </ pre >
418413 </ div >
419414 ) }
@@ -426,42 +421,6 @@ export const CodeBlock = forwardRef<HTMLDivElement, CodeBlockProps>(
426421
427422CodeBlock . displayName = "CodeBlock" ;
428423
429- /**
430- * Highlights search term matches in plain text
431- */
432- function highlightSearchInText ( text : string , searchTerm : string | undefined ) : React . ReactNode {
433- if ( ! searchTerm || searchTerm . trim ( ) === "" ) {
434- return text ;
435- }
436-
437- const escapedSearch = searchTerm . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
438- const regex = new RegExp ( escapedSearch , "gi" ) ;
439-
440- const parts : React . ReactNode [ ] = [ ] ;
441- let lastIndex = 0 ;
442- let match ;
443- let matchCount = 0 ;
444-
445- while ( ( match = regex . exec ( text ) ) !== null ) {
446- if ( match . index > lastIndex ) {
447- parts . push ( text . substring ( lastIndex , match . index ) ) ;
448- }
449- parts . push (
450- < span key = { `match-${ matchCount } ` } style = { SEARCH_HIGHLIGHT_STYLES } >
451- { match [ 0 ] }
452- </ span >
453- ) ;
454- lastIndex = regex . lastIndex ;
455- matchCount ++ ;
456- }
457-
458- if ( lastIndex < text . length ) {
459- parts . push ( text . substring ( lastIndex ) ) ;
460- }
461-
462- return parts . length > 0 ? parts : text ;
463- }
464-
465424function Chrome ( { title } : { title ?: string } ) {
466425 return (
467426 < div className = "grid h-7 grid-cols-[100px_auto_100px] border-b border-charcoal-800 bg-charcoal-900" >
@@ -607,40 +566,7 @@ function HighlightCode({
607566 const tokenProps = getTokenProps ( { token, key } ) ;
608567
609568 // Highlight search term matches in token
610- let content : React . ReactNode = token . content ;
611- if ( searchTerm && searchTerm . trim ( ) !== "" && token . content ) {
612- const escapedSearch = searchTerm . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
613- const regex = new RegExp ( escapedSearch , "gi" ) ;
614-
615- const parts : React . ReactNode [ ] = [ ] ;
616- let lastIndex = 0 ;
617- let match ;
618- let matchCount = 0 ;
619-
620- while ( ( match = regex . exec ( token . content ) ) !== null ) {
621- if ( match . index > lastIndex ) {
622- parts . push ( token . content . substring ( lastIndex , match . index ) ) ;
623- }
624- parts . push (
625- < span
626- key = { `match-${ matchCount } ` }
627- style = { SEARCH_HIGHLIGHT_STYLES }
628- >
629- { match [ 0 ] }
630- </ span >
631- ) ;
632- lastIndex = regex . lastIndex ;
633- matchCount ++ ;
634- }
635-
636- if ( lastIndex < token . content . length ) {
637- parts . push ( token . content . substring ( lastIndex ) ) ;
638- }
639-
640- if ( parts . length > 0 ) {
641- content = parts ;
642- }
643- }
569+ const content = highlightSearchText ( token . content , searchTerm ) ;
644570
645571 return (
646572 < span
0 commit comments