@@ -10,73 +10,81 @@ import {
1010} from "leetcode-card" ;
1111import { booleanize , normalize } from "./utils" ;
1212
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+ }
2625
27- if ( ! config . username ?. trim ( ) ) {
28- throw new Error ( "Missing username" ) ;
26+ if ( config . sheets ) {
27+ extensions . push ( RemoteStyleExtension ) ;
2928 }
30- sanitized . username = config . username . trim ( ) ;
3129
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)
3337 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}` ) ;
3639 }
3740
41+ // Handle show_rank (backward compatibility)
3842 if ( config . show_rank && booleanize ( config . show_rank ) === false ) {
39- sanitized . css . push ( `#ranking{display:none}` ) ;
43+ css . push ( `#ranking{display:none}` ) ;
4044 }
41- // #endregion
4245
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}` ) ;
4949 }
5050
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}` ) ) ;
5355 }
5456
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+ }
6359
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" ) ;
6663 }
6764
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+ } ;
7177
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 ( ) } ;
7885 }
7986
87+ // Handle border
8088 if ( config . border ) {
8189 const size = parseFloat ( config . border ) ?? 1 ;
8290 sanitized . extensions . push ( ( ) => ( generator , data , body , styles ) => {
@@ -88,23 +96,20 @@ export function sanitize(config: Record<string, string>): Config {
8896 } ) ;
8997 }
9098
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 ) ;
100101
102+ // Handle remote style sheets
101103 if ( config . sheets ) {
102104 sanitized . sheets = config . sheets . split ( "," ) . map ( ( x ) => x . trim ( ) ) ;
103- sanitized . extensions . push ( RemoteStyleExtension ) ;
104105 }
105106
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+ }
108113 }
109114
110115 return sanitized ;
0 commit comments