File size: 1,532 Bytes
03d9a71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { type Highlighter, type HighlighterOptions, getHighlighter } from 'shiki-es';
import { shallow } from 'zustand/shallow';
import { createWithEqualityFn } from 'zustand/traditional';

export const languageMap = [] as const;

/**
 * @title 代码高亮的存储对象
 */
interface Store {
  /**
   * @title Convert code to HTML string
   * @param text - The code text
   * @param language - The language of the code
   * @param isDarkMode - Whether it's in dark mode or not
   * @returns HTML string
   */
  codeToHtml: (text: string, language: string, isDarkMode: boolean) => string;
  /**
   * @title Highlighter object
   */
  highlighter?: Highlighter;
  /**
   * @title Initialize the highlighter object
   * @returns Initialization promise object
   */
  initHighlighter: (options?: HighlighterOptions) => Promise<void>;
}

export const useHighlight = createWithEqualityFn<Store>(
  (set, get) => ({
    codeToHtml: (text, language, isDarkMode) => {
      const { highlighter } = get();

      if (!highlighter) return '';

      try {
        return highlighter?.codeToHtml(text, {
          lang: language,
          theme: isDarkMode ? 'dark' : 'light',
        });
      } catch {
        return text;
      }
    },
    highlighter: undefined,

    initHighlighter: async(options) => {
      if (!get().highlighter) {
        const highlighter = await getHighlighter({
          langs: options?.langs,
          themes: options?.themes,
        });

        set({ highlighter });
      }
    },
  }),
  shallow,
);