diff --git a/embed/initial/base.html b/embed/initial/base.html
index 3bf225e..2afa524 100644
--- a/embed/initial/base.html
+++ b/embed/initial/base.html
@@ -7,6 +7,12 @@
{{ .Title }} - {{ .User.Config.Title }}
+ {{ if .User.FaviconUrl }}
+
+ {{ else }}
+
+ {{ end }}
+
{{ if .Description }}
diff --git a/renderer_test.go b/renderer_test.go
index f0f2d1d..c46d0e5 100644
--- a/renderer_test.go
+++ b/renderer_test.go
@@ -360,3 +360,13 @@ func TestOpenGraphTags(t *testing.T) {
}
}
+
+func TestAddFaviconIfExist(t *testing.T) {
+ user := getTestUser()
+ os.WriteFile(path.Join(user.MediaDir(), "favicon.png"), []byte("test"), 0644)
+
+ result, _ := owl.RenderIndexPage(user)
+ if !strings.Contains(result, "favicon.png") {
+ t.Error("favicon not rendered. Got: " + result)
+ }
+}
diff --git a/user.go b/user.go
index c843fc2..14f0be9 100644
--- a/user.go
+++ b/user.go
@@ -78,6 +78,16 @@ func (user User) AvatarUrl() string {
return ""
}
+func (user User) FaviconUrl() string {
+ for _, ext := range []string{".jpg", ".jpeg", ".png", ".gif", ".ico"} {
+ if fileExists(path.Join(user.MediaDir(), "favicon"+ext)) {
+ url, _ := url.JoinPath(user.MediaUrl(), "favicon"+ext)
+ return url
+ }
+ }
+ return ""
+}
+
func (user User) Posts() ([]*Post, error) {
postFiles := listDir(path.Join(user.Dir(), "public"))
posts := make([]*Post, 0)
diff --git a/user_test.go b/user_test.go
index 13405ce..b384741 100644
--- a/user_test.go
+++ b/user_test.go
@@ -324,3 +324,18 @@ func TestPostNameIllegalFileName(t *testing.T) {
t.Error("Should not have failed")
}
}
+
+func TestFaviconIfNotExist(t *testing.T) {
+ user := getTestUser()
+ if user.FaviconUrl() != "" {
+ t.Error("Favicon should be empty")
+ }
+}
+
+func TestFaviconSetIfFileExist(t *testing.T) {
+ user := getTestUser()
+ os.WriteFile(path.Join(user.MediaDir(), "favicon.ico"), []byte("test"), 0644)
+ if user.FaviconUrl() == "" {
+ t.Error("Favicon should not be empty")
+ }
+}