Replies: 2 comments 2 replies
-
|
So far, this is the best I came up with - showing a tooltip with a terminal hyperlink on click: from rich.text import Text
from textual.app import App, ComposeResult
from textual.events import Click
from textual.widgets import Markdown, MarkdownViewer, Static
class MarkdownLinkTooltip(Static, inherit_css=False):
DEFAULT_CSS = """
MarkdownLinkTooltip {
layer: _tooltips;
margin: 1 0;
padding: 1 2;
background: $panel;
width: auto;
height: auto;
constrain: inside inflect;
max-width: 40;
display: none;
offset-x: -50%;
}
"""
class _MarkdownViewer(MarkdownViewer):
DEFAULT_CSS = """
_MarkdownViewer {
layers: default _tooltips;
}
"""
def compose(self) -> ComposeResult:
yield from super().compose()
yield MarkdownLinkTooltip()
def on_markdown_link_clicked(self, message: Markdown.LinkClicked) -> None:
# We don't want the default behavior of opening the browser/navigating to a file on click.
message.prevent_default()
tooltip = self.get_child_by_type(MarkdownLinkTooltip)
tooltip.display = True
# You can't cycle over the links in MarkdownViewer (see Textualize/textual#3555)
# so using mouse position is fine.
# Textualize/textual#3555: https://github.com/Textualize/textual/discussions/3555
tooltip.absolute_offset = self.app.mouse_position
# For some reason, links only render correctly when Text has a span over the whole text
# with a link but not when Text just has a style applied to it directly, i.e.:
# Text(message.href, style=f"link {message.href}")
# will not work.
tooltip.update(Text().append(message.href, style=f"link {message.href}"))
def on_click(self, message: Click) -> None:
tooltip = self.get_child_by_type(MarkdownLinkTooltip)
tooltip.display = False
class MarkdownViewerApp(App):
def __init__(self, markdown_content: str) -> None:
self.markdown_content = markdown_content
super().__init__()
def compose(self) -> ComposeResult:
markdown_viewer = _MarkdownViewer(
self.markdown_content, show_table_of_contents=True, open_links=False
)
markdown_viewer.code_indent_guides = False
yield markdown_viewer
EXAMPLE_CONTENT = """
# Header one
- [#6449](https://github.com/Textualize/textual/discussions/6449) - check out this discussion
# Another header
More text
"""
if __name__ == "__main__":
MarkdownViewerApp(EXAMPLE_CONTENT).run()While I would probably still prefer using terminal hyperlinks instead of Textual links directly, this works pretty decently as an alternative. The only weird thing I've noticed is this part: # For some reason, links only render correctly when Text has a span over the whole text
# with a link but not when Text just has a style applied to it directly, i.e.:
# Text(message.href, style=f"link {message.href}")
# will not work.
tooltip.update(Text().append(message.href, style=f"link {message.href}"))I'm thinking this might be a bug. The problem is, I'm not sure whether the bug is that Separately, I hope that #2998 will be configurable, as I imagine that it could otherwise affect what I'm doing here. |
Beta Was this translation helpful? Give feedback.
-
|
Deleting the bulk of this, @Jackenmen is right that it was low-signal. Short version for anyone searching later: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think that
MarkdownVieweris pretty useful, but the problem I'm seeing is that, unlike with Rich'sMarkdownclass, the links are not rendered with the ANSI escape codes for hyperlinks. Instead, Textual links are used, which is great if you want to intercept them, but not so great if I'm just looking to use the widget as a simple way to view markdown. More specifically, clicking on links won't work when I'm running the app over SSH. I understand that the intercept use case is important, but is it possible to getMarkdownViewer(and I guess alsoMarkdown) to render these with the hyperlink ANSI escape sequence in some other way?I don't want to use Rich's
Markdownsince I actually need usable scrolling, and ToC is also useful.If it's not possible, would it be possible for you to add such a feature?
Beta Was this translation helpful? Give feedback.
All reactions