summaryrefslogtreecommitdiffhomepage
path: root/packages/ui/src/components/markdown.tsx
blob: 7615d1737a3d7cac45ce06dbedb5304338a184fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { useMarked } from "../context/marked"
import { ComponentProps, createResource, splitProps } from "solid-js"

export function Markdown(
  props: ComponentProps<"div"> & {
    text: string
    class?: string
    classList?: Record<string, boolean>
  },
) {
  const [local, others] = splitProps(props, ["text", "class", "classList"])
  const marked = useMarked()
  const [html] = createResource(
    () => local.text,
    async (markdown) => {
      return marked.parse(markdown)
    },
  )
  return (
    <div
      data-component="markdown"
      classList={{
        ...(local.classList ?? {}),
        [local.class ?? ""]: !!local.class,
      }}
      innerHTML={html()}
      {...others}
    />
  )
}