Skip to content

Commit d35abe8

Browse files
committed
feat: add expressive-code plugin for marks in code block
1 parent e57a488 commit d35abe8

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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>expos)):[^\*]+|(?<optional>opt))\*\//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+
}

src/styles/custom.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
--cppdoc-color-ill-formed: var(--sl-color-red);
1818
--cppdoc-color-ifndr: var(--sl-color-red);
1919

20+
--cppdoc-color-cxx-mark-exposition: #ff0000;
21+
--cppdoc-color-cxx-mark-optional: var(--sl-color-green);
22+
2023
--sl-sidebar-width: 22rem;
2124
}
2225

0 commit comments

Comments
 (0)