Add color to simple icons

This commit is contained in:
Florian Hoss 2024-06-11 18:36:49 +02:00
parent 985e6e930e
commit 9e43e027c5
No known key found for this signature in database
GPG key ID: 0638DC11BFD36662
3 changed files with 79 additions and 19 deletions

View file

@ -42,6 +42,7 @@ COPY assets/favicon ./assets/favicon
COPY --from=logo /app/logo.txt . COPY --from=logo /app/logo.txt .
COPY --from=nodeBuilder /app/assets/css/style.css ./assets/css/style.css 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/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/views ./views
COPY --from=goBuilder /app/components ./components COPY --from=goBuilder /app/components ./components
COPY --from=goBuilder /app/godash . COPY --from=goBuilder /app/godash .

View file

@ -1,6 +1,7 @@
package services package services
import ( import (
"encoding/json"
"io" "io"
"log/slog" "log/slog"
"os" "os"
@ -11,6 +12,7 @@ import (
) )
const simpleIconsFolder = "node_modules/simple-icons/icons/" const simpleIconsFolder = "node_modules/simple-icons/icons/"
const simpleIconsInfo = "node_modules/simple-icons/_data/simple-icons.json"
const storageFolder = "storage/" const storageFolder = "storage/"
const iconsFolder = storageFolder + "icons/" const iconsFolder = storageFolder + "icons/"
const bookmarkFile = storageFolder + "bookmarks.yaml" const bookmarkFile = storageFolder + "bookmarks.yaml"
@ -23,9 +25,13 @@ const defaultConfig = `links:
applications: applications:
- category: "Code" - category: "Code"
entries: entries:
- name: "Github" - name: "GitHub"
icon: "si/github.svg" icon: "si/github.svg"
url: "https://github.com"` ignore_color: true
url: "https://github.com"
- name: "Home Assistant"
icon: "si/homeassistant.svg"
url: "https://www.home-assistant.io/"`
func init() { func init() {
folders := []string{storageFolder, iconsFolder} folders := []string{storageFolder, iconsFolder}
@ -42,6 +48,7 @@ func init() {
func NewBookmarkService() *BookmarkService { func NewBookmarkService() *BookmarkService {
bs := BookmarkService{} bs := BookmarkService{}
bs.parseBookmarks() bs.parseBookmarks()
bs.parseIcons()
return &bs return &bs
} }
@ -78,23 +85,40 @@ func (bs *BookmarkService) readBookmarksFile() []byte {
} }
func (bs *BookmarkService) replaceIconString() { func (bs *BookmarkService) replaceIconString() {
for _, v := range bs.bookmarks.Applications { iconsByTitle := make(map[string]string)
for i, bookmark := range v.Entries { for _, icon := range bs.SimpleIcons.Icons {
rawHTML := "" 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 data []byte
var err error var err error
if filepath.Ext(bookmark.Icon) == ".svg" {
if strings.HasPrefix(bookmark.Icon, "si/") { if strings.HasPrefix(bookmark.Icon, "si/") {
data, err = os.ReadFile(simpleIconsFolder + strings.Replace(bookmark.Icon, "si/", "", 1)) title := strings.Replace(bookmark.Icon, "si/", "", 1)
} else { data, err = os.ReadFile(simpleIconsFolder + title)
data, err = os.ReadFile(iconsFolder + bookmark.Icon)
}
if err != nil { if err != nil {
continue continue
} }
rawHTML = string(data) 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
}
}
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()) slog.Error(err.Error())
return 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() bs.replaceIconString()
} }
func insertColor(svg, color string) string {
parts := strings.SplitN(svg, "<svg", 2)
if len(parts) != 2 {
return svg
}
return parts[0] + "<svg " + `fill="#` + color + `" ` + parts[1]
}

View file

@ -2,6 +2,7 @@ package services
type BookmarkService struct { type BookmarkService struct {
bookmarks Bookmarks bookmarks Bookmarks
SimpleIcons SimpleIcons
} }
type Bookmarks struct { type Bookmarks struct {
@ -23,6 +24,15 @@ type Link struct {
type Application struct { type Application struct {
Name string Name string
Icon string Icon string
Background string IgnoreColor bool `yaml:"ignore_color"`
URL string URL string
} }
type SimpleIcons struct {
Icons []SimpleIcon `json:"icons"`
}
type SimpleIcon struct {
Title string `json:"title"`
Hex string `json:"hex"`
}