Skip to content

Commit 501153e

Browse files
committed
Handle heading links
1 parent 5866ec9 commit 501153e

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/event_handler.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,25 @@ fn keyboard_mode_view(
455455
}
456456
LinkType::MarkdownFile(url) => {
457457
// Remove the first character, which is a '/'
458-
459458
let url = if let Some(url) = url.strip_prefix('/') {
460459
url
461460
} else {
462461
url
463462
};
464-
if !url.ends_with("md") {
465-
let _ = open::that(url);
466-
return KeyBoardAction::Continue;
467-
}
468-
let text = if let Ok(file) = read_to_string(url) {
463+
464+
let (url, heading) = if let Some((url, heading)) = url.split_once('#') {
465+
(url.to_string(), Some(heading.to_string().to_lowercase()))
466+
} else {
467+
(url.to_string(), None)
468+
};
469+
470+
let url = if url.ends_with(".md") {
471+
url
472+
} else {
473+
format!("{url}.md")
474+
};
475+
476+
let text = if let Ok(file) = read_to_string(&url) {
469477
app.vertical_scroll = 0;
470478
file
471479
} else {
@@ -481,8 +489,22 @@ fn keyboard_mode_view(
481489

482490
let path = std::path::Path::new(&url);
483491
let _ = watcher.watch(path, notify::RecursiveMode::NonRecursive);
484-
*markdown = parse_markdown(Some(url), &text, app.width() - 2);
492+
*markdown = parse_markdown(Some(&url), &text, app.width() - 2);
493+
let index = if let Some(heading) = heading {
494+
if let Ok(index) = markdown.heading_offset(&format!("#{heading}")) {
495+
cmp::min(index, markdown.height().saturating_sub(height / 2))
496+
} else {
497+
app.error_box
498+
.set_message(format!("Could not find heading {}", heading));
499+
app.boxes = Boxes::Error;
500+
0
501+
}
502+
} else {
503+
0
504+
};
505+
485506
app.reset();
507+
app.vertical_scroll = index;
486508
}
487509
}
488510
markdown.deselect();

0 commit comments

Comments
 (0)