From e0b376f081774ceb59cdf7e7cd0040084ec31a30 Mon Sep 17 00:00:00 2001 From: Hawli <126889064+hawl1@users.noreply.github.com> Date: Sat, 11 May 2024 16:06:11 +0000 Subject: [PATCH 1/2] Make a more healthly way to access IconsJSON --- api/icons.go | 7 +++++++ api/index.go | 2 +- build.go | 17 ++++------------- 3 files changed, 12 insertions(+), 14 deletions(-) create mode 100644 api/icons.go diff --git a/api/icons.go b/api/icons.go new file mode 100644 index 0000000..40897d0 --- /dev/null +++ b/api/icons.go @@ -0,0 +1,7 @@ +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 f27ae9c..5560fac 100644 --- a/api/index.go +++ b/api/index.go @@ -232,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 e38e035..2ba49a9 100644 --- a/build.go +++ b/build.go @@ -8,59 +8,50 @@ 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) } - // Read content of api/index.go - indexFile, err := os.ReadFile("./api/index.go") + indexFile, err := os.ReadFile("./api/icons.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[17] = fmt.Sprintf("\n\tvar iconsJSON = `%s`\n", iconsJSON) + lines[6] = fmt.Sprintf("\n\tvar iconsJSON string = `%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/index.go", []byte(modifiedContent), 0644) + err = os.WriteFile("./api/icons.go", []byte(modifiedContent), 0644) if err != nil { panic(err) } - fmt.Println("Modified content saved to api/index.go") + fmt.Println("Modified content saved to api/icons.go") } From 602223d39e0523ff8e889088a1fb53f9622d7513 Mon Sep 17 00:00:00 2001 From: Hawli <126889064+hawl1@users.noreply.github.com> Date: Sat, 11 May 2024 16:06:34 +0000 Subject: [PATCH 2/2] Remove lines for readability --- api/index.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/api/index.go b/api/index.go index 5560fac..d8a7142 100644 --- a/api/index.go +++ b/api/index.go @@ -14,9 +14,6 @@ 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",