折叠
折叠内联内容。
content.md
```jsx// !fold[/className="(.*?)"/gm]function Foo() {return (<div className="bg-red-200 opacity-50"><span className="block">hey</span></div>)}```
function Foo() {return (<div className=""><span className="">hey</span></div>)}
点击 ... 来展开 className
Implementation
fold.tsx
"use client"import { AnnotationHandler } from "codehike/code"import { useState } from "react"export const InlineFold: AnnotationHandler["Inline"] = ({ children }) => {const [folded, setFolded] = useState(true)if (!folded) {return children}return (<button onClick={() => setFolded(false)} aria-label="展开">...</button>)}
然后将处理器添加到您的 Code 组件中:
code.tsx
import { RawCode, Pre, highlight, AnnotationHandler } from "codehike/code"import { InlineFold } from "./fold"async function Code({ codeblock }: { codeblock: RawCode }) {const highlighted = await highlight(codeblock, "github-dark")return <Pre code={highlighted} handlers={[fold]} />}const fold: AnnotationHandler = {name: "fold",Inline: InlineFold,}