lib: don't prepend mailto to url in autolink Event

better to provide the original url, the event is already tagged as email

also avoids a string allocation
This commit is contained in:
Noah Hellman 2023-03-19 13:32:06 +01:00
parent 62e73100a6
commit 05a4992d99
2 changed files with 8 additions and 4 deletions

View file

@ -25,6 +25,7 @@
use crate::Alignment; use crate::Alignment;
use crate::Container; use crate::Container;
use crate::Event; use crate::Event;
use crate::LinkType;
use crate::ListKind; use crate::ListKind;
use crate::OrderedListNumbering::*; use crate::OrderedListNumbering::*;
use crate::Render; use crate::Render;
@ -161,11 +162,14 @@ impl<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write> Writer<'s, I, W> {
Container::DescriptionTerm => self.out.write_str("<dt")?, Container::DescriptionTerm => self.out.write_str("<dt")?,
Container::CodeBlock { .. } => self.out.write_str("<pre")?, Container::CodeBlock { .. } => self.out.write_str("<pre")?,
Container::Span | Container::Math { .. } => self.out.write_str("<span")?, Container::Span | Container::Math { .. } => self.out.write_str("<span")?,
Container::Link(dst, ..) => { Container::Link(dst, ty) => {
if dst.is_empty() { if dst.is_empty() {
self.out.write_str("<a")?; self.out.write_str("<a")?;
} else { } else {
self.out.write_str(r#"<a href=""#)?; self.out.write_str(r#"<a href=""#)?;
if matches!(ty, LinkType::Email) {
self.out.write_str("mailto:")?;
}
self.write_attr(dst)?; self.write_attr(dst)?;
self.out.write_char('"')?; self.out.write_char('"')?;
} }

View file

@ -746,10 +746,10 @@ impl<'s> Parser<'s> {
} }
inline::Container::Autolink => { inline::Container::Autolink => {
let url = self.inlines.src(inline.span); let url = self.inlines.src(inline.span);
let (url, ty) = if url.contains('@') { let ty = if url.contains('@') {
(format!("mailto:{}", url).into(), LinkType::Email) LinkType::Email
} else { } else {
(url, LinkType::AutoLink) LinkType::AutoLink
}; };
Container::Link(url, ty) Container::Link(url, ty)
} }