Skip to content

Commit e523952

Browse files
committed
add opengraph tags to HTML
1 parent 139e7e1 commit e523952

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed

Sources/DOM/HTML/HTML.Attribute.Factory (mxt).swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
extension HTML.Attribute.Factory
22
{
3+
@available(*, unavailable,
4+
message: "Use the typed 'property' property on 'HTML.AttributeEncoder' instead.")
5+
@inlinable public
6+
var property:HTML.Attribute { .property }
7+
38
@available(*, unavailable,
49
message: "Use the typed 'rel' property on 'HTML.AttributeEncoder' instead.")
510
@inlinable public

Sources/DOM/HTML/HTML.Attribute.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import DynamicLookupMacros
22

33
extension HTML
44
{
5-
@GenerateDynamicMemberFactory(excluding: "rel")
5+
@GenerateDynamicMemberFactory(excluding: "rel", "property")
66
@frozen public
77
enum Attribute:String, DOM.Attribute, Equatable, Hashable, Sendable
88
{
@@ -136,5 +136,9 @@ extension HTML
136136
case value
137137
case width
138138
case wrap
139+
140+
/// A non-standard HTML attribute defined in [RDFa](https://en.wikipedia.org/wiki/RDFa)
141+
/// for use in the `<meta>` tag.
142+
case property
139143
}
140144
}

Sources/HTML/Encoding/HTML.AttributeEncoder (ext).swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
extension HTML.AttributeEncoder
22
{
33
@inlinable public
4-
var rel:HTML.Attribute.Rel?
4+
var property:HTML.Attribute.Property?
55
{
6-
get
6+
get { nil }
7+
set (value)
78
{
8-
nil
9+
self[name: .property] = value?.rawValue
910
}
10-
set(value)
11+
}
12+
13+
@inlinable public
14+
var rel:HTML.Attribute.Rel?
15+
{
16+
get { nil }
17+
set (value)
1118
{
1219
self[name: .rel] = value?.rawValue
1320
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extension HTML.Attribute
2+
{
3+
/// See https://ogp.me/
4+
@frozen public
5+
enum Property:String, Equatable, Hashable, Sendable
6+
{
7+
case og_audio = "og:audio"
8+
case og_audio_type = "og:audio:type"
9+
10+
case og_image = "og:image"
11+
case og_image_alt = "og:image:alt"
12+
case og_image_type = "og:image:type"
13+
case og_image_width = "og:image:width"
14+
case og_image_height = "og:image:height"
15+
16+
case og_video = "og:video"
17+
case og_video_type = "og:video:type"
18+
case og_video_width = "og:video:width"
19+
case og_video_height = "og:video:height"
20+
21+
case og_description = "og:description"
22+
case og_determiner = "og:determiner"
23+
case og_locale = "og:locale"
24+
case og_locale_alternate = "og:locale:alternate"
25+
case og_site_name = "og:site_name"
26+
case og_title = "og:title"
27+
case og_type = "og:type"
28+
case og_url = "og:url"
29+
}
30+
}

Sources/SwiftinitRender/Pages/Swiftinit.RenderablePage.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ extension Swiftinit.RenderablePage
5656
{
5757
$0[.head]
5858
{
59+
let favicon:String = "\(format.assets[.favicon_png])"
60+
5961
$0[.title] = self.title
6062
$0[.meta] { $0.charset = "UTF-8" }
6163
$0[.meta]
@@ -65,7 +67,7 @@ extension Swiftinit.RenderablePage
6567
}
6668
$0[.link]
6769
{
68-
$0.href = "\(format.assets[.favicon_png])"
70+
$0.href = favicon
6971
$0.type = "\(MediaType.image(.png))"
7072
$0.rel = .icon
7173
}
@@ -98,9 +100,24 @@ extension Swiftinit.RenderablePage
98100

99101
if let description:String = self.description
100102
{
103+
// It is regrettable that we need to duplicate the description text here,
104+
// particularly because we do not compress dynamic content. However, it is
105+
// necessary for Onebox to render link previews correctly.
101106
$0[.meta] { $0.name = "description" ; $0.content = description }
107+
$0[.meta] { $0.property = .og_description ; $0.content = description }
108+
}
109+
else
110+
{
111+
$0[.meta]
112+
{
113+
$0.property = .og_description
114+
$0.content = "No overview available"
115+
}
102116
}
103117

118+
$0[.meta] { $0.property = .og_title ; $0.content = self.title }
119+
$0[.meta] { $0.property = .og_image ; $0.content = favicon }
120+
104121
self.head(augmenting: &$0, format: format)
105122
}
106123

0 commit comments

Comments
 (0)