Skip to content

Commit

Permalink
feat(content): add sync-sidebars command
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Nov 27, 2024
1 parent 8e4b4aa commit a4d4686
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 39 deletions.
7 changes: 6 additions & 1 deletion crates/rari-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rari_tools::history::gather_history;
use rari_tools::r#move::r#move;
use rari_tools::redirects::fix_redirects;
use rari_tools::remove::remove;
use rari_tools::sidebars::fmt_sidebars;
use rari_tools::sidebars::{fmt_sidebars, sync_sidebars};
use rari_tools::sync_translated_content::sync_translated_content;
use rari_types::globals::{build_out_root, content_root, content_translated_root, SETTINGS};
use rari_types::locale::Locale;
Expand Down Expand Up @@ -88,6 +88,8 @@ enum ContentSubcommand {
SyncTranslatedContent(SyncTranslatedContentArgs),
/// Formats all sidebars.
FmtSidebars,
/// Sync sidebars with redirects
SyncSidebars,
/// Fixes redirects across all locales.
///
/// This shortens multiple redirect chains to single ones.
Expand Down Expand Up @@ -399,6 +401,9 @@ fn main() -> Result<(), Error> {
ContentSubcommand::FmtSidebars => {
fmt_sidebars()?;
}
ContentSubcommand::SyncSidebars => {
sync_sidebars()?;
}
ContentSubcommand::FixRedirects => {
fix_redirects()?;
}
Expand Down
46 changes: 35 additions & 11 deletions crates/rari-doc/src/html/sidebar.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::borrow::Cow;
use std::collections::HashMap;
pub use std::ops::Deref;
use std::sync::{Arc, LazyLock};

use constcat::concat;
use dashmap::DashMap;
use indexmap::IndexMap;
use rari_types::fm_types::PageType;
use rari_types::globals::cache_content;
use rari_types::locale::Locale;
use rari_types::locale::{default_locale, Locale};
use rari_utils::concat_strs;
use scraper::{Html, Node, Selector};
use serde::{Deserialize, Serialize, Serializer};
Expand Down Expand Up @@ -547,19 +549,41 @@ impl SidebarMetaEntry {
child.render(out, locale, slug, l10n)?;
}
}
MetaChildren::ListSubPages(url, page_types, include_parent) => list_sub_pages_internal(
out,
url,
locale,
Some(1),
None,
page_types,
*include_parent,
)?,
MetaChildren::ListSubPages(url, page_types, include_parent) => {
let url = if url.starts_with(concat!("/", default_locale().as_url_str(), "/")) {
Cow::Borrowed(url)
} else {
Cow::Owned(concat_strs!(
"/",
Locale::default().as_url_str(),
"/docs",
url
))
};
list_sub_pages_internal(
out,
&url,
locale,
Some(1),
None,
page_types,
*include_parent,
)?
}
MetaChildren::ListSubPagesGrouped(url, page_types, include_parent) => {
let url = if url.starts_with(concat!("/", default_locale().as_url_str(), "/")) {
Cow::Borrowed(url)
} else {
Cow::Owned(concat_strs!(
"/",
Locale::default().as_url_str(),
"/docs",
url
))
};
list_sub_pages_grouped_internal(
out,
url,
&url,
locale,
None,
page_types,
Expand Down
7 changes: 6 additions & 1 deletion crates/rari-tools/src/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ fn do_move(
update_sidebars(
&pairs
.iter()
.map(|(from, to)| (from.clone(), Some(to.clone())))
.map(|(from, to)| {
(
Cow::Borrowed(from.as_str()),
Some(Cow::Borrowed(to.as_str())),
)
})
.collect::<Vec<_>>(),
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-tools/src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn do_remove(
.iter()
.map(|slug| {
let url = build_url(slug, locale, PageCategory::Doc)?;
Ok((url, None))
Ok((Cow::Owned(url), None))
})
.collect::<Result<Vec<_>, ToolError>>()?;
update_sidebars(&pairs)?;
Expand Down
77 changes: 52 additions & 25 deletions crates/rari-tools/src/sidebars.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fs;
use std::path::Path;

use const_format::concatcp;
use pretty_yaml::config::{FormatOptions, LanguageOptions};
use rari_doc::html::sidebar::{BasicEntry, Sidebar, SidebarEntry, SubPageEntry, WebExtApiEntry};
use rari_types::globals::content_root;
use rari_types::locale::default_locale;
use rari_types::locale::{default_locale, Locale};
use rari_utils::concat_strs;

use crate::error::ToolError;
use crate::redirects::{read_redirects_raw, redirects_path};

const PREFIX: &str = "# Do not add comments to this file. They will be lost.\n\n";
static SIDEBAR_PATH_PREFIX: &str = concatcp!("/", default_locale().as_url_str(), "/docs");
static EN_US_DOCS_PREFIX: &str = concatcp!("/", default_locale().as_url_str(), "/docs");

type Pair<'a> = (Cow<'a, str>, Option<Cow<'a, str>>);
type Pairs<'a> = &'a [Pair<'a>];

pub fn sync_sidebars() -> Result<(), ToolError> {
let mut redirects = HashMap::new();
let path = redirects_path(Locale::default())?;
read_redirects_raw(&path, &mut redirects)?;
let pairs = redirects
.iter()
.map(|(from, to)| {
(
from.strip_prefix(EN_US_DOCS_PREFIX)
.map(Cow::Borrowed)
.unwrap_or(Cow::Borrowed(from)),
Some(
to.strip_prefix(EN_US_DOCS_PREFIX)
.map(Cow::Borrowed)
.unwrap_or(Cow::Borrowed(to)),
),
)
})
.collect::<Vec<_>>();
update_sidebars(&pairs)?;
Ok(())
}

pub fn fmt_sidebars() -> Result<(), ToolError> {
for (path, sidebar) in read_sidebars()? {
Expand All @@ -20,30 +49,28 @@ pub fn fmt_sidebars() -> Result<(), ToolError> {
Ok(())
}

pub(crate) fn update_sidebars(pairs: &[(String, Option<String>)]) -> Result<(), ToolError> {
pub(crate) fn update_sidebars(pairs: Pairs<'_>) -> Result<(), ToolError> {
let sidebars = read_sidebars()?;

// add leading slash to pairs, because that is what the sidebars use
let pairs = &pairs
.iter()
.map(|(from, to)| {
let from = if from.starts_with('/') {
from.to_string()
Cow::Borrowed(from.as_ref())
} else {
concat_strs!("/", from)
Cow::Owned(concat_strs!("/", from))
};
let to = if let Some(to) = to {
let to = to.as_ref().map(|to| {
if to.starts_with('/') {
Some(to.to_string())
Cow::Borrowed(to.as_ref())
} else {
Some(concat_strs!("/", to))
Cow::Owned(concat_strs!("/", to))
}
} else {
None
};
});
(from, to)
})
.collect::<Vec<(String, Option<String>)>>();
.collect::<Vec<Pair<'_>>>();

// Walk the sidebars and potentially replace the links.
// `process_entry`` is called recursively to process all children
Expand Down Expand Up @@ -116,11 +143,11 @@ fn read_sidebars() -> Result<Vec<(std::path::PathBuf, Sidebar)>, ToolError> {
.collect()
}

fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Option<String> {
fn replace_pairs(link: Option<String>, pairs: Pairs<'_>) -> Option<String> {
match link {
Some(link) => {
let mut has_prefix = false;
let link = if let Some(l) = link.strip_prefix(SIDEBAR_PATH_PREFIX) {
let link = if let Some(l) = link.strip_prefix(EN_US_DOCS_PREFIX) {
has_prefix = true;
l.to_string()
} else {
Expand All @@ -130,9 +157,9 @@ fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Op
if link == *from {
if let Some(to) = to {
if has_prefix {
return Some(concat_strs!(SIDEBAR_PATH_PREFIX, to));
return Some(concat_strs!(EN_US_DOCS_PREFIX, to));
} else {
return Some(to.clone());
return Some(to.to_string());
}
} else {
return None;
Expand All @@ -145,7 +172,7 @@ fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Op
}
}

fn process_entry(entry: SidebarEntry, pairs: &[(String, Option<String>)]) -> SidebarEntry {
fn process_entry(entry: SidebarEntry, pairs: Pairs<'_>) -> SidebarEntry {
match entry {
SidebarEntry::Section(BasicEntry {
link,
Expand Down Expand Up @@ -322,22 +349,22 @@ mod test {
let _sidebars = SidebarFixtures::new(vec![sb]);
let pairs = vec![
(
"Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Block_Abspos_Tables".to_string(),
Some("Web/CSS/CSS_Box_Alignment/Something_New".to_string()),
Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Block_Abspos_Tables"),
Some(Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Something_New")),
),
(
"Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Grid_Layout".to_string(),
Some("Web/CSS/CSS_Box_Alignment/Also_New".to_string()),
Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Grid_Layout"),
Some(Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Also_New")),
),
(
"Web/HTTP/Headers".to_string(),
Some("Web/HTTP/Headers_New".to_string()),
Cow::Borrowed("Web/HTTP/Headers"),
Some(Cow::Borrowed("Web/HTTP/Headers_New")),
),
(
"/Web/CSS/CSS_Box_Alignment/Box_Alignment_in_Multi-column_Layout".to_string(),
Cow::Borrowed("/Web/CSS/CSS_Box_Alignment/Box_Alignment_in_Multi-column_Layout"),
None,
),
("/Web/CSS/CSS_Box_Alignment".to_string(), None),
(Cow::Borrowed("/Web/CSS/CSS_Box_Alignment"), None),
];
let res = update_sidebars(&pairs);
assert!(res.is_ok());
Expand Down

0 comments on commit a4d4686

Please sign in to comment.