Skip to content

Commit

Permalink
Add twitter card meta
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed May 29, 2024
1 parent dc5fcb8 commit fea3d37
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ trait HtmlPage extends PageDependencies {
metaSettings.ogDescription.map(c => meta(property := "og:description", content := c)),
metaSettings.ogSiteName.map(c => meta(property := "og:site_name", content := c)),
metaSettings.ogLocale.map(c => meta(property := "og:locale", content := c)),
metaSettings.articleAuthor.map(c => meta(property := "article:author", content := c))
metaSettings.articleAuthor.map(c => meta(property := "article:author", content := c)),
// twitter
metaSettings.twitterTitle.map(c => meta(name := "twitter:title", content := c)),
metaSettings.twitterCard.map(c => meta(name := "twitter:card", content := c)),
metaSettings.twitterSite.map(c => meta(name := "twitter:site", content := c)),
metaSettings.twitterDescription.map(c => meta(name := "twitter:description", content := c)),
metaSettings.twitterImage.map(c => meta(name := "twitter:image", content := c)),
metaSettings.twitterImageAlt.map(c => meta(name := "twitter:image:alt", content := c))
)

// <head>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ final class MetaSettings private (
val geoPosition: Option[String],
val geoRegion: Option[String],
val geoPlacename: Option[String],
// open graph (fb, twitter)
// note: twitter falls back to OG https://developer.twitter.com/en/docs/tweets/optimize-with-cards/guides/getting-started#twitter-cards-and-open-graph
// open graph fb and others
val ogUrl: Option[String],
val ogType: Option[String],
val ogTitle: Option[String],
Expand All @@ -27,7 +26,14 @@ final class MetaSettings private (
val ogDescription: Option[String],
val ogSiteName: Option[String],
val ogLocale: Option[String],
val articleAuthor: Option[String]
val articleAuthor: Option[String],
// twitter card
val twitterCard: Option[String], // usually set to "summary_large_image"
val twitterSite: Option[String], // The Twitter @username of author
val twitterTitle: Option[String],
val twitterDescription: Option[String],
val twitterImage: Option[String],
val twitterImageAlt: Option[String]
) {

def withCharset(charset: String): MetaSettings = copy(charset = charset)
Expand Down Expand Up @@ -121,6 +127,48 @@ final class MetaSettings private (
Option(articleAuthor)
)

def withTwitterCard(twitterCard: Option[String]): MetaSettings =
copy(twitterCard = twitterCard)

def withTwitterCard(twitterCard: String): MetaSettings = withArticleAuthor(
Option(twitterCard)
)

def withTwitterSite(twitterSite: Option[String]): MetaSettings =
copy(twitterSite = twitterSite)

def withTwitterSite(twitterSite: String): MetaSettings = withArticleAuthor(
Option(twitterSite)
)

def withTwitterTitle(twitterTitle: Option[String]): MetaSettings =
copy(twitterTitle = twitterTitle)

def withTwitterTitle(twitterTitle: String): MetaSettings = withArticleAuthor(
Option(twitterTitle)
)

def withTwitterDescription(twitterDescription: Option[String]): MetaSettings =
copy(twitterDescription = twitterDescription)

def withTwitterDescription(twitterDescription: String): MetaSettings = withArticleAuthor(
Option(twitterDescription)
)

def withTwitterImage(twitterImage: Option[String]): MetaSettings =
copy(twitterImage = twitterImage)

def withTwitterImage(twitterImage: String): MetaSettings = withArticleAuthor(
Option(twitterImage)
)

def withTwitterImageAlt(twitterImageAlt: Option[String]): MetaSettings =
copy(twitterImageAlt = twitterImageAlt)

def withTwitterImageAlt(twitterImageAlt: String): MetaSettings = withArticleAuthor(
Option(twitterImageAlt)
)

private def copy(
charset: String = charset,
xuaCompatible: String = xuaCompatible,
Expand All @@ -144,7 +192,13 @@ final class MetaSettings private (
ogDescription: Option[String] = ogDescription,
ogSiteName: Option[String] = ogSiteName,
ogLocale: Option[String] = ogLocale,
articleAuthor: Option[String] = articleAuthor
articleAuthor: Option[String] = articleAuthor,
twitterCard: Option[String] = twitterCard,
twitterSite: Option[String] = twitterSite,
twitterTitle: Option[String] = twitterTitle,
twitterDescription: Option[String] = twitterDescription,
twitterImage: Option[String] = twitterImage,
twitterImageAlt: Option[String] = twitterImageAlt
) =
new MetaSettings(
charset = charset,
Expand All @@ -169,7 +223,13 @@ final class MetaSettings private (
ogDescription = ogDescription,
ogSiteName = ogSiteName,
ogLocale = ogLocale,
articleAuthor = articleAuthor
articleAuthor = articleAuthor,
twitterCard = twitterCard,
twitterSite = twitterSite,
twitterTitle = twitterTitle,
twitterDescription = twitterDescription,
twitterImage = twitterImage,
twitterImageAlt = twitterImageAlt
)
}

Expand Down Expand Up @@ -197,5 +257,11 @@ object MetaSettings:
ogDescription = None,
ogSiteName = None,
ogLocale = None,
articleAuthor = None
articleAuthor = None,
twitterCard = Some("summary_large_image"),
twitterSite = None,
twitterTitle = None,
twitterDescription = None,
twitterImage = None,
twitterImageAlt = None
)

0 comments on commit fea3d37

Please sign in to comment.