diff --git a/internal/httpgetter/html_meta.go b/internal/httpgetter/html_meta.go index 2e72f1aa..8a5f3149 100644 --- a/internal/httpgetter/html_meta.go +++ b/internal/httpgetter/html_meta.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "net/url" + "strings" "time" "github.com/pkg/errors" @@ -175,11 +176,6 @@ func extractHTMLMeta(resp io.Reader) *HTMLMeta { token := tokenizer.Token() htmlMeta.Title = token.Data } else if token.DataAtom == atom.Meta { - description, ok := extractMetaProperty(token, "description") - if ok { - htmlMeta.Description = description - } - ogTitle, ok := extractMetaProperty(token, "og:title") if ok { htmlMeta.Title = ogTitle @@ -194,6 +190,11 @@ func extractHTMLMeta(resp io.Reader) *HTMLMeta { if ok { htmlMeta.Image = ogImage } + + description, ok := extractMetaProperty(token, "description") + if ok && htmlMeta.Description == "" { + htmlMeta.Description = description + } } } } @@ -204,7 +205,7 @@ func extractHTMLMeta(resp io.Reader) *HTMLMeta { func extractMetaProperty(token html.Token, prop string) (content string, ok bool) { content, ok = "", false for _, attr := range token.Attr { - if attr.Key == "property" && attr.Val == prop { + if (attr.Key == "property" || attr.Key == "name") && strings.EqualFold(attr.Val, prop) { ok = true } if attr.Key == "content" { diff --git a/internal/httpgetter/html_meta_test.go b/internal/httpgetter/html_meta_test.go index d5d77bad..f10e5ac8 100644 --- a/internal/httpgetter/html_meta_test.go +++ b/internal/httpgetter/html_meta_test.go @@ -55,6 +55,74 @@ func TestGetHTMLMeta(t *testing.T) { }, *metadata) } +func TestGetHTMLMetaWithNameOnly(t *testing.T) { + originalHTTPClient := httpClient + t.Cleanup(func() { + httpClient = originalHTTPClient + }) + + httpClient = &http.Client{ + Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { + require.Equal(t, "http://93.184.216.34/blog", req.URL.String()) + return &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": []string{"text/html; charset=utf-8"}}, + Body: io.NopCloser(strings.NewReader(` + +
+