markdown.tsx 672 B

1234567891011121314151617181920212223
  1. import { useMarked } from "@/context/marked"
  2. import { createResource } from "solid-js"
  3. function strip(text: string): string {
  4. const wrappedRe = /^\s*<([A-Za-z]\w*)>\s*([\s\S]*?)\s*<\/\1>\s*$/
  5. const match = text.match(wrappedRe)
  6. return match ? match[2] : text
  7. }
  8. export function Markdown(props: { text: string; class?: string }) {
  9. const marked = useMarked()
  10. const [html] = createResource(
  11. () => strip(props.text),
  12. async (markdown) => {
  13. return marked.parse(markdown)
  14. },
  15. )
  16. return (
  17. <div
  18. class={`min-w-0 max-w-full overflow-auto no-scrollbar text-14-regular text-text-base ${props.class ?? ""}`}
  19. innerHTML={html()}
  20. />
  21. )
  22. }