55from functools import partial
66from pathlib import Path , PurePath
77from typing import Callable , Iterable , Optional
8- from urllib .parse import unquote
8+ from urllib .parse import unquote , urlparse
99
1010from markdown_it import MarkdownIt
1111from 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,21 @@ 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 :
830+ return True
831+ return False
832+
818833 @staticmethod
819834 def sanitize_location (location : str ) -> tuple [Path , str ]:
820835 """Given a location, break out the path and any anchor.
@@ -1217,7 +1232,8 @@ async def forward(self) -> None:
12171232
12181233 async def _on_markdown_link_clicked (self , message : Markdown .LinkClicked ) -> None :
12191234 message .stop ()
1220- await self .go (message .href )
1235+ if not self .document .is_external_link (message .href ):
1236+ await self .go (message .href )
12211237
12221238 def watch_show_table_of_contents (self , show_table_of_contents : bool ) -> None :
12231239 self .set_class (show_table_of_contents , "-show-table-of-contents" )
0 commit comments