@@ -626,6 +626,73 @@ describe('parseIncompleteMarkdown', () => {
626626 } ) ;
627627 } ) ;
628628
629+ describe ( 'math blocks with underscores' , ( ) => {
630+ it ( 'should not complete underscores within inline math blocks' , ( ) => {
631+ const text = 'The variable $x_1$ represents the first element' ;
632+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( text ) ;
633+
634+ const text2 = 'Formula: $a_b + c_d = e_f$' ;
635+ expect ( parseIncompleteMarkdown ( text2 ) ) . toBe ( text2 ) ;
636+ } ) ;
637+
638+ it ( 'should not complete underscores within block math' , ( ) => {
639+ const text = '$$x_1 + y_2 = z_3$$' ;
640+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( text ) ;
641+
642+ const text2 = '$$\na_1 + b_2\nc_3 + d_4\n$$' ;
643+ expect ( parseIncompleteMarkdown ( text2 ) ) . toBe ( text2 ) ;
644+ } ) ;
645+
646+ it ( 'should not add underscore when math block has incomplete underscore' , ( ) => {
647+ // Incomplete math blocks get completed by handleIncompleteInlineKatex
648+ // The underscore inside should not be treated as italic
649+ const text = 'Math expression $x_' ;
650+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( 'Math expression $x_$' ) ;
651+
652+ const text2 = '$$formula_' ;
653+ expect ( parseIncompleteMarkdown ( text2 ) ) . toBe ( '$$formula_$$' ) ;
654+ } ) ;
655+
656+ it ( 'should handle underscores outside math blocks normally' , ( ) => {
657+ const text = 'Text with _italic_ and math $x_1$' ;
658+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( text ) ;
659+
660+ const text2 = '_italic text_ followed by $a_b$' ;
661+ expect ( parseIncompleteMarkdown ( text2 ) ) . toBe ( text2 ) ;
662+ } ) ;
663+
664+ it ( 'should complete italic underscore outside math but not inside' , ( ) => {
665+ const text = 'Start _italic with $x_1$' ;
666+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( 'Start _italic with $x_1$_' ) ;
667+ } ) ;
668+
669+ it ( 'should handle complex math expressions with multiple underscores' , ( ) => {
670+ const text = '$x_1 + x_2 + x_3 = y_1$' ;
671+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( text ) ;
672+
673+ const text2 = '$$\\sum_{i=1}^{n} x_i = \\prod_{j=1}^{m} y_j$$' ;
674+ expect ( parseIncompleteMarkdown ( text2 ) ) . toBe ( text2 ) ;
675+ } ) ;
676+
677+ it ( 'should handle escaped dollar signs correctly' , ( ) => {
678+ const text = 'Price is \\$50 and _this is italic_' ;
679+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( text ) ;
680+
681+ const text2 = 'Cost \\$100 with _incomplete' ;
682+ expect ( parseIncompleteMarkdown ( text2 ) ) . toBe ( 'Cost \\$100 with _incomplete_' ) ;
683+ } ) ;
684+
685+ it ( 'should handle mixed inline and block math' , ( ) => {
686+ const text = 'Inline $x_1$ and block $$y_2$$ math' ;
687+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( text ) ;
688+ } ) ;
689+
690+ it ( 'should not interfere with complete math blocks when adding underscores outside' , ( ) => {
691+ const text = '_italic start $x_1$ italic end_' ;
692+ expect ( parseIncompleteMarkdown ( text ) ) . toBe ( text ) ;
693+ } ) ;
694+ } ) ;
695+
629696 describe ( 'edge cases' , ( ) => {
630697 it ( 'should handle text ending with formatting characters' , ( ) => {
631698 expect ( parseIncompleteMarkdown ( 'Text ending with *' ) ) . toBe (
0 commit comments