From 9e43e027c5ac30812f4d5eefad395a1dbe046402 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Tue, 11 Jun 2024 18:36:49 +0200 Subject: [PATCH] Add color to simple icons --- Dockerfile | 1 + services/bookmark.services.go | 77 ++++++++++++++++++++++++++++------- services/bookmark.types.go | 20 ++++++--- 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 646eda7..9563b70 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,6 +42,7 @@ COPY assets/favicon ./assets/favicon COPY --from=logo /app/logo.txt . COPY --from=nodeBuilder /app/assets/css/style.css ./assets/css/style.css COPY --from=nodeBuilder /app/node_modules/simple-icons/icons ./node_modules/simple-icons/icons +COPY --from=nodeBuilder /app/node_modules/simple-icons/_data ./node_modules/simple-icons/_data COPY --from=goBuilder /app/views ./views COPY --from=goBuilder /app/components ./components COPY --from=goBuilder /app/godash . diff --git a/services/bookmark.services.go b/services/bookmark.services.go index bc50952..92dc37b 100644 --- a/services/bookmark.services.go +++ b/services/bookmark.services.go @@ -1,6 +1,7 @@ package services import ( + "encoding/json" "io" "log/slog" "os" @@ -11,6 +12,7 @@ import ( ) const simpleIconsFolder = "node_modules/simple-icons/icons/" +const simpleIconsInfo = "node_modules/simple-icons/_data/simple-icons.json" const storageFolder = "storage/" const iconsFolder = storageFolder + "icons/" const bookmarkFile = storageFolder + "bookmarks.yaml" @@ -23,9 +25,13 @@ const defaultConfig = `links: applications: - category: "Code" entries: - - name: "Github" - icon: "si/github.svg" - url: "https://github.com"` + - name: "GitHub" + icon: "si/github.svg" + ignore_color: true + url: "https://github.com" + - name: "Home Assistant" + icon: "si/homeassistant.svg" + url: "https://www.home-assistant.io/"` func init() { folders := []string{storageFolder, iconsFolder} @@ -42,6 +48,7 @@ func init() { func NewBookmarkService() *BookmarkService { bs := BookmarkService{} bs.parseBookmarks() + bs.parseIcons() return &bs } @@ -78,23 +85,40 @@ func (bs *BookmarkService) readBookmarksFile() []byte { } func (bs *BookmarkService) replaceIconString() { - for _, v := range bs.bookmarks.Applications { - for i, bookmark := range v.Entries { - rawHTML := "" - var data []byte - var err error + iconsByTitle := make(map[string]string) + for _, icon := range bs.SimpleIcons.Icons { + iconsByTitle[icon.Title] = icon.Hex + } + + for i, v := range bs.bookmarks.Applications { + for j, bookmark := range v.Entries { if filepath.Ext(bookmark.Icon) == ".svg" { + var data []byte + var err error if strings.HasPrefix(bookmark.Icon, "si/") { - data, err = os.ReadFile(simpleIconsFolder + strings.Replace(bookmark.Icon, "si/", "", 1)) + title := strings.Replace(bookmark.Icon, "si/", "", 1) + data, err = os.ReadFile(simpleIconsFolder + title) + if err != nil { + continue + } + color, ok := iconsByTitle[bookmark.Name] + if !(bookmark.IgnoreColor || !ok || color == "") { + data = []byte(insertColor(string(data), color)) + } + } else if strings.HasPrefix(bookmark.Icon, "di/") { + title := strings.Replace(bookmark.Icon, "di/", "", 1) + data, err = os.ReadFile(simpleIconsFolder + title) + if err != nil { + continue + } } else { data, err = os.ReadFile(iconsFolder + bookmark.Icon) + if err != nil { + continue + } } - if err != nil { - continue - } - rawHTML = string(data) + bs.bookmarks.Applications[i].Entries[j].Icon = string(data) } - v.Entries[i].Icon = rawHTML } } } @@ -106,5 +130,30 @@ func (bs *BookmarkService) parseBookmarks() { slog.Error(err.Error()) return } +} + +func (bs *BookmarkService) parseIcons() { + file, err := os.Open(simpleIconsInfo) + if err != nil { + slog.Error(err.Error()) + } + defer file.Close() + byteValue, err := io.ReadAll(file) + if err != nil { + slog.Error(err.Error()) + } + err = json.Unmarshal(byteValue, &bs.SimpleIcons) + if err != nil { + slog.Error(err.Error()) + return + } bs.replaceIconString() } + +func insertColor(svg, color string) string { + parts := strings.SplitN(svg, "