This commit is contained in:
Niko Abeler 2022-11-01 21:14:03 +01:00
parent 7667190a9c
commit 94918b5b62
4 changed files with 41 additions and 0 deletions

View File

@ -7,6 +7,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }} - {{ .User.Config.Title }}</title>
{{ if .User.FaviconUrl }}
<link rel="icon" href="{{ .User.FaviconUrl }}">
{{ else }}
<link rel="icon" href="data:,">
{{ end }}
<meta property="og:title" content="{{ .Title }}" />
{{ if .Description }}
<meta name="description" content="{{ .Description }}">

View File

@ -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)
}
}

10
user.go
View File

@ -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)

View File

@ -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")
}
}