From 016b499cb0be5b33c1da146ae230c74250c50d90 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Sat, 6 Jun 2026 23:55:21 +0100 Subject: [PATCH] fix: rich text link handling --- src/component/markdown.rs | 30 +++++++++++++++++++++++------- src/component/rich_text.rs | 13 ++++++++++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/component/markdown.rs b/src/component/markdown.rs index 9807a6f..a018b45 100644 --- a/src/component/markdown.rs +++ b/src/component/markdown.rs @@ -229,18 +229,30 @@ impl MarkdownText { let (description, src) = if cursor.node().kind_id() == MARKDOWN_KIND_ID_LINK_DESTINATION { - let node = cursor.node(); - let src = &content[node_range!()]; + let src = cursor + .node() + .utf8_text(content.as_ref()) + .ok() + .unwrap_or_default(); (src, src) } else { - let node = cursor.node(); - let description = &content[node_range!()]; + let description = cursor + .node() + .utf8_text(content.as_ref()) + .ok() + .unwrap_or_default(); if cursor.goto_next_sibling() { debug_assert!( cursor.node().kind_id() == MARKDOWN_KIND_ID_LINK_DESTINATION ); - let node = cursor.node(); - (description, &content[node_range!()]) + ( + description, + cursor + .node() + .utf8_text(content.as_ref()) + .ok() + .unwrap_or_default(), + ) } else { // no src for this link node (description, "") @@ -658,7 +670,11 @@ impl gpui::Render for MarkdownText { content, has_padding, } => match decoration { - | None => div().min_w_0().child(rich_text(content.clone())), + | None => div() + .min_w_0() + .child(rich_text(content.clone()).on_click(|asd, _, _| { + println!("link clicked {:?}", asd); + })), | Some(decoration) => div() .w_full() .flex() diff --git a/src/component/rich_text.rs b/src/component/rich_text.rs index fc14c34..4baa389 100644 --- a/src/component/rich_text.rs +++ b/src/component/rich_text.rs @@ -49,7 +49,8 @@ enum RichTextElement { }, } -enum RichTextClickTarget { +#[derive(Debug)] +pub(crate) enum RichTextClickTarget { Link(gpui::SharedString), } @@ -158,6 +159,16 @@ impl RichTextContentBuilder { } } +impl RichText { + pub(crate) fn on_click( + mut self, + f: impl Fn(&RichTextClickTarget, &mut gpui::Window, &mut gpui::App) + 'static, + ) -> Self { + self.on_click = Some(Rc::from(f)); + self + } +} + impl gpui::RenderOnce for RichText { fn render(self, _window: &mut gpui::Window, cx: &mut gpui::App) -> impl gpui::IntoElement { let theme = app::current_theme(cx);