-
Notifications
You must be signed in to change notification settings - Fork 10
/
seo.go
211 lines (180 loc) · 6.57 KB
/
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
package seo
import (
"fmt"
"html/template"
"net/url"
"reflect"
"regexp"
"strings"
"github.com/qor/admin"
"github.com/qor/media"
"github.com/qor/qor"
"github.com/qor/qor/resource"
)
func init() {
admin.RegisterViewPath("github.com/qor/seo/views")
}
// New initialize a SeoCollection instance
func New(name string) *Collection {
return &Collection{Name: name}
}
// Collection will hold registered seo configures and global setting definition and other configures
type Collection struct {
Name string
SettingResource *admin.Resource
registeredSEO []*SEO
resource *admin.Resource
globalResource *admin.Resource
globalSetting interface{}
}
// SEO represents a seo object for a page
type SEO struct {
Name string
Varibles []string
OpenGraph *OpenGraphConfig
Context func(...interface{}) map[string]string
collection *Collection
}
// OpenGraphConfig open graph config
type OpenGraphConfig struct {
ImageResource *admin.Resource
Size *media.Size
}
// RegisterGlobalVaribles register global setting struct and will represents as 'Site-wide Settings' part in admin
func (collection *Collection) RegisterGlobalVaribles(s interface{}) {
collection.globalSetting = s
}
// RegisterSEO register a seo
func (collection *Collection) RegisterSEO(seo *SEO) {
seo.collection = collection
if seo.OpenGraph == nil {
seo.OpenGraph = &OpenGraphConfig{}
}
collection.registeredSEO = append(collection.registeredSEO, seo)
}
const currentSeoFieldIndicator = "CurrentSeoField"
// GetSEOSetting return SEO title, keywords and description and open graph settings
func (collection Collection) GetSEOSetting(context *qor.Context, name string, objects ...interface{}) Setting {
var (
seoSetting Setting
db = context.GetDB()
seo = collection.GetSEO(name)
hasMultiSeoField = false
currentSeoFieldName = ""
)
// If passed objects has customzied SEO Setting field
for _, obj := range objects {
if value := reflect.Indirect(reflect.ValueOf(obj)); value.IsValid() && value.Kind() == reflect.Struct {
for i := 0; i < value.NumField(); i++ {
// To support multiple SEO field in one struct, you must add a field with name of currentSeoFieldIndicator's value
// NOTE: this field must be set before the seo field.
// When rendering the page, set record.CurrentSeoField = "current seo name".
// If the value of CurrentSeoField is empty, use the first seo field as default.
if value.Type().Field(i).Name == currentSeoFieldIndicator {
hasMultiSeoField = true
currentSeoFieldName = value.Field(i).String()
}
if value.Field(i).Type() == reflect.TypeOf(Setting{}) {
if hasMultiSeoField && currentSeoFieldName != "" {
if value.Type().Field(i).Name == currentSeoFieldName {
seoSetting = value.Field(i).Interface().(Setting)
break
}
} else {
seoSetting = value.Field(i).Interface().(Setting)
break
}
}
}
}
}
if !seoSetting.EnabledCustomize {
globalSeoSetting := collection.SettingResource.NewStruct().(QorSEOSettingInterface)
if !db.Where("name = ?", name).First(globalSeoSetting).RecordNotFound() {
seoSetting = globalSeoSetting.GetSEOSetting()
}
}
siteWideSetting := collection.SettingResource.NewStruct()
db.Where("is_global_seo = ? AND name = ?", true, collection.Name).First(siteWideSetting)
tagValues := siteWideSetting.(QorSEOSettingInterface).GetGlobalSetting()
if tagValues == nil {
tagValues = map[string]string{}
}
if seo.Context != nil {
for key, value := range seo.Context(objects...) {
tagValues[key] = value
}
}
return replaceTags(seoSetting, seo.Varibles, tagValues)
}
// Render render SEO Setting
func (collection Collection) Render(context *qor.Context, name string, objects ...interface{}) template.HTML {
seoSetting := collection.GetSEOSetting(context, name, objects...)
return seoSetting.FormattedHTML(context)
}
// GetSEO get a Seo by name
func (collection *Collection) GetSEO(name string) *SEO {
for _, s := range collection.registeredSEO {
if s.Name == name {
return s
}
}
return &SEO{Name: name, collection: collection}
}
// SEOSettingURL get setting inline edit url by name
func (collection *Collection) SEOSettingURL(name string) string {
qorAdmin := collection.resource.GetAdmin()
return fmt.Sprintf("%v/%v/!seo_setting?name=%v", qorAdmin.GetRouter().Prefix, collection.resource.ToParam(), url.QueryEscape(name))
}
// ConfigureQorResource configure seoCollection for qor admin
func (collection *Collection) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
Admin := res.GetAdmin()
collection.resource = res
if collection.SettingResource == nil {
collection.SettingResource = Admin.AddResource(&QorSEOSetting{}, &admin.Config{Invisible: true})
}
collection.SettingResource.UseTheme("seo")
collection.SettingResource.EditAttrs("Name", "Setting")
if nameMeta := collection.SettingResource.GetMeta("Name"); nameMeta != nil {
nameMeta.Type = "hidden"
}
globalSettingRes := Admin.AddResource(collection.globalSetting, &admin.Config{Invisible: true})
collection.globalResource = globalSettingRes
res.Config.Singleton = true
res.UseTheme("seo")
router := Admin.GetRouter()
controller := seoController{Collection: collection}
router.Get(res.ToParam(), controller.Index)
router.Put(fmt.Sprintf("%v/!seo_setting", res.ToParam()), controller.Update)
router.Get(fmt.Sprintf("%v/!seo_setting", res.ToParam()), controller.InlineEdit)
registerFuncMap(Admin)
}
}
// Helpers
func replaceTags(seoSetting Setting, validTags []string, values map[string]string) Setting {
replace := func(str string) string {
re := regexp.MustCompile("{{([a-zA-Z0-9]*)}}")
matches := re.FindAllStringSubmatch(str, -1)
for _, match := range matches {
str = strings.Replace(str, match[0], values[match[1]], 1)
}
return str
}
seoSetting.Title = replace(seoSetting.Title)
seoSetting.Description = replace(seoSetting.Description)
seoSetting.Keywords = replace(seoSetting.Keywords)
seoSetting.Type = replace(seoSetting.Type)
seoSetting.OpenGraphTitle = replace(seoSetting.OpenGraphTitle)
seoSetting.OpenGraphDescription = replace(seoSetting.OpenGraphDescription)
seoSetting.OpenGraphURL = replace(seoSetting.OpenGraphURL)
seoSetting.OpenGraphImageURL = replace(seoSetting.OpenGraphImageURL)
seoSetting.OpenGraphType = replace(seoSetting.OpenGraphType)
for idx, metadata := range seoSetting.OpenGraphMetadata {
seoSetting.OpenGraphMetadata[idx] = OpenGraphMetadata{
Property: replace(metadata.Property),
Content: replace(metadata.Content),
}
}
return seoSetting
}