Use url for markdown links

This commit is contained in:
Héctor Ramón Jiménez 2024-07-21 18:16:32 +02:00
parent 5443e4d828
commit f830454ffa
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 24 additions and 9 deletions

View file

@ -172,6 +172,7 @@ tiny-skia = "0.11"
tokio = "1.0" tokio = "1.0"
tracing = "0.1" tracing = "0.1"
unicode-segmentation = "1.0" unicode-segmentation = "1.0"
url = "2.5"
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"
wasm-timer = "0.2" wasm-timer = "0.2"
web-sys = "=0.3.67" web-sys = "=0.3.67"

View file

@ -16,7 +16,7 @@ struct Markdown {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Message { enum Message {
Edit(text_editor::Action), Edit(text_editor::Action),
LinkClicked(String), LinkClicked(markdown::Url),
} }
impl Markdown { impl Markdown {
@ -52,7 +52,7 @@ impl Markdown {
} }
} }
Message::LinkClicked(link) => { Message::LinkClicked(link) => {
let _ = open::that_in_background(link); let _ = open::that_in_background(link.to_string());
} }
} }
} }

View file

@ -24,7 +24,7 @@ svg = ["iced_renderer/svg"]
canvas = ["iced_renderer/geometry"] canvas = ["iced_renderer/geometry"]
qr_code = ["canvas", "dep:qrcode"] qr_code = ["canvas", "dep:qrcode"]
wgpu = ["iced_renderer/wgpu"] wgpu = ["iced_renderer/wgpu"]
markdown = ["dep:pulldown-cmark"] markdown = ["dep:pulldown-cmark", "dep:url"]
highlighter = ["dep:iced_highlighter"] highlighter = ["dep:iced_highlighter"]
advanced = [] advanced = []
@ -49,3 +49,6 @@ pulldown-cmark.optional = true
iced_highlighter.workspace = true iced_highlighter.workspace = true
iced_highlighter.optional = true iced_highlighter.optional = true
url.workspace = true
url.optional = true

View file

@ -10,17 +10,19 @@ use crate::core::theme::{self, Theme};
use crate::core::{self, Element, Length}; use crate::core::{self, Element, Length};
use crate::{column, container, rich_text, row, span, text}; use crate::{column, container, rich_text, row, span, text};
pub use url::Url;
/// A Markdown item. /// A Markdown item.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Item { pub enum Item {
/// A heading. /// A heading.
Heading(Vec<text::Span<'static, String>>), Heading(Vec<text::Span<'static, Url>>),
/// A paragraph. /// A paragraph.
Paragraph(Vec<text::Span<'static, String>>), Paragraph(Vec<text::Span<'static, Url>>),
/// A code block. /// A code block.
/// ///
/// You can enable the `highlighter` feature for syntax highligting. /// You can enable the `highlighter` feature for syntax highligting.
CodeBlock(Vec<text::Span<'static, String>>), CodeBlock(Vec<text::Span<'static, Url>>),
/// A list. /// A list.
List { List {
/// The first number of the list, if it is ordered. /// The first number of the list, if it is ordered.
@ -96,7 +98,16 @@ pub fn parse(
pulldown_cmark::Tag::Link { dest_url, .. } pulldown_cmark::Tag::Link { dest_url, .. }
if !metadata && !table => if !metadata && !table =>
{ {
link = Some(dest_url); match Url::parse(&dest_url) {
Ok(url)
if url.scheme() == "http"
|| url.scheme() == "https" =>
{
link = Some(url);
}
_ => {}
}
None None
} }
pulldown_cmark::Tag::List(first_item) if !metadata && !table => { pulldown_cmark::Tag::List(first_item) if !metadata && !table => {
@ -248,7 +259,7 @@ pub fn parse(
}; };
let span = if let Some(link) = link.as_ref() { let span = if let Some(link) = link.as_ref() {
span.color(palette.primary).link(link.to_string()) span.color(palette.primary).link(link.clone())
} else { } else {
span span
}; };
@ -278,7 +289,7 @@ pub fn parse(
/// You can obtain the items with [`parse`]. /// You can obtain the items with [`parse`].
pub fn view<'a, Message, Renderer>( pub fn view<'a, Message, Renderer>(
items: impl IntoIterator<Item = &'a Item>, items: impl IntoIterator<Item = &'a Item>,
on_link: impl Fn(String) -> Message + Copy + 'a, on_link: impl Fn(Url) -> Message + Copy + 'a,
) -> Element<'a, Message, Theme, Renderer> ) -> Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,