Skip to content

Commit

Permalink
fix md external link click
Browse files Browse the repository at this point in the history
  • Loading branch information
lowekoz committed Dec 3, 2024
1 parent a3fee68 commit 801b89c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import partial
from pathlib import Path, PurePath
from typing import Callable, Iterable, Optional
from urllib.parse import unquote
from urllib.parse import unquote, urlparse

from markdown_it import MarkdownIt
from markdown_it.token import Token
Expand Down Expand Up @@ -800,7 +800,7 @@ async def _on_mount(self, _: Mount) -> None:
await self.update(self._markdown)

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

def _watch_code_dark_theme(self) -> None:
Expand All @@ -815,6 +815,23 @@ def _watch_code_light_theme(self) -> None:
for block in self.query(MarkdownFence):
block._retheme()

@staticmethod
def is_external_link(link: str) -> bool:
"""Given a markdown href [text](link), determine if the link references a local disk resource.
Args:
link: The link to evaluate.
Returns:
A bool value True if the link points to external resource, i.e. not local file or anchor
"""
parsed_url = urlparse(link)
if parsed_url.scheme == "file":
return False
if parsed_url.scheme:
return True
return False

@staticmethod
def sanitize_location(location: str) -> tuple[Path, str]:
"""Given a location, break out the path and any anchor.
Expand Down Expand Up @@ -1217,7 +1234,8 @@ async def forward(self) -> None:

async def _on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
message.stop()
await self.go(message.href)
if not self.document.is_external_link(message.href):
await self.go(message.href)

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

0 comments on commit 801b89c

Please sign in to comment.