2023-09-14 19:24:46 +00:00
|
|
|
use std::ops::Range;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-09-22 19:10:48 +00:00
|
|
|
use crate::{Language, LanguageRegistry};
|
2023-09-14 19:24:46 +00:00
|
|
|
use futures::FutureExt;
|
2023-09-28 18:16:30 +00:00
|
|
|
use gpui::fonts::{HighlightStyle, Underline, Weight};
|
2023-09-14 19:24:46 +00:00
|
|
|
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
|
|
|
|
|
2023-09-22 19:10:48 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2023-10-03 14:58:08 +00:00
|
|
|
pub struct ParsedMarkdown {
|
2023-09-28 18:16:30 +00:00
|
|
|
pub text: String,
|
|
|
|
pub highlights: Vec<(Range<usize>, HighlightStyle)>,
|
|
|
|
pub region_ranges: Vec<Range<usize>>,
|
2023-10-03 14:58:08 +00:00
|
|
|
pub regions: Vec<ParsedRegion>,
|
2023-09-22 19:10:48 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 19:24:46 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2023-10-03 14:58:08 +00:00
|
|
|
pub struct ParsedRegion {
|
2023-09-15 15:51:57 +00:00
|
|
|
pub code: bool,
|
|
|
|
pub link_url: Option<String>,
|
2023-09-14 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 14:58:08 +00:00
|
|
|
pub fn parse_markdown(
|
2023-09-14 19:24:46 +00:00
|
|
|
markdown: &str,
|
|
|
|
language_registry: &Arc<LanguageRegistry>,
|
2023-10-03 14:58:08 +00:00
|
|
|
language: Option<Arc<Language>>,
|
2023-09-22 19:10:48 +00:00
|
|
|
style: &theme::Editor,
|
2023-10-03 14:58:08 +00:00
|
|
|
) -> ParsedMarkdown {
|
2023-09-14 19:24:46 +00:00
|
|
|
let mut text = String::new();
|
|
|
|
let mut highlights = Vec::new();
|
|
|
|
let mut region_ranges = Vec::new();
|
|
|
|
let mut regions = Vec::new();
|
|
|
|
|
2023-10-03 14:58:08 +00:00
|
|
|
parse_markdown_block(
|
2023-09-15 15:51:57 +00:00
|
|
|
markdown,
|
|
|
|
language_registry,
|
|
|
|
language,
|
|
|
|
style,
|
|
|
|
&mut text,
|
|
|
|
&mut highlights,
|
|
|
|
&mut region_ranges,
|
|
|
|
&mut regions,
|
|
|
|
);
|
|
|
|
|
2023-10-03 14:58:08 +00:00
|
|
|
ParsedMarkdown {
|
2023-09-22 19:10:48 +00:00
|
|
|
text,
|
|
|
|
highlights,
|
|
|
|
region_ranges,
|
|
|
|
regions,
|
|
|
|
}
|
2023-09-15 15:51:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 14:58:08 +00:00
|
|
|
pub fn parse_markdown_block(
|
2023-09-15 15:51:57 +00:00
|
|
|
markdown: &str,
|
|
|
|
language_registry: &Arc<LanguageRegistry>,
|
2023-10-03 14:58:08 +00:00
|
|
|
language: Option<Arc<Language>>,
|
2023-09-22 19:10:48 +00:00
|
|
|
style: &theme::Editor,
|
2023-09-15 15:51:57 +00:00
|
|
|
text: &mut String,
|
|
|
|
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
|
|
|
region_ranges: &mut Vec<Range<usize>>,
|
2023-10-03 14:58:08 +00:00
|
|
|
regions: &mut Vec<ParsedRegion>,
|
2023-09-15 15:51:57 +00:00
|
|
|
) {
|
2023-09-14 19:24:46 +00:00
|
|
|
let mut bold_depth = 0;
|
|
|
|
let mut italic_depth = 0;
|
|
|
|
let mut link_url = None;
|
|
|
|
let mut current_language = None;
|
|
|
|
let mut list_stack = Vec::new();
|
|
|
|
|
|
|
|
for event in Parser::new_ext(&markdown, Options::all()) {
|
|
|
|
let prev_len = text.len();
|
|
|
|
match event {
|
|
|
|
Event::Text(t) => {
|
|
|
|
if let Some(language) = ¤t_language {
|
2023-10-03 14:58:08 +00:00
|
|
|
highlight_code(text, highlights, t.as_ref(), language, style);
|
2023-09-14 19:24:46 +00:00
|
|
|
} else {
|
|
|
|
text.push_str(t.as_ref());
|
|
|
|
|
|
|
|
let mut style = HighlightStyle::default();
|
|
|
|
if bold_depth > 0 {
|
|
|
|
style.weight = Some(Weight::BOLD);
|
|
|
|
}
|
|
|
|
if italic_depth > 0 {
|
|
|
|
style.italic = Some(true);
|
|
|
|
}
|
|
|
|
if let Some(link_url) = link_url.clone() {
|
|
|
|
region_ranges.push(prev_len..text.len());
|
2023-10-03 14:58:08 +00:00
|
|
|
regions.push(ParsedRegion {
|
2023-09-14 19:24:46 +00:00
|
|
|
link_url: Some(link_url),
|
|
|
|
code: false,
|
|
|
|
});
|
|
|
|
style.underline = Some(Underline {
|
|
|
|
thickness: 1.0.into(),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if style != HighlightStyle::default() {
|
|
|
|
let mut new_highlight = true;
|
|
|
|
if let Some((last_range, last_style)) = highlights.last_mut() {
|
|
|
|
if last_range.end == prev_len && last_style == &style {
|
|
|
|
last_range.end = text.len();
|
|
|
|
new_highlight = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if new_highlight {
|
|
|
|
highlights.push((prev_len..text.len(), style));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Event::Code(t) => {
|
|
|
|
text.push_str(t.as_ref());
|
|
|
|
region_ranges.push(prev_len..text.len());
|
|
|
|
if link_url.is_some() {
|
|
|
|
highlights.push((
|
|
|
|
prev_len..text.len(),
|
|
|
|
HighlightStyle {
|
|
|
|
underline: Some(Underline {
|
|
|
|
thickness: 1.0.into(),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
2023-10-03 14:58:08 +00:00
|
|
|
regions.push(ParsedRegion {
|
2023-09-14 19:24:46 +00:00
|
|
|
code: true,
|
|
|
|
link_url: link_url.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Event::Start(tag) => match tag {
|
2023-09-15 15:51:57 +00:00
|
|
|
Tag::Paragraph => new_paragraph(text, &mut list_stack),
|
2023-09-14 19:24:46 +00:00
|
|
|
|
|
|
|
Tag::Heading(_, _, _) => {
|
2023-09-15 15:51:57 +00:00
|
|
|
new_paragraph(text, &mut list_stack);
|
2023-09-14 19:24:46 +00:00
|
|
|
bold_depth += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tag::CodeBlock(kind) => {
|
2023-09-15 15:51:57 +00:00
|
|
|
new_paragraph(text, &mut list_stack);
|
2023-09-14 19:24:46 +00:00
|
|
|
current_language = if let CodeBlockKind::Fenced(language) = kind {
|
|
|
|
language_registry
|
|
|
|
.language_for_name(language.as_ref())
|
|
|
|
.now_or_never()
|
|
|
|
.and_then(Result::ok)
|
|
|
|
} else {
|
|
|
|
language.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Tag::Emphasis => italic_depth += 1,
|
|
|
|
|
|
|
|
Tag::Strong => bold_depth += 1,
|
|
|
|
|
|
|
|
Tag::Link(_, url, _) => link_url = Some(url.to_string()),
|
|
|
|
|
|
|
|
Tag::List(number) => {
|
|
|
|
list_stack.push((number, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
Tag::Item => {
|
|
|
|
let len = list_stack.len();
|
|
|
|
if let Some((list_number, has_content)) = list_stack.last_mut() {
|
|
|
|
*has_content = false;
|
|
|
|
if !text.is_empty() && !text.ends_with('\n') {
|
|
|
|
text.push('\n');
|
|
|
|
}
|
|
|
|
for _ in 0..len - 1 {
|
|
|
|
text.push_str(" ");
|
|
|
|
}
|
|
|
|
if let Some(number) = list_number {
|
|
|
|
text.push_str(&format!("{}. ", number));
|
|
|
|
*number += 1;
|
|
|
|
*has_content = false;
|
|
|
|
} else {
|
|
|
|
text.push_str("- ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
|
|
|
|
Event::End(tag) => match tag {
|
|
|
|
Tag::Heading(_, _, _) => bold_depth -= 1,
|
|
|
|
Tag::CodeBlock(_) => current_language = None,
|
|
|
|
Tag::Emphasis => italic_depth -= 1,
|
|
|
|
Tag::Strong => bold_depth -= 1,
|
|
|
|
Tag::Link(_, _, _) => link_url = None,
|
|
|
|
Tag::List(_) => drop(list_stack.pop()),
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
|
|
|
|
Event::HardBreak => text.push('\n'),
|
|
|
|
|
|
|
|
Event::SoftBreak => text.push(' '),
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 14:58:08 +00:00
|
|
|
pub fn highlight_code(
|
2023-09-14 19:24:46 +00:00
|
|
|
text: &mut String,
|
|
|
|
highlights: &mut Vec<(Range<usize>, HighlightStyle)>,
|
|
|
|
content: &str,
|
|
|
|
language: &Arc<Language>,
|
2023-09-22 19:10:48 +00:00
|
|
|
style: &theme::Editor,
|
2023-09-14 19:24:46 +00:00
|
|
|
) {
|
|
|
|
let prev_len = text.len();
|
|
|
|
text.push_str(content);
|
|
|
|
for (range, highlight_id) in language.highlight_text(&content.into(), 0..content.len()) {
|
|
|
|
if let Some(style) = highlight_id.style(&style.syntax) {
|
|
|
|
highlights.push((prev_len + range.start..prev_len + range.end, style));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 15:51:57 +00:00
|
|
|
pub fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
|
2023-09-14 19:24:46 +00:00
|
|
|
let mut is_subsequent_paragraph_of_list = false;
|
|
|
|
if let Some((_, has_content)) = list_stack.last_mut() {
|
|
|
|
if *has_content {
|
|
|
|
is_subsequent_paragraph_of_list = true;
|
|
|
|
} else {
|
|
|
|
*has_content = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !text.is_empty() {
|
|
|
|
if !text.ends_with('\n') {
|
|
|
|
text.push('\n');
|
|
|
|
}
|
|
|
|
text.push('\n');
|
|
|
|
}
|
|
|
|
for _ in 0..list_stack.len().saturating_sub(1) {
|
|
|
|
text.push_str(" ");
|
|
|
|
}
|
|
|
|
if is_subsequent_paragraph_of_list {
|
|
|
|
text.push_str(" ");
|
|
|
|
}
|
|
|
|
}
|