Browse Source

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 <mackyle@gmail.com>
master
Kyle J. McKay 6 years ago
parent
commit
ac94bf3bbf
  1. 11
      Markdown.pl

11
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;
}

Loading…
Cancel
Save