Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 305c173

Browse files
committed
add diff support to code examples
1 parent 730e6d7 commit 305c173

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/components/code/Diff.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { AnnotationHandler, InnerLine, BlockAnnotation } from 'codehike/code'
2+
3+
export const diff: AnnotationHandler = {
4+
name: 'diff',
5+
onlyIfAnnotated: true,
6+
transform: (annotation: BlockAnnotation) => {
7+
const color = annotation.query == '-' ? '#f85149' : '#3fb950'
8+
return [annotation, { ...annotation, name: 'mark', query: color }]
9+
},
10+
Line: ({ annotation, ...props }) => (
11+
<>
12+
<div className="box-content min-w-[1ch] select-none pl-2 opacity-70">
13+
{annotation?.query}
14+
</div>
15+
<InnerLine merge={props} />
16+
</>
17+
),
18+
}

src/components/code/Mark.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { AnnotationHandler, InnerLine } from 'codehike/code'
2+
3+
export const mark: AnnotationHandler = {
4+
name: 'mark',
5+
Line: ({ annotation, ...props }) => {
6+
const color = annotation?.query || 'rgb(14 165 233)'
7+
return (
8+
<div
9+
className="flex"
10+
style={{
11+
borderLeft: 'solid 2px transparent',
12+
borderLeftColor: annotation && color,
13+
backgroundColor: annotation && `rgb(from ${color} r g b / 0.1)`,
14+
}}
15+
>
16+
<InnerLine merge={props} className="flex-1 px-2" />
17+
</div>
18+
)
19+
},
20+
Inline: ({ annotation, children }) => {
21+
const color = annotation?.query || 'rgb(14 165 233)'
22+
return (
23+
<span
24+
className="-mx-0.5 rounded px-0.5 py-0"
25+
style={{
26+
outline: `solid 1px rgb(from ${color} r g b / 0.5)`,
27+
background: `rgb(from ${color} r g b / 0.13)`,
28+
}}
29+
>
30+
{children}
31+
</span>
32+
)
33+
},
34+
}

src/components/code/Pre.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
import { tokenTransitions } from './annotations/token-transitions'
1212
import { cn } from '@/lib/utils'
1313
import { meta } from './meta'
14+
import { mark } from './Mark'
15+
import { diff } from './Diff'
1416

1517
export interface HandlerProps {
1618
enableTransitions?: boolean
@@ -32,7 +34,15 @@ const Pre: React.FC<Props> = ({
3234
}) => {
3335
const { title } = meta(highlighted)
3436

35-
let handlers = [callout, fold, collapse, collapseTrigger, collapseContent]
37+
let handlers = [
38+
callout,
39+
fold,
40+
collapse,
41+
collapseTrigger,
42+
collapseContent,
43+
mark,
44+
diff,
45+
]
3646

3747
// Getting an issue with color transitions on the -code-text token
3848
// Note: this also won't work with the Tabs component, only the CodeSwitcherSelect

0 commit comments

Comments
 (0)