@@ -16,18 +16,36 @@ const TEMPLATE_DIR = path.resolve(__dirname, "../templates");
1616
1717/** Simple terminal markdown renderer using chalk */
1818function renderMarkdown ( text : string ) : string {
19- return text
19+ // 1. Extract fenced code blocks first (before inline code regex eats backticks)
20+ const codeBlocks : string [ ] = [ ] ;
21+ let processed = text . replace ( / ` ` ` ( \w * ) \n ( [ \s \S ] * ?) ` ` ` / g, ( _m , lang , code ) => {
22+ const label = lang ? chalk . dim ( ` [${ lang } ]` ) : "" ;
23+ const formatted = chalk . dim ( "─" . repeat ( 40 ) ) + label + "\n" +
24+ chalk . yellow ( code . trimEnd ( ) ) + "\n" +
25+ chalk . dim ( "─" . repeat ( 40 ) ) ;
26+ codeBlocks . push ( formatted ) ;
27+ return `__CODE_BLOCK_${ codeBlocks . length - 1 } __` ;
28+ } ) ;
29+
30+ // 2. Apply inline formatting
31+ processed = processed
2032 . replace ( / ^ # # # ( .+ ) $ / gm, ( _m , s ) => chalk . green . bold ( ` ${ s } ` ) )
2133 . replace ( / ^ # # ( .+ ) $ / gm, ( _m , s ) => chalk . green . bold ( ` ${ s } ` ) )
2234 . replace ( / ^ # ( .+ ) $ / gm, ( _m , s ) => chalk . magenta . bold . underline ( s ) )
2335 . replace ( / \* \* ( .+ ?) \* \* / g, ( _m , s ) => chalk . bold ( s ) )
24- . replace ( / \* ( . + ? ) \* / g, ( _m , s ) => chalk . italic ( s ) )
36+ . replace ( / (?< ! \* ) \* ( [ ^ * ] + ) \* (? ! \* ) / g, ( _m , s ) => chalk . italic ( s ) )
2537 . replace ( / ` ( [ ^ ` ] + ) ` / g, ( _m , s ) => chalk . yellow ( s ) )
2638 . replace ( / ^ - / gm, " • " )
2739 . replace ( / ^ ( \d + ) \. / gm, ( _m , n ) => ` ${ n } . ` )
2840 . replace ( / ^ > ( .+ ) $ / gm, ( _m , s ) => chalk . gray . italic ( ` │ ${ s } ` ) )
29- . replace ( / ^ - - - $ / gm, chalk . dim ( "─" . repeat ( 40 ) ) )
30- . replace ( / ^ ` ` ` \w * $ / gm, chalk . dim ( "─" . repeat ( 40 ) ) ) ;
41+ . replace ( / ^ - - - $ / gm, chalk . dim ( "─" . repeat ( 40 ) ) ) ;
42+
43+ // 3. Re-insert code blocks
44+ for ( let i = 0 ; i < codeBlocks . length ; i ++ ) {
45+ processed = processed . replace ( `__CODE_BLOCK_${ i } __` , codeBlocks [ i ] ) ;
46+ }
47+
48+ return processed ;
3149}
3250
3351async function main ( ) {
@@ -198,13 +216,14 @@ async function main() {
198216 spinner . stop ( ) ;
199217 console . log ( chalk . dim ( "\nGoodbye! 🦐\n" ) ) ;
200218 agent . stop ( ) ;
219+ input . stop ( ) ;
220+ clearInterval ( keepAlive ) ;
201221 process . exit ( 0 ) ;
202222 } ) ;
203223
204224 // Keep the process alive — this interval prevents early exit
205225 // when stdin might momentarily have no active listeners
206226 const keepAlive = setInterval ( ( ) => { } , 1 << 30 ) ;
207- process . on ( "exit" , ( ) => clearInterval ( keepAlive ) ) ;
208227
209228 input . start ( ) ;
210229}
0 commit comments