-
Notifications
You must be signed in to change notification settings - Fork 7
/
metadata.go
655 lines (603 loc) · 15.1 KB
/
metadata.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package vite
import (
"context"
"fmt"
"strings"
"time"
)
type metadataKeyType string
var metadataKey = metadataKeyType("metadata")
// MetadataFromContext returns the metadata from the context.
// Use [MetadataToContext] to set the metadata in the context.
func MetadataFromContext(ctx context.Context) *Metadata {
if md, ok := ctx.Value(metadataKey).(*Metadata); ok {
return md
}
return nil
}
// MetadataToContext sets the metadata in the context.
// It is the inverse of [MetadataFromContext].
func MetadataToContext(ctx context.Context, md Metadata) context.Context {
return context.WithValue(ctx, metadataKey, &md)
}
type TitleData struct {
Template string
Default string
Absolute string
}
type Author struct {
Name string
URL string
}
type FormatDetection struct {
Email bool
Address bool
Telephone bool
}
type OpenGraph struct {
Title string
Description string
URL string
SiteName string
Images []OpenGraphImage
Locale string
Type string
PublishedTime time.Time
Authors []string
}
type OpenGraphImage struct {
URL string
Width int
Height int
Alt string
}
type Twitter struct {
Card string // e.g. "summary_large_image"
Title string
Description string
SiteID string
Creator string
CreatorID string
Images []string
App *TwitterApp
}
type TwitterApp struct {
Name string
ID *TwitterAppID
URL *TwitterAppURL
}
type TwitterAppID struct {
IPhone string
IPad string
GooglePlay string
}
type TwitterAppURL struct {
IPhone string
IPad string
}
type Robots struct {
Index bool
Follow bool
NoCache bool
GoogleBot *GoogleBot
}
type GoogleBot struct {
Index bool
Follow bool
NoImageIndex bool
MaxVideoPreview int // e.g. -1
MaxImagePreview string // e.g. "large"
MaxSnippet int // e.g. -1
}
type Icons struct {
Icon []Icon
Shortcut []string
Apple []AppleIcon
Other []OtherIcon
}
type Icon struct {
URL string
Media string
Type string
}
type AppleIcon struct {
URL string
Sizes []string
Type string
}
type OtherIcon struct {
Rel string
URL string
}
type Viewport struct {
ThemeColor []ThemeColor
Width string
InitialScale float64
MaximumScale float64
UserScalable *bool
ColorScheme string
}
type ThemeColor struct {
Name string
Color string
Media string
}
type Metadata struct {
Title string
TitleFunc func() TitleData
Description string
Generator string
ApplicationName string
Referrer string
Keywords []string
Authors []Author
Creator string
Publisher string
FormatDetection *FormatDetection
Canonical string
Languages map[string]string // "en-US": "/en-US"
OpenGraph *OpenGraph
Twitter *Twitter
Robots *Robots
Icons *Icons
Viewport *Viewport
Manifest string
// Verification map[string]string
// AppleWebApp
// Alternates
// AppLinks
// Archives
// Assets
// Bookmarks
// Category
Other map[string]string
}
// String output for the metadata.
func (m Metadata) String() string {
var sb strings.Builder
// Title
title := m.Title
if m.TitleFunc != nil {
titleData := m.TitleFunc()
if titleData.Absolute != "" {
title = titleData.Absolute
} else if titleData.Template != "" {
title = fmt.Sprintf(titleData.Template, m.Title)
} else if titleData.Default != "" {
title = titleData.Default
} else {
title = m.Title
}
}
sb.WriteString("<title>")
sb.WriteString(title)
sb.WriteString("</title>")
sb.WriteString("\n")
// Description
if m.Description != "" {
sb.WriteString(`<meta name="description" content="`)
sb.WriteString(m.Description)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Viewport
if m.Viewport != nil {
// Width
if m.Viewport.Width != "" {
sb.WriteString(`<meta name="viewport" content="width=`)
sb.WriteString(m.Viewport.Width)
if m.Viewport.InitialScale > 0 {
sb.WriteString(`,initial-scale=`)
sb.WriteString(fmt.Sprint(m.Viewport.InitialScale))
}
if m.Viewport.MaximumScale > 0 {
sb.WriteString(`,maximum-scale=`)
sb.WriteString(fmt.Sprint(m.Viewport.MaximumScale))
}
if m.Viewport.UserScalable != nil {
if *m.Viewport.UserScalable {
sb.WriteString(`,user-scalable=yes`)
} else {
sb.WriteString(`,user-scalable=no`)
}
}
if m.Viewport.ColorScheme != "" {
sb.WriteString(`,color-scheme=`)
sb.WriteString(m.Viewport.ColorScheme)
}
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// ThemeColor
for _, themeColor := range m.Viewport.ThemeColor {
sb.WriteString(`<meta name="theme-color" content="`)
sb.WriteString(themeColor.Color)
if themeColor.Media != "" {
sb.WriteString(`" media="`)
sb.WriteString(themeColor.Media)
}
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// ColorScheme
if m.Viewport.ColorScheme != "" {
sb.WriteString(`<meta name="color-scheme" content="`)
sb.WriteString(m.Viewport.ColorScheme)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
// Generator
if m.Generator != "" {
sb.WriteString(`<meta name="generator" content="`)
sb.WriteString(m.Generator)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// ApplicationName
if m.ApplicationName != "" {
sb.WriteString(`<meta name="application-name" content="`)
sb.WriteString(m.ApplicationName)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Referrer
if m.Referrer != "" {
sb.WriteString(`<meta name="referrer" content="`)
sb.WriteString(m.Referrer)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Keywords
if len(m.Keywords) > 0 {
sb.WriteString(`<meta name="keywords" content="`)
sb.WriteString(strings.Join(m.Keywords, ","))
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Authors
for _, author := range m.Authors {
if author.Name != "" {
sb.WriteString(`<meta name="author" content="`)
sb.WriteString(author.Name)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if author.URL != "" {
sb.WriteString(`<link rel="author" href="`)
sb.WriteString(author.URL)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
// Creator
if m.Creator != "" {
sb.WriteString(`<meta name="creator" content="`)
sb.WriteString(m.Creator)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Publisher
if m.Publisher != "" {
sb.WriteString(`<meta name="publisher" content="`)
sb.WriteString(m.Publisher)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// FormatDetection
if m.FormatDetection != nil {
sb.WriteString(`<meta name="format-detection" content="`)
if m.FormatDetection.Email {
sb.WriteString("email=no")
} else {
sb.WriteString("email=yes")
}
sb.WriteString(",")
if m.FormatDetection.Address {
sb.WriteString("address=no")
} else {
sb.WriteString("address=yes")
}
sb.WriteString(",")
if m.FormatDetection.Telephone {
sb.WriteString("telephone=no")
} else {
sb.WriteString("telephone=yes")
}
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Canonical
if m.Canonical != "" {
sb.WriteString(`<link rel="canonical" href="`)
sb.WriteString(m.Canonical)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Languages
for lang, href := range m.Languages {
sb.WriteString(`<link rel="alternate" hreflang="`)
sb.WriteString(lang)
sb.WriteString(`" href="`)
sb.WriteString(href)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// OpenGraph
if m.OpenGraph != nil {
if m.OpenGraph.Title != "" {
sb.WriteString(`<meta property="og:title" content="`)
sb.WriteString(m.OpenGraph.Title)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.OpenGraph.Description != "" {
sb.WriteString(`<meta property="og:description" content="`)
sb.WriteString(m.OpenGraph.Description)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.OpenGraph.URL != "" {
sb.WriteString(`<meta property="og:url" content="`)
sb.WriteString(m.OpenGraph.URL)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.OpenGraph.SiteName != "" {
sb.WriteString(`<meta property="og:site_name" content="`)
sb.WriteString(m.OpenGraph.SiteName)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
for _, image := range m.OpenGraph.Images {
sb.WriteString(`<meta property="og:image" content="`)
sb.WriteString(image.URL)
sb.WriteString(`" />`)
sb.WriteString("\n")
if image.Width > 0 {
sb.WriteString(`<meta property="og:image:width" content="`)
sb.WriteString(fmt.Sprint(image.Width))
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if image.Height > 0 {
sb.WriteString(`<meta property="og:image:height" content="`)
sb.WriteString(fmt.Sprint(image.Height))
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if image.Alt != "" {
sb.WriteString(`<meta property="og:image:alt" content="`)
sb.WriteString(image.Alt)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
if m.OpenGraph.Locale != "" {
sb.WriteString(`<meta property="og:locale" content="`)
sb.WriteString(m.OpenGraph.Locale)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.OpenGraph.Type != "" {
sb.WriteString(`<meta property="og:type" content="`)
sb.WriteString(m.OpenGraph.Type)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if !m.OpenGraph.PublishedTime.IsZero() {
sb.WriteString(`<meta property="article:published_time" content="`)
sb.WriteString(m.OpenGraph.PublishedTime.Format(time.RFC3339))
sb.WriteString(`" />`)
sb.WriteString("\n")
}
for _, author := range m.OpenGraph.Authors {
sb.WriteString(`<meta property="article:author" content="`)
sb.WriteString(author)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
// Twitter
if m.Twitter != nil {
if m.Twitter.Card != "" {
sb.WriteString(`<meta name="twitter:card" content="`)
sb.WriteString(m.Twitter.Card)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.Title != "" {
sb.WriteString(`<meta name="twitter:title" content="`)
sb.WriteString(m.Twitter.Title)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.Description != "" {
sb.WriteString(`<meta name="twitter:description" content="`)
sb.WriteString(m.Twitter.Description)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.SiteID != "" {
sb.WriteString(`<meta name="twitter:site:id" content="`)
sb.WriteString(m.Twitter.SiteID)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.Creator != "" {
sb.WriteString(`<meta name="twitter:creator" content="`)
sb.WriteString(m.Twitter.Creator)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.CreatorID != "" {
sb.WriteString(`<meta name="twitter:creator:id" content="`)
sb.WriteString(m.Twitter.CreatorID)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
for _, image := range m.Twitter.Images {
sb.WriteString(`<meta name="twitter:image" content="`)
sb.WriteString(image)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.App != nil {
if m.Twitter.App.Name != "" {
sb.WriteString(`<meta name="twitter:app:name" content="`)
sb.WriteString(m.Twitter.App.Name)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.App.ID != nil {
if m.Twitter.App.ID.IPhone != "" {
sb.WriteString(`<meta name="twitter:app:id:iphone" content="`)
sb.WriteString(m.Twitter.App.ID.IPhone)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.App.ID.IPad != "" {
sb.WriteString(`<meta name="twitter:app:id:ipad" content="`)
sb.WriteString(m.Twitter.App.ID.IPad)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.App.ID.GooglePlay != "" {
sb.WriteString(`<meta name="twitter:app:id:googleplay" content="`)
sb.WriteString(m.Twitter.App.ID.GooglePlay)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
if m.Twitter.App.URL != nil {
if m.Twitter.App.URL.IPhone != "" {
sb.WriteString(`<meta name="twitter:app:url:iphone" content="`)
sb.WriteString(m.Twitter.App.URL.IPhone)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
if m.Twitter.App.URL.IPad != "" {
sb.WriteString(`<meta name="twitter:app:url:ipad" content="`)
sb.WriteString(m.Twitter.App.URL.IPad)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
}
}
// Robots
if m.Robots != nil {
sb.WriteString(`<meta name="robots" content="`)
if m.Robots.Index {
sb.WriteString(`index`)
} else {
sb.WriteString(`noindex`)
}
if m.Robots.Follow {
sb.WriteString(`,follow`)
} else {
sb.WriteString(`,nofollow`)
}
if m.Robots.NoCache {
sb.WriteString(`,nocache`)
} else {
sb.WriteString(`,cache`)
}
sb.WriteString(`" />`)
sb.WriteString("\n")
if m.Robots.GoogleBot != nil {
sb.WriteString(`<meta name="googlebot" content="`)
if m.Robots.GoogleBot.Index {
sb.WriteString(`index`)
} else {
sb.WriteString(`noindex`)
}
if m.Robots.GoogleBot.Follow {
sb.WriteString(`,follow`)
} else {
sb.WriteString(`,nofollow`)
}
if m.Robots.GoogleBot.NoImageIndex {
sb.WriteString(`,noimageindex`)
} else {
sb.WriteString(`,imageindex`)
}
if m.Robots.GoogleBot.MaxVideoPreview >= 0 {
sb.WriteString(`,max-video-preview:`)
sb.WriteString(fmt.Sprint(m.Robots.GoogleBot.MaxVideoPreview))
}
if m.Robots.GoogleBot.MaxImagePreview != "" {
sb.WriteString(`,max-image-preview:`)
sb.WriteString(m.Robots.GoogleBot.MaxImagePreview)
}
if m.Robots.GoogleBot.MaxSnippet >= 0 {
sb.WriteString(`,max-snippet:`)
sb.WriteString(fmt.Sprint(m.Robots.GoogleBot.MaxSnippet))
}
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
// Icons
if m.Icons != nil {
for _, icon := range m.Icons.Icon {
sb.WriteString(`<link rel="icon" href="`)
sb.WriteString(icon.URL)
if icon.Type != "" {
sb.WriteString(`" type="`)
sb.WriteString(icon.Type)
}
if icon.Media != "" {
sb.WriteString(`" media="`)
sb.WriteString(icon.Media)
}
sb.WriteString(`" />`)
sb.WriteString("\n")
}
for _, shortcut := range m.Icons.Shortcut {
sb.WriteString(`<link rel="shortcut icon" href="`)
sb.WriteString(shortcut)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
for _, apple := range m.Icons.Apple {
sb.WriteString(`<link rel="apple-touch-icon" href="`)
sb.WriteString(apple.URL)
if len(apple.Sizes) > 0 {
sb.WriteString(`" sizes="`)
sb.WriteString(strings.Join(apple.Sizes, " "))
}
if apple.Type != "" {
sb.WriteString(`" type="`)
sb.WriteString(apple.Type)
}
sb.WriteString(`" />`)
sb.WriteString("\n")
}
for _, other := range m.Icons.Other {
sb.WriteString(`<link rel="`)
sb.WriteString(other.Rel)
sb.WriteString(`" href="`)
sb.WriteString(other.URL)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
}
// Manifest
if m.Manifest != "" {
sb.WriteString(`<link rel="manifest" href="`)
sb.WriteString(m.Manifest)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
// Other
for name, content := range m.Other {
sb.WriteString(`<meta name="`)
sb.WriteString(name)
sb.WriteString(`" content="`)
sb.WriteString(content)
sb.WriteString(`" />`)
sb.WriteString("\n")
}
return sb.String()
}