Skip to content

Commit 84b6358

Browse files
authored
feat(sidebars): add support for depth and nested (#78)
* feat(sidebars): add support for depth and nested * reorder properties
1 parent 3e015ab commit 84b6358

File tree

4 files changed

+322
-166
lines changed

4 files changed

+322
-166
lines changed

Diff for: crates/rari-doc/src/helpers/subpages.rs

+49-12
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub struct ListSubPagesContext<'a> {
137137
pub include_parent: bool,
138138
}
139139

140-
pub fn list_sub_pages_internal(
140+
pub fn list_sub_pages_flattened_internal(
141141
out: &mut String,
142142
url: &str,
143143
locale: Locale,
@@ -149,8 +149,7 @@ pub fn list_sub_pages_internal(
149149
include_parent,
150150
}: ListSubPagesContext<'_>,
151151
) -> Result<(), DocError> {
152-
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
153-
let depth = depth.map(|i| i.saturating_sub(1));
152+
let sub_pages = get_sub_pages(url, depth, sorter.unwrap_or_default())?;
154153
if include_parent {
155154
let page = Page::from_url_with_locale_and_fallback(url, locale)?;
156155
write_parent_li(out, &page, locale)?;
@@ -159,15 +158,46 @@ pub fn list_sub_pages_internal(
159158
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
160159
continue;
161160
}
161+
write_li_with_badges(out, &sub_page, locale, code, true)?;
162+
}
163+
Ok(())
164+
}
165+
pub fn list_sub_pages_nested_internal(
166+
out: &mut String,
167+
url: &str,
168+
locale: Locale,
169+
depth: Option<usize>,
170+
ListSubPagesContext {
171+
sorter,
172+
page_types,
173+
code,
174+
include_parent,
175+
}: ListSubPagesContext<'_>,
176+
) -> Result<(), DocError> {
177+
if depth == Some(0) {
178+
return Ok(());
179+
}
180+
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
181+
let depth = depth.map(|i| i.saturating_sub(1));
182+
if include_parent {
183+
let page = Page::from_url_with_locale_and_fallback(url, locale)?;
184+
write_parent_li(out, &page, locale)?;
185+
}
186+
for sub_page in sub_pages {
187+
let page_type_match = page_types.is_empty() || page_types.contains(&sub_page.page_type());
162188
let sub_sub_pages = get_sub_pages(sub_page.url(), depth, sorter.unwrap_or_default())?;
163189
if sub_sub_pages.is_empty() {
164-
write_li_with_badges(out, &sub_page, locale, code, true)?;
190+
if page_type_match {
191+
write_li_with_badges(out, &sub_page, locale, code, true)?;
192+
}
165193
} else {
166-
write_li_with_badges(out, &sub_page, locale, code, false)?;
167-
out.push_str("<ol>");
194+
if page_type_match {
195+
write_li_with_badges(out, &sub_page, locale, code, false)?;
196+
}
197+
let mut sub_pages_out = String::new();
168198

169-
list_sub_pages_internal(
170-
out,
199+
list_sub_pages_nested_internal(
200+
&mut sub_pages_out,
171201
sub_page.url(),
172202
locale,
173203
depth,
@@ -178,25 +208,32 @@ pub fn list_sub_pages_internal(
178208
include_parent,
179209
},
180210
)?;
181-
out.push_str("</ol>");
182-
out.push_str("</li>");
211+
if !sub_pages_out.is_empty() {
212+
out.push_str("<ol>");
213+
out.push_str(&sub_pages_out);
214+
out.push_str("</ol>");
215+
}
216+
if page_type_match {
217+
out.push_str("</li>");
218+
}
183219
}
184220
}
185221
Ok(())
186222
}
187223

188-
pub fn list_sub_pages_grouped_internal(
224+
pub fn list_sub_pages_flattened_grouped_internal(
189225
out: &mut String,
190226
url: &str,
191227
locale: Locale,
228+
depth: Option<usize>,
192229
ListSubPagesContext {
193230
sorter,
194231
page_types,
195232
code,
196233
include_parent,
197234
}: ListSubPagesContext<'_>,
198235
) -> Result<(), DocError> {
199-
let sub_pages = get_sub_pages(url, None, sorter.unwrap_or_default())?;
236+
let sub_pages = get_sub_pages(url, depth, sorter.unwrap_or_default())?;
200237

201238
let mut grouped = BTreeMap::new();
202239
for sub_page in sub_pages.iter() {

0 commit comments

Comments
 (0)