11import { Router } from "itty-router" ;
2- import {
3- ActivityExtension ,
4- AnimationExtension ,
5- Config ,
6- FontExtension ,
7- Generator ,
8- RemoteStyleExtension ,
9- ThemeExtension ,
10- } from "../core" ;
2+ import { Config , Generator } from "../core" ;
113import { Cache } from "./cache" ;
124import demo from "./demo" ;
135import Header from "./headers" ;
14- import { booleanize , normalize } from "./utils " ;
6+ import { sanitize } from "./sanitize " ;
157
168const router = Router ( ) ;
179
@@ -22,102 +14,6 @@ router.get("/favicon.ico", async () => {
2214 ) ;
2315} ) ;
2416
25- function sanitize ( config : Record < string , string > ) : Config {
26- const sanitized : Config = {
27- username : "jacoblincool" ,
28- site : "us" ,
29- width : 500 ,
30- height : 200 ,
31- css : [ ] ,
32- extensions : [ FontExtension , AnimationExtension , ThemeExtension ] ,
33- font : "baloo_2" ,
34- animation : true ,
35- theme : { light : "light" , dark : "dark" } ,
36- cache : 60 ,
37- } ;
38-
39- if ( ! config . username ?. trim ( ) ) {
40- throw new Error ( "Missing username" ) ;
41- }
42- sanitized . username = config . username . trim ( ) ;
43-
44- // #region backward compatibility
45- if ( config . border_radius ) {
46- const size = parseFloat ( config . border_radius ) ?? 1 ;
47- sanitized . css . push ( `#background{rx:${ size } px}` ) ;
48- }
49-
50- if ( config . show_rank && booleanize ( config . show_rank ) === false ) {
51- sanitized . css . push ( `#ranking{display:none}` ) ;
52- }
53- // #endregion
54-
55- if ( config . site ?. trim ( ) . toLowerCase ( ) === "cn" ) {
56- sanitized . site = "cn" ;
57- }
58-
59- if ( config . width ?. trim ( ) ) {
60- sanitized . width = parseInt ( config . width . trim ( ) ) ?? 500 ;
61- }
62-
63- if ( config . height ?. trim ( ) ) {
64- sanitized . height = parseInt ( config . height . trim ( ) ) ?? 200 ;
65- }
66-
67- if ( config . theme ?. trim ( ) ) {
68- const themes = config . theme . trim ( ) . split ( "," ) ;
69- if ( themes . length === 1 || themes [ 1 ] === "" ) {
70- sanitized . theme = themes [ 0 ] . trim ( ) ;
71- } else {
72- sanitized . theme = { light : themes [ 0 ] . trim ( ) , dark : themes [ 1 ] . trim ( ) } ;
73- }
74- }
75-
76- if ( config . font ?. trim ( ) ) {
77- sanitized . font = normalize ( config . font . trim ( ) ) ;
78- }
79-
80- if ( config . animation ?. trim ( ) ) {
81- sanitized . animation = booleanize ( config . animation . trim ( ) ) ;
82- }
83-
84- if ( config . ext === "activity" || config . extension === "activity" ) {
85- sanitized . extensions . push ( ActivityExtension ) ;
86- }
87-
88- if ( config . border ) {
89- const size = parseFloat ( config . border ) ?? 1 ;
90- sanitized . extensions . push ( ( ) => ( generator , data , body , styles ) => {
91- styles . push (
92- `#background{stroke-width:${ size } ;width:${ generator . config . width - size } px;height:${
93- generator . config . height - size
94- } px;transform:translate(${ size / 2 } px,${ size / 2 } px)}`,
95- ) ;
96- } ) ;
97- }
98-
99- if ( config . radius ) {
100- const size = parseFloat ( config . radius ) ?? 1 ;
101- sanitized . css . push ( `#background{rx:${ size } px}` ) ;
102- }
103-
104- if ( config . hide ) {
105- const targets = config . hide . split ( "," ) . map ( ( x ) => x . trim ( ) ) ;
106- sanitized . css . push ( ...targets . map ( ( x ) => `#${ x } {display:none}` ) ) ;
107- }
108-
109- if ( config . sheets ) {
110- sanitized . sheets = config . sheets . split ( "," ) . map ( ( x ) => x . trim ( ) ) ;
111- sanitized . extensions . push ( RemoteStyleExtension ) ;
112- }
113-
114- if ( config . cache && parseInt ( config . cache ) >= 0 && parseInt ( config . cache ) <= 60 * 60 * 24 * 7 ) {
115- sanitized . cache = parseInt ( config . cache ) ;
116- }
117-
118- return sanitized ;
119- }
120-
12117async function generate ( config : Record < string , string > ) : Promise < Response > {
12218 let sanitized : Config ;
12319 try {
0 commit comments