Skip to content

Commit f6b0d1d

Browse files
committed
feat: shiki block expand attr
Signed-off-by: Innei <[email protected]>
1 parent 32d5703 commit f6b0d1d

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/components/modules/shared/CodeBlock.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { ReactNode } from 'react'
66

77
import { HighLighterPrismCdn } from '~/components/ui/code-highlighter'
88
import { ShikiHighLighterWrapper } from '~/components/ui/code-highlighter/shiki/ShikiWrapper'
9+
import { parseShouldCollapsedFromAttrs } from '~/components/ui/code-highlighter/shiki/utils'
910
import { ExcalidrawLoading } from '~/components/ui/excalidraw/ExcalidrawLoading'
1011
import { isClientSide } from '~/lib/env'
1112

@@ -77,7 +78,10 @@ export const CodeBlockRender = (props: {
7778
}
7879

7980
const fallback = (
80-
<ShikiHighLighterWrapper {...nextProps}>
81+
<ShikiHighLighterWrapper
82+
{...nextProps}
83+
shouldCollapsed={parseShouldCollapsedFromAttrs(props.attrs || '')}
84+
>
8185
<pre className="bg-transparent px-5">
8286
<code className="!px-5 !text-base-content">
8387
{nextProps.content}

src/components/ui/code-highlighter/shiki/Shiki.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { FC } from 'react'
44
import { isServerSide } from '~/lib/env'
55

66
import { ShikiHighLighterWrapper } from './ShikiWrapper'
7+
import { parseShouldCollapsedFromAttrs } from './utils'
78

89
export interface ShikiProps {
910
lang: string | undefined
@@ -94,6 +95,7 @@ export const ShikiHighLighter: FC<ShikiProps> = (props) => {
9495

9596
return (
9697
<ShikiHighLighterWrapper
98+
shouldCollapsed={parseShouldCollapsedFromAttrs(attrs || '')}
9799
{...props}
98100
renderedHTML={renderedHtml}
99101
ref={setCodeBlockRef}

src/components/ui/code-highlighter/shiki/ShikiWrapper.tsx

+16-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@ interface Props {
3030

3131
export const ShikiHighLighterWrapper = forwardRef<
3232
HTMLDivElement,
33-
PropsWithChildren<Props>
33+
PropsWithChildren<
34+
Props & {
35+
shouldCollapsed?: boolean
36+
}
37+
>
3438
>((props, ref) => {
35-
const { lang: language, content: value, attrs } = props
39+
const {
40+
shouldCollapsed = true,
41+
lang: language,
42+
content: value,
43+
attrs,
44+
} = props
3645

3746
const handleCopy = useCallback(() => {
3847
navigator.clipboard.writeText(value)
@@ -43,9 +52,12 @@ export const ShikiHighLighterWrapper = forwardRef<
4352

4453
useImperativeHandle(ref, () => codeBlockRef!)
4554

46-
const [isCollapsed, setIsCollapsed] = useState(true)
55+
const [isCollapsed, setIsCollapsed] = useState(shouldCollapsed)
4756
const [isOverflow, setIsOverflow] = useState(false)
4857
useEffect(() => {
58+
if (!shouldCollapsed) {
59+
return
60+
}
4961
const $el = codeBlockRef
5062

5163
if (!$el) return
@@ -110,7 +122,7 @@ export const ShikiHighLighterWrapper = forwardRef<
110122
<MotionButtonBase
111123
onClick={handleCopy}
112124
className={clsx(
113-
'absolute right-2 top-2 z-[1] flex text-xs center',
125+
'center absolute right-2 top-2 z-[1] flex text-xs',
114126
'rounded-md border border-accent/5 bg-accent/80 p-1.5 text-white backdrop-blur duration-200',
115127
'opacity-0 group-hover:opacity-100',
116128
filename && '!top-12',

src/components/ui/code-highlighter/shiki/utils.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export const parseFilenameFromAttrs = (attrs: string) => {
1010
return null
1111
}
1212

13+
export const parseShouldCollapsedFromAttrs = (attrs: string) => {
14+
// collapsed
15+
return attrs.includes('collapsed') || !attrs.includes('expand')
16+
}
17+
1318
// const shikiSupportLangSet = new Set(Object.keys(bundledLanguages))
1419
export const isSupportedShikiLang = (lang: string) => {
1520
// require esm error, fuck nextjs 14.12.x

0 commit comments

Comments
 (0)