| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import {
- type JSX,
- onCleanup,
- splitProps,
- createEffect,
- createResource,
- } from "solid-js"
- import { codeToHtml } from "shiki"
- import styles from "./codeblock.module.css"
- import { transformerNotationDiff } from "@shikijs/transformers"
- interface CodeBlockProps extends JSX.HTMLAttributes<HTMLDivElement> {
- code: string
- lang?: string
- onRendered?: () => void
- }
- function CodeBlock(props: CodeBlockProps) {
- const [local, rest] = splitProps(props, ["code", "lang", "onRendered"])
- 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
- local.onRendered?.()
- }
- })
- return <div ref={containerRef} class={styles.codeblock} {...rest}></div>
- }
- export default CodeBlock
|