summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/marked.tsx
blob: 550a0456aaf90ad7f7dd672515ebfa75cec28dfe (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
31
32
33
34
35
36
37
38
39
40
41
42
43
import { createContext, useContext, type ParentProps } from "solid-js"
import { useShiki } from "@/context"
import { marked } from "marked"
import markedShiki from "marked-shiki"
import { bundledLanguages, type BundledLanguage } from "shiki"

function init(highlighter: ReturnType<typeof useShiki>) {
  return marked.use(
    markedShiki({
      async highlight(code, lang) {
        if (!(lang in bundledLanguages)) {
          lang = "text"
        }
        if (!highlighter.getLoadedLanguages().includes(lang)) {
          await highlighter.loadLanguage(lang as BundledLanguage)
        }
        return highlighter.codeToHtml(code, {
          lang: lang || "text",
          theme: "opencode",
          tabindex: false,
        })
      },
    }),
  )
}

type MarkedContext = ReturnType<typeof init>

const ctx = createContext<MarkedContext>()

export function MarkedProvider(props: ParentProps) {
  const highlighter = useShiki()
  const value = init(highlighter)
  return <ctx.Provider value={value}>{props.children}</ctx.Provider>
}

export function useMarked() {
  const value = useContext(ctx)
  if (!value) {
    throw new Error("useMarked must be used within a MarkedProvider")
  }
  return value
}