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