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 fs = fs::FakeFs::new(cx.background_executor().clone());
51 let language_registry = LanguageRegistry::new(cx.background_executor().clone());
52 language_registry.set_theme(cx.theme().clone());
53 let language_registry = Arc::new(language_registry);
54 languages::init(language_registry.clone(), fs, node_runtime, cx);
55 Assets.load_fonts(cx).unwrap();
56
57 cx.activate(true);
58 cx.open_window(WindowOptions::default(), |_, cx| {
59 cx.new(|cx| MarkdownExample::new(MARKDOWN_EXAMPLE.into(), language_registry, cx))
60 })
61 .unwrap();
62 });
63}
64
65struct MarkdownExample {
66 markdown: Entity<Markdown>,
67}
68
69impl MarkdownExample {
70 pub fn new(text: SharedString, language_registry: Arc<LanguageRegistry>, cx: &mut App) -> Self {
71 let markdown = cx
72 .new(|cx| Markdown::new(text, Some(language_registry), Some("TypeScript".into()), cx));
73 Self { markdown }
74 }
75}
76
77impl Render for MarkdownExample {
78 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
79 let markdown_style = MarkdownStyle {
80 base_text_style: gpui::TextStyle {
81 font_family: ".ZedSans".into(),
82 color: cx.theme().colors().terminal_ansi_black,
83 ..Default::default()
84 },
85 code_block: StyleRefinement::default()
86 .font_family(".ZedMono")
87 .m(rems(1.))
88 .bg(rgb(0xAAAAAAA)),
89 inline_code: gpui::TextStyleRefinement {
90 font_family: Some(".ZedMono".into()),
91 color: Some(cx.theme().colors().editor_foreground),
92 background_color: Some(cx.theme().colors().editor_background),
93 ..Default::default()
94 },
95 rule_color: Color::Muted.color(cx),
96 block_quote_border_color: Color::Muted.color(cx),
97 block_quote: gpui::TextStyleRefinement {
98 color: Some(Color::Muted.color(cx)),
99 ..Default::default()
100 },
101 link: gpui::TextStyleRefinement {
102 color: Some(Color::Accent.color(cx)),
103 underline: Some(gpui::UnderlineStyle {
104 thickness: px(1.),
105 color: Some(Color::Accent.color(cx)),
106 wavy: false,
107 }),
108 ..Default::default()
109 },
110 syntax: cx.theme().syntax().clone(),
111 selection_background_color: cx.theme().colors().element_selection_background,
112 ..Default::default()
113 };
114
115 div()
116 .id("markdown-example")
117 .debug_selector(|| "foo".into())
118 .relative()
119 .bg(gpui::white())
120 .size_full()
121 .p_4()
122 .overflow_y_scroll()
123 .child(MarkdownElement::new(self.markdown.clone(), markdown_style))
124 }
125}