Skip to content

Commit 2e1021d

Browse files
committed
Fix links to parent packages
1 parent a7085f1 commit 2e1021d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

scala3doc/src/dotty/dokka/site/StaticSiteLocationProvider.scala

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,10 @@ class StaticSiteLocationProvider(pageNode: RootPageNode)(using ctx: DokkaContext
112112
override def pathTo(node: PageNode, context: PageNode): String =
113113
if node == context then ""
114114
else
115-
val nodePaths = getPathsIndex.get(node).asScala
116-
val contextPaths = Option(context).fold(Nil)(getPathsIndex.get(_).asScala.dropRight(1))
117-
val commonPaths = nodePaths.zip(contextPaths).takeWhile{ case (a, b) => a == b }.size
115+
val nodePaths = getPathsIndex.get(node).asScala.toList
116+
val contextPaths = Option(context).fold(Nil)(getPathsIndex.get(_).asScala).toList
117+
relativePath(contextPaths, nodePaths)
118118

119-
val contextPath = contextPaths.drop(commonPaths).map(_ => "..")
120-
val nodePath = nodePaths.drop(commonPaths) match
121-
case l if l.isEmpty => Seq("index")
122-
case l => l
123-
(contextPath ++ nodePath).mkString("/")
124119

125120
val externalLocationProviders: List[(List[Regex], ExternalLocationProvider)] =
126121
val sourceSet = ctx.getConfiguration.getSourceSets.asScala(0)
@@ -155,4 +150,15 @@ class StaticSiteLocationProvider(pageNode: RootPageNode)(using ctx: DokkaContext
155150
regexes.exists(r => r.matches(path))
156151
}.fold(null)(_(1).resolve(dri.withNoOrigin))
157152
case None => null
158-
}
153+
}
154+
155+
def relativePath(fullFrom: Seq[String], to: Seq[String]): String =
156+
val from = fullFrom.dropRight(1)
157+
val commonPaths = to.zip(from).takeWhile{ case (a, b) => a == b }.size
158+
159+
val contextPath = from.drop(commonPaths).map(_ => "..")
160+
val nodePath = to.drop(commonPaths) match
161+
case Nil if contextPath.isEmpty && to.nonEmpty=> Seq("..", to.last)
162+
case Nil => Seq("index")
163+
case l => l
164+
(contextPath ++ nodePath).mkString("/")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dotty.dokka.site
2+
3+
import org.junit.Test
4+
import org.junit.Assert._
5+
6+
class RelativePathsLink:
7+
8+
@Test
9+
def testLinks() =
10+
def path(from: String, to: String) =
11+
relativePath(from.split('/').toList, to.split('/').toList)
12+
13+
assertEquals(relativePath(Nil, Seq("a", "b")), "a/b")
14+
15+
assertEquals(
16+
path("api/dotty/dokka/tasty/comments/wiki", "api/dotty/dokka/tasty/comments"),
17+
"../comments"
18+
)
19+
20+
assertEquals(
21+
path("api/dotty/dokka/tasty/comments/wiki", "api/index"),
22+
"../../../../index"
23+
)
24+

0 commit comments

Comments
 (0)