@@ -192,44 +192,56 @@ export function workEntryIconName(workEntry: TimelineWorkEntry): WorkEntryIconNa
192192 return workToneIcon ( workEntry . tone ) . iconName ;
193193}
194194
195+ // Look past the visible label so long destinations and inline delimiters can close.
196+ // This is bounded lookahead, not document-wide Markdown parity: a closing delimiter
197+ // or reference definition beyond this prefix remains unresolved by the parser.
198+ export const THOUGHT_PREVIEW_SOURCE_MAX_LENGTH = 8_192 ;
199+ export const THOUGHT_PREVIEW_MAX_LENGTH = 2_048 ;
200+
195201/** Compact reasoning keeps formulas while flattening other Markdown into label text. */
196202export function remarkThoughtPreview ( fallback : string ) {
197203 return ( tree : Root ) => {
198204 const children : RootContent [ ] = [ ] ;
199- let run : string [ ] = [ ] ;
200- const append = ( value : string ) => {
201- run . push ( value ) ;
202- } ;
205+ let remaining = THOUGHT_PREVIEW_MAX_LENGTH ;
206+ let fragments : string [ ] = [ ] ;
203207 const flush = ( ) => {
204- if ( run . length === 0 ) return ;
205- children . push ( { type : "text" , value : run . join ( "" ) . replace ( / \s + / g, " " ) } ) ;
206- run = [ ] ;
208+ if ( fragments . length === 0 ) return ;
209+ children . push ( { type : "text" , value : fragments . join ( "" ) . replace ( / \s + / g, " " ) } ) ;
210+ fragments = [ ] ;
211+ } ;
212+ const append = ( value : string ) => {
213+ const text = value . slice ( 0 , remaining ) ;
214+ remaining -= text . length ;
215+ fragments . push ( text ) ;
207216 } ;
208217 const visit = ( node : Root | RootContent ) => {
209- if ( node . type === "html" || node . type === "definition" ) return ;
218+ if ( remaining === 0 || node . type === "html" || node . type === "definition" ) return ;
210219 if ( node . type === "math" || node . type === "inlineMath" ) {
211220 flush ( ) ;
221+ const value = node . value . slice ( 0 , remaining ) ;
222+ remaining -= value . length ;
212223 children . push ( {
213224 type : "inlineMath" ,
214- value : node . value ,
225+ value,
215226 position : node . position ,
216227 data : {
217228 hName : "code" ,
218229 hProperties : { className : [ "language-math" , "math-inline" ] } ,
219- hChildren : [ { type : "text" , value : node . value } ] ,
230+ hChildren : [ { type : "text" , value } ] ,
220231 } ,
221232 } ) ;
222233 } else if ( "alt" in node ) append ( node . alt ?? "" ) ;
223234 else if ( "value" in node ) append ( node . value ) ;
224235 else if ( "children" in node ) {
225- node . children . forEach ( ( child , index ) => {
236+ for ( const [ index , child ] of node . children . entries ( ) ) {
237+ if ( remaining === 0 ) break ;
226238 if (
227239 index &&
228240 [ "root" , "blockquote" , "list" , "listItem" , "table" , "tableRow" ] . includes ( node . type )
229241 )
230242 append ( " " ) ;
231243 visit ( child ) ;
232- } ) ;
244+ }
233245 } else if ( node . type === "break" ) append ( " " ) ;
234246 } ;
235247 visit ( tree ) ;
0 commit comments