diff --git a/mdhtml/src/lib.rs b/mdhtml/src/lib.rs index 503b34f..a7a0368 100644 --- a/mdhtml/src/lib.rs +++ b/mdhtml/src/lib.rs @@ -23,15 +23,14 @@ impl TokenSink for Sink { ) { return TokenSinkResult::Continue } // skip this tag self.0.push('<'); + if !tag.self_closing && matches!(tag.kind, TagKind::EndTag) { self.0.push('/'); } self.0.push_str(tag.name.as_ref()); - if tag.self_closing { - self.0.push('/'); - } else { + if !matches!(tag.kind, TagKind::EndTag) { match tag.name.as_ref() { "img" => for attr in tag.attrs { match attr.name.local.as_ref() { @@ -42,6 +41,7 @@ impl TokenSink for Sink { } }, "a" => { + let any_attr = !tag.attrs.is_empty(); for attr in tag.attrs { match attr.name.local.as_ref() { "href" => self.0.push_str(&format!(" href=\"{}\"", attr.value.as_ref())), @@ -49,12 +49,18 @@ impl TokenSink for Sink { _ => {}, } } - self.0.push_str(" rel=\"nofollow noreferrer\" target=\"_blank\""); + if any_attr { + self.0.push_str(" rel=\"nofollow noreferrer\" target=\"_blank\""); + } }, _ => {}, } } + if tag.self_closing { + self.0.push('/'); + } + self.0.push('>'); }, Token::CharacterTokens(txt) => self.0.push_str(txt.as_ref()),