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,23 @@ 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 == "file" :
830
+ return False
831
+ if parsed_url .scheme :
832
+ return True
833
+ return False
834
+
818
835
@staticmethod
819
836
def sanitize_location (location : str ) -> tuple [Path , str ]:
820
837
"""Given a location, break out the path and any anchor.
@@ -1217,7 +1234,8 @@ async def forward(self) -> None:
1217
1234
1218
1235
async def _on_markdown_link_clicked (self , message : Markdown .LinkClicked ) -> None :
1219
1236
message .stop ()
1220
- await self .go (message .href )
1237
+ if not self .document .is_external_link (message .href ):
1238
+ await self .go (message .href )
1221
1239
1222
1240
def watch_show_table_of_contents (self , show_table_of_contents : bool ) -> None :
1223
1241
self .set_class (show_table_of_contents , "-show-table-of-contents" )
0 commit comments