From 980d4f1003e8bee5d9117440334aaf4eb97fc4f0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Robin=20Pf=C3=A4ffle?=
<67913738+rpfaeffle@users.noreply.github.com>
Date: Fri, 2 Feb 2024 15:51:05 +0100
Subject: [PATCH] Add inline code blocks in markdown preview (#7277)
Fixes #7236.
The reason why the code was not displayed correctly is due to missing
background color for the inline code block.
Previously to this PR:
After this PR:
Release Notes:
- Fixed inline code block not shown in markdown preview
([#7236](https://github.com/zed-industries/zed/issues/7236)).
---
crates/rich_text/src/rich_text.rs | 34 ++++++++++++++++++++-----------
1 file changed, 22 insertions(+), 12 deletions(-)
diff --git a/crates/rich_text/src/rich_text.rs b/crates/rich_text/src/rich_text.rs
index f5ab38c4cef0f4eec2ed3ed8ec2f577f62bbf1bb..1063d89db3d65627a22aefbca8f5eb21cde41896 100644
--- a/crates/rich_text/src/rich_text.rs
+++ b/crates/rich_text/src/rich_text.rs
@@ -13,6 +13,7 @@ use util::RangeExt;
pub enum Highlight {
Code,
Id(HighlightId),
+ InlineCode(bool),
Highlight(HighlightStyle),
Mention,
SelfMention,
@@ -67,6 +68,23 @@ impl RichText {
background_color: Some(code_background),
..id.style(theme.syntax()).unwrap_or_default()
},
+ Highlight::InlineCode(link) => {
+ if !*link {
+ HighlightStyle {
+ background_color: Some(code_background),
+ ..Default::default()
+ }
+ } else {
+ HighlightStyle {
+ background_color: Some(code_background),
+ underline: Some(UnderlineStyle {
+ thickness: 1.0.into(),
+ ..Default::default()
+ }),
+ ..Default::default()
+ }
+ }
+ }
Highlight::Highlight(highlight) => *highlight,
Highlight::Mention => HighlightStyle {
font_weight: Some(FontWeight::BOLD),
@@ -184,22 +202,14 @@ pub fn render_markdown_mut(
}
Event::Code(t) => {
text.push_str(t.as_ref());
- if link_url.is_some() {
- highlights.push((
- prev_len..text.len(),
- Highlight::Highlight(HighlightStyle {
- underline: Some(UnderlineStyle {
- thickness: 1.0.into(),
- ..Default::default()
- }),
- ..Default::default()
- }),
- ));
- }
+ let is_link = link_url.is_some();
+
if let Some(link_url) = link_url.clone() {
link_ranges.push(prev_len..text.len());
link_urls.push(link_url);
}
+
+ highlights.push((prev_len..text.len(), Highlight::InlineCode(is_link)))
}
Event::Start(tag) => match tag {
Tag::Paragraph => new_paragraph(text, &mut list_stack),