5
5
from functools import partial
6
6
from pathlib import Path , PurePath
7
7
from typing import Callable , Iterable , Optional
8
- from urllib .parse import unquote
8
+ from urllib .parse import unquote , urlparse
9
9
10
10
from markdown_it import MarkdownIt
11
11
from markdown_it .token import Token
@@ -800,7 +800,7 @@ async def _on_mount(self, _: Mount) -> None:
800
800
await self .update (self ._markdown )
801
801
802
802
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 :
804
804
self .app .open_url (event .href )
805
805
806
806
def _watch_code_dark_theme (self ) -> None :
@@ -815,6 +815,21 @@ def _watch_code_light_theme(self) -> None:
815
815
for block in self .query (MarkdownFence ):
816
816
block ._retheme ()
817
817
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
+
818
833
@staticmethod
819
834
def sanitize_location (location : str ) -> tuple [Path , str ]:
820
835
"""Given a location, break out the path and any anchor.
@@ -1217,7 +1232,8 @@ async def forward(self) -> None:
1217
1232
1218
1233
async def _on_markdown_link_clicked (self , message : Markdown .LinkClicked ) -> None :
1219
1234
message .stop ()
1220
- await self .go (message .href )
1235
+ if not self .document .is_external_link (message .href ):
1236
+ await self .go (message .href )
1221
1237
1222
1238
def watch_show_table_of_contents (self , show_table_of_contents : bool ) -> None :
1223
1239
self .set_class (show_table_of_contents , "-show-table-of-contents" )
0 commit comments