Skip to content

Commit a9876a8

Browse files
committed
Fix handling relative links for static site
1 parent 6fbe38b commit a9876a8

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

scaladoc/src/dotty/tools/scaladoc/renderers/Renderer.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ abstract class Renderer(rootPackage: Member, val members: Map[DRI, Member], prot
5757
// Let's gather the list of maps for each template with its in-order neighbours
5858
val newSettings: Seq[Map[String, Object]] = allTemplates.sliding(size = 3, step = 1).map {
5959
case prev :: mid :: next :: Nil =>
60-
val currDri = siteContext.driFor(mid.get.file.toPath)
61-
def dri(sibling: Option[LoadedTemplate]): Option[String] =
62-
sibling.map(n => siteContext.driFor(n.file.toPath)).flatMap { dri =>
63-
Some(pathToPage(currDri, dri)).filter(_ != UnresolvedLocationLink)
60+
def link(sibling: Option[LoadedTemplate]): Option[String] =
61+
def realPath(path: Path) = if Files.isDirectory(path) then Paths.get(path.toString, "index.html") else path
62+
sibling.map { n =>
63+
val realMidPath = realPath(mid.get.file.toPath)
64+
val realSiblingPath = realPath(n.file.toPath)
65+
realMidPath.relativize(realSiblingPath).toString.stripPrefix("../")
6466
}
65-
List(dri(prev).map("previous" -> _), dri(next).map("next" -> _)).flatten.toMap
67+
List(link(prev).map("previous" -> _), link(next).map("next" -> _)).flatten.toMap
6668
}.toSeq
6769

6870
// We update the immutable tree of templates by walking in-order

scaladoc/src/dotty/tools/scaladoc/renderers/SiteRenderer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait SiteRenderer(using DocContext) extends Locations:
3333
case HashRegex(path, prefix) => (path, prefix)
3434
case _ => (str, "")
3535

36-
val res = ctx.driForLink(content.template.templateFile, path).filter(driExists)
36+
val res = ctx.driForLink(content.template.file, path).filter(driExists)
3737
res.headOption.map(pathToPage(pageDri, _) + prefix)
3838

3939
def processLocalLink(str: String): String =

scaladoc/src/dotty/tools/scaladoc/site/StaticSiteContext.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class StaticSiteContext(
9090
val indexFiles = from.listFiles { file => file.getName == "index.md" || file.getName == "index.html" }
9191
indexes match
9292
case Nil => emptyTemplate(from, from.getName)
93-
case Seq(loadedTemplate) => loadedTemplate.templateFile.copy(file = from)
93+
case Seq(loadedTemplate) => loadedTemplate.templateFile
9494
case _ =>
9595
// TODO (https://github.com/lampepfl/scaladoc/issues/238): provide proper error handling
9696
val msg = s"ERROR: Multiple index pages for $from found in ${indexes.map(_.file)}"
@@ -145,9 +145,9 @@ class StaticSiteContext(
145145
case t: TemplateName.YamlDefined => t
146146
case _: TemplateName.FilenameDefined => TemplateName.SidebarDefined(title)
147147
case t: TemplateName.SidebarDefined => t // should never reach this path
148-
LoadedTemplate(template.copy(settings = template.settings + ("title" -> newTitle.name), file = file, title = newTitle), children, file)
148+
LoadedTemplate(template.copy(settings = template.settings + ("title" -> newTitle.name), title = newTitle), children, file)
149149
case None =>
150-
LoadedTemplate(template.copy(settings = template.settings, file = file), children, file)
150+
LoadedTemplate(template.copy(settings = template.settings), children, file)
151151

152152
case Sidebar.Category(optionTitle, optionIndexPath, nested) =>
153153
optionIndexPath match
@@ -157,7 +157,7 @@ class StaticSiteContext(
157157
val title = optionTitle match
158158
case Some(t) => t
159159
case None => "index"
160-
val fakeFile = new File(new File(root, "docs"), title)
160+
val fakeFile = Paths.get(root.toString, "docs", title, "index.html").toFile
161161
LoadedTemplate(
162162
args.defaultTemplate.fold(emptyTemplate(fakeFile, title))(layouts(_).copy(title = TemplateName.FilenameDefined(title))),
163163
nested.map(loadSidebarContent),
@@ -169,22 +169,25 @@ class StaticSiteContext(
169169
dir("docs").flatMap(_.listFiles()).flatMap(loadTemplate(_, isBlog = false))
170170
++ dir("blog").flatMap(loadTemplate(_, isBlog = true))
171171

172-
def driForLink(template: TemplateFile, link: String): Seq[DRI] =
172+
def driForLink(loadedTemplateFile: File, link: String): Seq[DRI] =
173173
val pathsDri: Option[Seq[DRI]] = Try {
174174
val baseFile =
175-
if link.startsWith("/") then root.toPath.resolve(link.drop(1))
176-
else template.file.toPath.getParent().resolve(link).normalize()
175+
if
176+
link.startsWith("/") then root.toPath.resolve(link.drop(1))
177+
else
178+
val path = loadedTemplateFile.toPath
179+
(if Files.isDirectory(path) then path else path.getParent).resolve(link).normalize
177180

178181
val fileName = baseFile.getFileName.toString
179182
val baseFileName = if fileName.endsWith(".md")
180183
then fileName.stripSuffix(".md")
181184
else fileName.stripSuffix(".html")
182-
183-
Seq(
185+
(Seq(
184186
Some(baseFile.resolveSibling(baseFileName + ".html")),
185-
Some(baseFile.resolveSibling(baseFileName + ".md")),
187+
Some(baseFile.resolveSibling(baseFileName + ".md"))
188+
).flatten.filter(Files.exists(_)) ++ Seq(
186189
Option.when(baseFileName == "index")(baseFile.getParent)
187-
).flatten.filter(Files.exists(_)).map(driFor)
190+
).flatten).map(driFor)
188191
}.toOption.filter(_.nonEmpty)
189192
pathsDri.getOrElse(memberLinkResolver(link).toList)
190193

0 commit comments

Comments
 (0)