Skip to content

Commit

Permalink
Add xhtml:link support
Browse files Browse the repository at this point in the history
  • Loading branch information
utay committed Apr 4, 2023
1 parent b496544 commit bf1299c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
37 changes: 30 additions & 7 deletions sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,33 @@ const (
Never ChangeFreq = "never"
)

// XHTMLLink entry in [URL].
type XHTMLLink struct {
Ref string `xml:"ref,attr"`
HrefLang string `xml:"hreflang,attr"`
Href string `xml:"href,attr"`
}

// URL entry in [Sitemap] or [SitemapIndex]. LastMod is a pointer
// to [time.Time] because omitempty does not work otherwise. Loc is the
// only mandatory item. ChangeFreq and Priority must be left empty when
// using with a sitemap index.
type URL struct {
Loc string `xml:"loc"`
LastMod *time.Time `xml:"lastmod,omitempty"`
ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
Loc string `xml:"loc"`
LastMod *time.Time `xml:"lastmod,omitempty"`
ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
XHTMLLinks []XHTMLLink `xml:"xhtml:link"`
}

// Sitemap represents a complete sitemap which can be marshaled to XML.
// New instances must be created with [New] in order to set the xmlns
// attribute correctly. Minify can be set to make the output less human
// readable.
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
XmlnsXHTML *string `xml:"xmlns:xhtml,attr"`

URLs []*URL `xml:"url"`

Expand All @@ -61,6 +70,19 @@ func New() *Sitemap {
}
}

// WithXHTML adds the xmlns:xhtml to a [Sitemap].
func (s *Sitemap) WithXHTML() *Sitemap {
xhtml := "http://www.w3.org/1999/xhtml"
s.XmlnsXHTML = &xhtml
return s
}

// WithMinify enables minification on a [Sitemap].
func (s *Sitemap) WithMinify() *Sitemap {
s.Minify = true
return s
}

// Add adds an [URL] to a [Sitemap].
func (s *Sitemap) Add(u *URL) {
s.URLs = append(s.URLs, u)
Expand Down Expand Up @@ -90,7 +112,8 @@ func (s *Sitemap) WriteTo(w io.Writer) (n int64, err error) {
var _ io.WriterTo = (*Sitemap)(nil)

// ReadFrom reads and parses an XML encoded sitemap from [io.Reader].
// Implements [io.ReaderFrom].
// Implements [io.ReaderFrom]. Due to https://github.com/golang/go/issues/9519,
// unmarshaling xhtml links doesn't work.
func (s *Sitemap) ReadFrom(r io.Reader) (n int64, err error) {
de := xml.NewDecoder(r)
err = de.Decode(s)
Expand Down
15 changes: 10 additions & 5 deletions sitemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,36 @@ func Example() {

// Sitemap with one URL.
func ExampleSitemap() {
sm := sitemap.New()
sm := sitemap.New().WithXHTML()
t := time.Date(1984, 1, 1, 0, 0, 0, 0, time.UTC)
sm.Add(&sitemap.URL{
Loc: "http://example.com/",
LastMod: &t,
ChangeFreq: sitemap.Daily,
Priority: 0.5,
XHTMLLinks: []sitemap.XHTMLLink{
{Ref: "alternate", HrefLang: "en", Href: "http://example.com"},
{Ref: "alternate", HrefLang: "fr", Href: "http://example.com?lang=fr"},
},
})
sm.WriteTo(os.Stdout)
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
// <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
// <url>
// <loc>http://example.com/</loc>
// <lastmod>1984-01-01T00:00:00Z</lastmod>
// <changefreq>daily</changefreq>
// <priority>0.5</priority>
// <xhtml:link ref="alternate" hreflang="en" href="http://example.com"></xhtml:link>
// <xhtml:link ref="alternate" hreflang="fr" href="http://example.com?lang=fr"></xhtml:link>
// </url>
// </urlset>
}

// Setting Minify to true omits indentation and newlines in generated sitemap.
// WithMinify omits indentation and newlines in generated sitemap.
func ExampleSitemap_minify() {
sm := sitemap.New()
sm.Minify = true
sm := sitemap.New().WithMinify()
t := time.Date(1984, 1, 1, 0, 0, 0, 0, time.UTC)
sm.Add(&sitemap.URL{
Loc: "http://example.com/",
Expand Down

0 comments on commit bf1299c

Please sign in to comment.