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