Skip to content

Commit 902fce8

Browse files
committed
fix external link click MarkdownViewer
no longer crashes on external link pressed in the MarkdownViewer widget
1 parent 44d250a commit 902fce8

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/textual/widgets/_markdown.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from functools import partial
66
from pathlib import Path, PurePath
77
from typing import Callable, Iterable, Optional
8-
from urllib.parse import unquote
8+
from urllib.parse import unquote, urlparse
99

1010
from markdown_it import MarkdownIt
1111
from markdown_it.token import Token
@@ -820,6 +820,23 @@ def _watch_code_light_theme(self) -> None:
820820
for block in self.query(MarkdownFence):
821821
block._retheme()
822822

823+
@staticmethod
824+
def is_external_link(link: str) -> bool:
825+
"""Given a markdown href [text](link), determine if the link references a local disk resource.
826+
827+
Args:
828+
link: The link to evaluate.
829+
830+
Returns:
831+
A bool value True if the link points to external resource, i.e. not local file or anchor
832+
"""
833+
parsed_url = urlparse(link)
834+
if parsed_url.scheme == "file":
835+
return False
836+
if parsed_url.scheme:
837+
return True
838+
return False
839+
823840
@staticmethod
824841
def sanitize_location(location: str) -> tuple[Path, str]:
825842
"""Given a location, break out the path and any anchor.
@@ -1224,7 +1241,8 @@ async def forward(self) -> None:
12241241

12251242
async def _on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
12261243
message.stop()
1227-
await self.go(message.href)
1244+
if not self.document.is_external_link(message.href):
1245+
await self.go(message.href)
12281246

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

0 commit comments

Comments
 (0)