Skip to content

Commit 7df1596

Browse files
committed
Also handle URL redirect links on print page
Signed-off-by: Hollow Man <[email protected]>
1 parent 6cfb971 commit 7df1596

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/utils/mod.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,24 +204,42 @@ fn adjust_links<'a>(
204204
// Fix redirect links
205205
let normalized_path_split: Vec<&str> = normalized_path.split('#').collect();
206206
for (original, redirect) in &redirects {
207-
if !SCHEME_LINK.is_match(&redirect)
208-
&& normalize_path(original.trim_start_matches('/'))
209-
.eq_ignore_ascii_case(&normalize_path(normalized_path_split[0]))
207+
if normalize_path(original.trim_start_matches('/'))
208+
.eq_ignore_ascii_case(&normalized_path)
209+
|| normalize_path(original.trim_start_matches('/'))
210+
.eq_ignore_ascii_case(&normalized_path_split[0])
210211
{
211212
let mut unnormalized_path = String::new();
212-
let base = PathBuf::from(normalized_path_split[0])
213-
.parent()
214-
.expect("path can't be empty")
215-
.to_str()
216-
.expect("utf-8 paths only")
217-
.to_owned();
218-
write!(unnormalized_path, "{}/{}", normalize_path(base), redirect)
219-
.unwrap();
220-
for i in 1..normalized_path_split.len() {
221-
unnormalized_path.push('#');
222-
unnormalized_path.push_str(normalized_path_split[i]);
213+
if SCHEME_LINK.is_match(&redirect) {
214+
unnormalized_path = redirect.to_string();
215+
} else {
216+
let base = PathBuf::from(normalized_path_split[0])
217+
.parent()
218+
.expect("path can't be empty")
219+
.to_str()
220+
.expect("utf-8 paths only")
221+
.to_owned();
222+
write!(unnormalized_path, "{}/{}", normalize_path(base), redirect)
223+
.unwrap();
224+
}
225+
226+
// original without anchors, need to append link anchors
227+
if !original.contains("#") {
228+
for i in 1..normalized_path_split.len() {
229+
if !unnormalized_path.contains("#") {
230+
unnormalized_path.push('#');
231+
} else {
232+
unnormalized_path.push('-');
233+
}
234+
unnormalized_path.push_str(normalized_path_split[i]);
235+
}
236+
}
237+
238+
if !SCHEME_LINK.is_match(&redirect) {
239+
normalized_path = normalize_path(unnormalized_path);
240+
} else {
241+
return CowStr::from(unnormalized_path);
223242
}
224-
normalized_path = normalize_path(unnormalized_path);
225243
break;
226244
}
227245
}

0 commit comments

Comments
 (0)