1use assets::Assets;
2use gpui::{Application, Entity, KeyBinding, StyleRefinement, WindowOptions, prelude::*, rgb};
3use language::{LanguageRegistry, language_settings::AllLanguageSettings};
4use markdown::{Markdown, MarkdownElement, MarkdownStyle};
5use node_runtime::NodeRuntime;
6use settings::SettingsStore;
7use std::sync::Arc;
8use theme::LoadThemes;
9use ui::prelude::*;
10use ui::{App, Window, div};
11
12const MARKDOWN_EXAMPLE: &str = r#"
13# Markdown Example Document
14
15## Headings
16Headings are created by adding one or more `#` symbols before your heading text. The number of `#` you use will determine the size of the heading.
17
18```
19function a(b: T) {
20
21}
22```
23
24Remember, markdown processors may have slight differences and extensions, so always refer to the specific documentation or guides relevant to your platform or editor for the best practices and additional features.
25
26## Images
27
28 item one
29
30 item two
31
32 item three
33
34"#;
35
36pub fn main() {
37 env_logger::init();
38 Application::new().with_assets(Assets).run(|cx| {
39 let store = SettingsStore::test(cx);
40 cx.set_global(store);
41 language::init(cx);
42 SettingsStore::update(cx, |store, cx| {
43 store.update_user_settings::<AllLanguageSettings>(cx, |_| {});
44 });
45 cx.bind_keys([KeyBinding::new("cmd-c", markdown::Copy, None)]);
46
47 let node_runtime = NodeRuntime::unavailable();
48 theme::init(LoadThemes::JustBase, cx);
49
50 let language_registry = LanguageRegistry::new(cx.background_executor().clone());
51 language_registry.set_theme(cx.theme().clone());
52 let language_registry = Arc::new(language_registry);
53 languages::init(language_registry.clone(), node_runtime, cx);
54 Assets.load_fonts(cx).unwrap();
55
56 cx.activate(true);
57 cx.open_window(WindowOptions::default(), |_, cx| {
58 cx.new(|cx| MarkdownExample::new(MARKDOWN_EXAMPLE.into(), language_registry, cx))
59 })
60 .unwrap();
61 });
62}
63
64struct MarkdownExample {
65 markdown: Entity<Markdown>,
66}
67
68impl MarkdownExample {
69 pub fn new(text: SharedString, language_registry: Arc<LanguageRegistry>, cx: &mut App) -> Self {
70 let markdown = cx.new(|cx| {
71 Markdown::new(
72 text,
73 Some(language_registry),
74 Some("TypeScript".to_string()),
75 cx,
76 )
77 });
78 Self { markdown }
79 }
80}
81
82impl Render for MarkdownExample {
83 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
84 let markdown_style = MarkdownStyle {
85 base_text_style: gpui::TextStyle {
86 font_family: "Zed Plex Sans".into(),
87 color: cx.theme().colors().terminal_ansi_black,
88 ..Default::default()
89 },
90 code_block: StyleRefinement::default()
91 .font_family("Zed Plex Mono")
92 .m(rems(1.))
93 .bg(rgb(0xAAAAAAA)),
94 inline_code: gpui::TextStyleRefinement {
95 font_family: Some("Zed Mono".into()),
96 color: Some(cx.theme().colors().editor_foreground),
97 background_color: Some(cx.theme().colors().editor_background),
98 ..Default::default()
99 },
100 rule_color: Color::Muted.color(cx),
101 block_quote_border_color: Color::Muted.color(cx),
102 block_quote: gpui::TextStyleRefinement {
103 color: Some(Color::Muted.color(cx)),
104 ..Default::default()
105 },
106 link: gpui::TextStyleRefinement {
107 color: Some(Color::Accent.color(cx)),
108 underline: Some(gpui::UnderlineStyle {
109 thickness: px(1.),
110 color: Some(Color::Accent.color(cx)),
111 wavy: false,
112 }),
113 ..Default::default()
114 },
115 syntax: cx.theme().syntax().clone(),
116 selection_background_color: {
117 let mut selection = cx.theme().players().local().selection;
118 selection.fade_out(0.7);
119 selection
120 },
121 ..Default::default()
122 };
123
124 div()
125 .id("markdown-example")
126 .debug_selector(|| "foo".into())
127 .relative()
128 .bg(gpui::white())
129 .size_full()
130 .p_4()
131 .overflow_y_scroll()
132 .child(MarkdownElement::new(self.markdown.clone(), markdown_style))
133 }
134}