From ff87c7f814c5c95c40abfad6bfa204d006c864a3 Mon Sep 17 00:00:00 2001 From: Baptiste Zahnow <75901978+LelouchFR@users.noreply.github.com> Date: Sat, 11 May 2024 19:00:09 +0200 Subject: [PATCH] Revert "fix: implement more healthly way to access iconsjson string (#17)" This reverts commit 2d3106632fb67c9ccc1bbc7d9f22d433614161e0. --- api/icons.go | 7 ------- api/index.go | 5 ++++- build.go | 17 +++++++++++++---- 3 files changed, 17 insertions(+), 12 deletions(-) delete mode 100644 api/icons.go diff --git a/api/icons.go b/api/icons.go deleted file mode 100644 index 40897d0..0000000 --- a/api/icons.go +++ /dev/null @@ -1,7 +0,0 @@ -package handler - -// Hey there! -// This file is for build.go to add the icons file as json, since it can make issue on index.go if we do a silly change -// So please dont touch this file unless you know what you're doing. - -var IconsJSON string = `{}` diff --git a/api/index.go b/api/index.go index d8a7142..f27ae9c 100644 --- a/api/index.go +++ b/api/index.go @@ -14,6 +14,9 @@ import ( var icons map[string]string = make(map[string]string) var iconNameList []string var themedIcons []string + + + var shortNames = map[string]string{ "js": "javascript", "ts": "typescript", @@ -229,7 +232,7 @@ func init() { } func Handler(w http.ResponseWriter, r *http.Request) { - decoder := json.NewDecoder(strings.NewReader(IconsJSON)) + decoder := json.NewDecoder(strings.NewReader(iconsJSON)) if err := decoder.Decode(&icons); err != nil { panic(err) } diff --git a/build.go b/build.go index 2ba49a9..e38e035 100644 --- a/build.go +++ b/build.go @@ -8,50 +8,59 @@ import ( ) func main() { + // Read directory contents iconsDir, err := os.ReadDir("./assets") if err != nil { panic(err) } + // Initialize map for icons icons := make(map[string]string) + // Iterate through directory contents for _, fileInfo := range iconsDir { if !fileInfo.IsDir() { + // Read file contents data, err := os.ReadFile("./assets/" + fileInfo.Name()) if err != nil { panic(err) } + // Add file content to map name := strings.TrimSuffix(strings.ToLower(fileInfo.Name()), ".svg") icons[name] = string(data) } } + // Convert icons to JSON iconsJSON, err := json.Marshal(icons) if err != nil { panic(err) } - indexFile, err := os.ReadFile("./api/icons.go") + // Read content of api/index.go + indexFile, err := os.ReadFile("./api/index.go") if err != nil { panic(err) } + // Convert content to string indexContent := string(indexFile) + // Split the content by newlines lines := strings.Split(indexContent, "\n") // Insert the iconsJSON into line 18 - lines[6] = fmt.Sprintf("\n\tvar iconsJSON string = `%s`\n", iconsJSON) + lines[17] = fmt.Sprintf("\n\tvar iconsJSON = `%s`\n", iconsJSON) // Join the lines back into a single string modifiedContent := strings.Join(lines, "\n") // Write the modified content back to the file - err = os.WriteFile("./api/icons.go", []byte(modifiedContent), 0644) + err = os.WriteFile("./api/index.go", []byte(modifiedContent), 0644) if err != nil { panic(err) } - fmt.Println("Modified content saved to api/icons.go") + fmt.Println("Modified content saved to api/index.go") }