-
Notifications
You must be signed in to change notification settings - Fork 801
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
Changes from all commits
c554122
6fedc8c
9a27714
f526ee3
89a44d6
0417883
688a95e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think this functionality should be built in to |
||
|
||
def watch_show_table_of_contents(self, show_table_of_contents: bool) -> None: | ||
self.set_class(show_table_of_contents, "-show-table-of-contents") | ||
|
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. |
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)) |
There was a problem hiding this comment.
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.