Social Card tweaks in the Infrastructure package. (#30)
This PR contains the work done to address certain tweaks in the newly-introduced _Social Card_ types in the **Infrastructure** package. Reviewed-on: rock-n-code/loud-amsterdam#30 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
@@ -70,52 +70,16 @@ public struct SocialCard: Sendable {
|
||||
|
||||
/// The card's meta tags, in a stable order: the Open Graph type, site name, title, description, URL, and locale, then the image group, and
|
||||
/// the Twitter card style last. A tag whose fact the card does not carry is left out.
|
||||
public var tags: [SocialCardTag] {
|
||||
let tags: [SocialCardTag?] = [
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: type,
|
||||
name: .type
|
||||
),
|
||||
siteName.map {
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: $0,
|
||||
name: .siteName
|
||||
)
|
||||
},
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: title,
|
||||
name: .title
|
||||
),
|
||||
summary.map {
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: $0,
|
||||
name: .description
|
||||
)
|
||||
},
|
||||
url.map {
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: $0,
|
||||
name: .url
|
||||
)
|
||||
},
|
||||
locale.map {
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: $0,
|
||||
name: .locale
|
||||
)
|
||||
},
|
||||
public var tags: [Tag] {
|
||||
let tags: [Tag?] = [
|
||||
Tag(type, name: .type),
|
||||
siteName.map { Tag($0, name: .siteName) },
|
||||
Tag(title, name: .title),
|
||||
summary.map { Tag($0, name: .description) },
|
||||
url.map { Tag($0, name: .url) },
|
||||
locale.map { Tag($0, name: .locale) },
|
||||
] + (image?.tags ?? []) + [
|
||||
SocialCardTag(
|
||||
attribute: .name,
|
||||
content: style.rawValue,
|
||||
name: .twitter
|
||||
),
|
||||
Tag(style.rawValue, name: .twitter),
|
||||
]
|
||||
|
||||
return tags.compactMap { $0 }
|
||||
|
||||
@@ -39,30 +39,12 @@ extension SocialCard {
|
||||
// MARK: Computed
|
||||
|
||||
/// The image's meta tags, in a stable order: its URL, width, and height, then its alt text when it carries one.
|
||||
public var tags: [SocialCardTag] {
|
||||
let tags: [SocialCardTag?] = [
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: url,
|
||||
name: .image
|
||||
),
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: String(width),
|
||||
name: .imageWidth
|
||||
),
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: String(height),
|
||||
name: .imageHeight
|
||||
),
|
||||
alt.map {
|
||||
SocialCardTag(
|
||||
attribute: .property,
|
||||
content: $0,
|
||||
name: .imageAlt
|
||||
)
|
||||
},
|
||||
public var tags: [Tag] {
|
||||
let tags: [Tag?] = [
|
||||
Tag(url, name: .image),
|
||||
Tag(String(width), name: .imageWidth),
|
||||
Tag(String(height), name: .imageHeight),
|
||||
alt.map { Tag($0, name: .imageAlt) },
|
||||
]
|
||||
|
||||
return tags.compactMap { $0 }
|
||||
|
||||
@@ -1,24 +1,44 @@
|
||||
/// A head meta tag of a ``SocialCard``: which attribute keys it, and its name and content.
|
||||
public struct SocialCardTag: Equatable, Sendable {
|
||||
extension SocialCard {
|
||||
/// A head meta tag of a ``SocialCard``: its name and content, keyed by the attribute its ``name`` dictates.
|
||||
public struct Tag: Equatable, Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
// MARK: Properties
|
||||
|
||||
/// The attribute the tag is keyed by.
|
||||
public let attribute: Attribute
|
||||
/// The tag's value.
|
||||
public let content: String
|
||||
|
||||
/// The tag's value.
|
||||
public let content: String
|
||||
/// The tag's name.
|
||||
public let name: Name
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// The tag's name.
|
||||
public let name: Name
|
||||
/// Creates a head meta tag.
|
||||
/// - Parameters:
|
||||
/// - content: the tag's value.
|
||||
/// - name: the tag's name, dictating the attribute the tag is keyed by.
|
||||
public init(
|
||||
_ content: String,
|
||||
name: Name
|
||||
) {
|
||||
self.content = content
|
||||
self.name = name
|
||||
}
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The attribute the tag is keyed by, dictated by its ``name``.
|
||||
public var attribute: Attribute {
|
||||
name.attribute
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Enumerations
|
||||
|
||||
extension SocialCardTag {
|
||||
|
||||
/// The meta attribute a ``SocialCardTag`` is keyed by, named by its raw value.
|
||||
extension SocialCard.Tag {
|
||||
|
||||
/// The meta attribute a ``SocialCard/Tag`` is keyed by, named by its raw value.
|
||||
public enum Attribute: String, Sendable {
|
||||
/// The `name` attribute, keying the Twitter tags.
|
||||
case name
|
||||
@@ -26,7 +46,7 @@ extension SocialCardTag {
|
||||
case property
|
||||
}
|
||||
|
||||
/// The name of a ``SocialCardTag``, carried in its raw value.
|
||||
/// The name of a ``SocialCard/Tag``, carried in its raw value.
|
||||
public enum Name: String, Sendable {
|
||||
/// The `og:description` tag, carrying the card's summary.
|
||||
case description = "og:description"
|
||||
@@ -34,10 +54,10 @@ extension SocialCardTag {
|
||||
case image = "og:image"
|
||||
/// The `og:image:alt` tag, carrying the share image's text for assistive technologies.
|
||||
case imageAlt = "og:image:alt"
|
||||
/// The `og:image:width` tag, carrying the share image's width in pixels.
|
||||
case imageWidth = "og:image:width"
|
||||
/// The `og:image:height` tag, carrying the share image's height in pixels.
|
||||
case imageHeight = "og:image:height"
|
||||
/// The `og:image:width` tag, carrying the share image's width in pixels.
|
||||
case imageWidth = "og:image:width"
|
||||
/// The `og:locale` tag, carrying the locale of the card's text.
|
||||
case locale = "og:locale"
|
||||
/// The `og:site_name` tag, carrying the name of the site the card belongs to.
|
||||
@@ -51,5 +71,18 @@ extension SocialCardTag {
|
||||
/// The `og:url` tag, carrying the absolute URL the card's page is served at.
|
||||
case url = "og:url"
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Implementations
|
||||
|
||||
public extension SocialCard.Tag.Name {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The attribute keying a tag with this name: `property` for the Open Graph names, `name` for the Twitter ones.
|
||||
var attribute: SocialCard.Tag.Attribute {
|
||||
self == .twitter ? .name : .property
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user