@@ -10,73 +10,81 @@ import {
10
10
} from "leetcode-card" ;
11
11
import { booleanize , normalize } from "./utils" ;
12
12
13
- export function sanitize ( config : Record < string , string > ) : Config {
14
- const sanitized : Config = {
15
- username : "jacoblincool" ,
16
- site : "us" ,
17
- width : 500 ,
18
- height : 200 ,
19
- css : [ ] ,
20
- extensions : [ FontExtension , AnimationExtension , ThemeExtension ] ,
21
- font : "baloo_2" ,
22
- animation : true ,
23
- theme : { light : "light" , dark : "dark" } ,
24
- cache : 60 ,
25
- } ;
13
+ // Helper functions to reduce complexity
14
+ function handleExtension ( config : Record < string , string > ) : Config [ "extensions" ] {
15
+ const extensions = [ FontExtension , AnimationExtension , ThemeExtension ] ;
16
+
17
+ const extName = config . ext || config . extension ;
18
+ if ( extName === "activity" ) {
19
+ extensions . push ( ActivityExtension ) ;
20
+ } else if ( extName === "contest" ) {
21
+ extensions . push ( ContestExtension ) ;
22
+ } else if ( extName === "heatmap" ) {
23
+ extensions . push ( HeatmapExtension ) ;
24
+ }
26
25
27
- if ( ! config . username ?. trim ( ) ) {
28
- throw new Error ( "Missing username" ) ;
26
+ if ( config . sheets ) {
27
+ extensions . push ( RemoteStyleExtension ) ;
29
28
}
30
- sanitized . username = config . username . trim ( ) ;
31
29
32
- // #region backward compatibility
30
+ return extensions ;
31
+ }
32
+
33
+ function handleCssRules ( config : Record < string , string > ) : string [ ] {
34
+ const css : string [ ] = [ ] ;
35
+
36
+ // Handle border radius (backward compatibility)
33
37
if ( config . border_radius ) {
34
- const size = parseFloat ( config . border_radius ) ?? 1 ;
35
- sanitized . css . push ( `#background{rx:${ size } px}` ) ;
38
+ css . push ( `#background{rx:${ parseFloat ( config . border_radius ) ?? 1 } px}` ) ;
36
39
}
37
40
41
+ // Handle show_rank (backward compatibility)
38
42
if ( config . show_rank && booleanize ( config . show_rank ) === false ) {
39
- sanitized . css . push ( `#ranking{display:none}` ) ;
43
+ css . push ( `#ranking{display:none}` ) ;
40
44
}
41
- // #endregion
42
45
43
- if ( config . site ?. trim ( ) . toLowerCase ( ) === "cn" ) {
44
- sanitized . site = "cn" ;
45
- }
46
-
47
- if ( config . width ?. trim ( ) ) {
48
- sanitized . width = parseInt ( config . width . trim ( ) ) ?? 500 ;
46
+ // Handle radius
47
+ if ( config . radius ) {
48
+ css . push ( `#background{rx:${ parseFloat ( config . radius ) ?? 4 } px}` ) ;
49
49
}
50
50
51
- if ( config . height ?. trim ( ) ) {
52
- sanitized . height = parseInt ( config . height . trim ( ) ) ?? 200 ;
51
+ // Handle hide elements
52
+ if ( config . hide ) {
53
+ const targets = config . hide . split ( "," ) . map ( ( x ) => x . trim ( ) ) ;
54
+ css . push ( ...targets . map ( ( x ) => `#${ x } {display:none}` ) ) ;
53
55
}
54
56
55
- if ( config . theme ?. trim ( ) ) {
56
- const themes = config . theme . trim ( ) . split ( "," ) ;
57
- if ( themes . length === 1 || themes [ 1 ] === "" ) {
58
- sanitized . theme = themes [ 0 ] . trim ( ) ;
59
- } else {
60
- sanitized . theme = { light : themes [ 0 ] . trim ( ) , dark : themes [ 1 ] . trim ( ) } ;
61
- }
62
- }
57
+ return css ;
58
+ }
63
59
64
- if ( config . font ?. trim ( ) ) {
65
- sanitized . font = normalize ( config . font . trim ( ) ) ;
60
+ export function sanitize ( config : Record < string , string > ) : Config {
61
+ if ( ! config . username ?. trim ( ) ) {
62
+ throw new Error ( "Missing username" ) ;
66
63
}
67
64
68
- if ( config . animation ?. trim ( ) ) {
69
- sanitized . animation = booleanize ( config . animation . trim ( ) ) ;
70
- }
65
+ const sanitized : Config = {
66
+ username : config . username . trim ( ) ,
67
+ site : config . site ?. trim ( ) . toLowerCase ( ) === "cn" ? "cn" : "us" ,
68
+ width : parseInt ( config . width ?. trim ( ) ) || 500 ,
69
+ height : parseInt ( config . height ?. trim ( ) ) || 200 ,
70
+ css : [ ] ,
71
+ extensions : handleExtension ( config ) ,
72
+ font : normalize ( config . font ?. trim ( ) ) || "baloo_2" ,
73
+ animation : config . animation ? booleanize ( config . animation . trim ( ) ) : true ,
74
+ theme : { light : "light" , dark : "dark" } ,
75
+ cache : 60 ,
76
+ } ;
71
77
72
- if ( config . ext === "activity" || config . extension === "activity" ) {
73
- sanitized . extensions . push ( ActivityExtension ) ;
74
- } else if ( config . ext === "contest" || config . extension === "contest" ) {
75
- sanitized . extensions . push ( ContestExtension ) ;
76
- } else if ( config . ext === "heatmap" || config . extension === "heatmap" ) {
77
- sanitized . extensions . push ( HeatmapExtension ) ;
78
+ // Handle theme
79
+ if ( config . theme ?. trim ( ) ) {
80
+ const themes = config . theme . trim ( ) . split ( "," ) ;
81
+ sanitized . theme =
82
+ themes . length === 1 || themes [ 1 ] === ""
83
+ ? themes [ 0 ] . trim ( )
84
+ : { light : themes [ 0 ] . trim ( ) , dark : themes [ 1 ] . trim ( ) } ;
78
85
}
79
86
87
+ // Handle border
80
88
if ( config . border ) {
81
89
const size = parseFloat ( config . border ) ?? 1 ;
82
90
sanitized . extensions . push ( ( ) => ( generator , data , body , styles ) => {
@@ -88,23 +96,20 @@ export function sanitize(config: Record<string, string>): Config {
88
96
} ) ;
89
97
}
90
98
91
- if ( config . radius ) {
92
- const size = parseFloat ( config . radius ) ?? 4 ;
93
- sanitized . css . push ( `#background{rx:${ size } px}` ) ;
94
- }
95
-
96
- if ( config . hide ) {
97
- const targets = config . hide . split ( "," ) . map ( ( x ) => x . trim ( ) ) ;
98
- sanitized . css . push ( ...targets . map ( ( x ) => `#${ x } {display:none}` ) ) ;
99
- }
99
+ // Handle CSS rules
100
+ sanitized . css = handleCssRules ( config ) ;
100
101
102
+ // Handle remote style sheets
101
103
if ( config . sheets ) {
102
104
sanitized . sheets = config . sheets . split ( "," ) . map ( ( x ) => x . trim ( ) ) ;
103
- sanitized . extensions . push ( RemoteStyleExtension ) ;
104
105
}
105
106
106
- if ( config . cache && parseInt ( config . cache ) >= 0 && parseInt ( config . cache ) <= 60 * 60 * 24 * 7 ) {
107
- sanitized . cache = parseInt ( config . cache ) ;
107
+ // Handle cache
108
+ if ( config . cache ) {
109
+ const cacheValue = parseInt ( config . cache ) ;
110
+ if ( cacheValue >= 0 && cacheValue <= 60 * 60 * 24 * 7 ) {
111
+ sanitized . cache = cacheValue ;
112
+ }
108
113
}
109
114
110
115
return sanitized ;
0 commit comments