Merge pull request #788 from trheyi/main

Enhance SEO metadata handling by adding keywords support and fixing o…
This commit is contained in:
Max 2024-11-29 11:31:11 +08:00 committed by GitHub
commit 8a0439ff19
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -224,27 +224,38 @@ func (page *Page) ReplaceDocument(doc *goquery.Document) {
if page.Config.SEO != nil {
if page.Config.SEO.Title != "" {
if doc.Find("meta[property=og:title]") != nil {
doc.Find("meta[property=og:title]").SetAttr("content", page.Config.SEO.Title)
}
}
if page.Config.SEO.Description != "" {
if doc.Find("meta[name=description]") != nil {
doc.Find("meta[name=description]").SetAttr("content", page.Config.SEO.Description)
}
}
if page.Config.SEO.Keywords != "" {
if doc.Find("meta[name=keywords]") != nil {
sel := doc.Find("meta[name=keywords]")
keywords := page.Config.SEO.Keywords
if sel.AttrOr("content", "") != "" {
keywords = keywords + "," + sel.AttrOr("content", "")
}
doc.Find("meta[name=keywords]").SetAttr("content", keywords)
}
}
if page.Config.SEO.Title != "" {
if doc.Find("meta[property='og:title']") != nil {
doc.Find("meta[property='og:title']").SetAttr("content", page.Config.SEO.Title)
}
}
if page.Config.SEO.Image != "" {
if doc.Find("meta[property=og:image]") != nil {
doc.Find("meta[property=og:image]").SetAttr("content", page.Config.SEO.Image)
if doc.Find("meta[property='og:image']") != nil {
doc.Find("meta[property='og:image']").SetAttr("content", page.Config.SEO.Image)
}
}
if page.Config.SEO.URL != "" {
if doc.Find("meta[property=og:url]") != nil {
doc.Find("meta[property=og:url]").SetAttr("content", page.Config.SEO.URL)
if doc.Find("meta[property='og:url']") != nil {
doc.Find("meta[property='og:url']").SetAttr("content", page.Config.SEO.URL)
}
}
}