({
snippet,
lang,
showLineNumbers,
highlightLines,
highlightWords
}: PropsType)
| 110 | } |
| 111 | |
| 112 | export function CodeBlock({ |
| 113 | snippet, |
| 114 | lang, |
| 115 | showLineNumbers, |
| 116 | highlightLines, |
| 117 | highlightWords |
| 118 | }: PropsType) { |
| 119 | const { resolvedTheme } = useTheme(); |
| 120 | const isDark = resolvedTheme === "dark"; |
| 121 | |
| 122 | const lineBg = isDark |
| 123 | ? "rgba(94, 132, 252, 0.15)" |
| 124 | : "rgba(55, 88, 249, 0.08)"; |
| 125 | const lineBorder = isDark ? "#5e84fc" : "#3758f9"; |
| 126 | const wordBg = isDark |
| 127 | ? "rgba(94, 132, 252, 0.25)" |
| 128 | : "rgba(55, 88, 249, 0.15)"; |
| 129 | |
| 130 | const highlightedLines = useMemo( |
| 131 | () => expandLineRanges(highlightLines || []), |
| 132 | [highlightLines] |
| 133 | ); |
| 134 | |
| 135 | const wordPattern = useMemo( |
| 136 | () => buildUnionPattern(highlightWords), |
| 137 | [highlightWords] |
| 138 | ); |
| 139 | |
| 140 | const hasInlineHighlight = !!wordPattern; |
| 141 | const hasLineHighlight = highlightedLines.size > 0; |
| 142 | |
| 143 | const linePosition = useMemo(() => { |
| 144 | const sorted = [...highlightedLines].sort((a, b) => a - b); |
| 145 | const map = new Map<number, "single" | "first" | "mid" | "last">(); |
| 146 | const len = sorted.length; |
| 147 | if (len === 0) return map; |
| 148 | |
| 149 | for (let i = 0; i < len; i++) { |
| 150 | const cur = sorted[i]!; |
| 151 | const isStart = i === 0 || sorted[i - 1]! !== cur - 1; |
| 152 | const isEnd = i === len - 1 || sorted[i + 1]! !== cur + 1; |
| 153 | |
| 154 | if (isStart && isEnd) { |
| 155 | map.set(cur, "single"); |
| 156 | } else if (isStart) { |
| 157 | map.set(cur, "first"); |
| 158 | } else if (isEnd) { |
| 159 | map.set(cur, "last"); |
| 160 | } else { |
| 161 | map.set(cur, "mid"); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return map; |
| 166 | }, [highlightedLines]); |
| 167 | |
| 168 | const lineProps = useMemo(() => { |
| 169 | if (!hasLineHighlight) return undefined; |
nothing calls this directly
no test coverage detected