Looking for a way to identify the inline code text in a Markdown widget when clicked #5434
-
My use case is I make heavy use of Markdown widgets to display output from LLM agents. When the user clicks on the widget, I copy the text in the widget to the clipboard to allow it to be pasted elsewhere. This works well, but is problematic when I only want to grab a code block, not the surrounding commentary. New GoalI'm trying to make this smarter and to detect if within the Markdown widget, they clicked within a From the Textual code, I see that the Markdown widget will use an internal MarkdownFence widget which contains the code block. What might be a good approach? I'm thinking of:
Better ideas? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I would probably just check if the click event for a from textual import events
from textual.app import App, ComposeResult
from textual.widgets import Markdown
from textual.widgets._markdown import MarkdownFence
MARKDOWN = """
# Example Markdown
This is a code block:
```python
def add(a, b):
return a + b
```
"""
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield Markdown(MARKDOWN)
def on_click(self, event: events.Click) -> None:
widget = event.widget
if widget is None:
return
if isinstance(widget.parent, MarkdownFence):
self.notify(widget.parent.code)
if __name__ == "__main__":
app = ExampleApp()
app.run() |
Beta Was this translation helpful? Give feedback.
I would probably just check if the click event for a
MarkdownFence
, even if this widget is internal it does seem the simplest way!