diff --git a/examples/markdown-widget/markdown.md b/examples/markdown-widget/markdown.md index ff07f895..cd2a83f7 100644 --- a/examples/markdown-widget/markdown.md +++ b/examples/markdown-widget/markdown.md @@ -20,6 +20,14 @@ Here are some basic Markdown syntax elements: - **Italic**: Use `*` or `_` for italic text. Example: `*italic text*` - **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 # Heading One diff --git a/src/widget/markdown/parser.rs b/src/widget/markdown/parser.rs index 9762cf92..1d954e31 100644 --- a/src/widget/markdown/parser.rs +++ b/src/widget/markdown/parser.rs @@ -139,8 +139,27 @@ fn parse_span<'a>(span: Span, attrs: Attrs<'a>) -> Vec<(&'a str, Attrs)> { attrs.family(Family::Monospace); result.push((Box::leak(code.into_boxed_str()), attrs)); } - Span::Link(_, _, _) => {} - Span::Image(_, _, _) => {} + Span::Link(name, url, _title) => { + 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) => { for item in emphasis { let attrs = attrs.family(Family::Cursive); @@ -204,6 +223,14 @@ fn highlight_code<'a>( 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 { let ts = cosmic_syntax();