1//! Provides Markdown-related constructs.
2
3use std::sync::Arc;
4use std::{ops::Range, path::PathBuf};
5
6use crate::{HighlightId, Language, LanguageRegistry};
7use gpui::{px, FontStyle, FontWeight, HighlightStyle, StrikethroughStyle, UnderlineStyle};
8use pulldown_cmark::{CodeBlockKind, Event, Parser, Tag, TagEnd};
9
10/// Parsed Markdown content.
11#[derive(Debug, Clone, Default)]
12pub struct ParsedMarkdown {
13 /// The Markdown text.
14 pub text: String,
15 /// The list of highlights contained in the Markdown document.
16 pub highlights: Vec<(Range<usize>, MarkdownHighlight)>,
17 /// The regions of the various ranges in the Markdown document.
18 pub region_ranges: Vec<Range<usize>>,
19 /// The regions of the Markdown document.
20 pub regions: Vec<ParsedRegion>,
21}
22
23/// A run of highlighted Markdown text.
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub enum MarkdownHighlight {
26 /// A styled Markdown highlight.
27 Style(MarkdownHighlightStyle),
28 /// A highlighted code block.
29 Code(HighlightId),
30}
31
32impl MarkdownHighlight {
33 /// Converts this [`MarkdownHighlight`] to a [`HighlightStyle`].
34 pub fn to_highlight_style(&self, theme: &theme::SyntaxTheme) -> Option<HighlightStyle> {
35 match self {
36 MarkdownHighlight::Style(style) => {
37 let mut highlight = HighlightStyle::default();
38
39 if style.italic {
40 highlight.font_style = Some(FontStyle::Italic);
41 }
42
43 if style.underline {
44 highlight.underline = Some(UnderlineStyle {
45 thickness: px(1.),
46 ..Default::default()
47 });
48 }
49
50 if style.strikethrough {
51 highlight.strikethrough = Some(StrikethroughStyle {
52 thickness: px(1.),
53 ..Default::default()
54 });
55 }
56
57 if style.weight != FontWeight::default() {
58 highlight.font_weight = Some(style.weight);
59 }
60
61 Some(highlight)
62 }
63
64 MarkdownHighlight::Code(id) => id.style(theme),
65 }
66 }
67}
68
69/// The style for a Markdown highlight.
70#[derive(Debug, Clone, Default, PartialEq, Eq)]
71pub struct MarkdownHighlightStyle {
72 /// Whether the text should be italicized.
73 pub italic: bool,
74 /// Whether the text should be underlined.
75 pub underline: bool,
76 /// Whether the text should be struck through.
77 pub strikethrough: bool,
78 /// The weight of the text.
79 pub weight: FontWeight,
80}
81
82/// A parsed region in a Markdown document.
83#[derive(Debug, Clone)]
84pub struct ParsedRegion {
85 /// Whether the region is a code block.
86 pub code: bool,
87 /// The link contained in this region, if it has one.
88 pub link: Option<Link>,
89}
90
91/// A Markdown link.
92#[derive(Debug, Clone)]
93pub enum Link {
94 /// A link to a webpage.
95 Web {
96 /// The URL of the webpage.
97 url: String,
98 },
99 /// A link to a path on the filesystem.
100 Path {
101 /// The path to the item.
102 path: PathBuf,
103 },
104}
105
106impl Link {
107 fn identify(text: String) -> Option<Link> {
108 if text.starts_with("http") {
109 return Some(Link::Web { url: text });
110 }
111
112 let path = PathBuf::from(text);
113 if path.is_absolute() {
114 return Some(Link::Path { path });
115 }
116
117 None
118 }
119}
120
121/// Parses a string of Markdown.
122pub async fn parse_markdown(
123 markdown: &str,
124 language_registry: &Arc<LanguageRegistry>,
125 language: Option<Arc<Language>>,
126) -> ParsedMarkdown {
127 let mut text = String::new();
128 let mut highlights = Vec::new();
129 let mut region_ranges = Vec::new();
130 let mut regions = Vec::new();
131
132 parse_markdown_block(
133 markdown,
134 language_registry,
135 language,
136 &mut text,
137 &mut highlights,
138 &mut region_ranges,
139 &mut regions,
140 )
141 .await;
142
143 ParsedMarkdown {
144 text,
145 highlights,
146 region_ranges,
147 regions,
148 }
149}
150
151/// Parses a Markdown block.
152pub async fn parse_markdown_block(
153 markdown: &str,
154 language_registry: &Arc<LanguageRegistry>,
155 language: Option<Arc<Language>>,
156 text: &mut String,
157 highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
158 region_ranges: &mut Vec<Range<usize>>,
159 regions: &mut Vec<ParsedRegion>,
160) {
161 let mut bold_depth = 0;
162 let mut italic_depth = 0;
163 let mut strikethrough_depth = 0;
164 let mut link_url = None;
165 let mut current_language = None;
166 let mut list_stack = Vec::new();
167
168 let mut options = pulldown_cmark::Options::all();
169 options.remove(pulldown_cmark::Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
170
171 for event in Parser::new_ext(markdown, options) {
172 let prev_len = text.len();
173 match event {
174 Event::Text(t) => {
175 if let Some(language) = ¤t_language {
176 highlight_code(text, highlights, t.as_ref(), language);
177 } else {
178 text.push_str(t.as_ref());
179
180 let mut style = MarkdownHighlightStyle::default();
181
182 if bold_depth > 0 {
183 style.weight = FontWeight::BOLD;
184 }
185
186 if italic_depth > 0 {
187 style.italic = true;
188 }
189
190 if strikethrough_depth > 0 {
191 style.strikethrough = true;
192 }
193
194 if let Some(link) = link_url.clone().and_then(|u| Link::identify(u)) {
195 region_ranges.push(prev_len..text.len());
196 regions.push(ParsedRegion {
197 code: false,
198 link: Some(link),
199 });
200 style.underline = true;
201 }
202
203 if style != MarkdownHighlightStyle::default() {
204 let mut new_highlight = true;
205 if let Some((last_range, MarkdownHighlight::Style(last_style))) =
206 highlights.last_mut()
207 {
208 if last_range.end == prev_len && last_style == &style {
209 last_range.end = text.len();
210 new_highlight = false;
211 }
212 }
213 if new_highlight {
214 let range = prev_len..text.len();
215 highlights.push((range, MarkdownHighlight::Style(style)));
216 }
217 }
218 }
219 }
220
221 Event::Code(t) => {
222 text.push_str(t.as_ref());
223 region_ranges.push(prev_len..text.len());
224
225 let link = link_url.clone().and_then(|u| Link::identify(u));
226 if link.is_some() {
227 highlights.push((
228 prev_len..text.len(),
229 MarkdownHighlight::Style(MarkdownHighlightStyle {
230 underline: true,
231 ..Default::default()
232 }),
233 ));
234 }
235 regions.push(ParsedRegion { code: true, link });
236 }
237
238 Event::Start(tag) => match tag {
239 Tag::Paragraph => new_paragraph(text, &mut list_stack),
240
241 Tag::Heading {
242 level: _,
243 id: _,
244 classes: _,
245 attrs: _,
246 } => {
247 new_paragraph(text, &mut list_stack);
248 bold_depth += 1;
249 }
250
251 Tag::CodeBlock(kind) => {
252 new_paragraph(text, &mut list_stack);
253 current_language = if let CodeBlockKind::Fenced(language) = kind {
254 language_registry
255 .language_for_name_or_extension(language.as_ref())
256 .await
257 .ok()
258 } else {
259 language.clone()
260 }
261 }
262
263 Tag::Emphasis => italic_depth += 1,
264
265 Tag::Strong => bold_depth += 1,
266
267 Tag::Strikethrough => strikethrough_depth += 1,
268
269 Tag::Link {
270 link_type: _,
271 dest_url,
272 title: _,
273 id: _,
274 } => link_url = Some(dest_url.to_string()),
275
276 Tag::List(number) => {
277 list_stack.push((number, false));
278 }
279
280 Tag::Item => {
281 let len = list_stack.len();
282 if let Some((list_number, has_content)) = list_stack.last_mut() {
283 *has_content = false;
284 if !text.is_empty() && !text.ends_with('\n') {
285 text.push('\n');
286 }
287 for _ in 0..len - 1 {
288 text.push_str(" ");
289 }
290 if let Some(number) = list_number {
291 text.push_str(&format!("{}. ", number));
292 *number += 1;
293 *has_content = false;
294 } else {
295 text.push_str("- ");
296 }
297 }
298 }
299
300 _ => {}
301 },
302
303 Event::End(tag) => match tag {
304 TagEnd::Heading(_) => bold_depth -= 1,
305 TagEnd::CodeBlock => current_language = None,
306 TagEnd::Emphasis => italic_depth -= 1,
307 TagEnd::Strong => bold_depth -= 1,
308 TagEnd::Strikethrough => strikethrough_depth -= 1,
309 TagEnd::Link => link_url = None,
310 TagEnd::List(_) => drop(list_stack.pop()),
311 _ => {}
312 },
313
314 Event::HardBreak => text.push('\n'),
315
316 Event::SoftBreak => text.push(' '),
317
318 _ => {}
319 }
320 }
321}
322
323/// Appends a highlighted run of text to the provided `text` buffer.
324pub fn highlight_code(
325 text: &mut String,
326 highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
327 content: &str,
328 language: &Arc<Language>,
329) {
330 let prev_len = text.len();
331 text.push_str(content);
332 for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
333 let highlight = MarkdownHighlight::Code(highlight_id);
334 highlights.push((prev_len + range.start..prev_len + range.end, highlight));
335 }
336}
337
338/// Appends a new paragraph to the provided `text` buffer.
339pub fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
340 let mut is_subsequent_paragraph_of_list = false;
341 if let Some((_, has_content)) = list_stack.last_mut() {
342 if *has_content {
343 is_subsequent_paragraph_of_list = true;
344 } else {
345 *has_content = true;
346 return;
347 }
348 }
349
350 if !text.is_empty() {
351 if !text.ends_with('\n') {
352 text.push('\n');
353 }
354 text.push('\n');
355 }
356 for _ in 0..list_stack.len().saturating_sub(1) {
357 text.push_str(" ");
358 }
359 if is_subsequent_paragraph_of_list {
360 text.push_str(" ");
361 }
362}
363
364#[cfg(test)]
365mod tests {
366
367 #[test]
368 fn test_dividers() {
369 let input = r#"
370### instance-method `format`
371
372---
373→ `void`
374Parameters:
375- `const int &`
376- `const std::tm &`
377- `int & dest`
378
379---
380```cpp
381// In my_formatter_flag
382public: void format(const int &, const std::tm &, int &dest)
383```
384"#;
385
386 let mut options = pulldown_cmark::Options::all();
387 options.remove(pulldown_cmark::Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
388
389 let parser = pulldown_cmark::Parser::new_ext(input, options);
390 for event in parser.into_iter() {
391 println!("{:?}", event);
392 }
393 }
394}