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
71 .new(|cx| Markdown::new(text, Some(language_registry), Some("TypeScript".into()), cx));
72 Self { markdown }
73 }
74}
75
76impl Render for MarkdownExample {
77 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
78 let markdown_style = MarkdownStyle {
79 base_text_style: gpui::TextStyle {
80 font_family: ".ZedSans".into(),
81 color: cx.theme().colors().terminal_ansi_black,
82 ..Default::default()
83 },
84 code_block: StyleRefinement::default()
85 .font_family(".ZedMono")
86 .m(rems(1.))
87 .bg(rgb(0xAAAAAAA)),
88 inline_code: gpui::TextStyleRefinement {
89 font_family: Some(".ZedMono".into()),
90 color: Some(cx.theme().colors().editor_foreground),
91 background_color: Some(cx.theme().colors().editor_background),
92 ..Default::default()
93 },
94 rule_color: Color::Muted.color(cx),
95 block_quote_border_color: Color::Muted.color(cx),
96 block_quote: gpui::TextStyleRefinement {
97 color: Some(Color::Muted.color(cx)),
98 ..Default::default()
99 },
100 link: gpui::TextStyleRefinement {
101 color: Some(Color::Accent.color(cx)),
102 underline: Some(gpui::UnderlineStyle {
103 thickness: px(1.),
104 color: Some(Color::Accent.color(cx)),
105 wavy: false,
106 }),
107 ..Default::default()
108 },
109 syntax: cx.theme().syntax().clone(),
110 selection_background_color: cx.theme().colors().element_selection_background,
111 ..Default::default()
112 };
113
114 div()
115 .id("markdown-example")
116 .debug_selector(|| "foo".into())
117 .relative()
118 .bg(gpui::white())
119 .size_full()
120 .p_4()
121 .overflow_y_scroll()
122 .child(MarkdownElement::new(self.markdown.clone(), markdown_style))
123 }
124}