Skip to content

Commit

Permalink
Merge pull request #1271 from windymelt/introduce-ogp
Browse files Browse the repository at this point in the history
Show meta tag for Open Graph Protocol
  • Loading branch information
adpi2 authored Sep 19, 2023
2 parents 722ff36 + 9a60097 commit 3247280
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.7.9"
version = "3.7.14"
runner.dialect = scala213source3
maxColumn = 120
align.preset = some
Expand Down
Binary file modified bin/scalafmt
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scaladex.core.model

/**
* A meta tag in head that uses property attribute.
*
* @param property The property attribute
* @param content The content attribute
*/
case class HeadMetaProperty(property: String, content: String)
30 changes: 30 additions & 0 deletions modules/core/shared/src/main/scala/scaladex/core/model/OGP.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 toHeadMetaProperty: Seq[HeadMetaProperty] = Seq(
HeadMetaProperty(property = "og:title", content = title),
HeadMetaProperty(property = "og:url", content = url.target),
HeadMetaProperty(property = "og:type", content = `type`),
HeadMetaProperty(property = "og:description", content = description),
HeadMetaProperty(property = "og:site_name", content = siteName)
) ++ image.map(c => HeadMetaProperty(property = "og:image", content = c.target)) ++ imageAlt.map(c =>
HeadMetaProperty(property = "og:image:alt", c)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions modules/template/src/main/twirl/scaladex/view/main.scala.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import scaladex.core.model.search.SearchParams
@import scaladex.core.model.UserState
@import scaladex.core.model.HeadMeta
@import scaladex.core.model.HeadMetaProperty
@import scaladex.core.model.Env

@(
Expand All @@ -10,6 +11,7 @@
showSearch: Boolean = true,
params: SearchParams = SearchParams(), you: Boolean = false,
extraMeta: Seq[HeadMeta] = Seq.empty,
extraMetaProperty: Seq[HeadMetaProperty] = Seq.empty,
totalProjects: Option[Long] = None, totalArtifacts: Option[Long] = None
)(content: Html)
<!DOCTYPE HTML>
Expand Down Expand Up @@ -47,6 +49,9 @@
@for(meta <- extraMeta) {
<meta name="@meta.name" content="@meta.content">
}
@for(meta <- extraMetaProperty) {
<meta property="@meta.property" content="@meta.content">
}
</head>

<body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, extraMetaProperty = project.ogp.toHeadMetaProperty) {
<main id="container-project">
@headproject(env, user, project, header, "Badges")
<div class="container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, extraMetaProperty = project.ogp.toHeadMetaProperty) {
<main id="container-project">
@headproject(env, user, project, header, "Project")
<div class="container">
Expand Down

0 comments on commit 3247280

Please sign in to comment.