content-code.tsx 966 B

1234567891011121314151617181920212223242526272829303132
  1. import { codeToHtml, bundledLanguages } from "shiki"
  2. import { createResource, Suspense } from "solid-js"
  3. import { transformerNotationDiff } from "@shikijs/transformers"
  4. import style from "./content-code.module.css"
  5. interface Props {
  6. code: string
  7. lang?: string
  8. flush?: boolean
  9. }
  10. export function ContentCode(props: Props) {
  11. const [html] = createResource(
  12. () => [props.code, props.lang],
  13. async ([code, lang]) => {
  14. // TODO: For testing delays
  15. // await new Promise((resolve) => setTimeout(resolve, 3000))
  16. return (await codeToHtml(code || "", {
  17. lang: lang && lang in bundledLanguages ? lang : "text",
  18. themes: {
  19. light: "github-light",
  20. dark: "github-dark",
  21. },
  22. transformers: [transformerNotationDiff()],
  23. })) as string
  24. },
  25. )
  26. return (
  27. <Suspense>
  28. <div innerHTML={html()} class={style.root} data-flush={props.flush === true ? true : undefined} />
  29. </Suspense>
  30. )
  31. }