@@ -76,26 +76,51 @@ class CSharpViewer extends HTMLElement {
7676 'orderby' , 'group' , 'by' , 'into' , 'on' , 'equals' , 'ascending' , 'descending'
7777 ] ;
7878
79- // Highlight comments (// and /* */)
80- code = code . replace ( / ( \/ \/ . * ? $ ) / gm , '<span class="cs-comment">$1</span>' ) ;
81- code = code . replace ( / ( \/ \* [ \s \S ] * ? \* \/ ) / g , '<span class="cs-comment">$1</span>' ) ;
79+ // Use placeholders to protect already-highlighted content
80+ const protectedSections = [ ] ;
81+ let protectIndex = 0 ;
8282
83- // Highlight strings
84- code = code . replace ( / ( " (?: [ ^ " \\ ] | \\ .) * " ) / g, '<span class="cs-string">$1</span>' ) ;
83+ // 1. Protect and highlight comments first (they have highest priority)
84+ code = code . replace ( / ( \/ \/ .* ?$ ) / gm, ( match ) => {
85+ const placeholder = `___PROTECTED_${ protectIndex } ___` ;
86+ protectedSections [ protectIndex ] = `<span class="cs-comment">${ match } </span>` ;
87+ protectIndex ++ ;
88+ return placeholder ;
89+ } ) ;
8590
86- // Highlight numbers
91+ code = code . replace ( / ( \/ \* [ \s \S ] * ?\* \/ ) / g, ( match ) => {
92+ const placeholder = `___PROTECTED_${ protectIndex } ___` ;
93+ protectedSections [ protectIndex ] = `<span class="cs-comment">${ match } </span>` ;
94+ protectIndex ++ ;
95+ return placeholder ;
96+ } ) ;
97+
98+ // 2. Protect and highlight strings
99+ code = code . replace ( / ( " (?: [ ^ " \\ ] | \\ .) * " ) / g, ( match ) => {
100+ const placeholder = `___PROTECTED_${ protectIndex } ___` ;
101+ protectedSections [ protectIndex ] = `<span class="cs-string">${ match } </span>` ;
102+ protectIndex ++ ;
103+ return placeholder ;
104+ } ) ;
105+
106+ // 3. Highlight numbers (only outside strings/comments)
87107 code = code . replace ( / \b ( \d + \. ? \d * [ f F d D m M ] ? ) \b / g, '<span class="cs-number">$1</span>' ) ;
88108
89- // Highlight keywords
109+ // 4. Highlight keywords (only outside strings/comments)
90110 const keywordPattern = new RegExp ( `\\b(${ keywords . join ( '|' ) } )\\b` , 'g' ) ;
91111 code = code . replace ( keywordPattern , '<span class="cs-keyword">$1</span>' ) ;
92112
93- // Highlight types ( PascalCase words)
94- code = code . replace ( / \b ( [ A - Z ] [ a - z A - Z 0 - 9 ] * (?: & l t ; [ ^ & ] * & g t ; ) ? ) \b / g, '<span class="cs-type">$1</span>' ) ;
113+ // 5. Highlight types - PascalCase words (only outside strings/comments )
114+ code = code . replace ( / \b ( [ A - Z ] [ a - z A - Z 0 - 9 ] * (?: & l t ; [ ^ & ] * & g t ; ) ? ) \b (? ! [ ^ < ] * < \/ s p a n > ) / g, '<span class="cs-type">$1</span>' ) ;
95115
96- // Highlight method calls
116+ // 6. Highlight method calls (only outside strings/comments)
97117 code = code . replace ( / \b ( [ a - z A - Z _ ] [ a - z A - Z 0 - 9 _ ] * ) \s * (? = \( ) / g, '<span class="cs-method">$1</span>' ) ;
98118
119+ // 7. Restore protected sections (comments and strings)
120+ protectedSections . forEach ( ( section , index ) => {
121+ code = code . replace ( `___PROTECTED_${ index } ___` , section ) ;
122+ } ) ;
123+
99124 return code ;
100125 }
101126
0 commit comments