Skip to content

Commit b3a6fcd

Browse files
authored
feat: enhance C++ syntax highlighting with attribute support (#562)
1 parent 398b5a2 commit b3a6fcd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

antora-ui/src/css/cpp-highlight.css

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* This replaces highlight.js's C++ highlighting for consistent styling
44
*/
55

6-
/* C++ Keywords - blue, bold */
6+
/* C++ Keywords - blue */
77
code.cpp-highlight .cpp-keyword,
88
.doc pre.highlight code.cpp-highlight .cpp-keyword {
99
color: #00f;
10-
font-weight: bold;
1110
}
1211

1312
/* C++ Strings - dark red */
@@ -29,6 +28,12 @@ code.cpp-highlight .cpp-comment,
2928
font-style: italic;
3029
}
3130

31+
/* C++ Attributes - gray */
32+
code.cpp-highlight .cpp-attribute,
33+
.doc pre.highlight code.cpp-highlight .cpp-attribute {
34+
color: #9e9e9e;
35+
}
36+
3237
/* Base text in C++ blocks - plain black (identifiers, function names, etc.) */
3338
code.cpp-highlight,
3439
.doc pre.highlight code.cpp-highlight {

antora-ui/src/js/vendor/cpp-highlight.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* - string : String and character literals
77
* - preprocessor: Preprocessor directives (#include, #define, etc.)
88
* - comment : C and C++ style comments
9+
* - attribute : C++ attributes ([[nodiscard]], [[noreturn]], etc.)
910
*/
1011

1112
const CppHighlight = (function () {
@@ -59,6 +60,26 @@ const CppHighlight = (function () {
5960
const n = code.length
6061

6162
while (i < n) {
63+
// C++ attributes [[...]] with nesting support
64+
if (code[i] === '[' && code[i + 1] === '[') {
65+
const start = i
66+
i += 2
67+
let depth = 1
68+
while (i < n && depth > 0) {
69+
if (code[i] === '[' && code[i + 1] === '[') {
70+
depth++
71+
i += 2
72+
} else if (code[i] === ']' && code[i + 1] === ']') {
73+
depth--
74+
i += 2
75+
} else {
76+
i++
77+
}
78+
}
79+
result.push(span('attribute', code.slice(start, i)))
80+
continue
81+
}
82+
6283
// Line comment
6384
if (code[i] === '/' && code[i + 1] === '/') {
6485
let end = i + 2

0 commit comments

Comments
 (0)