diff --git a/README.md b/README.md index 72fc336..fc560e7 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,7 @@ n, err = sm.WriteTo(&buf) - [ ] Video sitemaps - [x] Image sitemaps - [ ] News sitemaps - - [ ] Alternate Links + - [x] Alternate Links - [ ] Module Stability: - [x] Increase test coverage to more than %80. current coverage is: 86.3% of statements - [x] Write tests for different usages. diff --git a/smg/loc.go b/smg/loc.go index 7a21516..e9189c6 100644 --- a/smg/loc.go +++ b/smg/loc.go @@ -7,12 +7,13 @@ import ( // SitemapLoc contains data related to tag in Sitemap. type SitemapLoc struct { - XMLName xml.Name `xml:"url"` - Loc string `xml:"loc"` - LastMod *time.Time `xml:"lastmod,omitempty"` - ChangeFreq ChangeFreq `xml:"changefreq,omitempty"` - Priority float32 `xml:"priority,omitempty"` - Images []*SitemapImage `xml:"image:image,omitempty"` + XMLName xml.Name `xml:"url"` + Loc string `xml:"loc"` + LastMod *time.Time `xml:"lastmod,omitempty"` + ChangeFreq ChangeFreq `xml:"changefreq,omitempty"` + Priority float32 `xml:"priority,omitempty"` + Images []*SitemapImage `xml:"image:image,omitempty"` + Alternate []*SitemapAlternateLoc `xml:"xhtml:link,omitempty"` } // SitemapImage contains data related to tag in Sitemap @@ -26,3 +27,10 @@ type SitemapIndexLoc struct { Loc string `xml:"loc"` LastMod *time.Time `xml:"lastmod,omitempty"` } + +// SitemapAlternateLoc contains data related to tag in Sitemap +type SitemapAlternateLoc struct { + Hreflang string `xml:"hreflang,attr"` + Href string `xml:"href,attr"` + Rel string `xml:"rel,attr"` +} diff --git a/smg/sitemap_test.go b/smg/sitemap_test.go index 1421342..837b568 100644 --- a/smg/sitemap_test.go +++ b/smg/sitemap_test.go @@ -18,18 +18,25 @@ type UrlSet struct { } type UrlData struct { - XMLName xml.Name `xml:"url"` - Loc string `xml:"loc"` - LasMod string `xml:"lastmod"` - ChangeFreq string `xml:"changefreq"` - Priority string `xml:"priority"` - Images []SitemapImageData `xml:"image"` + XMLName xml.Name `xml:"url"` + Loc string `xml:"loc"` + LasMod string `xml:"lastmod"` + ChangeFreq string `xml:"changefreq"` + Priority string `xml:"priority"` + Images []SitemapImageData `xml:"image"` + Alternate []SitemapAlternateData `xml:"xhtml link"` } type SitemapImageData struct { ImageLoc string `xml:"loc,omitempty"` } +type SitemapAlternateData struct { + Hreflang string `xml:"hreflang,attr"` + Href string `xml:"href,attr"` + Rel string `xml:"rel,attr"` +} + // TestSingleSitemap tests the module against Single-file sitemap usage format. func TestSingleSitemap(t *testing.T) { path := t.TempDir() @@ -83,6 +90,16 @@ func TestSitemapAdd(t *testing.T) { testLocation := "/test?foo=bar" testImage := "/path-to-image.jpg" testImage2 := "/path-to-image-2.jpg" + testAlternate1 := &SitemapAlternateLoc{ + Hreflang: "en", + Href: fmt.Sprintf("%s%s", baseURL, "/en/test"), + Rel: "alternate", + } + testAlternate2 := &SitemapAlternateLoc{ + Hreflang: "de", + Href: fmt.Sprintf("%s%s", baseURL, "/de/test"), + Rel: "alternate", + } now := time.Now().UTC() sm := NewSitemap(true) @@ -98,6 +115,7 @@ func TestSitemapAdd(t *testing.T) { ChangeFreq: Always, Priority: 0.4, Images: []*SitemapImage{{testImage}, {testImage2}}, + Alternate: []*SitemapAlternateLoc{testAlternate1, testAlternate2}, }) if err != nil { t.Fatal("Unable to add SitemapLoc:", err) @@ -129,6 +147,17 @@ func TestSitemapAdd(t *testing.T) { actualImage2 := urlSet.Urls[0].Images[1].ImageLoc assert.Equal(t, expectedImage2, actualImage2) + + for i, expAlter := range []*SitemapAlternateLoc{testAlternate1, testAlternate2} { + actualAlternate := urlSet.Urls[0].Alternate[i].Href + assert.Equal(t, expAlter.Href, actualAlternate) + + actualAlternate = urlSet.Urls[0].Alternate[i].Rel + assert.Equal(t, expAlter.Rel, actualAlternate) + + actualAlternate = urlSet.Urls[0].Alternate[i].Hreflang + assert.Equal(t, expAlter.Hreflang, actualAlternate) + } } func TestWriteTo(t *testing.T) {