Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Alternate Links #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 14 additions & 6 deletions smg/loc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (

// SitemapLoc contains data related to <url> 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 <image:image> tag in Sitemap <url>
Expand All @@ -26,3 +27,10 @@ type SitemapIndexLoc struct {
Loc string `xml:"loc"`
LastMod *time.Time `xml:"lastmod,omitempty"`
}

// SitemapAlternateLoc contains data related to <xhtml:link> tag in Sitemap <url>
type SitemapAlternateLoc struct {
Hreflang string `xml:"hreflang,attr"`
Href string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
}
41 changes: 35 additions & 6 deletions smg/sitemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down