@@ -4,6 +4,7 @@ import { visit } from 'unist-util-visit';
44
55import { getNow } from './internal' ;
66import { createStreamTailNode , type StreamTailData } from './StreamTail' ;
7+ import { STREAM_INLINE_CLASS } from './styles' ;
78
89export interface StreamBirthPacing {
910 capMs : number ;
@@ -16,6 +17,12 @@ export interface StreamAnimatedRuntime {
1617 * `pacing` (or supplied up front by legacy callers).
1718 */
1819 births : number [ ] ;
20+ /**
21+ * Inline code fades as one box starting at its first char's birth. The
22+ * delay is frozen at first render, keyed by that char's index, because a
23+ * changed animation-delay on a running animation jumps it.
24+ */
25+ inlineFades ?: Map < number , { birth : number ; delay : number } > ;
1926 pacing ?: StreamBirthPacing ;
2027 /**
2128 * Per char, indexed like `births`: `undefined` = not rendered yet,
@@ -73,6 +80,12 @@ const frameOf = (birth: number) => Math.floor(birth / STREAM_FRAME_MS);
7380const BLOCK_TAGS = new Set ( [ 'p' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' , 'li' , 'td' , 'th' ] ) ;
7481const SKIP_TAGS = new Set ( [ 'pre' , 'code' , 'svg' ] ) ;
7582
83+ const classListOf = ( node : Element ) : string [ ] => {
84+ const className = node . properties . className ;
85+ if ( Array . isArray ( className ) ) return className . map ( String ) ;
86+ return className ? [ String ( className ) ] : [ ] ;
87+ } ;
88+
7689function hasClass ( node : Element , cls : string ) : boolean {
7790 const cn : unknown = node . properties ?. className ;
7891 if ( Array . isArray ( cn ) ) return cn . some ( ( c ) => String ( c ) . includes ( cls ) ) ;
@@ -105,11 +118,7 @@ export const rehypeStreamAnimated = (options: StreamAnimatedOptions = {}) => {
105118 if ( block ) {
106119 for ( const node of tree . children ) {
107120 if ( node . type !== 'element' ) continue ;
108- const className = node . properties . className ;
109- node . properties . className = [
110- ...( Array . isArray ( className ) ? className : className ? [ String ( className ) ] : [ ] ) ,
111- 'stream-block' ,
112- ] ;
121+ node . properties . className = [ ...classListOf ( node ) , 'stream-block' ] ;
113122 }
114123 return ;
115124 }
@@ -200,13 +209,42 @@ export const rehypeStreamAnimated = (options: StreamAnimatedOptions = {}) => {
200209 return createStreamTailNode ( { items, text } ) ;
201210 } ;
202211
212+ const inlineFadeDelay = ( index : number ) : number | null => {
213+ const birth = resolvedRuntime ?. births [ index ] ?? now ;
214+ if ( isSkipped ( index ) || now - birth >= fadeDuration ) return null ;
215+ if ( ! resolvedRuntime ) return birth - now ;
216+ const fades = ( resolvedRuntime . inlineFades ??= new Map ( ) ) ;
217+ const frozen = fades . get ( index ) ;
218+ if ( frozen ?. birth === birth ) return frozen . delay ;
219+ const delay = birth - now ;
220+ fades . set ( index , { birth, delay } ) ;
221+ return delay ;
222+ } ;
223+
224+ const wrapInlineCode = ( node : Element ) => {
225+ const startIndex = globalCharIndex ;
226+ wrapText ( node ) ;
227+ if ( globalCharIndex === startIndex ) return ;
228+ const delay = inlineFadeDelay ( startIndex ) ;
229+ if ( delay === null ) {
230+ resolvedRuntime ?. inlineFades ?. delete ( startIndex ) ;
231+ return ;
232+ }
233+ node . properties . className = [ ...classListOf ( node ) , STREAM_INLINE_CLASS ] ;
234+ const style = node . properties . style ;
235+ const fadeStyle = `animation-delay:${ delay } ms` ;
236+ node . properties . style = style ? `${ String ( style ) } ;${ fadeStyle } ` : fadeStyle ;
237+ } ;
238+
203239 const wrapText = ( node : Element ) => {
204240 const newChildren : ElementContent [ ] = [ ] ;
205241 for ( const child of node . children ) {
206242 if ( child . type === 'text' ) {
207243 newChildren . push ( wrapTextValue ( child . value ) ) ;
208244 } else if ( child . type === 'element' ) {
209- if ( ! shouldSkip ( child ) ) {
245+ if ( child . tagName === 'code' ) {
246+ wrapInlineCode ( child ) ;
247+ } else if ( ! shouldSkip ( child ) ) {
210248 wrapText ( child ) ;
211249 }
212250 newChildren . push ( child ) ;
0 commit comments