|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | 3 | import type { ReactNode } from 'react'; |
4 | | -import { |
5 | | - CodeHighlightAdapterProvider, |
6 | | - stripShikiCodeBlocks, |
7 | | -} from '@mantine/code-highlight'; |
| 4 | +import { CodeHighlightAdapterProvider, stripShikiCodeBlocks } from '@mantine/code-highlight'; |
8 | 5 |
|
9 | | -import { |
10 | | - transformerNotationDiff, |
11 | | - transformerNotationHighlight, |
12 | | -} from '@shikijs/transformers'; |
| 6 | +import { |
| 7 | + transformerNotationDiff, |
| 8 | + transformerNotationHighlight, |
| 9 | + transformerNotationFocus, |
| 10 | + transformerMetaHighlight, |
| 11 | +} from '@shikijs/transformers' |
13 | 12 |
|
14 | 13 | import { darkTheme, lightTheme } from './code-highlight-themes'; |
15 | 14 |
|
| 15 | +const preloadedLanguages = [ |
| 16 | + 'tsx', 'scss', 'html', 'bash', |
| 17 | + 'json', 'javascript', 'typescript', 'css', |
| 18 | + 'markdown', 'go', 'python', 'java', 'kotlin', |
| 19 | + 'rust', 'php', 'ruby', 'swift', 'dart', 'scala', |
| 20 | +]; |
| 21 | + |
16 | 22 | async function loadShiki() { |
17 | 23 | const { createHighlighter } = await import('shiki'); |
18 | | - const shiki = await createHighlighter({ |
19 | | - langs: ['tsx', 'scss', 'html', 'bash', 'json'], |
| 24 | + const highlighter = await createHighlighter({ |
| 25 | + langs: preloadedLanguages, |
20 | 26 | themes: [], |
21 | 27 | }); |
22 | 28 |
|
23 | | - return shiki; |
| 29 | + return highlighter; |
24 | 30 | } |
25 | 31 |
|
26 | 32 | const customShikiAdapter: any = { |
27 | 33 | loadContext: loadShiki, |
28 | 34 |
|
29 | 35 | getHighlighter: (ctx: any) => { |
30 | 36 | if (!ctx) { |
31 | | - return ({ code }: { code: string }) => ({ |
32 | | - highlightedCode: code, |
33 | | - isHighlighted: false, |
34 | | - }); |
| 37 | + return ({ code }: { code: string }) => ({highlightedCode: code, isHighlighted: false }); |
35 | 38 | } |
36 | 39 |
|
37 | | - return ({ |
38 | | - code, |
39 | | - language, |
40 | | - colorScheme, |
41 | | - }: { |
42 | | - code: string; |
43 | | - language: string; |
44 | | - colorScheme: 'light' | 'dark'; |
45 | | - }) => ({ |
46 | | - isHighlighted: true, |
47 | | - highlightedCode: stripShikiCodeBlocks( |
48 | | - ctx.codeToHtml(code, { |
49 | | - lang: language, |
50 | | - theme: (colorScheme === 'light' ? lightTheme : darkTheme) as any, |
51 | | - transformers: [transformerNotationDiff(), transformerNotationHighlight()], |
52 | | - }) |
53 | | - ), |
54 | | - }); |
| 40 | + return ({ code, language, colorScheme }) => { |
| 41 | + |
| 42 | + // if the language is not loaded don't highlight it |
| 43 | + if (!ctx.getLoadedLanguages().includes(language)) { |
| 44 | + return {highlightedCode: code, isHighlighted: false }; |
| 45 | + } |
| 46 | + |
| 47 | + return { |
| 48 | + isHighlighted: true, |
| 49 | + highlightedCode: stripShikiCodeBlocks( |
| 50 | + ctx.codeToHtml(code, { |
| 51 | + lang: language, |
| 52 | + theme: (colorScheme === 'light' ? lightTheme : darkTheme) as any, |
| 53 | + transformers: [ |
| 54 | + transformerNotationDiff(), |
| 55 | + transformerNotationHighlight(), |
| 56 | + transformerNotationFocus(), |
| 57 | + transformerMetaHighlight(), |
| 58 | + ], |
| 59 | + }) |
| 60 | + ), |
| 61 | + }; |
| 62 | + }; |
55 | 63 | }, |
56 | 64 | }; |
57 | 65 |
|
|
0 commit comments