Skip to content

Commit ac9fd79

Browse files
Statically expand the sidebar
1 parent 9f4c1ad commit ac9fd79

File tree

5 files changed

+24
-26
lines changed

5 files changed

+24
-26
lines changed

doc-tool/resources/_includes/sidebar.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<nav class="sidebar">
22
<ul class="toc">
33
<!-- Redacted documents -->
4-
{% assign parent = page.path | first %}
54
{% for title in sidebar.titles %}
6-
{% renderTitle title, parent %}
5+
{% renderTitle title, page.url %}
76
{% endfor %}
87
<!-- API documentation -->
98
{% if docs.size > 0 %}

doc-tool/resources/css/sidebar.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
padding-left: 1em;
4242
display: none;
4343
}
44+
.sidebar li.section ul.toggled {
45+
display: block;
46+
}
4447

4548
.sidebar li.section.index-entities ul {
4649
padding-left: 0;

doc-tool/resources/js/sidebar.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
// Toggles a sidebar section
22
function toggleSection(titleElement) {
33
const title = $(titleElement);
4-
title.siblings("ul").toggle();
4+
title.siblings("ul").toggleClass("toggled");
55
title.children("i.fas").toggleClass("fa-angle-right").toggleClass("fa-angle-down");
66
}
7-
8-
// Unfolds the sidebar section corresponding to the current page
9-
(function () {
10-
let activeEntry = document.querySelector("#active-toc-entry");
11-
while (activeEntry != null && activeEntry.tagName.toLowerCase() === "ul") {
12-
activeEntry.style.display = "block";
13-
activeEntry = activeEntry.parentElement.parentElement;
14-
}
15-
})();

doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ case class DefaultParams(
6969
}
7070

7171
case class PageInfo(url: String, date: String = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).toString ) {
72-
val path: Array[String] = url.split('/').reverse.drop(1)
72+
val path: Array[String] = url.split('/').drop(1)
7373
}
7474

7575
case class SiteInfo(

doc-tool/src/dotty/tools/dottydoc/staticsite/tags.scala

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,25 @@ object tags {
157157
* `Title`.
158158
*
159159
* ```html
160-
* {% renderTitle title, parent %}
160+
* {% renderTitle title, page.url %}
161161
* ```
162-
*
163-
* The rendering currently works on depths up to 2. This means that each
164-
* title can have a subsection with its own titles.
165162
*/
166163
case class RenderTitle(params: Map[String, AnyRef])(implicit ctx: Context)
167164
extends Tag("renderTitle") with ParamConverter {
168-
private def renderTitle(t: Title, parent: String): String = {
165+
private def isParent(t: Title, htmlPath: String): Boolean = {
166+
t.url match {
167+
case Some(url) => url == htmlPath
168+
case None => t.subsection.exists(isParent(_, htmlPath))
169+
}
170+
}
171+
private def renderTitle(t: Title, pageUrl: String): String = {
169172
if (!t.url.isDefined && t.subsection.nonEmpty) {
173+
val htmlPath = pageUrl.replace(".md", ".html")
174+
val marker = if (isParent(t, htmlPath)) "class=\"toggled\"" else ""
170175
s"""|<li class="section">
171176
| <a onclick='toggleSection(this);'>${t.title}</a>
172-
| <ul id="${ if(parent == t.title.toLowerCase.split(" ").mkString("-")) "active-toc-entry" else "" }">
173-
| ${ t.subsection.map(renderTitle(_, parent)).mkString("\n") }
177+
| <ul $marker>
178+
| ${ t.subsection.map(renderTitle(_, htmlPath)).mkString("\n") }
174179
| </ul>
175180
|</li>
176181
|""".stripMargin
@@ -188,13 +193,13 @@ object tags {
188193
}
189194

190195
override def render(ctx: TemplateContext, nodes: LNode*): AnyRef =
191-
(nodes(0).render(ctx), nodes(1).render(ctx)) match {
192-
case (map: JMap[String, AnyRef] @unchecked, parent: String) =>
193-
Title(map).map(renderTitle(_, parent)).getOrElse(null)
196+
((nodes(0).render(ctx), nodes(1).render(ctx)) match {
197+
case (map: JMap[String, AnyRef] @unchecked, url: String) =>
198+
Title(map).map(renderTitle(_, url))
194199
case (map: JMap[String, AnyRef] @unchecked, _) =>
195-
Title(map).map(renderTitle(_, "./")).getOrElse(null) // file is in top dir
196-
case _ => null
197-
}
200+
Title(map).map(renderTitle(_, "./")) // file is in top dir
201+
case _ => None
202+
}).orNull
198203
}
199204

200205
/** Allows the extraction of docstrings from the given path. E.g:

0 commit comments

Comments
 (0)