add links and images to parser

This commit is contained in:
Piotr 2024-06-15 15:50:12 +02:00
parent 45fef2d551
commit 9eb37da669
2 changed files with 37 additions and 2 deletions

View file

@ -20,6 +20,14 @@ Here are some basic Markdown syntax elements:
- **Italic**: Use `*` or `_` for italic text. Example: `*italic text*` - **Italic**: Use `*` or `_` for italic text. Example: `*italic text*`
- **Lists**: Use `-` or `*` for unordered lists and numbers for ordered lists. - **Lists**: Use `-` or `*` for unordered lists and numbers for ordered lists.
### Links
My favorite search engine is [Duck Duck Go](https://duckduckgo.com "The best search engine for privacy") privacy search engine.
### Images
![The San Juan Mountains are beautiful!](/assets/images/san-juan-mountains.jpg "San Juan Mountains")
## Example ## Example
# Heading One # Heading One

View file

@ -139,8 +139,27 @@ fn parse_span<'a>(span: Span, attrs: Attrs<'a>) -> Vec<(&'a str, Attrs)> {
attrs.family(Family::Monospace); attrs.family(Family::Monospace);
result.push((Box::leak(code.into_boxed_str()), attrs)); result.push((Box::leak(code.into_boxed_str()), attrs));
} }
Span::Link(_, _, _) => {} Span::Link(name, url, _title) => {
Span::Image(_, _, _) => {} let spans: &[(&str, Attrs)] = &[
(Box::leak(format!("{}: ", name).into_boxed_str()), attrs),
(Box::leak(url.into_boxed_str()), attrs.color(link_color())),
];
result.extend_from_slice(spans);
}
Span::Image(alt, url, title) => {
let spans: &[(&str, Attrs)] = if let Some(title) = title {
&[
(Box::leak(format!("{}: ", title).into_boxed_str()), attrs),
(Box::leak(url.into_boxed_str()), attrs.color(link_color())),
]
} else {
&[
(Box::leak(format!("{}: ", alt).into_boxed_str()), attrs),
(Box::leak(url.into_boxed_str()), attrs.color(link_color())),
]
};
result.extend_from_slice(spans);
}
Span::Emphasis(emphasis) => { Span::Emphasis(emphasis) => {
for item in emphasis { for item in emphasis {
let attrs = attrs.family(Family::Cursive); let attrs = attrs.family(Family::Cursive);
@ -204,6 +223,14 @@ fn highlight_code<'a>(
result result
} }
fn link_color() -> Color {
if !crate::theme::is_dark() {
return Color::rgb(5, 200, 95);
}
Color::rgb(125, 206, 243)
}
fn syntax_theme() -> Theme { fn syntax_theme() -> Theme {
let ts = cosmic_syntax(); let ts = cosmic_syntax();