Skip to content

Commit

Permalink
feat(sidebar): add consolidation to fmt-sidebars
Browse files Browse the repository at this point in the history
This removes unused titles and hashes in the sidebars l10n.
  • Loading branch information
fiji-flo committed Dec 18, 2024
1 parent e56abb6 commit 9624a86
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/rari-doc/src/html/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn build_sidebars(doc: &Doc) -> Result<Option<String>, DocError> {
pub struct SidebarL10n {
// Keep the translations in order of insertion,
// so Sidebar manipulations are deterministic.
l10n: IndexMap<Locale, IndexMap<String, String>>,
pub l10n: IndexMap<Locale, IndexMap<String, String>>,
}

impl SidebarL10n {
Expand Down
60 changes: 58 additions & 2 deletions crates/rari-tools/src/sidebars.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;

Expand Down Expand Up @@ -42,8 +42,64 @@ pub fn sync_sidebars() -> Result<(), ToolError> {
Ok(())
}

fn traverse_and_extract_l10nable<'a, 'b: 'a>(entry: &'a SidebarEntry) -> HashSet<&'a str> {
let (title, hash, children) = match entry {
SidebarEntry::Section(basic_entry) => (
basic_entry.title.as_deref(),
basic_entry.hash.as_deref(),
Some(&basic_entry.children),
),
SidebarEntry::ListSubPages(sub_page_entry) => (
sub_page_entry.title.as_deref(),
sub_page_entry.hash.as_deref(),
None,
),
SidebarEntry::ListSubPagesGrouped(sub_page_entry) => (
sub_page_entry.title.as_deref(),
sub_page_entry.hash.as_deref(),
None,
),
SidebarEntry::WebExtApi(web_ext_api_entry) => {
(Some(web_ext_api_entry.title.as_str()), None, None)
}
SidebarEntry::Default(basic_entry) => (
basic_entry.title.as_deref(),
basic_entry.hash.as_deref(),
Some(&basic_entry.children),
),
_ => (None, None, None),
};
let mut set: HashSet<_> = if let Some(children) = children {
children
.iter()
.flat_map(traverse_and_extract_l10nable)
.collect()
} else {
Default::default()
};
if let Some(hash) = hash {
set.insert(hash);
}
if let Some(title) = title {
set.insert(title);
}
set
}

fn consolidate_l10n(sidebar: &mut Sidebar) {
let titles = sidebar
.sidebar
.iter()
.flat_map(traverse_and_extract_l10nable)
.collect::<HashSet<_>>();
for (_, map) in sidebar.l10n.l10n.iter_mut() {
map.retain(|k, _| titles.contains(k.as_str()));
}
}

pub fn fmt_sidebars() -> Result<(), ToolError> {
for (path, sidebar) in read_sidebars()? {
for (path, mut sidebar) in read_sidebars()? {
consolidate_l10n(&mut sidebar);
write_sidebar(&sidebar, &path)?;
}
Ok(())
Expand Down

0 comments on commit 9624a86

Please sign in to comment.