Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

markdown_viewer: jump to anchor position when clicking on an anchor link #2941

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- TCSS styles `layer` and `layers` can be strings https://github.com/Textualize/textual/pull/3169
- `MarkdownViewer` now correctly handles anchors in links https://github.com/Textualize/textual/pull/2941

### Changed

Expand All @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fixed flicker when calling pop_screen multiple times https://github.com/Textualize/textual/issues/3126
- Fixed setting styles.layout not updating https://github.com/Textualize/textual/issues/3047
- Fixed a crash in `MarkdownViewer` when clicking on a link containing an anchor https://github.com/Textualize/textual/issues/3094

## [0.35.1]

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest using str.partition here, so we can avoid the additional branch.

await self.go(link)

def scroll_to_anchor() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a docstring. I know it seems self explanatory, but it's how we roll.

toc = self.table_of_contents.table_of_contents
assert toc is not None
for _, name, block_id in toc:
if name.lower().replace(" ", "-") == anchor:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is making a somewhat naive assumption about how fragments are generated from headers. It wont always be as simple as converting spaces to hyphens and lower casing. Consider characters with accents for example.

I think it makes sense to match what GitHub markdown does, and replicate that.

block = self.query_one(f"#{block_id}", MarkdownBlock)
self.scroll_to_widget(block, top=True)
break

self.call_later(scroll_to_anchor)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call_later probably isn't the right approach here. It will execute after all other messages have been processed. This is normally very quick, but it is possible that an entirely new document has been loaded in the meantime.

I think this functionality should be built in to go, so we can be sure that it applies to the correct document.


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
15 changes: 15 additions & 0 deletions tests/test_markdownviewer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
* [First](test_markdownviewer.md#first)
* [Second](test_markdownviewer.md#second)
* [Third](test_markdownviewer.md#third)

# First

The first.

# Second

The second.

# Third

The third.
25 changes: 25 additions & 0 deletions tests/test_markdownviewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pathlib import Path

from textual.app import App, ComposeResult
from textual.geometry import Offset
from textual.widgets import Markdown, MarkdownViewer

class MarkdownViewerApp(App[None]):

def compose(self) -> ComposeResult:
yield MarkdownViewer()

async def on_mount(self) -> None:
self.query_one(MarkdownViewer).show_table_of_contents = False
await self.query_one(MarkdownViewer).go(
Path(__file__).with_suffix(".md")
)


async def test_markdown_viewer_anchor_link() -> None:
"""Test https://github.com/Textualize/textual/issues/3094"""
async with MarkdownViewerApp().run_test() as pilot:
# There's not really anything to test *for* here, but the lack of an
# exception is the win (before the fix this is testing it would have
# been FileNotFoundError).
davep marked this conversation as resolved.
Show resolved Hide resolved
await pilot.click(Markdown, Offset(2, 1))
Loading