Skip to content

Commit 801b89c

Browse files
committed
fix md external link click
1 parent a3fee68 commit 801b89c

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/textual/widgets/_markdown.py

+21-3
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
@@ -800,7 +800,7 @@ async def _on_mount(self, _: Mount) -> None:
800800
await self.update(self._markdown)
801801

802802
def on_markdown_link_clicked(self, event: LinkClicked) -> None:
803-
if self._open_links:
803+
if self.is_external_link(event.href) and self._open_links:
804804
self.app.open_url(event.href)
805805

806806
def _watch_code_dark_theme(self) -> None:
@@ -815,6 +815,23 @@ def _watch_code_light_theme(self) -> None:
815815
for block in self.query(MarkdownFence):
816816
block._retheme()
817817

818+
@staticmethod
819+
def is_external_link(link: str) -> bool:
820+
"""Given a markdown href [text](link), determine if the link references a local disk resource.
821+
822+
Args:
823+
link: The link to evaluate.
824+
825+
Returns:
826+
A bool value True if the link points to external resource, i.e. not local file or anchor
827+
"""
828+
parsed_url = urlparse(link)
829+
if parsed_url.scheme == "file":
830+
return False
831+
if parsed_url.scheme:
832+
return True
833+
return False
834+
818835
@staticmethod
819836
def sanitize_location(location: str) -> tuple[Path, str]:
820837
"""Given a location, break out the path and any anchor.
@@ -1217,7 +1234,8 @@ async def forward(self) -> None:
12171234

12181235
async def _on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
12191236
message.stop()
1220-
await self.go(message.href)
1237+
if not self.document.is_external_link(message.href):
1238+
await self.go(message.href)
12211239

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

0 commit comments

Comments
 (0)