summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop/src/components/markdown.tsx
blob: e0f185f5f7d2e841c09e51709b85fdf5b1f00e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { useMarked } from "@/context/marked"
import { createResource } from "solid-js"

function strip(text: string): string {
  const wrappedRe = /^\s*<([A-Za-z]\w*)>\s*([\s\S]*?)\s*<\/\1>\s*$/
  const match = text.match(wrappedRe)
  return match ? match[2] : text
}
export function Markdown(props: { text: string; class?: string }) {
  const marked = useMarked()
  const [html] = createResource(
    () => strip(props.text),
    async (markdown) => {
      return marked.parse(markdown)
    },
  )
  return (
    <div
      class={`min-w-0 max-w-full overflow-auto no-scrollbar text-14-regular text-text-base ${props.class ?? ""}`}
      innerHTML={html()}
    />
  )
}