|
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
|
@@ -820,6 +820,23 @@ def _watch_code_light_theme(self) -> None:
|
820 | 820 | for block in self.query(MarkdownFence):
|
821 | 821 | block._retheme()
|
822 | 822 |
|
| 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 | + |
823 | 840 | @staticmethod
|
824 | 841 | def sanitize_location(location: str) -> tuple[Path, str]:
|
825 | 842 | """Given a location, break out the path and any anchor.
|
@@ -1224,7 +1241,8 @@ async def forward(self) -> None:
|
1224 | 1241 |
|
1225 | 1242 | async def _on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
|
1226 | 1243 | message.stop()
|
1227 |
| - await self.go(message.href) |
| 1244 | + if not self.document.is_external_link(message.href): |
| 1245 | + await self.go(message.href) |
1228 | 1246 |
|
1229 | 1247 | def watch_show_table_of_contents(self, show_table_of_contents: bool) -> None:
|
1230 | 1248 | self.set_class(show_table_of_contents, "-show-table-of-contents")
|
|
0 commit comments