From ac94bf3bbfd59ca5f5069c3421b6e6f77af56cff Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Wed, 14 Feb 2018 02:12:30 -0800 Subject: [PATCH] Markdown.pl: _PrefixURL more intelligently Instead of turning an empty URL into an href="" attribute that effectively does nothing, change it into an href="#" attribute that creates a link to the current page. When adding a relative/image prefix leave fragment-only links unmolested. They are meant to link somewhere on the current page and must not be changed. When inspecting the destination to determine whether to use the -i prefix instead of the -r prefix when both are given, ignore any trailing fragment. Fragments don't really make sense on image links and should never actually be sent to the server anyway by a behaving client, but match them properly in any case. Also make sure that URLs only get a prefix added at most once. Signed-off-by: Kyle J. McKay --- Markdown.pl | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index 33070e5..9ffc3fd 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -111,6 +111,7 @@ BEGIN { # Table of hash values for escaped characters: my %g_escape_table; BEGIN { + $g_escape_table{""} = "\2\3"; foreach my $char (split //, "\\\`*_~{}[]()>#+-.!|:") { $g_escape_table{$char} = block_id($char,1); } @@ -2238,16 +2239,20 @@ sub _PrefixURL { # Add URL prefix if needed # my $url = shift; + $url =~ s/^\s+//; + $url =~ s/\s+$//; + $url = "#" unless $url ne ""; return $url unless $opt{url_prefix} ne '' || $opt{img_prefix} ne ''; - return $url if $url =~ m,^//, || $url =~ /^[A-Za-z][A-Za-z0-9+.-]*:/; + return $url if $url =~ m"^\002\003" || $url =~ m"^#" || + $url =~ m,^//, || $url =~ /^[A-Za-z][A-Za-z0-9+.-]*:/; my $ans = $opt{url_prefix}; $ans = $opt{img_prefix} - if $opt{img_prefix} ne '' && $url =~ /\.(?:png|gif|jpe?g|svgz?)$/i; + if $opt{img_prefix} ne '' && $url =~ m"^[^#]*\.(?:png|gif|jpe?g|svgz?)(?:#|$)"i; return $url unless $ans ne ''; $ans .= '/' if substr($ans, -1, 1) ne '/'; $ans .= substr($url, 0, 1) eq '/' ? substr($url, 1) : $url; - return $ans; + return "\2\3".$ans; }