Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

markdown_preview: Add heading_font_weight setting (#64421)

andrew28d5832adb44
diff --git a/assets/settings/default.json b/assets/settings/default.json
index a2dc6fa31c4..1bd9b7f3240 100644
--- a/assets/settings/default.json
+++ b/assets/settings/default.json
@@ -118,6 +118,8 @@
"font_family": null,
// The font family for code blocks in the markdown preview. Falls back to the editor font family if unset.
"code_font_family": null,
+ // The font weight for headings in the markdown preview, in CSS units from 100 to 900.
+ "heading_font_weight": 600,
// Whether to automatically open Markdown files in the preview.
"open_markdown_files_in_preview": false,
// Whether to limit the width of the rendered markdown content. When
diff --git a/crates/markdown/src/markdown.rs b/crates/markdown/src/markdown.rs
index c25884cd98b..2e57237610a 100644
--- a/crates/markdown/src/markdown.rs
+++ b/crates/markdown/src/markdown.rs
@@ -339,13 +339,20 @@ impl MarkdownStyle {
};
if is_preview {
- style.with_preview_overrides(colors)
+ style.with_preview_overrides(
+ colors,
+ theme_settings.markdown_preview_heading_font_weight(),
+ )
} else {
style
}
}
- fn with_preview_overrides(mut self, colors: &theme::ThemeColors) -> Self {
+ fn with_preview_overrides(
+ mut self,
+ colors: &theme::ThemeColors,
+ heading_font_weight: FontWeight,
+ ) -> Self {
let body_font_size = rems(1.0);
self.base_text_style.font_size = body_font_size.into();
self.container_style.text.font_size = Some(body_font_size.into());
@@ -384,7 +391,7 @@ impl MarkdownStyle {
let heading_text_style = |font_size: Rems| TextStyleRefinement {
font_size: Some(font_size.into()),
- font_weight: Some(FontWeight::SEMIBOLD),
+ font_weight: Some(heading_font_weight),
line_height: Some(relative(1.25)),
..Default::default()
};
@@ -7643,6 +7650,74 @@ mod tests {
);
}
+ fn preview_heading_weights(cx: &mut TestAppContext, font: MarkdownFont) -> [FontWeight; 6] {
+ let (_, cx) = cx.add_window_view(|_, _| TestWindow);
+ cx.update(|window, cx| {
+ let style = MarkdownStyle::themed(font, window, cx);
+ let levels = style
+ .heading_level_styles
+ .expect("preview markdown should define per-level heading styles");
+ [
+ levels.h1, levels.h2, levels.h3, levels.h4, levels.h5, levels.h6,
+ ]
+ .map(|level| {
+ level
+ .and_then(|level| level.font_weight)
+ .expect("every preview heading level should set a font weight")
+ })
+ })
+ }
+
+ #[gpui::test]
+ fn test_markdown_preview_heading_font_weight_defaults_to_semibold(cx: &mut TestAppContext) {
+ ensure_theme_initialized(cx);
+
+ assert_eq!(
+ preview_heading_weights(cx, MarkdownFont::Preview),
+ [FontWeight::SEMIBOLD; 6]
+ );
+ }
+
+ #[gpui::test]
+ fn test_markdown_preview_heading_font_weight_follows_setting(cx: &mut TestAppContext) {
+ ensure_theme_initialized(cx);
+
+ cx.update(|cx| {
+ settings::SettingsStore::update_global(cx, |store, cx| {
+ store.update_user_settings(cx, |settings| {
+ settings
+ .markdown_preview
+ .get_or_insert_default()
+ .heading_font_weight = Some(400.0.into());
+ });
+ });
+ });
+ cx.run_until_parked();
+
+ assert_eq!(
+ preview_heading_weights(cx, MarkdownFont::Preview),
+ [FontWeight::NORMAL; 6]
+ );
+
+ let (_, cx) = cx.add_window_view(|_, _| TestWindow);
+ cx.update(|window, cx| {
+ let editor_style = MarkdownStyle::themed(MarkdownFont::Editor, window, cx);
+ assert!(
+ editor_style.heading_level_styles.is_none(),
+ "the preview heading weight must not leak into editor markdown"
+ );
+ let agent_style = MarkdownStyle::themed(MarkdownFont::Agent, window, cx);
+ let agent_h1 = agent_style
+ .heading_level_styles
+ .and_then(|levels| levels.h1)
+ .expect("agent markdown defines an h1 style");
+ assert_eq!(
+ agent_h1.font_weight, None,
+ "the preview heading weight must not leak into agent markdown"
+ );
+ });
+ }
+
#[gpui::test]
fn test_ui_zoom_does_not_affect_markdown_preview(cx: &mut TestAppContext) {
ensure_theme_initialized(cx);
diff --git a/crates/settings_content/src/settings_content.rs b/crates/settings_content/src/settings_content.rs
index 290a14c0d98..5de2a86ad9f 100644
--- a/crates/settings_content/src/settings_content.rs
+++ b/crates/settings_content/src/settings_content.rs
@@ -1302,6 +1302,12 @@ pub struct MarkdownPreviewSettingsContent {
/// The theme to use for the markdown preview.
/// Falls back to the main editor theme if unset.
pub theme: Option<ThemeSelection>,
+ /// The weight of headings (H1 through H6) in the markdown preview, in CSS
+ /// units from 100 to 900. Also applies to rendered Markdown cells in
+ /// notebooks, which share the preview typography.
+ ///
+ /// Default: 600
+ pub heading_font_weight: Option<FontWeightContent>,
/// Whether to automatically open Markdown files in the preview.
///
/// Default: false
diff --git a/crates/settings_ui/src/page_data.rs b/crates/settings_ui/src/page_data.rs
index 972be0aba15..529537344d4 100644
--- a/crates/settings_ui/src/page_data.rs
+++ b/crates/settings_ui/src/page_data.rs
@@ -1246,7 +1246,7 @@ fn appearance_page() -> SettingsPage {
]
}
- fn markdown_preview_font_section() -> [SettingsPageItem; 4] {
+ fn markdown_preview_font_section() -> [SettingsPageItem; 5] {
[
SettingsPageItem::SectionHeader("Markdown Preview Font"),
SettingsPageItem::SettingItem(SettingItem {
@@ -1318,6 +1318,29 @@ fn appearance_page() -> SettingsPage {
metadata: None,
files: USER,
}),
+ SettingsPageItem::SettingItem(SettingItem {
+ title: "Heading Font Weight",
+ description: "Font weight for headings in the markdown preview, in CSS units from 100 to 900.",
+ field: Box::new(SettingField {
+ organization_override: None,
+ json_path: Some("markdown_preview.heading_font_weight"),
+ pick: |settings_content| {
+ settings_content
+ .markdown_preview
+ .as_ref()?
+ .heading_font_weight
+ .as_ref()
+ },
+ write: |settings_content, value, _| {
+ settings_content
+ .markdown_preview
+ .get_or_insert_default()
+ .heading_font_weight = value;
+ },
+ }),
+ metadata: None,
+ files: USER,
+ }),
]
}
diff --git a/crates/theme_settings/src/settings.rs b/crates/theme_settings/src/settings.rs
index e72179bdc50..76213f33355 100644
--- a/crates/theme_settings/src/settings.rs
+++ b/crates/theme_settings/src/settings.rs
@@ -4,8 +4,8 @@ use crate::schema::{status_colors_refinement, syntax_overrides, theme_colors_ref
use crate::{merge_accent_colors, merge_player_colors};
use collections::HashMap;
use gpui::{
- App, Context, Font, FontFallbacks, FontStyle, Global, Pixels, SharedString, Subscription,
- Window, px,
+ App, Context, Font, FontFallbacks, FontStyle, FontWeight, Global, Pixels, SharedString,
+ Subscription, Window, px,
};
use refineable::Refineable;
use schemars::JsonSchema;
@@ -75,6 +75,8 @@ pub struct ThemeSettings {
/// The theme to use for the markdown preview.
/// Falls back to the main editor theme if unset.
pub markdown_preview_theme: Option<ThemeSelection>,
+ /// The font weight for headings in the markdown preview.
+ markdown_preview_heading_font_weight: FontWeight,
/// The line height for buffers, and the terminal.
///
/// Changing this may affect the spacing of some UI elements.
@@ -442,6 +444,11 @@ impl ThemeSettings {
.unwrap_or(&self.buffer_font.family)
}
+ /// Returns the font weight to use for headings in the markdown preview.
+ pub fn markdown_preview_heading_font_weight(&self) -> FontWeight {
+ self.markdown_preview_heading_font_weight
+ }
+
/// Returns the markdown preview font size.
///
/// Note: the fallback deliberately uses `self.ui_font_size` instead of `ui_font_size(cx)`,
@@ -756,6 +763,10 @@ impl settings::Settings for ThemeSettings {
markdown_preview_theme: markdown_preview
.and_then(|preview| preview.theme.clone())
.map(ThemeSelection::from),
+ markdown_preview_heading_font_weight: markdown_preview
+ .and_then(|preview| preview.heading_font_weight)
+ .map(|weight| weight.into_gpui())
+ .unwrap_or(FontWeight::SEMIBOLD),
theme: theme_selection,
experimental_theme_overrides: content.experimental_theme_overrides.clone(),
theme_overrides: content.theme_overrides.clone(),
diff --git a/docs/src/visual-customization.md b/docs/src/visual-customization.md
index c727ee4f175..e08f38cfeb8 100644
--- a/docs/src/visual-customization.md
+++ b/docs/src/visual-customization.md
@@ -87,7 +87,10 @@ If you would like to use distinct themes for light mode/dark mode that can be se
"font_family": null,
// Controls the font family for code blocks in the markdown preview.
// If not specified, it falls back to the editor font family.
- "code_font_family": null
+ "code_font_family": null,
+ // Controls the font weight for headings (H1 through H6) in the markdown preview,
+ // in CSS units from 100 to 900. Defaults to 600.
+ "heading_font_weight": 600
}
```