feat: enhance C++ syntax highlighting with attribute support (#562)

This commit is contained in:
Julio C. Estrada
2025-12-15 15:33:57 -05:00
committed by GitHub
parent 398b5a20a5
commit b3a6fcdfe2
2 changed files with 28 additions and 2 deletions

View File

@@ -3,11 +3,10 @@
* This replaces highlight.js's C++ highlighting for consistent styling
*/
/* C++ Keywords - blue, bold */
/* C++ Keywords - blue */
code.cpp-highlight .cpp-keyword,
.doc pre.highlight code.cpp-highlight .cpp-keyword {
color: #00f;
font-weight: bold;
}
/* C++ Strings - dark red */
@@ -29,6 +28,12 @@ code.cpp-highlight .cpp-comment,
font-style: italic;
}
/* C++ Attributes - gray */
code.cpp-highlight .cpp-attribute,
.doc pre.highlight code.cpp-highlight .cpp-attribute {
color: #9e9e9e;
}
/* Base text in C++ blocks - plain black (identifiers, function names, etc.) */
code.cpp-highlight,
.doc pre.highlight code.cpp-highlight {

View File

@@ -6,6 +6,7 @@
* - string : String and character literals
* - preprocessor: Preprocessor directives (#include, #define, etc.)
* - comment : C and C++ style comments
* - attribute : C++ attributes ([[nodiscard]], [[noreturn]], etc.)
*/
const CppHighlight = (function () {
@@ -59,6 +60,26 @@ const CppHighlight = (function () {
const n = code.length
while (i < n) {
// C++ attributes [[...]] with nesting support
if (code[i] === '[' && code[i + 1] === '[') {
const start = i
i += 2
let depth = 1
while (i < n && depth > 0) {
if (code[i] === '[' && code[i + 1] === '[') {
depth++
i += 2
} else if (code[i] === ']' && code[i + 1] === ']') {
depth--
i += 2
} else {
i++
}
}
result.push(span('attribute', code.slice(start, i)))
continue
}
// Line comment
if (code[i] === '/' && code[i + 1] === '/') {
let end = i + 2