Skip to content

Commit 7d99486

Browse files
committed
handle http/https links in MarkDown, handle relative MD links in MDViewer
1 parent f59e89f commit 7d99486

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/textual/widgets/_markdown.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from functools import partial
77
from pathlib import Path, PurePath
88
from typing import Callable, Iterable, Optional
9-
from urllib.parse import unquote
9+
from urllib.parse import unquote, urlparse
1010

1111
from markdown_it import MarkdownIt
1212
from markdown_it.token import Token
@@ -1128,8 +1128,9 @@ async def stream_markdown(self) -> None:
11281128
return updater
11291129

11301130
def on_markdown_link_clicked(self, event: LinkClicked) -> None:
1131-
if self._open_links:
1131+
if self._open_links and is_http_url(event.href):
11321132
self.app.open_url(event.href)
1133+
event.stop()
11331134

11341135
@staticmethod
11351136
def sanitize_location(location: str) -> tuple[Path, str]:
@@ -1628,8 +1629,9 @@ async def forward(self) -> None:
16281629
self.post_message(self.NavigatorUpdated())
16291630

16301631
async def _on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
1631-
message.stop()
1632-
await self.go(message.href)
1632+
if not is_http_url(message.href):
1633+
message.stop()
1634+
await self.go(message.href)
16331635

16341636
def watch_show_table_of_contents(self, show_table_of_contents: bool) -> None:
16351637
self.set_class(show_table_of_contents, "-show-table-of-contents")
@@ -1657,3 +1659,9 @@ def _on_markdown_table_of_contents_selected(
16571659
block = self.query_one(block_selector, MarkdownBlock)
16581660
self.scroll_to_widget(block, top=True)
16591661
message.stop()
1662+
1663+
1664+
def is_http_url(url: str) -> bool:
1665+
"""Check if a URL is an HTTP or HTTPS URL."""
1666+
parsed = urlparse(url)
1667+
return parsed.scheme in ("http", "https")

0 commit comments

Comments
 (0)