content-code.tsx 851 B

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