Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the API base URI to the Laika config #225

Open
wants to merge 2 commits into
base: series/0.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions site/src/main/scala/org/typelevel/sbt/TypelevelSitePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import laika.helium.config.Favicon
import laika.helium.config.HeliumIcon
import laika.helium.config.IconLink
import laika.helium.config.ImageLink
import laika.rewrite.link.ApiLinks
import laika.rewrite.link.LinkConfig
import laika.sbt.LaikaPlugin
import laika.theme.ThemeProvider
import mdoc.MdocPlugin
Expand All @@ -49,6 +51,9 @@ object TypelevelSitePlugin extends AutoPlugin {
settingKey[Option[ModuleID]]("The module that publishes API docs")
lazy val tlSiteApiPackage = settingKey[Option[String]](
"The top-level package for your API docs (e.g. org.typlevel.sbt)")
lazy val tlSiteApiIndexUrl =
settingKey[Option[URL]](
"The URL of the index page for the top-level package of your API docs")
lazy val tlSiteRelatedProjects =
settingKey[Seq[(String, URL)]]("A list of related projects (default: cats)")

Expand Down Expand Up @@ -85,6 +90,7 @@ object TypelevelSitePlugin extends AutoPlugin {
tlSitePublishBranch := Some("main"),
tlSitePublishTags := tlSitePublishBranch.value.isEmpty,
tlSiteApiUrl := None,
tlSiteApiIndexUrl := None,
tlSiteApiPackage := None,
tlSiteRelatedProjects := Seq(TypelevelProject.Cats),
tlSiteKeepFiles := true,
Expand All @@ -105,6 +111,12 @@ object TypelevelSitePlugin extends AutoPlugin {
.value: @nowarn("cat=other-pure-statement"),
tlSitePreview := previewTask.value,
Laika / sourceDirectories := Seq(mdocOut.value),
laikaConfig := {
val currentConfig = laikaConfig.value
tlSiteApiUrl.value.fold(currentConfig) { apiUrl =>
currentConfig.withConfigValue(LinkConfig(apiLinks = Seq(ApiLinks(apiUrl.toString))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following on the additivity issue, .withConfigValue is definitely additive. But I assume that this will override any previously set LinkConfig and esp. all apiLinks previously set?

Copy link
Member Author

@DavidGregory084 DavidGregory084 Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess only way is to test it!

scala> import laika.sbt._
import laika.sbt._

scala> import laika.rewrite.link._
import laika.rewrite.link._

scala> val conf = LaikaConfig.defaults
conf: laika.sbt.LaikaConfig = LaikaConfig(UTF-8,<function1>,laika.config.ConfigBuilder@302e25d3,laika.ast.MessageFilter$$anon$1@78ea076,None,laika.ast.MessageFilter$$anon$1@56276b5d)

scala> val confWithLinks = conf.withConfigValue(LinkConfig(apiLinks = List(ApiLinks(baseUri = "https://www.javadoc.io/doc/com.github.cb372/scalacache-unidocs_2.13/0.28.0/"))))
confWithLinks: laika.sbt.LaikaConfig = LaikaConfig(UTF-8,<function1>,laika.config.ConfigBuilder@623f79d4,laika.ast.MessageFilter$$anon$1@78ea076,None,laika.ast.MessageFilter$$anon$1@56276b5d)
                                                                                                                                                                                                           
scala> val confWithMoreLinks = confWithLinks.withConfigValue(LinkConfig(apiLinks = List(ApiLinks(baseUri = "https://www.javadoc.io/doc/org.typelevel/sbt-typelevel-docs_2.12/latest/", "org.typelevel.sbt"))))
confWithMoreLinks: laika.sbt.LaikaConfig = LaikaConfig(UTF-8,<function1>,laika.config.ConfigBuilder@1698fd02,laika.ast.MessageFilter$$anon$1@78ea076,None,laika.ast.MessageFilter$$anon$1@56276b5d)

scala> confWithLinks.configBuilder.build.get[LinkConfig]("laika.links")
res0: laika.config.Config.ConfigResult[laika.rewrite.link.LinkConfig] = Right(LinkConfig(List(),List(),List(ApiLinks(https://www.javadoc.io/doc/com.github.cb372/scalacache-unidocs_2.13/0.28.0/,*,index.html)),List()))

scala> confWithMoreLinks.configBuilder.build.get[LinkConfig]("laika.links")
res1: laika.config.Config.ConfigResult[laika.rewrite.link.LinkConfig] = Right(LinkConfig(List(),List(),List(ApiLinks(https://www.javadoc.io/doc/org.typelevel/sbt-typelevel-docs_2.12/latest/,org.typelevel.sbt,index.html)),List()))

scala>

It looks like yes, latest ApiLinks wins 😬

}
},
laikaTheme := tlSiteHeliumConfig.value.build.extend(tlSiteHeliumExtensions.value),
mdocVariables := {
mdocVariables.value ++
Expand All @@ -113,7 +125,7 @@ object TypelevelSitePlugin extends AutoPlugin {
"PRERELEASE_VERSION" -> currentPreRelease.value.getOrElse(version.value),
"SNAPSHOT_VERSION" -> version.value
) ++
tlSiteApiUrl.value.map("API_URL" -> _.toString).toMap
tlSiteApiIndexUrl.value.map("API_URL" -> _.toString).toMap
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried adding a new setting to capture the distinction between the index page + the API base URL, but I don't love that these names are now out of sync

Copy link
Member

@armanbilge armanbilge Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I think instead we should add a new setting tlSiteApiLinks that's maybe like Map[String -> url] package name to API docs url. Then we can make a best effort to populate this.

},
tlSiteHeliumExtensions := TypelevelHeliumExtensions(
licenses.value.headOption,
Expand All @@ -132,12 +144,15 @@ object TypelevelSitePlugin extends AutoPlugin {
val o = moduleId.organization
val n = cross(moduleId.name)
val v = version
val p = tlSiteApiPackage.value.fold("")(_.replace('.', '/') + "/index.html")
url(s"https://www.javadoc.io/doc/$o/$n/$v/$p")
url(s"https://www.javadoc.io/doc/$o/$n/$v/")
}

tlSiteApiUrl.value.orElse(javadocioUrl)
},
tlSiteApiIndexUrl := tlSiteApiUrl.value.map { apiURL =>
val apiIndex = tlSiteApiPackage.value.fold("")(_.replace('.', '/') + "/index.html")
apiURL.toURI.resolve(apiIndex).toURL
},
tlSiteHeliumConfig := {
Helium
.defaults
Expand Down Expand Up @@ -170,9 +185,9 @@ object TypelevelSitePlugin extends AutoPlugin {
"https://typelevel.org",
Image.external(s"https://typelevel.org/img/logo.svg")
),
navLinks = tlSiteApiUrl.value.toList.map { url =>
navLinks = tlSiteApiIndexUrl.value.toList.map { apiIndex =>
IconLink.external(
url.toString,
apiIndex.toString,
HeliumIcon.api,
options = Styles("svg-link")
)
Expand Down