Skip to content

Commit 4360e26

Browse files
committed
feat(redirects): vaildate to urls better
1 parent 6f284f4 commit 4360e26

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

crates/rari-tools/src/redirects.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ fn fix_redirects_internal(
381381
// Split the clean pairs into their respective locales
382382
let locale_pairs = clean_pairs.into_iter().try_fold(
383383
HashMap::<Locale, HashMap<String, String>>::new(),
384-
|mut acc, (from, to)| -> Result<HashMap<Locale, HashMap<String, String>>, ToolError> {
384+
|mut acc, (from, mut to)| -> Result<HashMap<Locale, HashMap<String, String>>, ToolError> {
385385
// Extract the locale string from the 'from' path
386386
let locale_str = from.split('/').nth(1).unwrap_or_default();
387387

@@ -392,6 +392,14 @@ fn fix_redirects_internal(
392392
from, from, to
393393
)))
394394
})?;
395+
if to.starts_with('/') {
396+
let (bare_url, hash) = to.split_once('#').map(|(u, h)| (u, Some(h))).unwrap_or((&to, None));
397+
if let Ok(page) = Page::from_url(bare_url) {
398+
if page.url() != bare_url {
399+
to = if let Some(hash) = hash { concat_strs!(page.url(), "#", hash) } else { page.url().to_string() } ;
400+
}
401+
}
402+
}
395403
let locale_map = acc.entry(locale).or_default();
396404
locale_map.insert(from, to);
397405
Ok(acc)
@@ -654,6 +662,17 @@ fn validate_to_url(url: &str, locale: Locale) -> Result<(), ToolError> {
654662
locale
655663
)));
656664
}
665+
if let Ok(page) = Page::from_url(bare_url) {
666+
if page.url() != bare_url {
667+
return Err(ToolError::InvalidRedirectToURL(format!(
668+
"To-URL '{}' does not equal page url '{}' for '{}' for locale '{}'.",
669+
bare_url,
670+
page.url(),
671+
path.display(),
672+
locale
673+
)));
674+
}
675+
}
657676
} else {
658677
return Err(ToolError::InvalidRedirectToURL(format!(
659678
"To-URL '{}' has to be external (https://) or start with '/' for locale '{}'.",

0 commit comments

Comments
 (0)