From 66f96f23802d0b19b0643d9affeac893befac00a Mon Sep 17 00:00:00 2001 From: windymelt Date: Sun, 10 Sep 2023 02:35:26 +0900 Subject: [PATCH] Show meta tag for Open Graph Protocol --- .../main/scala/scaladex/core/model/OGP.scala | 29 +++++++++++++++++++ .../scala/scaladex/core/model/Project.scala | 10 +++++++ .../scaladex/view/project/badges.scala.html | 2 +- .../scaladex/view/project/project.scala.html | 2 +- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 modules/core/shared/src/main/scala/scaladex/core/model/OGP.scala diff --git a/modules/core/shared/src/main/scala/scaladex/core/model/OGP.scala b/modules/core/shared/src/main/scala/scaladex/core/model/OGP.scala new file mode 100644 index 000000000..a2d6f1894 --- /dev/null +++ b/modules/core/shared/src/main/scala/scaladex/core/model/OGP.scala @@ -0,0 +1,29 @@ +package scaladex.core.model + +/** + * Open Graph Protocol, see https://ogp.me/ + * + * @param title + * @param url + * @param description + * @param image + * @param imageAlt + */ +case class OGP( + title: String, + url: Url, + description: String, + image: Option[Url] = None, + imageAlt: Option[String] = None +) { + val `type`: String = "article" + val siteName: String = "Scaladex" + def toHeadMeta: Seq[HeadMeta] = Seq( + HeadMeta(name = "og:title", content = title), + HeadMeta(name = "og:url", content = url.toString()), + HeadMeta(name = "og:description", content = description), + HeadMeta(name = "og:site_name", content = siteName) + ) ++ image.map(c => HeadMeta(name = "og:image", content = c.target)) ++ imageAlt.map(c => + HeadMeta(name = "og:image:alt", c) + ) +} diff --git a/modules/core/shared/src/main/scala/scaladex/core/model/Project.scala b/modules/core/shared/src/main/scala/scaladex/core/model/Project.scala index 1c79a0e9c..d929a678c 100644 --- a/modules/core/shared/src/main/scala/scaladex/core/model/Project.scala +++ b/modules/core/shared/src/main/scala/scaladex/core/model/Project.scala @@ -28,6 +28,16 @@ case class Project( githubInfo.flatMap(_.logo) ) + /** + * This is used in embedding to another website to render the card of a scaladex project link. + */ + def ogp: OGP = OGP( + title = s"Scaladex - ${organization.toString()} / ${repository.toString()}", + url = Url(s"https://index.scala-lang.org/${organization.toString()}/${repository.toString()}"), + description = githubInfo.flatMap(_.description).getOrElse(""), + image = githubInfo.flatMap(_.logo).orElse(Some(Url("https://index.scala-lang.org/assets/img/scaladex-brand.svg"))), + ) + def scaladoc(artifact: Artifact): Option[DocumentationLink] = settings.customScalaDoc .map(DocumentationPattern("Scaladoc", _).eval(artifact)) diff --git a/modules/template/src/main/twirl/scaladex/view/project/badges.scala.html b/modules/template/src/main/twirl/scaladex/view/project/badges.scala.html index eb2607d97..991316984 100644 --- a/modules/template/src/main/twirl/scaladex/view/project/badges.scala.html +++ b/modules/template/src/main/twirl/scaladex/view/project/badges.scala.html @@ -15,7 +15,7 @@ header: ProjectHeader, artifact: Artifact ) -@main(env, title = project.repository.value, user, extraMeta = project.twitterCard.toHeadMeta) { +@main(env, title = project.repository.value, user, extraMeta = project.twitterCard.toHeadMeta ++ project.ogp.toHeadMeta) {
@headproject(env, user, project, header, "Badges")
diff --git a/modules/template/src/main/twirl/scaladex/view/project/project.scala.html b/modules/template/src/main/twirl/scaladex/view/project/project.scala.html index 0770ac6b1..9b67ca091 100644 --- a/modules/template/src/main/twirl/scaladex/view/project/project.scala.html +++ b/modules/template/src/main/twirl/scaladex/view/project/project.scala.html @@ -17,7 +17,7 @@ directDependencies: Map[Project.Reference, (ArtifactDependency.Scope, Seq[SemanticVersion])], reverseDependency: Map[Project.Reference, (ArtifactDependency.Scope, SemanticVersion)], ) -@main(env, title = project.repository.value, user, extraMeta = project.twitterCard.toHeadMeta) { +@main(env, title = project.repository.value, user, extraMeta = project.twitterCard.toHeadMeta ++ project.ogp.toHeadMeta) {
@headproject(env, user, project, header, "Project")