Add alt and title to markdown images
This commit is contained in:
parent
a6e64eac6f
commit
387abafa3a
2 changed files with 31 additions and 17 deletions
|
|
@ -195,7 +195,7 @@ impl Markdown {
|
||||||
let preview = markdown::view_with(
|
let preview = markdown::view_with(
|
||||||
self.content.items(),
|
self.content.items(),
|
||||||
&self.theme,
|
&self.theme,
|
||||||
&MarkdownViewer {
|
&CustomViewer {
|
||||||
images: &self.images,
|
images: &self.images,
|
||||||
now: self.now,
|
now: self.now,
|
||||||
},
|
},
|
||||||
|
|
@ -251,12 +251,12 @@ impl Markdown {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MarkdownViewer<'a> {
|
struct CustomViewer<'a> {
|
||||||
images: &'a HashMap<markdown::Url, Image>,
|
images: &'a HashMap<markdown::Url, Image>,
|
||||||
now: Instant,
|
now: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> markdown::Viewer<'a, Message> for MarkdownViewer<'a> {
|
impl<'a> markdown::Viewer<'a, Message> for CustomViewer<'a> {
|
||||||
fn on_link_clicked(url: markdown::Url) -> Message {
|
fn on_link_clicked(url: markdown::Url) -> Message {
|
||||||
Message::LinkClicked(url)
|
Message::LinkClicked(url)
|
||||||
}
|
}
|
||||||
|
|
@ -264,8 +264,9 @@ impl<'a> markdown::Viewer<'a, Message> for MarkdownViewer<'a> {
|
||||||
fn image(
|
fn image(
|
||||||
&self,
|
&self,
|
||||||
_settings: markdown::Settings,
|
_settings: markdown::Settings,
|
||||||
_title: &markdown::Text,
|
|
||||||
url: &'a markdown::Url,
|
url: &'a markdown::Url,
|
||||||
|
_title: &'a str,
|
||||||
|
_alt: &markdown::Text,
|
||||||
) -> Element<'a, Message> {
|
) -> Element<'a, Message> {
|
||||||
if let Some(Image::Ready { handle, fade_in }) = self.images.get(url) {
|
if let Some(Image::Ready { handle, fade_in }) = self.images.get(url) {
|
||||||
center_x(
|
center_x(
|
||||||
|
|
|
||||||
|
|
@ -195,7 +195,9 @@ pub enum Item {
|
||||||
/// The destination URL of the image.
|
/// The destination URL of the image.
|
||||||
url: Url,
|
url: Url,
|
||||||
/// The title of the image.
|
/// The title of the image.
|
||||||
title: Text,
|
title: String,
|
||||||
|
/// The alternative text of the image.
|
||||||
|
alt: Text,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -374,7 +376,7 @@ impl Highlighter {
|
||||||
parser: iced_highlighter::Stream::new(
|
parser: iced_highlighter::Stream::new(
|
||||||
&iced_highlighter::Settings {
|
&iced_highlighter::Settings {
|
||||||
theme: iced_highlighter::Theme::Base16Ocean,
|
theme: iced_highlighter::Theme::Base16Ocean,
|
||||||
token: language.to_string(),
|
token: language.to_owned(),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
language: language.to_owned(),
|
language: language.to_owned(),
|
||||||
|
|
@ -488,7 +490,7 @@ fn parse_with<'a>(
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
let _ = RefCell::borrow_mut(&broken_links)
|
let _ = RefCell::borrow_mut(&broken_links)
|
||||||
.insert(broken_link.reference.to_string());
|
.insert(broken_link.reference.into_string());
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
@ -559,10 +561,12 @@ fn parse_with<'a>(
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
pulldown_cmark::Tag::Image { dest_url, .. }
|
pulldown_cmark::Tag::Image {
|
||||||
if !metadata && !table =>
|
dest_url, title, ..
|
||||||
{
|
} if !metadata && !table => {
|
||||||
image = Url::parse(&dest_url).ok();
|
image = Url::parse(&dest_url)
|
||||||
|
.ok()
|
||||||
|
.map(|url| (url, title.into_string()));
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
pulldown_cmark::Tag::List(first_item) if !metadata && !table => {
|
pulldown_cmark::Tag::List(first_item) if !metadata && !table => {
|
||||||
|
|
@ -700,13 +704,18 @@ fn parse_with<'a>(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
pulldown_cmark::TagEnd::Image if !metadata && !table => {
|
pulldown_cmark::TagEnd::Image if !metadata && !table => {
|
||||||
let url = image.take()?;
|
let (url, title) = image.take()?;
|
||||||
let title = Text::new(spans.drain(..).collect());
|
let alt = Text::new(spans.drain(..).collect());
|
||||||
|
|
||||||
let state = state.borrow_mut();
|
let state = state.borrow_mut();
|
||||||
let _ = state.images.insert(url.clone());
|
let _ = state.images.insert(url.clone());
|
||||||
|
|
||||||
produce(state, &mut stack, Item::Image { url, title }, source)
|
produce(
|
||||||
|
state,
|
||||||
|
&mut stack,
|
||||||
|
Item::Image { url, title, alt },
|
||||||
|
source,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
pulldown_cmark::TagEnd::CodeBlock if !metadata && !table => {
|
pulldown_cmark::TagEnd::CodeBlock if !metadata && !table => {
|
||||||
#[cfg(feature = "highlighter")]
|
#[cfg(feature = "highlighter")]
|
||||||
|
|
@ -1001,7 +1010,9 @@ where
|
||||||
Renderer: core::text::Renderer<Font = Font> + 'a,
|
Renderer: core::text::Renderer<Font = Font> + 'a,
|
||||||
{
|
{
|
||||||
match item {
|
match item {
|
||||||
Item::Image { title, url } => viewer.image(settings, title, url),
|
Item::Image { url, title, alt } => {
|
||||||
|
viewer.image(settings, url, title, alt)
|
||||||
|
}
|
||||||
Item::Heading(level, text) => {
|
Item::Heading(level, text) => {
|
||||||
viewer.heading(settings, level, text, index)
|
viewer.heading(settings, level, text, index)
|
||||||
}
|
}
|
||||||
|
|
@ -1194,13 +1205,15 @@ where
|
||||||
fn image(
|
fn image(
|
||||||
&self,
|
&self,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
title: &Text,
|
|
||||||
url: &'a Url,
|
url: &'a Url,
|
||||||
|
title: &'a str,
|
||||||
|
alt: &Text,
|
||||||
) -> Element<'a, Message, Theme, Renderer> {
|
) -> Element<'a, Message, Theme, Renderer> {
|
||||||
let _url = url;
|
let _url = url;
|
||||||
|
let _title = title;
|
||||||
|
|
||||||
container(
|
container(
|
||||||
rich_text(title.spans(settings.style))
|
rich_text(alt.spans(settings.style))
|
||||||
.on_link_clicked(Self::on_link_clicked),
|
.on_link_clicked(Self::on_link_clicked),
|
||||||
)
|
)
|
||||||
.padding(settings.spacing.0)
|
.padding(settings.spacing.0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue