UNPKG

@mantine/code-highlight

Version:

Code highlight with Mantine theme

1 lines 2.35 kB
{"version":3,"file":"CodeHighlightProvider.mjs","names":[],"sources":["../../src/CodeHighlightProvider/CodeHighlightProvider.tsx"],"sourcesContent":["import { createContext, use, useEffect, useMemo, useState } from 'react';\nimport { plainTextAdapter } from './adapters/plain-text-adapter';\n\ninterface HighlighterInput {\n colorScheme: 'light' | 'dark' | (string & {});\n code: string;\n language?: string;\n}\n\ntype Highlighter = (input: HighlighterInput) => {\n /** Highlighted code (html markup) */\n highlightedCode: string;\n\n /** `true` if the code is represented with html string, `false` for plain text string */\n isHighlighted: boolean;\n\n /** Props to pass down to `<code>` tag */\n codeElementProps?: Record<string, any>;\n};\n\nexport interface CodeHighlightAdapter {\n loadContext?: () => Promise<any>;\n getHighlighter: (ctx: any) => Highlighter;\n}\n\ninterface CodeHighlightProviderContext {\n adapter: CodeHighlightAdapter;\n highlight: Highlighter;\n}\n\nexport const CodeHighlightContext = createContext<CodeHighlightProviderContext>({\n adapter: plainTextAdapter,\n highlight: plainTextAdapter.getHighlighter(null),\n});\n\nexport interface CodeHighlightAdapterProviderProps {\n adapter: CodeHighlightAdapter;\n children: React.ReactNode;\n}\n\nexport function CodeHighlightAdapterProvider({\n adapter,\n children,\n}: CodeHighlightAdapterProviderProps) {\n const [ctx, setCtx] = useState<any>(null);\n const highlight = useMemo(() => adapter.getHighlighter(ctx), [adapter, ctx]);\n\n useEffect(() => {\n if (adapter.loadContext) {\n adapter.loadContext().then(setCtx);\n }\n }, [adapter]);\n\n return <CodeHighlightContext value={{ adapter, highlight }}>{children}</CodeHighlightContext>;\n}\n\nexport function useHighlight() {\n const ctx = use(CodeHighlightContext);\n return ctx?.highlight || plainTextAdapter.getHighlighter(null);\n}\n"],"mappings":";;;;;AA8BA,MAAa,uBAAuB,cAA4C;CAC9E,SAAS;CACT,WAAW,iBAAiB,eAAe,IAAI;AACjD,CAAC;AAOD,SAAgB,6BAA6B,EAC3C,SACA,YACoC;CACpC,MAAM,CAAC,KAAK,UAAU,SAAc,IAAI;CACxC,MAAM,YAAY,cAAc,QAAQ,eAAe,GAAG,GAAG,CAAC,SAAS,GAAG,CAAC;CAE3E,gBAAgB;EACd,IAAI,QAAQ,aACV,QAAQ,YAAY,EAAE,KAAK,MAAM;CAErC,GAAG,CAAC,OAAO,CAAC;CAEZ,OAAO,oBAAC,sBAAD;EAAsB,OAAO;GAAE;GAAS;EAAU;EAAI;CAA+B,CAAA;AAC9F;AAEA,SAAgB,eAAe;CAE7B,OADY,IAAI,oBACP,GAAG,aAAa,iBAAiB,eAAe,IAAI;AAC/D"}