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