1+ import { definePlugin , InlineStyleAnnotation , type ExpressiveCodePlugin } from "@astrojs/starlight/expressive-code" ;
2+
3+ export function pluginCxxMark ( ) : ExpressiveCodePlugin {
4+ return definePlugin ( {
5+ name : "CXX mark" ,
6+ hooks : {
7+ postprocessAnalyzedCode : ( context ) => {
8+ context . codeBlock . getLines ( ) . forEach ( ( line ) => {
9+ if ( context . codeBlock . meta . includes ( "cxx-mark" ) ) {
10+ const matches = [ ...line . text . matchAll ( / \/ \* \$ ( ( (?< syntax > s ) | (?< exposition > e x p o s ) ) : [ ^ \* ] + | (?< optional > o p t ) ) \* \/ / g) ] . reverse ( )
11+ matches . forEach ( ( match ) => {
12+ const begin = match . index
13+ const end = begin + match [ 0 ] . length
14+ if ( match . groups ?. syntax != undefined ) {
15+ line . addAnnotation (
16+ new InlineStyleAnnotation ( {
17+ inlineRange : {
18+ columnStart : begin ,
19+ columnEnd : end
20+ } ,
21+ // color of syntax notation should be same with comments
22+ italic : true
23+ } )
24+ )
25+ line . editText ( begin , end , match [ 0 ] . slice ( 5 , - 2 ) )
26+ } else if ( match . groups ?. exposition != undefined ) {
27+ line . addAnnotation (
28+ new InlineStyleAnnotation ( {
29+ inlineRange : {
30+ columnStart : begin ,
31+ columnEnd : end
32+ } ,
33+ color : "var(--cppdoc-color-cxx-mark-exposition)" ,
34+ italic : true
35+ } )
36+
37+ )
38+ line . editText ( begin + 2 , begin + 9 , "" )
39+ } else if ( match . groups ?. optional != undefined ) {
40+ const new_str = "(optional)"
41+ line . editText ( begin , end , new_str )
42+ line . addAnnotation (
43+ new InlineStyleAnnotation ( {
44+ inlineRange : {
45+ columnStart : begin ,
46+ columnEnd : begin + new_str . length
47+ } ,
48+ color : "var(--cppdoc-color-cxx-mark-optional)"
49+ } )
50+ )
51+ }
52+ } )
53+ }
54+ } )
55+ }
56+ }
57+ } )
58+ }
0 commit comments