From 3d00bf57a225369120fd98bee36f65a541260da1 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 5 Sep 2022 15:03:24 +0200 Subject: Fix to implement GFM autolink literals exactly --- src/compiler.rs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src/compiler.rs') diff --git a/src/compiler.rs b/src/compiler.rs index 681ec00..0ea1638 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -871,6 +871,7 @@ fn on_exit_autolink_email(context: &mut CompileContext) { &Position::from_exit_event(context.events, context.index), ) .as_str(), + false, ); } @@ -884,6 +885,7 @@ fn on_exit_autolink_protocol(context: &mut CompileContext) { &Position::from_exit_event(context.events, context.index), ) .as_str(), + false, ); } @@ -1154,6 +1156,7 @@ fn on_exit_gfm_autolink_literal_protocol(context: &mut CompileContext) { &Position::from_exit_event(context.events, context.index), ) .as_str(), + true, ); } @@ -1167,12 +1170,22 @@ fn on_exit_gfm_autolink_literal_www(context: &mut CompileContext) { &Position::from_exit_event(context.events, context.index), ) .as_str(), + true, ); } /// Handle [`Exit`][Kind::Exit]:[`GfmAutolinkLiteralEmail`][Name::GfmAutolinkLiteralEmail]. fn on_exit_gfm_autolink_literal_email(context: &mut CompileContext) { - on_exit_autolink_email(context); + generate_autolink( + context, + Some("mailto:"), + Slice::from_position( + context.bytes, + &Position::from_exit_event(context.events, context.index), + ) + .as_str(), + true, + ); } /// Handle [`Exit`][Kind::Exit]:[`GfmFootnoteCall`][Name::GfmFootnoteCall]. @@ -1822,8 +1835,24 @@ fn generate_footnote_item(context: &mut CompileContext, index: usize) { } /// Generate an autolink (used by unicode autolinks and GFM autolink literals). -fn generate_autolink(context: &mut CompileContext, protocol: Option<&str>, value: &str) { - if !context.image_alt_inside { +fn generate_autolink( + context: &mut CompileContext, + protocol: Option<&str>, + value: &str, + is_gfm_literal: bool, +) { + let mut is_in_link = false; + let mut index = 0; + + while index < context.media_stack.len() { + if !context.media_stack[index].image { + is_in_link = true; + break; + } + index += 1; + } + + if !context.image_alt_inside && (!is_in_link || !is_gfm_literal) { context.push(", value context.push(&encode(value, context.encode_html)); - if !context.image_alt_inside { + if !context.image_alt_inside && (!is_in_link || !is_gfm_literal) { context.push(""); } } -- cgit