-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathcode.tsx
158 lines (151 loc) · 4.12 KB
/
code.tsx
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
AnnotationHandler,
HighlightedCode,
Inline,
Pre,
RawCode,
highlight,
} from "codehike/code"
import { cn } from "@/lib/utils"
import { CopyButton } from "@/components/copy-button"
import { fold } from "./annotations/fold"
import { link } from "./annotations/link"
import theme from "@/theme.mjs"
import { lineNumbers } from "./annotations/line-numbers"
import { CodeIcon } from "./annotations/icons"
import { collapse } from "./annotations/collapse"
import { callout } from "./annotations/callout"
import { mark } from "./annotations/mark"
import { pill } from "./annotations/pill"
import { ruler } from "./annotations/ruler"
import { wordWrap } from "./annotations/word-wrap"
import { tokenTransitions } from "./annotations/token-transitions"
import { focus } from "./annotations/focus"
import { diff } from "./annotations/diff"
import { tooltip } from "./annotations/tooltip"
export async function InlineCode({ codeblock }: { codeblock: RawCode }) {
const highlighted = await highlight(codeblock, theme)
return (
<Inline
code={highlighted}
style={highlighted.style}
className="selection:bg-editor-selectionBackground"
/>
)
}
export async function Code({
codeblock,
...rest
}: {
codeblock: RawCode
className?: string
style?: React.CSSProperties
extraHandlers?: AnnotationHandler[]
}) {
const { flags } = extractFlags(codeblock)
const highlighted = await highlight(codeblock, theme, {
annotationPrefix: flags.includes("p") ? "!!" : undefined,
})
return <HighCode highlighted={highlighted} {...rest} />
}
export function HighCode({
highlighted,
className,
style,
extraHandlers = [],
}: {
highlighted: HighlightedCode
className?: string
style?: React.CSSProperties
extraHandlers?: AnnotationHandler[]
}) {
const { title, flags } = extractFlags(highlighted)
const h = { ...highlighted, meta: title }
const handlers = [
...extraHandlers,
mark,
tooltip,
pill,
fold,
link,
focus,
ruler,
flags.includes("a") && tokenTransitions,
flags.includes("n") && lineNumbers,
diff,
...collapse,
flags.includes("w") && wordWrap,
callout,
].filter(Boolean) as AnnotationHandler[]
const pre = (
<Pre
code={h}
className="m-0 py-2 px-0 bg-editor-background rounded-none group flex-1 selection:bg-editor-selectionBackground"
handlers={handlers}
style={{
backgroundColor: "var(--bg-color)",
}}
/>
)
if (title) {
return (
<div
className={cn(
"border border-editorGroup-border rounded overflow-hidden my-2",
className,
)}
style={
{
"--border-color": "var(--ch-23)",
borderColor: "var(--border-color)",
...style,
} as any
}
>
<div
className="px-3 py-2 border-b border-editorGroup-border bg-editorGroupHeader-tabsBackground text-sm text-tab-activeForeground flex"
style={{ borderColor: "var(--border-color)" }}
>
<div className="text-tab-activeForeground text-sm flex items-center gap-3">
<CodeIcon title={title} />
<span>{title}</span>
</div>
{flags.includes("c") && (
<CopyButton text={h.code} className="ml-auto" />
)}
</div>
{pre}
</div>
)
} else {
return (
<div
className={cn(
"border border-editorGroup-border rounded overflow-hidden my-2 relative",
className,
)}
style={
{
"--border-color": "var(--ch-23)",
borderColor: "var(--border-color)",
...style,
} as any
}
>
{flags.includes("c") && (
<CopyButton text={h.code} className="absolute right-4 my-0 top-2" />
)}
{pre}
</div>
)
}
}
export function extractFlags(codeblock: RawCode) {
const flags =
codeblock.meta.split(" ").filter((flag) => flag.startsWith("-"))[0] ?? ""
const title =
codeblock.meta === flags
? ""
: codeblock.meta.replace(" " + flags, "").trim()
return { title, flags: flags.slice(1).split("") }
}