Skip to content

Commit a4d4686

Browse files
committed
feat(content): add sync-sidebars command
1 parent 8e4b4aa commit a4d4686

File tree

5 files changed

+100
-39
lines changed

5 files changed

+100
-39
lines changed

crates/rari-cli/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rari_tools::history::gather_history;
2626
use rari_tools::r#move::r#move;
2727
use rari_tools::redirects::fix_redirects;
2828
use rari_tools::remove::remove;
29-
use rari_tools::sidebars::fmt_sidebars;
29+
use rari_tools::sidebars::{fmt_sidebars, sync_sidebars};
3030
use rari_tools::sync_translated_content::sync_translated_content;
3131
use rari_types::globals::{build_out_root, content_root, content_translated_root, SETTINGS};
3232
use rari_types::locale::Locale;
@@ -88,6 +88,8 @@ enum ContentSubcommand {
8888
SyncTranslatedContent(SyncTranslatedContentArgs),
8989
/// Formats all sidebars.
9090
FmtSidebars,
91+
/// Sync sidebars with redirects
92+
SyncSidebars,
9193
/// Fixes redirects across all locales.
9294
///
9395
/// This shortens multiple redirect chains to single ones.
@@ -399,6 +401,9 @@ fn main() -> Result<(), Error> {
399401
ContentSubcommand::FmtSidebars => {
400402
fmt_sidebars()?;
401403
}
404+
ContentSubcommand::SyncSidebars => {
405+
sync_sidebars()?;
406+
}
402407
ContentSubcommand::FixRedirects => {
403408
fix_redirects()?;
404409
}

crates/rari-doc/src/html/sidebar.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
use std::borrow::Cow;
12
use std::collections::HashMap;
23
pub use std::ops::Deref;
34
use std::sync::{Arc, LazyLock};
45

6+
use constcat::concat;
57
use dashmap::DashMap;
68
use indexmap::IndexMap;
79
use rari_types::fm_types::PageType;
810
use rari_types::globals::cache_content;
9-
use rari_types::locale::Locale;
11+
use rari_types::locale::{default_locale, Locale};
1012
use rari_utils::concat_strs;
1113
use scraper::{Html, Node, Selector};
1214
use serde::{Deserialize, Serialize, Serializer};
@@ -547,19 +549,41 @@ impl SidebarMetaEntry {
547549
child.render(out, locale, slug, l10n)?;
548550
}
549551
}
550-
MetaChildren::ListSubPages(url, page_types, include_parent) => list_sub_pages_internal(
551-
out,
552-
url,
553-
locale,
554-
Some(1),
555-
None,
556-
page_types,
557-
*include_parent,
558-
)?,
552+
MetaChildren::ListSubPages(url, page_types, include_parent) => {
553+
let url = if url.starts_with(concat!("/", default_locale().as_url_str(), "/")) {
554+
Cow::Borrowed(url)
555+
} else {
556+
Cow::Owned(concat_strs!(
557+
"/",
558+
Locale::default().as_url_str(),
559+
"/docs",
560+
url
561+
))
562+
};
563+
list_sub_pages_internal(
564+
out,
565+
&url,
566+
locale,
567+
Some(1),
568+
None,
569+
page_types,
570+
*include_parent,
571+
)?
572+
}
559573
MetaChildren::ListSubPagesGrouped(url, page_types, include_parent) => {
574+
let url = if url.starts_with(concat!("/", default_locale().as_url_str(), "/")) {
575+
Cow::Borrowed(url)
576+
} else {
577+
Cow::Owned(concat_strs!(
578+
"/",
579+
Locale::default().as_url_str(),
580+
"/docs",
581+
url
582+
))
583+
};
560584
list_sub_pages_grouped_internal(
561585
out,
562-
url,
586+
&url,
563587
locale,
564588
None,
565589
page_types,

crates/rari-tools/src/move.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ fn do_move(
194194
update_sidebars(
195195
&pairs
196196
.iter()
197-
.map(|(from, to)| (from.clone(), Some(to.clone())))
197+
.map(|(from, to)| {
198+
(
199+
Cow::Borrowed(from.as_str()),
200+
Some(Cow::Borrowed(to.as_str())),
201+
)
202+
})
198203
.collect::<Vec<_>>(),
199204
)?;
200205
}

crates/rari-tools/src/remove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn do_remove(
230230
.iter()
231231
.map(|slug| {
232232
let url = build_url(slug, locale, PageCategory::Doc)?;
233-
Ok((url, None))
233+
Ok((Cow::Owned(url), None))
234234
})
235235
.collect::<Result<Vec<_>, ToolError>>()?;
236236
update_sidebars(&pairs)?;

crates/rari-tools/src/sidebars.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
1+
use std::borrow::Cow;
2+
use std::collections::HashMap;
13
use std::fs;
24
use std::path::Path;
35

46
use const_format::concatcp;
57
use pretty_yaml::config::{FormatOptions, LanguageOptions};
68
use rari_doc::html::sidebar::{BasicEntry, Sidebar, SidebarEntry, SubPageEntry, WebExtApiEntry};
79
use rari_types::globals::content_root;
8-
use rari_types::locale::default_locale;
10+
use rari_types::locale::{default_locale, Locale};
911
use rari_utils::concat_strs;
1012

1113
use crate::error::ToolError;
14+
use crate::redirects::{read_redirects_raw, redirects_path};
1215

1316
const PREFIX: &str = "# Do not add comments to this file. They will be lost.\n\n";
14-
static SIDEBAR_PATH_PREFIX: &str = concatcp!("/", default_locale().as_url_str(), "/docs");
17+
static EN_US_DOCS_PREFIX: &str = concatcp!("/", default_locale().as_url_str(), "/docs");
18+
19+
type Pair<'a> = (Cow<'a, str>, Option<Cow<'a, str>>);
20+
type Pairs<'a> = &'a [Pair<'a>];
21+
22+
pub fn sync_sidebars() -> Result<(), ToolError> {
23+
let mut redirects = HashMap::new();
24+
let path = redirects_path(Locale::default())?;
25+
read_redirects_raw(&path, &mut redirects)?;
26+
let pairs = redirects
27+
.iter()
28+
.map(|(from, to)| {
29+
(
30+
from.strip_prefix(EN_US_DOCS_PREFIX)
31+
.map(Cow::Borrowed)
32+
.unwrap_or(Cow::Borrowed(from)),
33+
Some(
34+
to.strip_prefix(EN_US_DOCS_PREFIX)
35+
.map(Cow::Borrowed)
36+
.unwrap_or(Cow::Borrowed(to)),
37+
),
38+
)
39+
})
40+
.collect::<Vec<_>>();
41+
update_sidebars(&pairs)?;
42+
Ok(())
43+
}
1544

1645
pub fn fmt_sidebars() -> Result<(), ToolError> {
1746
for (path, sidebar) in read_sidebars()? {
@@ -20,30 +49,28 @@ pub fn fmt_sidebars() -> Result<(), ToolError> {
2049
Ok(())
2150
}
2251

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

2655
// add leading slash to pairs, because that is what the sidebars use
2756
let pairs = &pairs
2857
.iter()
2958
.map(|(from, to)| {
3059
let from = if from.starts_with('/') {
31-
from.to_string()
60+
Cow::Borrowed(from.as_ref())
3261
} else {
33-
concat_strs!("/", from)
62+
Cow::Owned(concat_strs!("/", from))
3463
};
35-
let to = if let Some(to) = to {
64+
let to = to.as_ref().map(|to| {
3665
if to.starts_with('/') {
37-
Some(to.to_string())
66+
Cow::Borrowed(to.as_ref())
3867
} else {
39-
Some(concat_strs!("/", to))
68+
Cow::Owned(concat_strs!("/", to))
4069
}
41-
} else {
42-
None
43-
};
70+
});
4471
(from, to)
4572
})
46-
.collect::<Vec<(String, Option<String>)>>();
73+
.collect::<Vec<Pair<'_>>>();
4774

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

119-
fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Option<String> {
146+
fn replace_pairs(link: Option<String>, pairs: Pairs<'_>) -> Option<String> {
120147
match link {
121148
Some(link) => {
122149
let mut has_prefix = false;
123-
let link = if let Some(l) = link.strip_prefix(SIDEBAR_PATH_PREFIX) {
150+
let link = if let Some(l) = link.strip_prefix(EN_US_DOCS_PREFIX) {
124151
has_prefix = true;
125152
l.to_string()
126153
} else {
@@ -130,9 +157,9 @@ fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Op
130157
if link == *from {
131158
if let Some(to) = to {
132159
if has_prefix {
133-
return Some(concat_strs!(SIDEBAR_PATH_PREFIX, to));
160+
return Some(concat_strs!(EN_US_DOCS_PREFIX, to));
134161
} else {
135-
return Some(to.clone());
162+
return Some(to.to_string());
136163
}
137164
} else {
138165
return None;
@@ -145,7 +172,7 @@ fn replace_pairs(link: Option<String>, pairs: &[(String, Option<String>)]) -> Op
145172
}
146173
}
147174

148-
fn process_entry(entry: SidebarEntry, pairs: &[(String, Option<String>)]) -> SidebarEntry {
175+
fn process_entry(entry: SidebarEntry, pairs: Pairs<'_>) -> SidebarEntry {
149176
match entry {
150177
SidebarEntry::Section(BasicEntry {
151178
link,
@@ -322,22 +349,22 @@ mod test {
322349
let _sidebars = SidebarFixtures::new(vec![sb]);
323350
let pairs = vec![
324351
(
325-
"Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Block_Abspos_Tables".to_string(),
326-
Some("Web/CSS/CSS_Box_Alignment/Something_New".to_string()),
352+
Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Block_Abspos_Tables"),
353+
Some(Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Something_New")),
327354
),
328355
(
329-
"Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Grid_Layout".to_string(),
330-
Some("Web/CSS/CSS_Box_Alignment/Also_New".to_string()),
356+
Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Box_Alignment_In_Grid_Layout"),
357+
Some(Cow::Borrowed("Web/CSS/CSS_Box_Alignment/Also_New")),
331358
),
332359
(
333-
"Web/HTTP/Headers".to_string(),
334-
Some("Web/HTTP/Headers_New".to_string()),
360+
Cow::Borrowed("Web/HTTP/Headers"),
361+
Some(Cow::Borrowed("Web/HTTP/Headers_New")),
335362
),
336363
(
337-
"/Web/CSS/CSS_Box_Alignment/Box_Alignment_in_Multi-column_Layout".to_string(),
364+
Cow::Borrowed("/Web/CSS/CSS_Box_Alignment/Box_Alignment_in_Multi-column_Layout"),
338365
None,
339366
),
340-
("/Web/CSS/CSS_Box_Alignment".to_string(), None),
367+
(Cow::Borrowed("/Web/CSS/CSS_Box_Alignment"), None),
341368
];
342369
let res = update_sidebars(&pairs);
343370
assert!(res.is_ok());

0 commit comments

Comments
 (0)