-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsitemap.go
247 lines (236 loc) · 6.44 KB
/
sitemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"cmp"
"encoding/xml"
"fmt"
"io"
"net/http"
"path"
"time"
"github.com/araddon/dateparse"
"github.com/snabb/sitemap"
"go.goblog.app/app/pkgs/contenttype"
)
const (
sitemapPath = "/sitemap.xml"
sitemapBlogPath = "/sitemap-blog.xml"
sitemapBlogFeaturesPath = "/sitemap-blog-features.xml"
sitemapBlogArchivesPath = "/sitemap-blog-archives.xml"
sitemapBlogPostsPath = "/sitemap-blog-posts.xml"
)
func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
// Create sitemap
sm := sitemap.NewSitemapIndex()
// Add blog sitemap indices
now := time.Now().UTC()
for _, bc := range a.cfg.Blogs {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(sitemapBlogPath)),
LastMod: &now,
})
}
// Write sitemap
a.writeSitemapXML(w, r, sm)
}
func (a *goBlog) serveSitemapBlog(w http.ResponseWriter, r *http.Request) {
// Create sitemap
sm := sitemap.NewSitemapIndex()
// Add blog sitemaps
_, bc := a.getBlog(r)
now := time.Now().UTC()
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(sitemapBlogFeaturesPath)),
LastMod: &now,
})
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(sitemapBlogArchivesPath)),
LastMod: &now,
})
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(sitemapBlogPostsPath)),
LastMod: &now,
})
// Write sitemap
a.writeSitemapXML(w, r, sm)
}
func (a *goBlog) serveSitemapBlogFeatures(w http.ResponseWriter, r *http.Request) {
// Create sitemap
sm := sitemap.New()
// Add features to sitemap
_, bc := a.getBlog(r)
// Home
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath("")),
})
// Photos
if pc := bc.Photos; pc != nil && pc.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(cmp.Or(pc.Path, defaultPhotosPath))),
})
}
// Search
if bsc := bc.Search; bsc != nil && bsc.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(cmp.Or(bsc.Path, defaultSearchPath))),
})
}
// Stats
if bsc := bc.BlogStats; bsc != nil && bsc.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(cmp.Or(bsc.Path, defaultBlogStatsPath))),
})
}
// Blogroll
if blogrollEnabled, blogrollPath := bc.getBlogrollPath(); blogrollEnabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(blogrollPath),
})
}
// Geo map
if mc := bc.Map; mc != nil && mc.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(cmp.Or(mc.Path, defaultGeoMapPath))),
})
}
// Contact
if cc := bc.Contact; cc != nil && cc.Enabled {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(cmp.Or(cc.Path, defaultContactPath))),
})
}
// Write sitemap
a.writeSitemapXML(w, r, sm)
}
func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request) {
// Create sitemap
sm := sitemap.New()
// Add archives to sitemap
b, bc := a.getBlog(r)
// Sections
for _, section := range bc.Sections {
if section.Name != "" {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(section.Name)),
})
datePaths, _ := a.sitemapDatePaths(b, []string{section.Name})
for _, p := range datePaths {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(path.Join(section.Name, p))),
})
}
}
}
// Taxonomies
for _, taxonomy := range bc.Taxonomies {
if taxonomy.Name != "" {
// Taxonomy
taxPath := bc.getRelativePath("/" + taxonomy.Name)
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(taxPath),
})
// Values
if taxValues, err := a.db.allTaxonomyValues(b, taxonomy.Name); err == nil {
for _, tv := range taxValues {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(taxPath + "/" + urlize(tv)),
})
}
}
}
}
// Date based archives
datePaths, _ := a.sitemapDatePaths(b, nil)
for _, p := range datePaths {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(p)),
})
}
// Write sitemap
a.writeSitemapXML(w, r, sm)
}
// Serve sitemap with all the blog's posts
func (a *goBlog) serveSitemapBlogPosts(w http.ResponseWriter, r *http.Request) {
// Create sitemap
sm := sitemap.New()
// Request posts
blog, _ := a.getBlog(r)
posts, _ := a.getPosts(&postsRequestConfig{
status: []postStatus{statusPublished},
visibility: []postVisibility{visibilityPublic},
blogs: []string{blog},
fetchWithoutParams: true,
})
// Add posts to sitemap
for _, p := range posts {
item := &sitemap.URL{Loc: a.fullPostURL(p)}
lastMod := noError(dateparse.ParseLocal(cmp.Or(p.Updated, p.Published)))
if !lastMod.IsZero() {
item.LastMod = &lastMod
}
sm.Add(item)
}
// Write sitemap
a.writeSitemapXML(w, r, sm)
}
func (a *goBlog) writeSitemapXML(w http.ResponseWriter, _ *http.Request, sm any) {
pr, pw := io.Pipe()
go func() {
_, _ = io.WriteString(pw, xml.Header)
_, _ = io.WriteString(pw, `<?xml-stylesheet type="text/xsl" href="`)
_, _ = io.WriteString(pw, a.assetFileName("sitemap.xsl"))
_, _ = io.WriteString(pw, `" ?>`)
_ = pw.CloseWithError(xml.NewEncoder(pw).Encode(sm))
}()
w.Header().Set(contentType, contenttype.XMLUTF8)
_ = pr.CloseWithError(a.min.Get().Minify(contenttype.XML, w, pr))
}
const sitemapDatePathsSql = `
with filteredposts as ( %s ),
alldates as (
select distinct
substr(published, 1, 4) as year,
substr(published, 6, 2) as month,
substr(published, 9, 2) as day
from (
select tolocal(published) as published
from filteredposts
where coalesce(published, '') != ''
)
)
select distinct '/' || year from alldates
union
select distinct '/' || year || '/' || month from alldates
union
select distinct '/' || year || '/' || month || '/' || day from alldates
union
select distinct '/x/' || month from alldates
union
select distinct '/x/' || month || '/' || day from alldates
union
select distinct '/x/x/' || day from alldates;
`
func (a *goBlog) sitemapDatePaths(blog string, sections []string) (paths []string, err error) {
query, args, err := buildPostsQuery(&postsRequestConfig{
blogs: []string{blog},
sections: sections,
status: []postStatus{statusPublished},
visibility: []postVisibility{visibilityPublic},
}, "published")
if err != nil {
return
}
rows, err := a.db.Query(fmt.Sprintf(sitemapDatePathsSql, query), args...)
if err != nil {
return nil, err
}
defer rows.Close()
var path string
for rows.Next() {
err = rows.Scan(&path)
if err != nil {
return nil, err
}
paths = append(paths, path)
}
return
}