This repository was archived by the owner on May 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathSimpleDiffHighlighter.tsx
More file actions
74 lines (63 loc) · 1.8 KB
/
Copy pathSimpleDiffHighlighter.tsx
File metadata and controls
74 lines (63 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React from 'react';
import { hasDiffContent, parseDiffLines, type DiffLine } from '../utils/simpleDiffDetector';
import { useTheme } from '../hooks/useSettings';
interface SimpleDiffHighlighterProps {
content: string;
className?: string;
}
interface DiffLineProps {
line: DiffLine;
isDark: boolean;
}
function DiffLineComponent({ line, isDark }: DiffLineProps) {
const getLineStyles = () => {
switch (line.type) {
case 'addition':
return isDark
? `bg-green-900/30 text-green-300`
: `bg-green-50 text-green-700`;
case 'removal':
return isDark
? `bg-red-900/30 text-red-300`
: `bg-red-50 text-red-700`;
default: // context
return isDark
? `text-emerald-300`
: `text-emerald-700`;
}
};
return (
<span className={getLineStyles()}>
{line.content}
</span>
);
}
export function SimpleDiffHighlighter({ content, className = '' }: SimpleDiffHighlighterProps) {
const { theme } = useTheme();
const isDark = theme === 'dark';
// Check if content contains diff lines
const isDiff = hasDiffContent(content);
// If not a diff, render as plain pre
if (!isDiff) {
return (
<pre className={`whitespace-pre-wrap font-mono leading-relaxed ${className}`}>
{content}
</pre>
);
}
// Parse diff lines and render with highlighting
const diffLines = parseDiffLines(content);
return (
<pre className={`whitespace-pre-wrap font-mono leading-relaxed ${className}`}>
{diffLines.map((line, index) => (
<React.Fragment key={index}>
<DiffLineComponent
line={line}
isDark={isDark}
/>
{index < diffLines.length - 1 && '\n'}
</React.Fragment>
))}
</pre>
);
}