Implement basic collapsible sidebar items

This commit is contained in:
Vincent Esche 2017-04-18 15:35:22 +02:00
parent 9602acce80
commit ca4df4fe4b
3 changed files with 26 additions and 10 deletions

View File

@ -24,7 +24,9 @@ impl HelperDef for RenderToc {
let mut current_level = 1; let mut current_level = 1;
for item in decoded { let mut peekable = decoded.into_iter().peekable();
while let Some(item) = peekable.next() {
let next_item = peekable.peek();
// Spacer // Spacer
if item.get("spacer").is_some() { if item.get("spacer").is_some() {
@ -32,15 +34,13 @@ impl HelperDef for RenderToc {
continue; continue;
} }
let level = if let Some(s) = item.get("section") { let level = item.get("section").map(|s| s.matches(".").count()).unwrap_or(1);
s.matches(".").count() let next_level = next_item.map(|i| {
} else { i.get("section").map(|s| s.matches(".").count()).unwrap_or(1)
1 }).unwrap_or(level);
};
if level > current_level { if level > current_level {
while level > current_level { while level > current_level {
try!(rc.writer.write_all("<li>".as_bytes()));
try!(rc.writer.write_all("<ul class=\"section\">".as_bytes())); try!(rc.writer.write_all("<ul class=\"section\">".as_bytes()));
current_level += 1; current_level += 1;
} }
@ -48,6 +48,7 @@ impl HelperDef for RenderToc {
} else if level < current_level { } else if level < current_level {
while level < current_level { while level < current_level {
try!(rc.writer.write_all("</ul>".as_bytes())); try!(rc.writer.write_all("</ul>".as_bytes()));
try!(rc.writer.write_all("</details>".as_bytes()));
try!(rc.writer.write_all("</li>".as_bytes())); try!(rc.writer.write_all("</li>".as_bytes()));
current_level -= 1; current_level -= 1;
} }
@ -60,6 +61,11 @@ impl HelperDef for RenderToc {
try!(rc.writer.write_all(">".as_bytes())); try!(rc.writer.write_all(">".as_bytes()));
} }
if level < next_level {
try!(rc.writer.write_all("<details open>".as_bytes()));
try!(rc.writer.write_all("<summary>".as_bytes()));
}
// Link // Link
let path_exists = if let Some(path) = item.get("path") { let path_exists = if let Some(path) = item.get("path") {
if !path.is_empty() { if !path.is_empty() {
@ -122,12 +128,15 @@ impl HelperDef for RenderToc {
if path_exists { if path_exists {
try!(rc.writer.write_all("</a>".as_bytes())); try!(rc.writer.write_all("</a>".as_bytes()));
} }
if level < next_level {
try!(rc.writer.write_all("</li>".as_bytes())); try!(rc.writer.write_all("</summary>".as_bytes()));
} else {
try!(rc.writer.write_all("</li>".as_bytes()));
}
} }
while current_level > 1 { while current_level > 1 {
try!(rc.writer.write_all("</ul>".as_bytes())); try!(rc.writer.write_all("</ul>".as_bytes()));
try!(rc.writer.write_all("</details>".as_bytes()));
try!(rc.writer.write_all("</li>".as_bytes())); try!(rc.writer.write_all("</li>".as_bytes()));
current_level -= 1; current_level -= 1;
} }

View File

@ -40,6 +40,9 @@ table td {
table thead td { table thead td {
font-weight: 700; font-weight: 700;
} }
details > summary {
outline: none;
}
.sidebar { .sidebar {
position: absolute; position: absolute;
left: 0; left: 0;

View File

@ -38,3 +38,7 @@ table {
td { font-weight: 700; } td { font-weight: 700; }
} }
} }
details > summary {
outline: none;
}