Skip to content

Commit 61ea7f5

Browse files
authored
Only highlight the active nav link (#1067)
1 parent ff4e23e commit 61ea7f5

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

pgml-dashboard/src/api/docs.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,9 @@ async fn render<'a>(
140140
.to_str()
141141
.expect("path must convert to a string")
142142
.to_string();
143-
let mut url = path.clone();
143+
let url = path.clone();
144144
if path.ends_with("/") {
145145
path.push_str("README");
146-
url.push_str("./");
147146
}
148147

149148
// Get the document content
@@ -153,8 +152,14 @@ async fn render<'a>(
153152

154153
// Read to string
155154
let contents = match tokio::fs::read_to_string(&path).await {
156-
Ok(contents) => contents,
157-
Err(_) => return Err(Status::NotFound),
155+
Ok(contents) => {
156+
info!("loading markdown file: '{:?}", path);
157+
contents
158+
}
159+
Err(err) => {
160+
warn!("Error parsing markdown file: '{:?}' {:?}", path, err);
161+
return Err(Status::NotFound);
162+
}
158163
};
159164
let parts = contents.split("---").collect::<Vec<&str>>();
160165
let ((image, description), contents) = if parts.len() > 1 {

pgml-dashboard/src/templates/docs.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct NavLink {
1212
pub href: String,
1313
pub children: Vec<NavLink>,
1414
pub open: bool,
15+
pub active: bool,
1516
}
1617

1718
impl NavLink {
@@ -23,6 +24,7 @@ impl NavLink {
2324
href: "#".to_owned(),
2425
children: vec![],
2526
open: false,
27+
active: false,
2628
}
2729
}
2830

@@ -42,20 +44,14 @@ impl NavLink {
4244
/// Automatically expand the link and it's parents
4345
/// when one of the children is visible.
4446
pub fn should_open(&mut self, path: &str) -> bool {
45-
self.open = self.href.contains(&path);
46-
let open = if self.children.is_empty() {
47-
self.open
48-
} else {
49-
for child in self.children.iter_mut() {
50-
if child.should_open(path) {
51-
self.open = true;
52-
}
47+
self.active = self.href.ends_with(&path);
48+
self.open = self.active;
49+
for child in self.children.iter_mut() {
50+
if child.should_open(path) {
51+
self.open = true;
5352
}
54-
55-
self.open
56-
};
57-
58-
open
53+
}
54+
self.open
5955
}
6056
}
6157

pgml-dashboard/src/utils/markdown.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,12 @@ pub fn get_sub_links(list: &markdown::mdast::List) -> Result<Vec<NavLink>> {
572572
for node in link.children.iter() {
573573
match node {
574574
markdown::mdast::Node::Text(text) => {
575-
let mut url =
576-
Path::new(&link.url).with_extension("");
575+
let mut url = Path::new(&link.url)
576+
.with_extension("")
577+
.to_string_lossy()
578+
.to_string();
577579
if url.ends_with("README") {
578-
url =
579-
url.parent().unwrap().join("./").into();
580+
url = url.replace("README", "");
580581
}
581582
let url = Path::new("/docs/guides")
582583
.join(url)

pgml-dashboard/templates/components/link.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="nav flex-column" role="tablist" aria-orientation="vertical">
22
<%
3-
let color = if open {
3+
4+
let color = if active {
45
"purple"
56
} else {
67
""

0 commit comments

Comments
 (0)