Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown widget - hyperlinks does not open browser on click #4738

Closed
thomasht86 opened this issue Jul 14, 2024 · 5 comments
Closed

Markdown widget - hyperlinks does not open browser on click #4738

thomasht86 opened this issue Jul 14, 2024 · 5 comments

Comments

@thomasht86
Copy link

thomasht86 commented Jul 14, 2024

Have you checked closed issues? https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed
Yes.

To reproduce:

# Sample textual app that renders markdown widget with url link
from textual.widgets import Markdown
from textual.app import App, ComposeResult

MARKDOWN = """
# This is an h1

Rich can do a pretty *decent* job of rendering markdown.

1. This is a list item
2. This is another list item

[Google](https://www.google.com)
"""

class HyperlinkApp(App):

    def compose(self) -> ComposeResult:
        yield Markdown(MARKDOWN)

if __name__ == "__main__":
    app = HyperlinkApp()
    app.run()

When running corresponding rich-snippet, it does open the url in browser on click.

from rich.console import Console
from rich.markdown import Markdown

console = Console()
md = Markdown(MARKDOWN)
console.print(md)

If this is not supported, I will be happy to change it to a feature request.

@Textualize Textualize deleted a comment from github-actions bot Jul 14, 2024
@willmcgugan
Copy link
Collaborator

It's left to the developer to implement how to open links. If you want links to open a browser you can handle the LinkClicked message.

@thomasht86
Copy link
Author

Ah, ok! Noted. Thanks for the swift reply.
Did you consider keeping the default behavior to open a browser from rich?
Thanks a lot for all your great work btw. 🙏
Really good devX!

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@willmcgugan
Copy link
Collaborator

Did you consider keeping the default behavior to open a browser from rich?

The links in Rich work differently. They are handled by the terminal, which typically opens them in a browser. But you can't intercept those links. The links in Textual Markdown offer more flexibility, because an app can intercept them.

@thomasht86
Copy link
Author

I see. Thanks again!

    import webbrowser
    @on(Markdown.LinkClicked)
    def handle_link_clicked(self, event: Markdown.LinkClicked) -> None:
        # Open in a new tab, if possible, else in the default browser
        webbrowser.open_new_tab(event.href)

worked like a charm! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants