-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogp.go
154 lines (134 loc) · 2.84 KB
/
ogp.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
// Copyright 2023 Furqan Software Ltd. All rights reserved.
// Package ogp provides types that represent Open Graph metadata.
// https://ogp.me/
package ogp
import (
"html/template"
"strconv"
"strings"
)
type OpenGraph struct {
// Basic
Title string
Type string
Image URL // Ignored if Images is set.
Images []Image
URL URL
// Optional
Audio URL
Description string
Determiner Determiner
Locale Locale
LocaleAlternate []Locale
SiteName string
Video URL
}
func (o OpenGraph) HTML() template.HTML {
b := strings.Builder{}
meta := func(property, content string) {
b.WriteString(`<meta property="`)
b.WriteString(template.HTMLEscaper(property))
b.WriteString(`" content="`)
b.WriteString(template.HTMLEscaper(content))
b.WriteString(`" />`)
b.WriteByte('\n')
}
if o.Title != "" {
meta("og:title", o.Title)
}
if o.Type != "" {
meta("og:type", o.Type)
}
if o.Images == nil && o.Image.IsValid() {
meta("og:type", string(o.Image))
}
for _, g := range o.Images {
if !g.URL.IsValid() {
continue
}
if g.URL != "" {
// Property og:image:url is identical to og:image.
meta("og:image", string(g.URL))
}
if g.SecureURL != "" {
meta("og:image:secure_url", string(g.SecureURL))
}
if g.Type != "" {
meta("og:image:type", g.Type)
}
if g.Width > 0 {
meta("og:image:width", strconv.Itoa(g.Width))
}
if g.Height > 0 {
meta("og:image:height", strconv.Itoa(g.Height))
}
if g.Alt != "" {
meta("og:image:alt", g.Alt)
}
}
if o.URL.IsValid() {
meta("og:url", string(o.URL))
}
if o.Audio != "" {
meta("og:audio", string(o.Audio))
}
if o.Description != "" {
meta("og:description", o.Description)
}
if o.Determiner != "" {
meta("og:determiner", string(o.Determiner))
}
if o.Locale != "" {
meta("og:locale", string(o.Locale))
}
for _, loc := range o.LocaleAlternate {
meta("og:locale:alternate", string(loc))
}
if o.SiteName != "" {
meta("og:site_name", o.SiteName)
}
if o.Video.IsValid() {
meta("og:video", string(o.Video))
}
return template.HTML(b.String())
}
type Image struct {
URL URL
SecureURL SecureURL
Type string
Width int
Height int
Alt string
}
type Video struct {
URL URL
SecureURL SecureURL
Type string
Width int
Height int
}
type Audio struct {
URL URL
SecureURL SecureURL
Type string
}
type URL string
func (u URL) IsValid() bool {
return strings.HasPrefix(string(u), "http://") || strings.HasPrefix(string(u), "https://")
}
type SecureURL URL
func (s SecureURL) IsValid() bool {
return strings.HasPrefix(string(s), "https://")
}
type Determiner string
const (
DetBlank Determiner = ""
DetAuto Determiner = "auto"
DetA Determiner = "a"
DetAn Determiner = "an"
DetThe Determiner = "the"
)
type Locale string
const (
LocEnUS Locale = "en_US"
)