-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_seo.go
266 lines (216 loc) · 5.97 KB
/
page_seo.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package http
import (
"strings"
"github.com/gowool/cms"
)
var _ PageSEO = (*pageSEO)(nil)
type PageSEO interface {
AddFromSite(site cms.Site) PageSEO
AddFromPage(page cms.Page) PageSEO
Title() string
SetTitle(title string) PageSEO
AddTitlePrefix(prefix string) PageSEO
AddTitleSuffix(suffix string) PageSEO
OriginalTitle() string
SetSeparator(separator string) PageSEO
Metas() map[string]map[string]string
SetMetas(metas map[string]map[string]string) PageSEO
AddMeta(typ, name, content string) PageSEO
RemoveMeta(typ, name string) PageSEO
HasMeta(typ, name string) bool
HTMLAttributes() map[string]string
SetHTMLAttributes(attrs map[string]string) PageSEO
AddHTMLAttribute(name, content string) PageSEO
RemoveHTMLAttribute(name string) PageSEO
HasHTMLAttribute(name string) bool
HeadAttributes() map[string]string
SetHeadAttributes(attrs map[string]string) PageSEO
AddHeadAttribute(name, content string) PageSEO
RemoveHeadAttribute(name string) PageSEO
HasHeadAttribute(name string) bool
LinkCanonical() string
SetLinkCanonical(link string) PageSEO
RemoveLinkCanonical() PageSEO
LangAlternates() map[string]string
SetLangAlternates(langAlternates map[string]string) PageSEO
AddLangAlternate(href, hreflang string) PageSEO
RemoveLangAlternate(href string) PageSEO
HasLangAlternate(href string) bool
OEmbedLinks() map[string]string
AddOEmbedLink(title, link string) PageSEO
}
func NewPageSEO() PageSEO {
return &pageSEO{
separator: " - ",
metas: map[string]map[string]string{},
htmlAttrs: map[string]string{},
headAttrs: map[string]string{},
langAlternates: map[string]string{},
oembedLinks: map[string]string{},
}
}
type pageSEO struct {
title string
originalTitle string
separator string
linkCanonical string
metas map[string]map[string]string
htmlAttrs map[string]string
headAttrs map[string]string
langAlternates map[string]string
oembedLinks map[string]string
}
func (seo *pageSEO) AddFromSite(site cms.Site) PageSEO {
if site.Title != "" {
seo.SetTitle(site.Title)
}
if site.Separator != "" {
seo.SetSeparator(site.Separator)
}
if site.Locale != "" {
lang, _, _ := strings.Cut(site.Locale, "_")
seo.AddHTMLAttribute("lang", lang)
}
seo.setMetas(site.Metas)
return seo
}
func (seo *pageSEO) AddFromPage(page cms.Page) PageSEO {
if page.Title != "" {
seo.AddTitlePrefix(page.Title)
}
seo.AddMeta(cms.MetaProperty.String(), "og:type", "website")
seo.setMetas(page.Metas)
seo.AddHTMLAttribute("prefix", "og: http://ogp.me/ns#")
return seo
}
func (seo *pageSEO) setMetas(metas []cms.Meta) {
for _, meta := range metas {
if meta.Content == "" && (meta.Key == "keywords" || meta.Key == "description") {
continue
}
seo.AddMeta(meta.Type.String(), meta.Key, meta.Content)
}
}
func (seo *pageSEO) Title() string {
return seo.title
}
func (seo *pageSEO) SetTitle(title string) PageSEO {
seo.title = title
seo.originalTitle = title
return seo
}
func (seo *pageSEO) AddTitlePrefix(prefix string) PageSEO {
seo.title = prefix + seo.separator + seo.title
return seo
}
func (seo *pageSEO) AddTitleSuffix(suffix string) PageSEO {
seo.title += seo.separator + suffix
return seo
}
func (seo *pageSEO) OriginalTitle() string {
return seo.originalTitle
}
func (seo *pageSEO) SetSeparator(separator string) PageSEO {
seo.separator = separator
return seo
}
func (seo *pageSEO) Metas() map[string]map[string]string {
return seo.metas
}
func (seo *pageSEO) SetMetas(metas map[string]map[string]string) PageSEO {
seo.metas = metas
return seo
}
func (seo *pageSEO) AddMeta(typ, name, content string) PageSEO {
if _, ok := seo.metas[typ]; !ok {
seo.metas[typ] = map[string]string{}
}
seo.metas[typ][name] = content
return seo
}
func (seo *pageSEO) RemoveMeta(typ, name string) PageSEO {
delete(seo.metas[typ], name)
return seo
}
func (seo *pageSEO) HasMeta(typ, name string) bool {
if _, ok := seo.metas[typ]; !ok {
return false
}
_, ok := seo.metas[typ][name]
return ok
}
func (seo *pageSEO) HTMLAttributes() map[string]string {
return seo.htmlAttrs
}
func (seo *pageSEO) SetHTMLAttributes(attrs map[string]string) PageSEO {
seo.htmlAttrs = attrs
return seo
}
func (seo *pageSEO) AddHTMLAttribute(name, content string) PageSEO {
seo.htmlAttrs[name] = content
return seo
}
func (seo *pageSEO) RemoveHTMLAttribute(name string) PageSEO {
delete(seo.htmlAttrs, name)
return seo
}
func (seo *pageSEO) HasHTMLAttribute(name string) bool {
_, ok := seo.htmlAttrs[name]
return ok
}
func (seo *pageSEO) HeadAttributes() map[string]string {
return seo.headAttrs
}
func (seo *pageSEO) SetHeadAttributes(attrs map[string]string) PageSEO {
seo.headAttrs = attrs
return seo
}
func (seo *pageSEO) AddHeadAttribute(name, content string) PageSEO {
seo.headAttrs[name] = content
return seo
}
func (seo *pageSEO) RemoveHeadAttribute(name string) PageSEO {
delete(seo.headAttrs, name)
return seo
}
func (seo *pageSEO) HasHeadAttribute(name string) bool {
_, ok := seo.headAttrs[name]
return ok
}
func (seo *pageSEO) LinkCanonical() string {
return seo.linkCanonical
}
func (seo *pageSEO) SetLinkCanonical(link string) PageSEO {
seo.linkCanonical = link
return seo
}
func (seo *pageSEO) RemoveLinkCanonical() PageSEO {
seo.linkCanonical = ""
return seo
}
func (seo *pageSEO) LangAlternates() map[string]string {
return seo.langAlternates
}
func (seo *pageSEO) SetLangAlternates(langAlternates map[string]string) PageSEO {
seo.langAlternates = langAlternates
return seo
}
func (seo *pageSEO) AddLangAlternate(href, hreflang string) PageSEO {
seo.langAlternates[href] = hreflang
return seo
}
func (seo *pageSEO) RemoveLangAlternate(href string) PageSEO {
delete(seo.langAlternates, href)
return seo
}
func (seo *pageSEO) HasLangAlternate(href string) bool {
_, ok := seo.langAlternates[href]
return ok
}
func (seo *pageSEO) OEmbedLinks() map[string]string {
return seo.oembedLinks
}
func (seo *pageSEO) AddOEmbedLink(title, link string) PageSEO {
seo.oembedLinks[title] = link
return seo
}