chat: auto detect links (#8028)

@ConradIrwin here's our current implementation for auto detecting links
in the chat.
We also fixed an edge case where the close reply to preview button was
cut off (rendered off screen).

Release Notes:

- Added auto detection for links in the chat panel.

---------

Co-authored-by: Remco Smits <62463826+RemcoSmitsDev@users.noreply.github.com>
This commit is contained in:
Bennet Bo Fenner
2024-02-19 21:49:47 -07:00
committed by GitHub
co-authored by Remco Smits
parent 1e44bac418
commit 3ef8a9910d
6 changed files with 155 additions and 12 deletions
+41 -5
View File
@@ -175,19 +175,54 @@ pub fn render_markdown_mut(
if italic_depth > 0 {
style.font_style = Some(FontStyle::Italic);
}
if let Some(link_url) = link_url.clone() {
let last_run_len = if let Some(link_url) = link_url.clone() {
link_ranges.push(prev_len..text.len());
link_urls.push(link_url);
style.underline = Some(UnderlineStyle {
thickness: 1.0.into(),
..Default::default()
});
}
prev_len
} else {
// Manually scan for links
let mut finder = linkify::LinkFinder::new();
finder.kinds(&[linkify::LinkKind::Url]);
let mut last_link_len = prev_len;
for link in finder.links(&t) {
let start = link.start();
let end = link.end();
let range = (prev_len + start)..(prev_len + end);
link_ranges.push(range.clone());
link_urls.push(link.as_str().to_string());
if style != HighlightStyle::default() {
// If there is a style before we match a link, we have to add this to the highlighted ranges
if style != HighlightStyle::default() && last_link_len < link.start() {
highlights.push((
last_link_len..link.start(),
Highlight::Highlight(style),
));
}
highlights.push((
range,
Highlight::Highlight(HighlightStyle {
underline: Some(UnderlineStyle {
thickness: 1.0.into(),
..Default::default()
}),
..style
}),
));
last_link_len = end;
}
last_link_len
};
if style != HighlightStyle::default() && last_run_len < text.len() {
let mut new_highlight = true;
if let Some((last_range, last_style)) = highlights.last_mut() {
if last_range.end == prev_len
if last_range.end == last_run_len
&& last_style == &Highlight::Highlight(style)
{
last_range.end = text.len();
@@ -195,7 +230,8 @@ pub fn render_markdown_mut(
}
}
if new_highlight {
highlights.push((prev_len..text.len(), Highlight::Highlight(style)));
highlights
.push((last_run_len..text.len(), Highlight::Highlight(style)));
}
}
}