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: &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_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                    level: _,
244                    id: _,
245                    classes: _,
246                    attrs: _,
247                } => {
248                    new_paragraph(text, &mut list_stack);
249                    bold_depth += 1;
250                }
251
252                Tag::CodeBlock(kind) => {
253                    new_paragraph(text, &mut list_stack);
254                    current_language = if let CodeBlockKind::Fenced(language) = kind {
255                        language_registry
256                            .language_for_name_or_extension(language.as_ref())
257                            .await
258                            .ok()
259                    } else {
260                        language.clone()
261                    }
262                }
263
264                Tag::Emphasis => italic_depth += 1,
265
266                Tag::Strong => bold_depth += 1,
267
268                Tag::Strikethrough => strikethrough_depth += 1,
269
270                Tag::Link {
271                    link_type: _,
272                    dest_url,
273                    title: _,
274                    id: _,
275                } => link_url = Some(dest_url.to_string()),
276
277                Tag::List(number) => {
278                    list_stack.push((number, false));
279                }
280
281                Tag::Item => {
282                    let len = list_stack.len();
283                    if let Some((list_number, has_content)) = list_stack.last_mut() {
284                        *has_content = false;
285                        if !text.is_empty() && !text.ends_with('\n') {
286                            text.push('\n');
287                        }
288                        for _ in 0..len - 1 {
289                            text.push_str("  ");
290                        }
291                        if let Some(number) = list_number {
292                            text.push_str(&format!("{}. ", number));
293                            *number += 1;
294                            *has_content = false;
295                        } else {
296                            text.push_str("- ");
297                        }
298                    }
299                }
300
301                _ => {}
302            },
303
304            Event::End(tag) => match tag {
305                TagEnd::Heading(_) => bold_depth -= 1,
306                TagEnd::CodeBlock => current_language = None,
307                TagEnd::Emphasis => italic_depth -= 1,
308                TagEnd::Strong => bold_depth -= 1,
309                TagEnd::Strikethrough => strikethrough_depth -= 1,
310                TagEnd::Link => link_url = None,
311                TagEnd::List(_) => drop(list_stack.pop()),
312                _ => {}
313            },
314
315            Event::HardBreak => text.push('\n'),
316
317            Event::SoftBreak => text.push(' '),
318
319            _ => {}
320        }
321    }
322}
323
324/// Appends a highlighted run of text to the provided `text` buffer.
325pub fn highlight_code(
326    text: &mut String,
327    highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
328    content: &str,
329    language: &Arc<Language>,
330) {
331    let prev_len = text.len();
332    text.push_str(content);
333    for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
334        let highlight = MarkdownHighlight::Code(highlight_id);
335        highlights.push((prev_len + range.start..prev_len + range.end, highlight));
336    }
337}
338
339/// Appends a new paragraph to the provided `text` buffer.
340pub fn new_paragraph(text: &mut String, list_stack: &mut [(Option<u64>, bool)]) {
341    let mut is_subsequent_paragraph_of_list = false;
342    if let Some((_, has_content)) = list_stack.last_mut() {
343        if *has_content {
344            is_subsequent_paragraph_of_list = true;
345        } else {
346            *has_content = true;
347            return;
348        }
349    }
350
351    if !text.is_empty() {
352        if !text.ends_with('\n') {
353            text.push('\n');
354        }
355        text.push('\n');
356    }
357    for _ in 0..list_stack.len().saturating_sub(1) {
358        text.push_str("  ");
359    }
360    if is_subsequent_paragraph_of_list {
361        text.push_str("  ");
362    }
363}
364
365#[cfg(test)]
366mod tests {
367
368    #[test]
369    fn test_dividers() {
370        let input = r#"
371### instance-method `format`
372
373---
374→ `void`
375Parameters:
376- `const int &`
377- `const std::tm &`
378- `int & dest`
379
380---
381```cpp
382// In my_formatter_flag
383public: void format(const int &, const std::tm &, int &dest)
384```
385"#;
386
387        let mut options = pulldown_cmark::Options::all();
388        options.remove(pulldown_cmark::Options::ENABLE_DEFINITION_LIST);
389        options.remove(pulldown_cmark::Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
390
391        let parser = pulldown_cmark::Parser::new_ext(input, options);
392        for event in parser.into_iter() {
393            println!("{:?}", event);
394        }
395    }
396}