Skip to content

Commit b021819

Browse files
committed
🔨 Partially handle an anchor
1 parent 1390c05 commit b021819

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/hike/widgets/viewer.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,27 +541,33 @@ def _handle_link(self, message: Markdown.LinkClicked) -> None:
541541
)
542542
return
543543

544+
# Having eliminated URLs, it's likely something more local. First
545+
# off, on the off-chance that there's an anchor involved still...
546+
file_name, _, anchor = message.href.partition("#")
547+
544548
# A local file that exists?
545-
if (local_file := Path(message.href).expanduser()).exists():
549+
if (local_file := Path(file_name).expanduser()).exists():
546550
self.post_message(OpenLocation(local_file.resolve()))
547551
return
548552

549553
# A local file relative to the current location?
550554
if (
551555
isinstance(self.location, Path)
552-
and (local_file := self.location.parent / Path(message.href))
556+
and (local_file := self.location.parent / Path(file_name))
553557
.absolute()
554558
.exists()
555559
):
556560
self.post_message(OpenLocation(local_file))
557561
return
558562

559563
# Some sort of internal anchor perhaps?
560-
if message.href.startswith("#") and message.markdown.goto_anchor(
561-
message.href[1:]
562-
):
564+
if anchor and message.markdown.goto_anchor(anchor):
563565
return
564566

567+
# TODO: One case that isn't yet handled is a file name that also has
568+
# an anchor: `some-file.md#some-anchor`. We should handle that too
569+
# at some point.
570+
565571
self.notify(
566572
f"The clicked link could not be handled:\n\n{message.href}",
567573
title="Unknown link type",

0 commit comments

Comments
 (0)