1use std::{
2 any::Any,
3 collections::HashMap,
4 fmt,
5 sync::{
6 Arc,
7 atomic::{AtomicU64, Ordering},
8 },
9};
10
11use gpui::{AnyElement, App, IntoElement, SharedString, Window};
12use markdown::{ParseOptions, mdast};
13
14use crate::text::node::Span;
15
16static MARKDOWN_EXTENSIONS_REVISION: AtomicU64 = AtomicU64::new(1);
17
18pub use markdown::mdast as markdown_ast;
20
21pub type MarkdownBlockParserFn =
27 dyn for<'a> Fn(&mdast::Node, &MarkdownParseContext<'a>) -> Option<MarkdownNode> + Send + Sync;
28
29pub type MarkdownBlockRenderFn =
31 dyn Fn(&MarkdownNode, &mut Window, &mut App) -> AnyElement + Send + Sync;
32
33pub trait MarkdownPlugin: Send + Sync + 'static {
35 fn is_block(&self) -> bool {
40 false
41 }
42
43 fn name(&self) -> &str;
45
46 fn parse(&self, node: &mdast::Node, cx: &MarkdownParseContext<'_>) -> Option<MarkdownNode>;
48
49 fn render(&self, node: &MarkdownNode, window: &mut Window, cx: &mut App) -> impl IntoElement;
51}
52
53pub struct MarkdownParseContext<'a> {
55 source: &'a str,
56 offset: usize,
57}
58
59impl<'a> MarkdownParseContext<'a> {
60 pub(crate) fn new(source: &'a str, offset: usize) -> Self {
61 Self { source, offset }
62 }
63
64 pub fn source(&self) -> &'a str {
66 self.source
67 }
68
69 pub fn offset(&self) -> usize {
72 self.offset
73 }
74
75 pub fn node_source(&self, node: &mdast::Node) -> Option<&'a str> {
77 let position = node.position()?;
78 self.source.get(position.start.offset..position.end.offset)
79 }
80}
81
82#[derive(Clone)]
84pub struct MarkdownNode {
85 name: SharedString,
86 text: SharedString,
87 markdown: SharedString,
88 data: Arc<dyn Any + Send + Sync>,
89 pub(crate) span: Option<Span>,
90}
91
92impl MarkdownNode {
93 pub fn new<T>(name: impl Into<SharedString>, data: T) -> Self
95 where
96 T: Any + Send + Sync + 'static,
97 {
98 Self {
99 name: name.into(),
100 text: SharedString::default(),
101 markdown: SharedString::default(),
102 data: Arc::new(data),
103 span: None,
104 }
105 }
106
107 pub fn name(&self) -> &str {
109 &self.name
110 }
111
112 pub fn as_text(&self) -> &str {
114 &self.text
115 }
116
117 pub fn as_markdown(&self) -> &str {
119 &self.markdown
120 }
121
122 pub fn text(mut self, text: impl Into<SharedString>) -> Self {
124 self.text = text.into();
125 self
126 }
127
128 pub fn markdown(mut self, markdown: impl Into<SharedString>) -> Self {
130 self.markdown = markdown.into();
131 self
132 }
133
134 pub fn data<T>(&self) -> Option<&T>
136 where
137 T: Any + Send + Sync + 'static,
138 {
139 self.data.downcast_ref()
140 }
141
142 pub(crate) fn set_span(&mut self, span: Option<Span>) {
143 self.span = span;
144 }
145
146 pub(crate) fn to_markdown(&self) -> String {
147 if self.markdown.is_empty() {
148 self.text.to_string()
149 } else {
150 self.markdown.to_string()
151 }
152 }
153}
154
155impl fmt::Debug for MarkdownNode {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 f.debug_struct("MarkdownNode")
158 .field("name", &self.name)
159 .field("text", &self.text)
160 .field("markdown", &self.markdown)
161 .field("span", &self.span)
162 .finish_non_exhaustive()
163 }
164}
165
166impl PartialEq for MarkdownNode {
167 fn eq(&self, other: &Self) -> bool {
168 self.name == other.name
169 && self.text == other.text
170 && self.markdown == other.markdown
171 && self.span == other.span
172 }
173}
174
175#[derive(Clone, Default)]
177pub struct MarkdownExtensions {
178 enable_mdx: bool,
179 block_parsers: Vec<Arc<MarkdownBlockParserFn>>,
180 block_renderers: HashMap<SharedString, Arc<MarkdownBlockRenderFn>>,
181 revision: u64,
182}
183
184impl MarkdownExtensions {
185 pub fn mdx(mut self) -> Self {
190 self.enable_mdx = true;
191 self.bump_revision();
192 self
193 }
194
195 pub fn block_parser<F>(mut self, parser: F) -> Self
197 where
198 F: for<'a> Fn(&mdast::Node, &MarkdownParseContext<'a>) -> Option<MarkdownNode>
199 + Send
200 + Sync
201 + 'static,
202 {
203 self.push_block_parser(parser);
204 self
205 }
206
207 pub fn block_renderer<F, E>(mut self, name: impl Into<SharedString>, renderer: F) -> Self
209 where
210 F: Fn(&MarkdownNode, &mut Window, &mut App) -> E + Send + Sync + 'static,
211 E: IntoElement,
212 {
213 self.push_block_renderer(name, renderer);
214 self
215 }
216
217 pub fn plugin<P>(self, plugin: P) -> Self
219 where
220 P: MarkdownPlugin,
221 {
222 let plugin = Arc::new(plugin);
223 let name = SharedString::from(plugin.name().to_string());
224 let parser = plugin.clone();
225 let renderer = plugin;
226
227 if parser.is_block() {
228 let mut extensions = self.block_parser(move |node, cx| parser.parse(node, cx));
229 extensions.push_block_renderer(name, move |node, window, cx| {
230 renderer.render(node, window, cx).into_any_element()
231 });
232 extensions
233 } else {
234 panic!("inline Markdown plugins are not supported by TextView yet")
235 }
236 }
237
238 pub(crate) fn revision(&self) -> u64 {
239 self.revision
240 }
241
242 pub(crate) fn push_block_parser<F>(&mut self, parser: F)
243 where
244 F: for<'a> Fn(&mdast::Node, &MarkdownParseContext<'a>) -> Option<MarkdownNode>
245 + Send
246 + Sync
247 + 'static,
248 {
249 self.block_parsers.push(Arc::new(parser));
250 self.bump_revision();
251 }
252
253 pub(crate) fn push_block_renderer<F, E>(&mut self, name: impl Into<SharedString>, renderer: F)
254 where
255 F: Fn(&MarkdownNode, &mut Window, &mut App) -> E + Send + Sync + 'static,
256 E: IntoElement,
257 {
258 self.block_renderers.insert(
259 name.into(),
260 Arc::new(move |node, window, cx| renderer(node, window, cx).into_any_element()),
261 );
262 self.bump_revision();
263 }
264
265 pub(crate) fn parse_options(&self) -> ParseOptions {
266 let mut options = ParseOptions::gfm();
267 if self.enable_mdx {
268 options.constructs.html_flow = false;
269 options.constructs.html_text = false;
270 options.constructs.mdx_expression_flow = true;
271 options.constructs.mdx_expression_text = true;
272 options.constructs.mdx_jsx_flow = true;
273 options.constructs.mdx_jsx_text = true;
274 }
275 options
276 }
277
278 pub(crate) fn parse_block(
279 &self,
280 node: &mdast::Node,
281 cx: &MarkdownParseContext<'_>,
282 ) -> Option<MarkdownNode> {
283 for parser in &self.block_parsers {
284 if let Some(node) = parser(node, cx) {
285 return Some(node);
286 }
287 }
288 None
289 }
290
291 pub(crate) fn render_block(
292 &self,
293 node: &MarkdownNode,
294 window: &mut Window,
295 cx: &mut App,
296 ) -> Option<AnyElement> {
297 self.block_renderers
298 .get(node.name())
299 .map(|render| render(node, window, cx))
300 }
301
302 fn bump_revision(&mut self) {
303 self.revision = MARKDOWN_EXTENSIONS_REVISION.fetch_add(1, Ordering::Relaxed);
304 }
305}