11import { getFiletypeFromFileName } from "@pierre/diffs" ;
22import type { ProjectContentMatch } from "@t3tools/contracts" ;
3- import { memo , Suspense , use , useMemo , type CSSProperties } from "react" ;
3+ import { memo , Suspense , use , type CSSProperties } from "react" ;
44
55import { resolveDiffThemeName } from "~/lib/diffRendering" ;
66import { getSyntaxHighlighterPromise } from "~/lib/syntaxHighlighting" ;
@@ -125,23 +125,28 @@ function HighlightedTokens(props: {
125125 ) ;
126126}
127127
128+ function firstLineTokens (
129+ highlighter : Awaited < ReturnType < typeof getSyntaxHighlighterPromise > > ,
130+ line : string ,
131+ language : string ,
132+ theme : "light" | "dark" ,
133+ ) {
134+ try {
135+ return highlighter . codeToTokens ( line , { lang : language , theme : resolveDiffThemeName ( theme ) } )
136+ . tokens [ 0 ] ;
137+ } catch {
138+ return undefined ;
139+ }
140+ }
141+
128142function SyntaxHighlightedTokens ( props : {
129143 readonly line : string ;
130144 readonly language : string ;
131145 readonly ranges : ReadonlyArray < Range > ;
132146 readonly theme : "light" | "dark" ;
133147} ) {
134148 const highlighter = use ( getSyntaxHighlighterPromise ( props . language ) ) ;
135- const tokens = useMemo ( ( ) => {
136- try {
137- return highlighter . codeToTokens ( props . line , {
138- lang : props . language ,
139- theme : resolveDiffThemeName ( props . theme ) ,
140- } ) . tokens [ 0 ] ;
141- } catch {
142- return undefined ;
143- }
144- } , [ highlighter , props . language , props . line , props . theme ] ) ;
149+ const tokens = firstLineTokens ( highlighter , props . line , props . language , props . theme ) ;
145150
146151 return tokens ? (
147152 < HighlightedTokens line = { props . line } ranges = { props . ranges } tokens = { tokens } />
@@ -159,7 +164,7 @@ export const HighlightedSearchLine = memo(function HighlightedSearchLine(props:
159164 readonly path : string ;
160165 readonly theme : "light" | "dark" ;
161166} ) {
162- const ranges = useMemo ( ( ) => normalizeRanges ( props . match ) , [ props . match ] ) ;
167+ const ranges = normalizeRanges ( props . match ) ;
163168 const fallback = (
164169 < HighlightedTokens
165170 line = { props . match . lineContent }
0 commit comments