@@ -5,7 +5,6 @@ import useSWR, { SWRResponse } from 'swr';
5
5
import { Md5 } from 'ts-md5' ;
6
6
7
7
// 缓存已验证的图表内容
8
- const mermaidCache = new Map < string , boolean > ( ) ;
9
8
const MD5_LENGTH_THRESHOLD = 10_000 ;
10
9
11
10
// 懒加载 mermaid 实例
@@ -15,61 +14,10 @@ const loadMermaid = () => {
15
14
} ;
16
15
const mermaidPromise = loadMermaid ( ) ;
17
16
18
- /**
19
- * 验证并处理 Mermaid 图表内容
20
- */
21
- export const useMermaid = ( id : string , content : string ) : SWRResponse < string , Error > => {
22
- // 用于存储最近一次有效的内容
23
- const [ validContent , setValidContent ] = useState < string > ( '' ) ;
24
-
25
- // 为长内容生成哈希键
26
- const cacheKey = useMemo ( ( ) : string => {
27
- const hash = content . length < MD5_LENGTH_THRESHOLD ? content : Md5 . hashStr ( content ) ;
28
- return `mermaid-${ id } -${ hash } ` ;
29
- } , [ content , id ] ) ;
30
-
31
- return useSWR (
32
- cacheKey ,
33
- async ( ) : Promise < string > => {
34
- // 检查缓存中是否已验证过
35
- if ( mermaidCache . has ( cacheKey ) ) return validContent ;
36
-
37
- try {
38
- const mermaidInstance = await mermaidPromise ;
39
- if ( ! mermaidInstance ) return content ;
40
-
41
- // 验证语法
42
- const isValid = await mermaidInstance . parse ( content ) ;
43
-
44
- if ( isValid ) {
45
- // 更新有效内容状态
46
- const { svg } = await mermaidInstance . render ( id , content ) ;
47
- setValidContent ( svg ) ;
48
- // 缓存验证结果
49
- mermaidCache . set ( cacheKey , true ) ;
50
- return svg ;
51
- } else {
52
- throw new Error ( 'Mermaid 语法无效' ) ;
53
- }
54
- } catch ( error ) {
55
- console . error ( 'Mermaid 解析错误:' , error ) ;
56
- // 返回最近一次有效的内容,或空字符串
57
- return validContent || '' ;
58
- }
59
- } ,
60
- {
61
- dedupingInterval : 3000 ,
62
- errorRetryCount : 2 ,
63
- revalidateOnFocus : false ,
64
- revalidateOnReconnect : false ,
65
- } ,
66
- ) ;
67
- } ;
68
-
69
17
/**
70
18
* 初始化 Mermaid 配置
71
19
*/
72
- export const useMermaidInit = ( ) : void => {
20
+ export const useMermaidInit = ( ) => {
73
21
const theme = useTheme ( ) ;
74
22
75
23
// 提取主题相关配置到 useMemo 中
@@ -120,3 +68,91 @@ export const useMermaidInit = (): void => {
120
68
initMermaid ( ) ;
121
69
} , [ mermaidConfig ] ) ;
122
70
} ;
71
+
72
+ /**
73
+ * 验证并处理 Mermaid 图表内容
74
+ */
75
+ export const useMermaid = ( id : string , content : string ) : SWRResponse < string , Error > => {
76
+ const theme = useTheme ( ) ;
77
+ // 用于存储最近一次有效的内容
78
+ const [ validContent , setValidContent ] = useState < string > ( '' ) ;
79
+
80
+ // 提取主题相关配置到 useMemo 中
81
+ const mermaidConfig : MermaidConfig = useMemo (
82
+ ( ) => ( {
83
+ fontFamily : theme . fontFamilyCode ,
84
+ gantt : {
85
+ useWidth : 1920 ,
86
+ } ,
87
+ securityLevel : 'loose' ,
88
+ startOnLoad : false ,
89
+ theme : theme . isDarkMode ? 'dark' : 'neutral' ,
90
+ themeVariables : {
91
+ errorBkgColor : theme . colorTextDescription ,
92
+ errorTextColor : theme . colorTextDescription ,
93
+ fontFamily : theme . fontFamily ,
94
+ fontSize : 14 ,
95
+ lineColor : theme . colorTextSecondary ,
96
+ mainBkg : theme . colorBgContainer ,
97
+ noteBkgColor : theme . colorInfoBg ,
98
+ noteTextColor : theme . colorInfoText ,
99
+ pie1 : theme . geekblue ,
100
+ pie2 : theme . colorWarning ,
101
+ pie3 : theme . colorSuccess ,
102
+ pie4 : theme . colorError ,
103
+ primaryBorderColor : theme . colorBorder ,
104
+ primaryColor : theme . colorBgContainer ,
105
+ primaryTextColor : theme . colorText ,
106
+ secondaryBorderColor : theme . colorInfoBorder ,
107
+ secondaryColor : theme . colorInfoBg ,
108
+ secondaryTextColor : theme . colorInfoText ,
109
+ tertiaryBorderColor : theme . colorSuccessBorder ,
110
+ tertiaryColor : theme . colorSuccessBg ,
111
+ tertiaryTextColor : theme . colorSuccessText ,
112
+ textColor : theme . colorText ,
113
+ } ,
114
+ } ) ,
115
+ [ theme . isDarkMode ] ,
116
+ ) ;
117
+
118
+ // 为长内容生成哈希键
119
+ const cacheKey = useMemo ( ( ) : string => {
120
+ const hash = content . length < MD5_LENGTH_THRESHOLD ? content : Md5 . hashStr ( content ) ;
121
+ return `${ id } -${ theme . isDarkMode ? 'd' : 'l' } -${ hash } ` ;
122
+ } , [ content , id , theme . isDarkMode ] ) ;
123
+
124
+ return useSWR (
125
+ cacheKey ,
126
+ async ( ) : Promise < string > => {
127
+ // 检查缓存中是否已验证过
128
+ try {
129
+ const mermaidInstance = await mermaidPromise ;
130
+ if ( ! mermaidInstance ) return content ;
131
+
132
+ // 验证语法
133
+ const isValid = await mermaidInstance . parse ( content ) ;
134
+
135
+ if ( isValid ) {
136
+ // 更新有效内容状态
137
+ mermaidInstance . initialize ( mermaidConfig ) ;
138
+ const { svg } = await mermaidInstance . render ( id , content ) ;
139
+ setValidContent ( svg ) ;
140
+ // 缓存验证结果
141
+ return svg ;
142
+ } else {
143
+ throw new Error ( 'Mermaid 语法无效' ) ;
144
+ }
145
+ } catch ( error ) {
146
+ console . error ( 'Mermaid 解析错误:' , error ) ;
147
+ // 返回最近一次有效的内容,或空字符串
148
+ return validContent || '' ;
149
+ }
150
+ } ,
151
+ {
152
+ dedupingInterval : 3000 ,
153
+ errorRetryCount : 2 ,
154
+ revalidateOnFocus : false ,
155
+ revalidateOnReconnect : false ,
156
+ } ,
157
+ ) ;
158
+ } ;
0 commit comments