Files
demo/frontend-starter/hooks/use-highlighted-code.ts
T
AdminandClaude Opus 5.5 78c8b702f0 Galerie aller shadcn/ui-Komponenten auf der Startseite
- Alle 63 Komponenten aus dem shadcn-Register installiert (Stil base-luma)
- Galerie unter components/galerie mit 7 Kategorien und Sprungnavigation
- Inhaltsbreite auf max-w-6xl erweitert
- shadcn-Hauptfarbe auf E&P-Tiefblau, größter Radius eckig
- App-Titel "UI-Galerie"

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 18:07:19 +00:00

76 lines
2.0 KiB
TypeScript

import * as React from "react"
import { loadHighlighter } from "@/lib/highlighter"
export function useHighlightedCode(value: string, language: string) {
// A trailing newline would render as an extra empty line.
const code = value.endsWith("\n") ? value.slice(0, -1) : value
const [loaded, setLoaded] = React.useState<{
language: string
highlighter: Awaited<ReturnType<typeof loadHighlighter>>
} | null>(null)
const highlighter = loaded?.language === language ? loaded.highlighter : null
React.useEffect(() => {
let cancelled = false
loadHighlighter(language)
.then((next) => {
if (!cancelled) {
setLoaded({ language, highlighter: next })
}
})
.catch(() => {})
return () => {
cancelled = true
}
}, [language])
// The complete lines, highlighted. While the code streams, only the new
// lines are tokenized, starting from the grammar state of the last one.
const [done, setDone] = React.useState<{
highlighter: typeof highlighter
text: string
lines: React.ReactNode[]
state?: Parameters<NonNullable<typeof highlighter>["highlight"]>[1]
}>({ highlighter: null, text: "", lines: [] })
let next = done
if (next.highlighter !== highlighter || !code.startsWith(next.text)) {
next = { highlighter, text: "", lines: [] }
}
const end = code.lastIndexOf("\n") + 1
if (highlighter && end > next.text.length) {
const result = highlighter.highlight(
code.slice(next.text.length, end - 1),
next.state
)
next = {
highlighter,
text: code.slice(0, end),
lines: [...next.lines, ...result.lines],
state: result.state,
}
}
if (next !== done) {
setDone(next)
}
// The last line is still being written, so it is tokenized on every render.
const tail = code.slice(end)
return React.useMemo(() => {
if (!highlighter) {
return code.split("\n")
}
return [...next.lines, highlighter.highlight(tail, next.state).lines[0]]
}, [code, highlighter, next, tail])
}