Skip to content

Commit

Permalink
markdown_viewer: handle anchors when jumping to new link
Browse files Browse the repository at this point in the history
  • Loading branch information
blob42 committed Jul 27, 2023
1 parent 6356be1 commit c554122
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `DataTable.remove_column` method https://github.com/Textualize/textual/pull/2899
- Added notifications https://github.com/Textualize/textual/pull/2866
- Added `on_complete` callback to scroll methods https://github.com/Textualize/textual/pull/2903
- Make `MarkdownViewer` link click handler jump to the anchor position in anchor
links.

### Fixed

Expand Down
17 changes: 16 additions & 1 deletion src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,22 @@ async def forward(self) -> None:

async def _on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
message.stop()
await self.go(message.href)
if message.href.find("#") == -1:
await self.go(message.href)
else:
link, anchor = message.href.split("#", 1)
await self.go(link)

def scroll_to_anchor() -> None:
toc = self.table_of_contents.table_of_contents
assert toc is not None
for _, name, block_id in toc:
if name.lower().replace(" ", "-") == anchor:
block = self.query_one(f"#{block_id}", MarkdownBlock)
self.scroll_to_widget(block, top=True)
break

self.call_later(scroll_to_anchor)

def watch_show_table_of_contents(self, show_table_of_contents: bool) -> None:
self.set_class(show_table_of_contents, "-show-table-of-contents")
Expand Down

0 comments on commit c554122

Please sign in to comment.