Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for images in Markdown previews #16192

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a008b62
add markdown image preview
dovakin0007 Aug 14, 2024
e89f63f
fmt fix
dovakin0007 Aug 17, 2024
5e1befb
fix image not rendering side by side
dovakin0007 Aug 23, 2024
94b4efe
Merge branch 'zed-industries:main' into main
dovakin0007 Aug 29, 2024
425cd03
use Image Source to check if image is valid
dovakin0007 Aug 29, 2024
e2e71bb
test fixes
dovakin0007 Aug 31, 2024
3686ce7
fix image not rendering in table
dovakin0007 Sep 1, 2024
e1122d6
fix image not rendering together with text
dovakin0007 Sep 7, 2024
1097dab
fix
dovakin0007 Sep 7, 2024
ba105e1
fix
dovakin0007 Sep 7, 2024
4bc4741
fix local images not rendering
dovakin0007 Sep 8, 2024
067a906
init
dovakin0007 Sep 8, 2024
302b917
fix
dovakin0007 Sep 10, 2024
100b09d
Merge branch 'zed-industries:main' into main
dovakin0007 Sep 16, 2024
43697e2
removed unwanted code
dovakin0007 Sep 17, 2024
4f0ad6a
Merge branch 'zed-industries:main' into main
dovakin0007 Sep 17, 2024
128c555
fix
dovakin0007 Sep 17, 2024
2963ae1
Merge branch 'zed-industries:main' into main
dovakin0007 Sep 17, 2024
005956f
fmt fix
dovakin0007 Sep 17, 2024
cb16b35
Update markdown_renderer.rs
dovakin0007 Oct 1, 2024
33c7b89
Update markdown_renderer.rs
dovakin0007 Oct 1, 2024
c9401fc
renamed x
dovakin0007 Oct 1, 2024
27b46a2
fix
dovakin0007 Oct 1, 2024
65189ce
merge conflict fix
dovakin0007 Oct 3, 2024
ec36f15
Merge branch 'main' into main
dovakin0007 Oct 15, 2024
e500677
Update markdown_renderer.rs
dovakin0007 Oct 15, 2024
e149b39
nameing changes
dovakin0007 Oct 15, 2024
e45171f
Merge branch 'main' into main
dovakin0007 Oct 28, 2024
2c1eeee
test fix
dovakin0007 Oct 28, 2024
1de2b6e
Merge branch 'main' into main
dovakin0007 Nov 18, 2024
5a91d67
wip image fallback
dovakin0007 Nov 19, 2024
d985a6a
added fallback text
dovakin0007 Nov 21, 2024
4b0fceb
fmt fix
dovakin0007 Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 135 additions & 6 deletions crates/markdown_preview/src/markdown_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum ParsedMarkdownElement {
BlockQuote(ParsedMarkdownBlockQuote),
CodeBlock(ParsedMarkdownCodeBlock),
/// A paragraph of text and other inline elements.
Paragraph(ParsedMarkdownText),
Paragraph(Vec<MarkdownParagraph>),
HorizontalRule(Range<usize>),
}

Expand All @@ -25,7 +25,13 @@ impl ParsedMarkdownElement {
Self::Table(table) => table.source_range.clone(),
Self::BlockQuote(block_quote) => block_quote.source_range.clone(),
Self::CodeBlock(code_block) => code_block.source_range.clone(),
Self::Paragraph(text) => text.source_range.clone(),
Self::Paragraph(text) => match &text[0] {
MarkdownParagraph::MarkdownText(t) => t.source_range.clone(),
MarkdownParagraph::MarkdownImage(image) => match image {
Image::Web { source_range, .. } => source_range.clone(),
Image::Path { source_range, .. } => source_range.clone(),
},
},
Self::HorizontalRule(range) => range.clone(),
}
}
Expand All @@ -35,6 +41,13 @@ impl ParsedMarkdownElement {
}
}

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub enum MarkdownParagraph {
MarkdownText(ParsedMarkdownText),
MarkdownImage(Image),
}

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ParsedMarkdown {
Expand Down Expand Up @@ -73,7 +86,7 @@ pub struct ParsedMarkdownCodeBlock {
pub struct ParsedMarkdownHeading {
pub source_range: Range<usize>,
pub level: HeadingLevel,
pub contents: ParsedMarkdownText,
pub contents: Vec<MarkdownParagraph>,
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -107,7 +120,7 @@ pub enum ParsedMarkdownTableAlignment {
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct ParsedMarkdownTableRow {
pub children: Vec<ParsedMarkdownText>,
pub children: Vec<Vec<MarkdownParagraph>>,
dovakin0007 marked this conversation as resolved.
Show resolved Hide resolved
}

impl Default for ParsedMarkdownTableRow {
Expand All @@ -123,7 +136,7 @@ impl ParsedMarkdownTableRow {
}
}

pub fn with_children(children: Vec<ParsedMarkdownText>) -> Self {
pub fn with_children(children: Vec<Vec<MarkdownParagraph>>) -> Self {
Self { children }
}
}
Expand All @@ -135,7 +148,7 @@ pub struct ParsedMarkdownBlockQuote {
pub children: Vec<ParsedMarkdownElement>,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ParsedMarkdownText {
/// Where the text is located in the source Markdown document.
pub source_range: Range<usize>,
Expand Down Expand Up @@ -273,3 +286,119 @@ impl Display for Link {
}
}
}

/// A Markdown Image
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Image {
Web {
source_range: Range<usize>,
/// The URL of the Image.
url: String,
/// Link URL if exists.
link: Option<Link>,
/// alt text if it exists
alt_text: Option<ParsedMarkdownText>,
},
/// Image path on the filesystem.
Path {
source_range: Range<usize>,
/// The path as provided in the Markdown document.
display_path: PathBuf,
/// The absolute path to the item.
path: PathBuf,
/// Link URL if exists.
link: Option<Link>,
/// alt text if it exists
alt_text: Option<ParsedMarkdownText>,
},
}

impl Image {
pub fn identify(
source_range: Range<usize>,
file_location_directory: Option<PathBuf>,
text: String,
link: Option<Link>,
) -> Option<Image> {
if text.starts_with("http") {
return Some(Image::Web {
source_range,
url: text,
link,
alt_text: None,
});
}
let path = PathBuf::from(&text);
if path.is_absolute() {
return Some(Image::Path {
source_range,
display_path: path.clone(),
path,
link,
alt_text: None,
});
}
if let Some(file_location_directory) = file_location_directory {
let display_path = path;
let path = file_location_directory.join(text);
return Some(Image::Path {
source_range,
display_path,
path,
link,
alt_text: None,
});
}
None
}

pub fn with_alt_text(&self, alt_text: ParsedMarkdownText) -> Self {
match self {
Image::Web {
ref source_range,
ref url,
ref link,
..
} => Image::Web {
source_range: source_range.clone(),
url: url.clone(),
link: link.clone(),
alt_text: Some(alt_text),
},
Image::Path {
ref source_range,
ref display_path,
ref path,
ref link,
..
} => Image::Path {
source_range: source_range.clone(),
display_path: display_path.clone(),
path: path.clone(),
link: link.clone(),
alt_text: Some(alt_text),
},
}
}
}

impl Display for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Image::Web {
source_range: _,
url,
link: _,
alt_text: _,
} => write!(f, "{}", url),
Image::Path {
source_range: _,
display_path,
path: _,
link: _,
alt_text: _,
} => write!(f, "{}", display_path.display()),
}
}
}
Loading
Loading