-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aih): special treatment for shellscript code blocks (#370)
- Loading branch information
1 parent
3c913bc
commit e1dcf42
Showing
3 changed files
with
57 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,40 @@ | ||
import React from 'react' | ||
import { highlight, Pre, RawCode } from 'codehike/code' | ||
|
||
import { cn } from '@coursebuilder/ui/utils/cn' | ||
|
||
import { CopyButton } from './copy-button' | ||
import { callout, diff, focus, fold, link, mark } from './handlers' | ||
|
||
export async function Code({ codeblock }: { codeblock: RawCode }) { | ||
const highlighted = await highlight(codeblock, 'github-dark') | ||
const isTerminalCode = highlighted.lang === 'shellscript' | ||
return ( | ||
<Pre | ||
code={highlighted} | ||
className="bg-background text-xs sm:text-sm" | ||
style={{ ...highlighted.style, padding: '1rem', borderRadius: '0.5rem' }} | ||
handlers={[callout, fold, mark, diff, focus, link]} | ||
/> | ||
<div data-pre="" className="group relative"> | ||
{isTerminalCode && ( | ||
<div | ||
aria-hidden="true" | ||
className="bg-background mt-8 flex items-center gap-1 rounded-t-[0.5rem] border-x border-t p-4" | ||
> | ||
<div className="h-2 w-2 rounded-full bg-gray-700" /> | ||
<div className="h-2 w-2 rounded-full bg-gray-700" /> | ||
<div className="h-2 w-2 rounded-full bg-gray-700" /> | ||
</div> | ||
)} | ||
<CopyButton text={highlighted.code} /> | ||
<Pre | ||
code={highlighted} | ||
className={cn('bg-background text-xs sm:text-sm', { | ||
'!mt-0 !rounded-t-none rounded-b border-x border-b border-t-0': | ||
isTerminalCode, | ||
})} | ||
style={{ | ||
...highlighted.style, | ||
padding: '1rem', | ||
borderRadius: '0.5rem', | ||
}} | ||
handlers={[callout, fold, mark, diff, focus, link]} | ||
/> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use client' | ||
|
||
import React, { useState } from 'react' | ||
import { Check, Copy } from 'lucide-react' | ||
|
||
import { Button } from '@coursebuilder/ui' | ||
|
||
export function CopyButton({ text }: { text: string }) { | ||
const [copied, setCopied] = useState(false) | ||
|
||
return ( | ||
<Button | ||
variant="outline" | ||
size="icon" | ||
className="absolute bottom-1 right-1 rounded opacity-0 transition group-focus-within:opacity-100 group-hover:opacity-100" | ||
aria-label="Copy to clipboard" | ||
onClick={() => { | ||
navigator.clipboard.writeText(text) | ||
setCopied(true) | ||
setTimeout(() => setCopied(false), 1200) | ||
}} | ||
> | ||
{copied ? <Check size={16} /> : <Copy size={16} />} | ||
</Button> | ||
) | ||
} |