1- import { createContext , useContext , useEffect , useState } from "react" ;
1+ import { createContext , memo , useContext , useEffect , useMemo , useState } from "react" ;
22import { Image , Platform , ScrollView , Text , useColorScheme , View } from "react-native" ;
33import type { MarkdownNode } from "react-native-nitro-markdown/headless" ;
44
55import { CopyTextButton } from "./CopyTextButton" ;
66import { MarkdownTextPrimitive } from "./MarkdownTextPrimitive" ;
77import { nativeMarkdownDocumentRuns , nativeMarkdownListItemBlocks } from "./nativeMarkdownText" ;
8+ import { pendingCodeHighlight } from "./pendingCodeHighlight" ;
89import { NativeMarkdownSelectableText } from "./NativeMarkdownSelectableText" ;
910import type {
1011 MarkdownCodeHighlighter ,
@@ -29,7 +30,7 @@ const MONO_FONT_FAMILY = Platform.select({
2930} ) ;
3031
3132function nodeKey ( node : MarkdownNode , index : number ) : string {
32- return `${ node . type } :${ node . beg ?? index } : ${ node . end ?? index } ` ;
33+ return `${ node . type } :${ node . beg ?? index } ` ;
3334}
3435
3536/** Code inside markdown scales with the base text size (12pt at the default 15pt body). */
@@ -93,6 +94,7 @@ function loadHighlightedCode(
9394 language : string | undefined ,
9495 theme : "light" | "dark" ,
9596 highlightCode : MarkdownCodeHighlighter ,
97+ session : object ,
9698) : Promise < HighlightedCode > {
9799 const key = codeHighlightCacheKey ( code , language , theme ) ;
98100 const cached = highlightedCodeCache . get ( key ) ;
@@ -105,7 +107,7 @@ function loadHighlightedCode(
105107 return pending ;
106108 }
107109
108- const promise = highlightCode ( { code, language, theme } )
110+ const promise = highlightCode ( { code, language, theme, session } )
109111 . then ( ( tokens ) => {
110112 cacheHighlightedCode ( key , tokens ) ;
111113 highlightedCodePromiseCache . delete ( key ) ;
@@ -125,116 +127,130 @@ function useHighlightedCode(
125127 theme : "light" | "dark" ,
126128 highlightCode : MarkdownCodeHighlighter ,
127129) : HighlightedCode | null {
130+ const [ session ] = useState ( ( ) => ( { } ) ) ;
128131 const key = codeHighlightCacheKey ( code , language , theme ) ;
132+ const ready = useMemo (
133+ ( ) => highlightedCodeCache . get ( key ) ?? highlightCode . read ?.( { code, language, theme, session } ) ,
134+ [ code , language , theme , key , highlightCode , session ] ,
135+ ) ;
129136 const [ highlighted , setHighlighted ] = useState < {
130137 readonly key : string ;
138+ readonly code : string ;
139+ readonly language : string | undefined ;
140+ readonly theme : "light" | "dark" ;
131141 readonly tokens : HighlightedCode | null ;
132142 } > ( ( ) => ( {
143+ code,
144+ language,
145+ theme,
133146 key,
134147 tokens : highlightedCodeCache . get ( key ) ?? null ,
135148 } ) ) ;
136149
137150 useEffect ( ( ) => {
151+ if ( ready ) return ;
138152 let active = true ;
139153 const cached = highlightedCodeCache . get ( key ) ;
140154 if ( cached ) {
141155 cacheHighlightedCode ( key , cached ) ;
142- setHighlighted ( { key, tokens : cached } ) ;
156+ setHighlighted ( { code , language , theme , key, tokens : cached } ) ;
143157 return ( ) => {
144158 active = false ;
145159 } ;
146160 }
147161
148- void loadHighlightedCode ( code , language , theme , highlightCode )
162+ void loadHighlightedCode ( code , language , theme , highlightCode , session )
149163 . then ( ( tokens ) => {
150164 if ( active ) {
151- setHighlighted ( { key, tokens } ) ;
165+ setHighlighted ( { code , language , theme , key, tokens } ) ;
152166 }
153167 } )
154168 . catch ( ( ) => {
155169 if ( active ) {
156- setHighlighted ( { key, tokens : null } ) ;
170+ setHighlighted ( { code , language , theme , key, tokens : null } ) ;
157171 }
158172 } ) ;
159173 return ( ) => {
160174 active = false ;
161175 } ;
162- } , [ code , highlightCode , key , language , theme ] ) ;
176+ } , [ code , highlightCode , key , language , theme , ready , session ] ) ;
163177
164- return highlighted . key === key ? highlighted . tokens : null ;
178+ if ( ready ) return ready ;
179+ if ( highlighted . key === key ) return highlighted . tokens ;
180+ if ( highlighted . tokens && highlighted . language === language && highlighted . theme === theme ) {
181+ return pendingCodeHighlight ( highlighted . code , code , highlighted . tokens ) ;
182+ }
183+ return null ;
165184}
166185
167- function HighlightedCodeText ( props : {
168- readonly content : string ;
169- readonly highlighted : HighlightedCode | null ;
170- readonly textStyle : NativeMarkdownTextStyle ;
186+ const HighlightedCodeLine = memo ( function HighlightedCodeLine ( props : {
187+ readonly tokens : ReadonlyArray < MarkdownHighlightedToken > ;
188+ readonly color : string ;
189+ readonly newline : boolean ;
171190} ) {
172- if ( ! props . highlighted ) {
173- return (
191+ let offset = 0 ;
192+ const children = [ ] ;
193+ for ( const token of props . tokens ) {
194+ if ( ! token . content ) continue ;
195+ children . push (
174196 < MarkdownTextPrimitive
175- uiTextView
176- selectable
197+ key = { offset }
177198 style = { {
178- color : props . textStyle . codeColor ,
199+ color : token . color ?? props . color ,
179200 fontFamily : MONO_FONT_FAMILY ,
180- fontSize : codeBlockFontSize ( props . textStyle ) ,
181- lineHeight : codeBlockLineHeight ( props . textStyle ) ,
201+ fontStyle : token . fontStyle !== null && ( token . fontStyle & 1 ) === 1 ? "italic" : "normal" ,
202+ fontWeight : token . fontStyle !== null && ( token . fontStyle & 2 ) === 2 ? "700" : "400" ,
182203 } }
183204 >
184- { props . content }
185- </ MarkdownTextPrimitive >
205+ { token . content }
206+ </ MarkdownTextPrimitive > ,
186207 ) ;
208+ offset += token . content . length ;
187209 }
188- const highlighted = props . highlighted ;
189- let sourceOffset = 0 ;
190- const keyOccurrences = new Map < string , number > ( ) ;
191- const keyedLines = highlighted . map ( ( line ) => {
192- const lineStart = sourceOffset ;
193- const tokens = line . map ( ( token ) => {
194- const start = sourceOffset ;
195- sourceOffset += token . content . length ;
196- const signature = `${ start } :${ token . content } :${ token . color ?? "" } :${ token . fontStyle ?? "" } ` ;
197- const occurrence = keyOccurrences . get ( signature ) ?? 0 ;
198- keyOccurrences . set ( signature , occurrence + 1 ) ;
199- return { key : `${ signature } :${ occurrence } ` , token } ;
200- } ) ;
201- sourceOffset += 1 ;
202- return {
203- key : `line:${ lineStart } :${ line . map ( ( token ) => token . content ) . join ( "" ) } ` ,
204- tokens,
205- } ;
206- } ) ;
210+ return (
211+ < MarkdownTextPrimitive >
212+ { children }
213+ { props . newline ? "\n" : "" }
214+ </ MarkdownTextPrimitive >
215+ ) ;
216+ } ) ;
207217
218+ function HighlightedCodeText ( props : {
219+ readonly content : string ;
220+ readonly highlighted : HighlightedCode | null ;
221+ readonly textStyle : NativeMarkdownTextStyle ;
222+ } ) {
223+ // The text root provides inherited styles through context. A new style object
224+ // would rerender every token even when its completed line is unchanged.
225+ const fontSize = codeBlockFontSize ( props . textStyle ) ;
226+ const lineHeight = codeBlockLineHeight ( props . textStyle ) ;
227+ const style = useMemo (
228+ ( ) => ( {
229+ color : props . textStyle . codeColor ,
230+ fontFamily : MONO_FONT_FAMILY ,
231+ fontSize,
232+ lineHeight,
233+ } ) ,
234+ [ props . textStyle . codeColor , fontSize , lineHeight ] ,
235+ ) ;
236+ let offset = 0 ;
237+ const lines = [ ] ;
238+ if ( props . highlighted ) {
239+ for ( const tokens of props . highlighted ) {
240+ lines . push (
241+ < HighlightedCodeLine
242+ key = { offset }
243+ tokens = { tokens }
244+ color = { props . textStyle . codeColor }
245+ newline = { lines . length + 1 < props . highlighted . length }
246+ /> ,
247+ ) ;
248+ offset += tokens . reduce ( ( length , token ) => length + token . content . length , 0 ) + 1 ;
249+ }
250+ }
208251 return (
209- < MarkdownTextPrimitive
210- uiTextView
211- selectable
212- style = { {
213- color : props . textStyle . codeColor ,
214- fontFamily : MONO_FONT_FAMILY ,
215- fontSize : codeBlockFontSize ( props . textStyle ) ,
216- lineHeight : codeBlockLineHeight ( props . textStyle ) ,
217- } }
218- >
219- { keyedLines . map ( ( line , lineIndex ) => (
220- < MarkdownTextPrimitive key = { line . key } >
221- { line . tokens . map ( ( { key, token } ) => (
222- < MarkdownTextPrimitive
223- key = { key }
224- style = { {
225- color : token . color ?? props . textStyle . codeColor ,
226- fontFamily : MONO_FONT_FAMILY ,
227- fontStyle :
228- token . fontStyle !== null && ( token . fontStyle & 1 ) === 1 ? "italic" : "normal" ,
229- fontWeight : token . fontStyle !== null && ( token . fontStyle & 2 ) === 2 ? "700" : "400" ,
230- } }
231- >
232- { token . content }
233- </ MarkdownTextPrimitive >
234- ) ) }
235- { lineIndex + 1 < keyedLines . length ? "\n" : "" }
236- </ MarkdownTextPrimitive >
237- ) ) }
252+ < MarkdownTextPrimitive uiTextView selectable style = { style } >
253+ { props . highlighted ? lines : props . content }
238254 </ MarkdownTextPrimitive >
239255 ) ;
240256}
0 commit comments