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, UnderlineStyle};
8use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
9
10/// Parsed Markdown content.
11#[derive(Debug, Clone)]
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.weight != FontWeight::default() {
51 highlight.font_weight = Some(style.weight);
52 }
53
54 Some(highlight)
55 }
56
57 MarkdownHighlight::Code(id) => id.style(theme),
58 }
59 }
60}
61
62/// The style for a Markdown highlight.
63#[derive(Debug, Clone, Default, PartialEq, Eq)]
64pub struct MarkdownHighlightStyle {
65 /// Whether the text should be italicized.
66 pub italic: bool,
67 /// Whether the text should be underlined.
68 pub underline: bool,
69 /// The weight of the text.
70 pub weight: FontWeight,
71}
72
73/// A parsed region in a Markdown document.
74#[derive(Debug, Clone)]
75pub struct ParsedRegion {
76 /// Whether the region is a code block.
77 pub code: bool,
78 /// The link contained in this region, if it has one.
79 pub link: Option<Link>,
80}
81
82/// A Markdown link.
83#[derive(Debug, Clone)]
84pub enum Link {
85 /// A link to a webpage.
86 Web {
87 /// The URL of the webpage.
88 url: String,
89 },
90 /// A link to a path on the filesystem.
91 Path {
92 /// The path to the item.
93 path: PathBuf,
94 },
95}
96
97impl Link {
98 fn identify(text: String) -> Option<Link> {
99 if text.starts_with("http") {
100 return Some(Link::Web { url: text });
101 }
102
103 let path = PathBuf::from(text);
104 if path.is_absolute() {
105 return Some(Link::Path { path });
106 }
107
108 None
109 }
110}
111
112/// Parses a string of Markdown.
113pub async fn parse_markdown(
114 markdown: &str,
115 language_registry: &Arc<LanguageRegistry>,
116 language: Option<Arc<Language>>,
117) -> ParsedMarkdown {
118 let mut text = String::new();
119 let mut highlights = Vec::new();
120 let mut region_ranges = Vec::new();
121 let mut regions = Vec::new();
122
123 parse_markdown_block(
124 markdown,
125 language_registry,
126 language,
127 &mut text,
128 &mut highlights,
129 &mut region_ranges,
130 &mut regions,
131 )
132 .await;
133
134 ParsedMarkdown {
135 text,
136 highlights,
137 region_ranges,
138 regions,
139 }
140}
141
142/// Parses a Markdown block.
143pub async fn parse_markdown_block(
144 markdown: &str,
145 language_registry: &Arc<LanguageRegistry>,
146 language: Option<Arc<Language>>,
147 text: &mut String,
148 highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
149 region_ranges: &mut Vec<Range<usize>>,
150 regions: &mut Vec<ParsedRegion>,
151) {
152 let mut bold_depth = 0;
153 let mut italic_depth = 0;
154 let mut link_url = None;
155 let mut current_language = None;
156 let mut list_stack = Vec::new();
157
158 for event in Parser::new_ext(markdown, Options::all()) {
159 let prev_len = text.len();
160 match event {
161 Event::Text(t) => {
162 if let Some(language) = ¤t_language {
163 highlight_code(text, highlights, t.as_ref(), language);
164 } else {
165 text.push_str(t.as_ref());
166
167 let mut style = MarkdownHighlightStyle::default();
168
169 if bold_depth > 0 {
170 style.weight = FontWeight::BOLD;
171 }
172
173 if italic_depth > 0 {
174 style.italic = true;
175 }
176
177 if let Some(link) = link_url.clone().and_then(|u| Link::identify(u)) {
178 region_ranges.push(prev_len..text.len());
179 regions.push(ParsedRegion {
180 code: false,
181 link: Some(link),
182 });
183 style.underline = true;
184 }
185
186 if style != MarkdownHighlightStyle::default() {
187 let mut new_highlight = true;
188 if let Some((last_range, MarkdownHighlight::Style(last_style))) =
189 highlights.last_mut()
190 {
191 if last_range.end == prev_len && last_style == &style {
192 last_range.end = text.len();
193 new_highlight = false;
194 }
195 }
196 if new_highlight {
197 let range = prev_len..text.len();
198 highlights.push((range, MarkdownHighlight::Style(style)));
199 }
200 }
201 }
202 }
203
204 Event::Code(t) => {
205 text.push_str(t.as_ref());
206 region_ranges.push(prev_len..text.len());
207
208 let link = link_url.clone().and_then(|u| Link::identify(u));
209 if link.is_some() {
210 highlights.push((
211 prev_len..text.len(),
212 MarkdownHighlight::Style(MarkdownHighlightStyle {
213 underline: true,
214 ..Default::default()
215 }),
216 ));
217 }
218 regions.push(ParsedRegion { code: true, link });
219 }
220
221 Event::Start(tag) => match tag {
222 Tag::Paragraph => new_paragraph(text, &mut list_stack),
223
224 Tag::Heading(_, _, _) => {
225 new_paragraph(text, &mut list_stack);
226 bold_depth += 1;
227 }
228
229 Tag::CodeBlock(kind) => {
230 new_paragraph(text, &mut list_stack);
231 current_language = if let CodeBlockKind::Fenced(language) = kind {
232 language_registry
233 .language_for_name(language.as_ref())
234 .await
235 .ok()
236 } else {
237 language.clone()
238 }
239 }
240
241 Tag::Emphasis => italic_depth += 1,
242
243 Tag::Strong => bold_depth += 1,
244
245 Tag::Link(_, url, _) => link_url = Some(url.to_string()),
246
247 Tag::List(number) => {
248 list_stack.push((number, false));
249 }
250
251 Tag::Item => {
252 let len = list_stack.len();
253 if let Some((list_number, has_content)) = list_stack.last_mut() {
254 *has_content = false;
255 if !text.is_empty() && !text.ends_with('\n') {
256 text.push('\n');
257 }
258 for _ in 0..len - 1 {
259 text.push_str(" ");
260 }
261 if let Some(number) = list_number {
262 text.push_str(&format!("{}. ", number));
263 *number += 1;
264 *has_content = false;
265 } else {
266 text.push_str("- ");
267 }
268 }
269 }
270
271 _ => {}
272 },
273
274 Event::End(tag) => match tag {
275 Tag::Heading(_, _, _) => bold_depth -= 1,
276 Tag::CodeBlock(_) => current_language = None,
277 Tag::Emphasis => italic_depth -= 1,
278 Tag::Strong => bold_depth -= 1,
279 Tag::Link(_, _, _) => link_url = None,
280 Tag::List(_) => drop(list_stack.pop()),
281 _ => {}
282 },
283
284 Event::HardBreak => text.push('\n'),
285
286 Event::SoftBreak => text.push(' '),
287
288 _ => {}
289 }
290 }
291}
292
293/// Appends a highlighted run of text to the provided `text` buffer.
294pub fn highlight_code(
295 text: &mut String,
296 highlights: &mut Vec<(Range<usize>, MarkdownHighlight)>,
297 content: &str,
298 language: &Arc<Language>,
299) {
300 let prev_len = text.len();
301 text.push_str(content);
302 for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
303 let highlight = MarkdownHighlight::Code(highlight_id);
304 highlights.push((prev_len + range.start..prev_len + range.end, highlight));
305 }
306}
307
308/// Appends a new paragraph to the provided `text` buffer.
309pub fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
310 let mut is_subsequent_paragraph_of_list = false;
311 if let Some((_, has_content)) = list_stack.last_mut() {
312 if *has_content {
313 is_subsequent_paragraph_of_list = true;
314 } else {
315 *has_content = true;
316 return;
317 }
318 }
319
320 if !text.is_empty() {
321 if !text.ends_with('\n') {
322 text.push('\n');
323 }
324 text.push('\n');
325 }
326 for _ in 0..list_stack.len().saturating_sub(1) {
327 text.push_str(" ");
328 }
329 if is_subsequent_paragraph_of_list {
330 text.push_str(" ");
331 }
332}