Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sidebars): add support for depth and nested #78

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions crates/rari-doc/src/helpers/subpages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct ListSubPagesContext<'a> {
pub include_parent: bool,
}

pub fn list_sub_pages_internal(
pub fn list_sub_pages_flattened_internal(
out: &mut String,
url: &str,
locale: Locale,
Expand All @@ -149,8 +149,7 @@ pub fn list_sub_pages_internal(
include_parent,
}: ListSubPagesContext<'_>,
) -> Result<(), DocError> {
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
let depth = depth.map(|i| i.saturating_sub(1));
let sub_pages = get_sub_pages(url, depth, sorter.unwrap_or_default())?;
if include_parent {
let page = Page::from_url_with_locale_and_fallback(url, locale)?;
write_parent_li(out, &page, locale)?;
Expand All @@ -159,15 +158,46 @@ pub fn list_sub_pages_internal(
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
continue;
}
write_li_with_badges(out, &sub_page, locale, code, true)?;
}
Ok(())
}
pub fn list_sub_pages_nested_internal(
out: &mut String,
url: &str,
locale: Locale,
depth: Option<usize>,
ListSubPagesContext {
sorter,
page_types,
code,
include_parent,
}: ListSubPagesContext<'_>,
) -> Result<(), DocError> {
if depth == Some(0) {
return Ok(());
}
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
let depth = depth.map(|i| i.saturating_sub(1));
if include_parent {
let page = Page::from_url_with_locale_and_fallback(url, locale)?;
write_parent_li(out, &page, locale)?;
}
for sub_page in sub_pages {
let page_type_match = page_types.is_empty() || page_types.contains(&sub_page.page_type());
let sub_sub_pages = get_sub_pages(sub_page.url(), depth, sorter.unwrap_or_default())?;
if sub_sub_pages.is_empty() {
write_li_with_badges(out, &sub_page, locale, code, true)?;
if page_type_match {
write_li_with_badges(out, &sub_page, locale, code, true)?;
}
} else {
write_li_with_badges(out, &sub_page, locale, code, false)?;
out.push_str("<ol>");
if page_type_match {
write_li_with_badges(out, &sub_page, locale, code, false)?;
}
let mut sub_pages_out = String::new();

list_sub_pages_internal(
out,
list_sub_pages_nested_internal(
&mut sub_pages_out,
sub_page.url(),
locale,
depth,
Expand All @@ -178,25 +208,32 @@ pub fn list_sub_pages_internal(
include_parent,
},
)?;
out.push_str("</ol>");
out.push_str("</li>");
if !sub_pages_out.is_empty() {
out.push_str("<ol>");
out.push_str(&sub_pages_out);
out.push_str("</ol>");
}
if page_type_match {
out.push_str("</li>");
}
}
}
Ok(())
}

pub fn list_sub_pages_grouped_internal(
pub fn list_sub_pages_flattened_grouped_internal(
out: &mut String,
url: &str,
locale: Locale,
depth: Option<usize>,
ListSubPagesContext {
sorter,
page_types,
code,
include_parent,
}: ListSubPagesContext<'_>,
) -> Result<(), DocError> {
let sub_pages = get_sub_pages(url, None, sorter.unwrap_or_default())?;
let sub_pages = get_sub_pages(url, depth, sorter.unwrap_or_default())?;

let mut grouped = BTreeMap::new();
for sub_page in sub_pages.iter() {
Expand Down
Loading
Loading