import { createSignal, onMount, Show, createEffect } from "solid-js" import type { Highlighter } from "shiki" import { useTheme } from "../lib/theme" import { getSharedHighlighter, escapeHtml } from "../lib/markdown" interface CodeBlockInlineProps { code: string language?: string } export function CodeBlockInline(props: CodeBlockInlineProps) { const { isDark } = useTheme() const [html, setHtml] = createSignal("") const [copied, setCopied] = createSignal(false) const [ready, setReady] = createSignal(false) let highlighter: Highlighter | null = null onMount(async () => { highlighter = await getSharedHighlighter() setReady(true) updateHighlight() }) createEffect(() => { if (ready()) { updateHighlight() } }) const updateHighlight = () => { if (!highlighter) return if (!props.language) { setHtml(`
${escapeHtml(props.code)}`)
return
}
try {
const highlighted = highlighter.codeToHtml(props.code, {
lang: props.language,
theme: isDark() ? "github-dark" : "github-light",
})
setHtml(highlighted)
} catch {
setHtml(`${escapeHtml(props.code)}`)
}
}
const copyCode = async () => {
await navigator.clipboard.writeText(props.code)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}
return (
{props.code}
}
>