Skip to content

Looking for a way to identify the inline code text in a Markdown widget when clicked #5434

Answered by TomJGooding
jspv asked this question in Q&A
Discussion options

You must be logged in to vote

I would probably just check if the click event for a MarkdownFence, even if this widget is internal it does seem the simplest way!

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.n…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jspv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants