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