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

MarkdownViewer link click behaviour #5223

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Fixed issue with alignment in auto containers https://github.com/Textualize/textual/pull/5360
- Fixed issue with markdown viewer link click crash https://github.com/Textualize/textual/pull/5223

## [0.89.1] - 2024-12-05

Expand Down
22 changes: 20 additions & 2 deletions src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import partial
from pathlib import Path, PurePath
from typing import Callable, Iterable, Optional
from urllib.parse import unquote
from urllib.parse import unquote, urlparse

from markdown_it import MarkdownIt
from markdown_it.token import Token
Expand Down Expand Up @@ -820,6 +820,23 @@ def _watch_code_light_theme(self) -> None:
for block in self.query(MarkdownFence):
block._retheme()

@staticmethod
def is_external_link(link: str) -> bool:
"""Given a markdown href [text](link), determine if the link references a local disk resource.

Args:
link: The link to evaluate.

Returns:
A bool value True if the link points to external resource, i.e. not local file or anchor
"""
parsed_url = urlparse(link)
if parsed_url.scheme == "file":
return False
if parsed_url.scheme:
return True
return False

@staticmethod
def sanitize_location(location: str) -> tuple[Path, str]:
"""Given a location, break out the path and any anchor.
Expand Down Expand Up @@ -1224,7 +1241,8 @@ async def forward(self) -> None:

async def _on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
message.stop()
await self.go(message.href)
if not self.document.is_external_link(message.href):
await self.go(message.href)

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