File tree Expand file tree Collapse file tree
ui/components/MessageView Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,6 +42,21 @@ test("renderMarkdown styles inline code without removing it", () => {
4242 assert . equal ( result . includes ( "npm install" ) , true ) ;
4343} ) ;
4444
45+ test ( "renderMarkdown preserves underscores inside inline code" , ( ) => {
46+ const source =
47+ "Use `redo_completed_tasks2_1min`, replace `execute_query` with `select_one`/`select_all`, and check `ocr_result`." ;
48+ const result = stripAnsi ( renderMarkdown ( source ) ) ;
49+ assert . equal (
50+ result ,
51+ "Use redo_completed_tasks2_1min, replace execute_query with select_one/select_all, and check ocr_result."
52+ ) ;
53+ } ) ;
54+
55+ test ( "renderMarkdown preserves underscores in plain identifiers" , ( ) => {
56+ const result = stripAnsi ( renderMarkdown ( "Check redo_completed_tasks2_1min and ocr_result values." ) ) ;
57+ assert . equal ( result , "Check redo_completed_tasks2_1min and ocr_result values." ) ;
58+ } ) ;
59+
4560test ( "renderMarkdown keeps bullet markers" , ( ) => {
4661 const result = stripAnsi ( renderMarkdown ( "- item one\n- item two" ) ) ;
4762 assert . equal ( result . includes ( "- item one" ) , true ) ;
Original file line number Diff line number Diff line change @@ -396,10 +396,31 @@ function renderInlineLine(line: string): string {
396396
397397function renderInlineSpans ( text : string ) : string {
398398 if ( ! text ) return text ;
399+
400+ const parts : string [ ] = [ ] ;
401+ const codeRe = / ` ( [ ^ ` ] + ) ` / g;
402+ let lastIndex = 0 ;
403+ let match : RegExpExecArray | null ;
404+
405+ while ( ( match = codeRe . exec ( text ) ) !== null ) {
406+ if ( match . index > lastIndex ) {
407+ parts . push ( renderEmphasisSpans ( text . slice ( lastIndex , match . index ) ) ) ;
408+ }
409+ parts . push ( chalk . cyan ( match [ 1 ] ?? "" ) ) ;
410+ lastIndex = match . index + match [ 0 ] . length ;
411+ }
412+
413+ if ( lastIndex < text . length ) {
414+ parts . push ( renderEmphasisSpans ( text . slice ( lastIndex ) ) ) ;
415+ }
416+
417+ return parts . join ( "" ) ;
418+ }
419+
420+ function renderEmphasisSpans ( text : string ) : string {
399421 let result = text ;
400- result = result . replace ( / ` ( [ ^ ` ] + ) ` / g, ( _ , inner ) => chalk . cyan ( inner ) ) ;
401422 result = result . replace ( / \* \* ( [ ^ * ] + ) \* \* / g, ( _ , inner ) => chalk . bold ( inner ) ) ;
402423 result = result . replace ( / (?< ! \* ) \* ( [ ^ * ] + ) \* (? ! \* ) / g, ( _ , inner ) => chalk . italic ( inner ) ) ;
403- result = result . replace ( / _ ( [ ^ _ \n ] + ) _ / g , ( _ , inner ) => chalk . italic ( inner ) ) ;
424+ result = result . replace ( / (?< ! [ \p { L } \p { N } _ ] ) _ ( [ ^ _ \n ] + ) _ (? ! [ \p { L } \p { N } _ ] ) / gu , ( _ , inner ) => chalk . italic ( inner ) ) ;
404425 return result ;
405426}
You can’t perform that action at this time.
0 commit comments