diff options
| author | Jay V <[email protected]> | 2025-05-30 13:58:32 -0400 |
|---|---|---|
| committer | Jay V <[email protected]> | 2025-05-30 13:58:34 -0400 |
| commit | a4e46e6e18140afbf376ef3baa26aa5e90c27d94 (patch) | |
| tree | 5024cfe0c94f650b95183a2781ba05dbeee6506b /app/packages/web/src/components/CodeBlock.tsx | |
| parent | 680d52016c69887c331dbbb37de7109158ee9020 (diff) | |
| download | opencode-a4e46e6e18140afbf376ef3baa26aa5e90c27d94.tar.gz opencode-a4e46e6e18140afbf376ef3baa26aa5e90c27d94.zip | |
share page diff
Diffstat (limited to 'app/packages/web/src/components/CodeBlock.tsx')
| -rw-r--r-- | app/packages/web/src/components/CodeBlock.tsx | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/app/packages/web/src/components/CodeBlock.tsx b/app/packages/web/src/components/CodeBlock.tsx new file mode 100644 index 000000000..17559ece1 --- /dev/null +++ b/app/packages/web/src/components/CodeBlock.tsx @@ -0,0 +1,47 @@ +import { + type JSX, + onCleanup, + splitProps, + createEffect, + createResource, +} from "solid-js" +import { codeToHtml } from "shiki" +import { transformerNotationDiff } from '@shikijs/transformers' + +interface CodeBlockProps extends JSX.HTMLAttributes<HTMLDivElement> { + code: string + lang?: string +} +function CodeBlock(props: CodeBlockProps) { + const [local, rest] = splitProps(props, ["code", "lang"]) + let containerRef!: HTMLDivElement + + const [html] = createResource(async () => { + return (await codeToHtml(local.code, { + lang: local.lang || "text", + themes: { + light: 'github-light', + dark: 'github-dark', + }, + transformers: [ + transformerNotationDiff(), + ], + })) as string + }) + + onCleanup(() => { + if (containerRef) containerRef.innerHTML = "" + }) + + createEffect(() => { + if (html() && containerRef) { + containerRef.innerHTML = html() as string + } + }) + + return ( + <div ref={containerRef} {...rest}></div> + ) +} + +export default CodeBlock |
