1- import { type AnnotationHandler , highlight , Pre , type RawCode } from 'codehike/code' ;
2- import React from 'react' ;
3- import { cn } from '@/lib/utils' ;
4- import { callout } from './annotations/callout' ;
5- import { collapse } from './annotations/collapse' ;
6- import { diff } from './annotations/diff' ;
7- import { fold } from './annotations/fold' ;
8- import { hover } from './annotations/hover' ;
9- import { lineNumbers } from './annotations/line-numbers' ;
10- import { link } from './annotations/link' ;
11- import { mark } from './annotations/mark' ;
12- import { tokenTransitions } from './annotations/token-transitions' ;
13- import { tooltip } from './annotations/tooltip' ;
14- import { wordWrap } from './annotations/word-wrap' ;
15- import { MultiCode } from './code.client' ;
16- import { CODEBLOCK , type CodeGroup , flagsToOptions , TITLEBAR } from './code-group' ;
17- import { CodeIcon } from './code-icon' ;
18- import { CollapsiblePre } from './collapsible-pre' ;
19- import { CopyButton } from './copy-button' ;
20- import theme from './theme.mjs' ;
1+ import {
2+ type AnnotationHandler ,
3+ highlight ,
4+ type RawCode ,
5+ Pre ,
6+ } from "codehike/code" ;
7+ import { CollapsiblePre } from "./collapsible-pre" ;
8+ import React from "react" ;
9+ import { cn } from "@/lib/utils" ;
10+ import { MultiCode } from "./code.client" ;
11+ import {
12+ CODEBLOCK ,
13+ type CodeGroup ,
14+ flagsToOptions ,
15+ TITLEBAR ,
16+ } from "./code-group" ;
17+ import { CodeIcon } from "./code-icon" ;
18+ import { CopyButton } from "./copy-button" ;
19+ import theme from "./theme.mjs" ;
2120
2221export async function Code ( props : {
2322 codeblocks : RawCode [ ] ;
@@ -59,38 +58,58 @@ export function CodeSync(props: {
5958}
6059
6160// Shared client component
62- function CodeClient ( { group, className } : { group : CodeGroup ; className ?: string } ) {
61+ function CodeClient ( {
62+ group,
63+ className,
64+ } : {
65+ group : CodeGroup ;
66+ className ?: string ;
67+ } ) {
6368 return group . tabs . length === 1 ? (
6469 < SingleCode group = { group } className = { className } />
6570 ) : (
6671 < MultiCode group = { group } key = { group . storage } className = { className } />
6772 ) ;
6873}
6974
70- function SingleCode ( { group, className } : { group : CodeGroup ; className ?: string } ) {
75+ function SingleCode ( {
76+ group,
77+ className,
78+ } : {
79+ group : CodeGroup ;
80+ className ?: string ;
81+ } ) {
7182 const tab = group . tabs [ 0 ] ;
7283 if ( ! tab ) return null ;
7384
7485 const { pre, style, code, title, icon, options, filename } = tab ;
75- const hasTitle = title ?. trim ( ) !== '' ;
86+ const hasTitle = title ?. trim ( ) !== "" ;
7687 return (
7788 < div
78- className = { cn ( CODEBLOCK , ! hasTitle && 'border-none' , className , 'rounded-lg' ) }
89+ className = { cn (
90+ CODEBLOCK ,
91+ ! hasTitle && "border-none" ,
92+ className ,
93+ "rounded-lg" ,
94+ ) }
7995 style = { style }
8096 >
8197 { hasTitle && (
8298 < div
8399 className = { cn (
84100 TITLEBAR ,
85- ' flex items-center gap-2' ,
86- ' text-muted-foreground text-sm font-mono' ,
101+ " flex items-center gap-2" ,
102+ " text-muted-foreground text-sm font-mono" ,
87103 ) }
88104 >
89105 < span className = "pl-2 pr-1" > { icon } </ span >
90106 { title }
91- { options . copyButton && filename === '' && (
92- < div className = { cn ( 'ml-auto mr-1 items-center' ) } >
93- < CopyButton text = { code } className = "text-ch-tab-inactive-foreground" />
107+ { options . copyButton && filename === "" && (
108+ < div className = { cn ( "ml-auto mr-1 items-center" ) } >
109+ < CopyButton
110+ text = { code }
111+ className = "text-ch-tab-inactive-foreground"
112+ />
94113 </ div >
95114 ) }
96115 </ div >
@@ -100,7 +119,9 @@ function SingleCode({ group, className }: { group: CodeGroup; className?: string
100119 className = "flex items-center w-full text-sm font-mono rounded-t-none bg-[var(--ch-0)] p-1"
101120 style = { { minHeight : 32 } }
102121 >
103- < span className = "pl-2 pr-2 flex-1 font-mono text-foreground" > { filename } </ span >
122+ < span className = "pl-2 pr-2 flex-1 font-mono text-foreground" >
123+ { filename }
124+ </ span >
104125 < span className = "ml-auto pr-3" >
105126 < CopyButton
106127 text = { code }
@@ -135,40 +156,23 @@ export async function toCodeGroup(props: {
135156 const tabOptions = flagsToOptions ( flags ) ;
136157 const options = { ...groupOptions , ...tabOptions } ;
137158 const highlighted = await highlight ( tab , theme ) ;
138- const handlers = getHandlers ( options ) ;
139159 return {
140160 options,
141161 title,
142162 filename,
143163 style : highlighted . style ,
144164 code : highlighted . code ,
145165 icon : < CodeIcon title = { title } lang = { tab . lang } /> ,
146- // pre: (
147- // <CollapsiblePre
148- // code={highlighted}
149- // className={cn(
150- // !title && "!m-0",
151- // "overflow-x-auto px-0 py-2 rounded-lg !bg-ch-code max-w-full", // rounded-lg
152- // props.preClassName,
153- // )}
154- // style={highlighted.style}
155- // // handlers={handlers}
156- // />
157- // ),
158-
159166 pre : (
160167 < div className = "bg-[var(--ch-0)] p-1" >
161- < Pre
168+ < CollapsiblePre
162169 code = { highlighted }
163- className = { cn (
164- ! title && '!m-0' ,
165- 'overflow-x-auto p-3 rounded-lg font-mono bg-[var(--ch-18)] max-w-full' ,
166- props . preClassName ,
167- ) }
170+ title = { title }
171+ preClassName = { props . preClassName }
172+ options = { options }
168173 style = { {
169174 ...highlighted . style ,
170175 } }
171- handlers = { handlers }
172176 />
173177 </ div >
174178 ) ,
@@ -183,22 +187,6 @@ export async function toCodeGroup(props: {
183187 } ;
184188}
185189
186- function getHandlers ( options : CodeGroup [ 'options' ] ) {
187- return [
188- mark ,
189- tooltip ,
190- fold ,
191- link ,
192- options . animate && tokenTransitions ,
193- options . lineNumbers && lineNumbers ,
194- diff ,
195- ...collapse ,
196- options . wordWrap && wordWrap ,
197- callout ,
198- hover ,
199- ] . filter ( Boolean ) as AnnotationHandler [ ] ;
200- }
201-
202190/**
203191 * Extracts flags and title from the metadata of a code block.
204192 *
@@ -213,15 +201,15 @@ function getHandlers(options: CodeGroup['options']) {
213201
214202function extractFlags ( codeblock : RawCode ) {
215203 // Support: foo.js -abc -f filename.txt
216- const meta = codeblock . meta || '' ;
204+ const meta = codeblock . meta || "" ;
217205 const flagMatch = meta . match ( / - ( \w + ) / ) ;
218206 const fileMatch = meta . match ( / - f \s + ( [ ^ \s ] + ) / ) ;
219- const flags = flagMatch ? flagMatch [ 1 ] : '' ;
220- const filename = fileMatch ? fileMatch [ 1 ] : '' ;
207+ const flags = flagMatch ? flagMatch [ 1 ] : "" ;
208+ const filename = fileMatch ? fileMatch [ 1 ] : "" ;
221209 // Remove flags and filename from title
222210 let title = meta
223- . replace ( flagMatch ? flagMatch [ 0 ] : '' , '' )
224- . replace ( fileMatch ? fileMatch [ 0 ] : '' , '' )
211+ . replace ( flagMatch ? flagMatch [ 0 ] : "" , "" )
212+ . replace ( fileMatch ? fileMatch [ 0 ] : "" , "" )
225213 . trim ( ) ;
226214 return { title, flags, filename } ;
227215}
0 commit comments