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

Scaladoc: fix issues with incorrect external links and special characters #14516

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix handling of the external links in Scaladoc
This includes both fixing links found in searchbar, and ones accessible
from method names. In searchbar, links are resolved based on whether
they are absolute (pointing to an external website, like java docs) or
relative (pointing to another sub-website, with root being appended on
runtime). This is achieved by serializing another boolean field in
PageEntry.
  • Loading branch information
jchyb committed Feb 22, 2022
commit ca35a822604776307bd0ae7ebb7c9824344f658a
13 changes: 8 additions & 5 deletions scaladoc-js/main/src/searchbar/PageEntry.scala
Original file line number Diff line number Diff line change
@@ -4,17 +4,19 @@ import scala.scalajs.js

@js.native
trait PageEntryJS extends js.Object {
val n: String = js.native
val t: String = js.native
val d: String = js.native
val l: String = js.native
val k: String = js.native
val n: String = js.native
val t: String = js.native
val d: String = js.native
val l: String = js.native
val e: Boolean = js.native
val k: String = js.native
}

case class PageEntry(
fullName: String,
description: String,
location: String,
isLocationExternal: Boolean,
shortName: String,
kind: String,
tokens: List[String]
@@ -34,6 +36,7 @@ object PageEntry {
jsObj.t,
jsObj.d,
jsObj.l,
jsObj.e,
jsObj.n.toLowerCase,
jsObj.k,
StringUtils.createCamelCaseTokens(jsObj.n)
7 changes: 6 additions & 1 deletion scaladoc-js/main/src/searchbar/SearchbarComponent.scala
Original file line number Diff line number Diff line change
@@ -22,7 +22,12 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
icon.classList.add(p.kind.take(2))

val resultA = document.createElement("a").asInstanceOf[html.Anchor]
resultA.href = Globals.pathToRoot + p.location
resultA.href =
if (p.isLocationExternal) {
p.location
} else {
Globals.pathToRoot + p.location
}
resultA.text = s"${p.fullName}"
resultA.onclick = (event: Event) =>
if (document.body.contains(rootDiv)) {
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@ class MatchersTest:
s"$kind $name",
"",
"",
false,
s"$name",
kind,
StringUtils.createCamelCaseTokens(name)
3 changes: 3 additions & 0 deletions scaladoc/src/dotty/tools/scaladoc/renderers/Locations.scala
Original file line number Diff line number Diff line change
@@ -85,6 +85,9 @@ trait Locations(using ctx: DocContext):
def escapedAbsolutePathWithAnchor(dri: DRI, extension: String = "html"): String =
s"${escapeUrl(absolutePath(dri, extension))}#${dri.anchor}"

def relativeInternalOrAbsoluteExternalPath(dri: DRI): String =
dri.externalLink.getOrElse(escapedAbsolutePathWithAnchor(dri))

def resolveLink(dri: DRI, url: String): String =
if URI(url).isAbsolute then url else resolveRoot(dri, url)

4 changes: 3 additions & 1 deletion scaladoc/src/dotty/tools/scaladoc/renderers/Renderer.scala
Original file line number Diff line number Diff line change
@@ -118,7 +118,9 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
val signatureRenderer = new SignatureRenderer:
def currentDri: DRI = page.link.dri
def link(dri: DRI): Option[String] =
Some(pathToPage(currentDri, dri)).filter(_ != UnresolvedLocationLink)
dri.externalLink.orElse(
Some(pathToPage(currentDri, dri)).filter(_ != UnresolvedLocationLink)
)

MemberRenderer(signatureRenderer).fullMember(m)
case t: ResolvedTemplate => siteContent(page.link.dri, t)
3 changes: 2 additions & 1 deletion scaladoc/src/dotty/tools/scaladoc/renderers/Resources.scala
Original file line number Diff line number Diff line change
@@ -133,7 +133,8 @@ trait Resources(using ctx: DocContext) extends Locations, Writer:
}.mkString

def mkEntry(dri: DRI, name: String, text: String, descr: String, kind: String) = jsonObject(
"l" -> jsonString(escapedAbsolutePathWithAnchor(dri)),
"l" -> jsonString(relativeInternalOrAbsoluteExternalPath(dri)),
"e" -> (if dri.externalLink.isDefined then rawJSON("true") else rawJSON("false")),
"n" -> jsonString(name),
"t" -> jsonString(text),
"d" -> jsonString(descr),