markdown.rs

  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: Option<&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: Option<&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_DEFINITION_LIST);
170    options.remove(pulldown_cmark::Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
171
172    for event in Parser::new_ext(markdown, options) {
173        let prev_len = text.len();
174        match event {
175            Event::Text(t) => {
176                if let Some(language) = &current_language {
177                    highlight_code(text, highlights, t.as_ref(), language);
178                } else {
179                    text.push_str(t.as_ref());
180
181                    let mut style = MarkdownHighlightStyle::default();
182
183                    if bold_depth > 0 {
184                        style.weight = FontWeight::BOLD;
185                    }
186
187                    if italic_depth > 0 {
188                        style.italic = true;
189                    }
190
191                    if strikethrough_depth > 0 {
192                        style.strikethrough = true;
193                    }
194
195                    if let Some(link) = link_url.clone().and_then(Link::identify) {
196                        region_ranges.push(prev_len..text.len());
197                        regions.push(ParsedRegion {
198                            code: false,
199                            link: Some(link),
200                        });
201                        style.underline = true;
202                    }
203
204                    if style != MarkdownHighlightStyle::default() {
205                        let mut new_highlight = true;
206                        if let Some((last_range, MarkdownHighlight::Style(last_style))) =
207                            highlights.last_mut()
208                        {
209                            if last_range.end == prev_len && last_style == &style {
210                                last_range.end = text.len();
211                                new_highlight = false;
212                            }
213                        }
214                        if new_highlight {
215                            let range = prev_len..text.len();
216                            highlights.push((range, MarkdownHighlight::Style(style)));
217                        }
218                    }
219                }
220            }
221
222            Event::Code(t) => {
223                text.push_str(t.as_ref());
224                region_ranges.push(prev_len..text.len());
225
226                let link = link_url.clone().and_then(Link::identify);
227                if link.is_some() {
228                    highlights.push((
229                        prev_len..text.len(),
230                        MarkdownHighlight::Style(MarkdownHighlightStyle {
231                            underline: true,
232                            ..Default::default()
233                        }),
234                    ));
235                }
236                regions.push(ParsedRegion { code: true, link });
237            }
238
239            Event::Start(tag) => match tag {
240                Tag::Paragraph => new_paragraph(text, &mut list_stack),
241
242                Tag::Heading { .. } => {
243                    new_paragraph(text, &mut list_stack);
244                    bold_depth += 1;
245                }
246
247                Tag::CodeBlock(kind) => {
248                    new_paragraph(text, &mut list_stack);
249                    current_language = if let CodeBlockKind::Fenced(language) = kind {
250                        match language_registry {
251                            None => None,
252                            Some(language_registry) => language_registry
253                                .language_for_name_or_extension(language.as_ref())
254                                .await
255                                .ok(),
256                        }
257                    } else {
258                        language.clone()
259                    }
260                }
261
262                Tag::Emphasis => italic_depth += 1,
263
264                Tag::Strong => bold_depth += 1,
265
266                Tag::Strikethrough => strikethrough_depth += 1,
267
268                Tag::Link { dest_url, .. } => link_url = Some(dest_url.to_string()),
269
270                Tag::List(number) => {
271                    list_stack.push((number, false));
272                }
273
274                Tag::Item => {
275                    let len = list_stack.len();
276                    if let Some((list_number, has_content)) = list_stack.last_mut() {
277                        *has_content = false;
278                        if !text.is_empty() && !text.ends_with('\n') {
279                            text.push('\n');
280                        }
281                        for _ in 0..len - 1 {
282                            text.push_str("  ");
283                        }
284                        if let Some(number) = list_number {
285                            text.push_str(&format!("{}. ", number));
286                            *number += 1;
287                            *has_content = false;
288                        } else {
289                            text.push_str("- ");
290                        }
291                    }
292                }
293
294                _ => {}
295            },
296
297            Event::End(tag) => match tag {
298                TagEnd::Heading(_) => bold_depth -= 1,
299                TagEnd::CodeBlock => current_language = None,
300                TagEnd::Emphasis => italic_depth -= 1,
301                TagEnd::Strong => bold_depth -= 1,
302                TagEnd::Strikethrough => strikethrough_depth -= 1,
303                TagEnd::Link => link_url = None,
304                TagEnd::List(_) => drop(list_stack.pop()),
305                _ => {}
306            },
307
308            Event::HardBreak => text.push('\n'),
309
310            Event::SoftBreak => text.push(' '),
311
312            _ => {}
313        }
314    }
315}
316
317/// Appends a highlighted run of text to the provided `text` buffer.
318pub fn highlight_code(
319    text: &mut String,
320    highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
321    content: &str,
322    language: &Arc<Language>,
323) {
324    let prev_len = text.len();
325    text.push_str(content);
326    for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
327        let highlight = MarkdownHighlight::Code(highlight_id);
328        highlights.push((prev_len + range.start..prev_len + range.end, highlight));
329    }
330}
331
332/// Appends a new paragraph to the provided `text` buffer.
333pub fn new_paragraph(text: &mut String, list_stack: &mut [(Option<u64>, bool)]) {
334    let mut is_subsequent_paragraph_of_list = false;
335    if let Some((_, has_content)) = list_stack.last_mut() {
336        if *has_content {
337            is_subsequent_paragraph_of_list = true;
338        } else {
339            *has_content = true;
340            return;
341        }
342    }
343
344    if !text.is_empty() {
345        if !text.ends_with('\n') {
346            text.push('\n');
347        }
348        text.push('\n');
349    }
350    for _ in 0..list_stack.len().saturating_sub(1) {
351        text.push_str("  ");
352    }
353    if is_subsequent_paragraph_of_list {
354        text.push_str("  ");
355    }
356}
357
358#[cfg(test)]
359mod tests {
360
361    #[test]
362    fn test_dividers() {
363        let input = r#"
364### instance-method `format`
365
366---
367→ `void`
368Parameters:
369- `const int &`
370- `const std::tm &`
371- `int & dest`
372
373---
374```cpp
375// In my_formatter_flag
376public: void format(const int &, const std::tm &, int &dest)
377```
378"#;
379
380        let mut options = pulldown_cmark::Options::all();
381        options.remove(pulldown_cmark::Options::ENABLE_DEFINITION_LIST);
382        options.remove(pulldown_cmark::Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
383
384        let parser = pulldown_cmark::Parser::new_ext(input, options);
385        for event in parser.into_iter() {
386            println!("{:?}", event);
387        }
388    }
389}