Skip to content

Commit

Permalink
fix external link click MarkdownViewer
Browse files Browse the repository at this point in the history
no longer crashes on external link pressed
in the MarkdownViewer widget
  • Loading branch information
lowekoz committed Dec 11, 2024
1 parent 44d250a commit 902fce8
Showing 1 changed file with 20 additions and 2 deletions.
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

0 comments on commit 902fce8

Please sign in to comment.