Render svg icons directly

This commit is contained in:
Florian Hoss 2024-06-11 11:27:17 +02:00
parent 1291a5f231
commit 1fffc6f18b
No known key found for this signature in database
GPG key ID: 0638DC11BFD36662
3 changed files with 24 additions and 33 deletions

View file

@ -2,33 +2,13 @@ package components
import ( import (
"gitlab.unjx.de/flohoss/godash/services" "gitlab.unjx.de/flohoss/godash/services"
"html/template"
) )
var ImageTemplate = template.Must(template.New("bar").Parse("<div class=\"{{ .Background }}img rounded w-8 h-8 bg-contain bg-center bg-origin-content bg-no-repeat opacity-90\" style=\"background-image: url({{ .Icon }})\"></div>"))
type Image struct {
Background string
Icon string
}
func backgroundColor(config string) string {
result := "p-[0.05rem] "
switch config {
case "dark":
return result + "bg-black "
case "light":
return result + "bg-white "
case "base":
return result + "bg-base-300 "
default:
return ""
}
}
templ Application(application services.Application) { templ Application(application services.Application) {
<a href={ templ.URL(application.URL) } class="flex items-center hover-effect"> <a href={ templ.URL(application.URL) } class="flex items-center hover-effect">
@templ.FromGoHTML(ImageTemplate, Image{Background: backgroundColor(application.Background), Icon: application.Icon}) <div class="w-8 h-8 dark:text-white fill-current">
@templ.Raw(application.Icon)
</div>
<div class="uppercase truncate ml-2">{ application.Name }</div> <div class="uppercase truncate ml-2">{ application.Name }</div>
</a> </a>
} }

View file

@ -12,8 +12,5 @@ func SetupRoutes(router *http.ServeMux, sse *sse.Server, appHandler *AppHandler)
fsAssets := http.FileServer(http.Dir("assets")) fsAssets := http.FileServer(http.Dir("assets"))
router.Handle("GET /assets/", http.StripPrefix("/assets/", fsAssets)) router.Handle("GET /assets/", http.StripPrefix("/assets/", fsAssets))
fsIcons := http.FileServer(http.Dir("storage/icons"))
router.Handle("GET /storage/icons/", http.StripPrefix("/storage/icons/", fsIcons))
router.HandleFunc("GET /", appHandler.appHandler) router.HandleFunc("GET /", appHandler.appHandler)
} }

View file

@ -4,14 +4,16 @@ import (
"io" "io"
"log/slog" "log/slog"
"os" "os"
"path/filepath"
"strings" "strings"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
const storageDir = "storage/" const simpleIconsFolder = "node_modules/simple-icons/icons/"
const iconsDir = storageDir + "icons/" const storageFolder = "storage/"
const bookmarkFile = storageDir + "bookmarks.yaml" const iconsFolder = storageFolder + "icons/"
const bookmarkFile = storageFolder + "bookmarks.yaml"
const defaultConfig = `links: const defaultConfig = `links:
- category: "Code" - category: "Code"
entries: entries:
@ -22,11 +24,11 @@ applications:
- category: "Code" - category: "Code"
entries: entries:
- name: "Github" - name: "Github"
icon: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" icon: "si/github.svg"
url: "https://github.com"` url: "https://github.com"`
func init() { func init() {
folders := []string{storageDir, iconsDir} folders := []string{storageFolder, iconsFolder}
for _, path := range folders { for _, path := range folders {
err := os.MkdirAll(path, 0755) err := os.MkdirAll(path, 0755)
if err != nil { if err != nil {
@ -78,9 +80,21 @@ func (bs *BookmarkService) readBookmarksFile() []byte {
func (bs *BookmarkService) replaceIconString() { func (bs *BookmarkService) replaceIconString() {
for _, v := range bs.bookmarks.Applications { for _, v := range bs.bookmarks.Applications {
for i, bookmark := range v.Entries { for i, bookmark := range v.Entries {
if !strings.Contains(bookmark.Icon, "http") { rawHTML := ""
v.Entries[i].Icon = "/" + iconsDir + bookmark.Icon var data []byte
var err error
if filepath.Ext(bookmark.Icon) == ".svg" {
if strings.HasPrefix(bookmark.Icon, "si/") {
data, err = os.ReadFile(simpleIconsFolder + strings.Replace(bookmark.Icon, "si/", "", 1))
} else {
data, err = os.ReadFile(iconsFolder + bookmark.Icon)
} }
if err != nil {
continue
}
rawHTML = string(data)
}
v.Entries[i].Icon = rawHTML
} }
} }
} }