13th June 2021

Ampersands in Markdown URLs

In this blog I reference web-page- or image-URLs, which contain ampersands ("&"). For example, https://www.amazon.com/s?k=Richard+Stevens&ref=nb_sb_noss. Unfortunately, Markdown as specified by John Gruber does not allow this. This can be checked with John Gruber's "dingus" web-form.

So official Markdown when confronted with

abc [uvw](../iop&a) xyz

using "dingus" results in

<p>abc <a href="../iop&amp;a">uvw</a> xyz</p>

I consider this to be an error. On 12-May-2021 I wrote an e-mail to John Gruber, but didn't receive any reply up to today. So apparently he won't fix it.

So to use ampersands, I had to add a special rule to the Saaze extension MathParser to not destroy ampersands. The logic is as follows:

private function amplink($html) {
    $begintag = array(" href=\"http", " src=\"http");
    $i = 0;
    foreach($begintag as $tag) {
        $last = 0;
        for(;;) {
            $start = strpos($html,$tag,$last);
            if ($start === false) break;
            $last = $start + 10;
            $end = strpos($html,"\"",$last);
            if ($end === false) break;
            $link = substr($html,$start,$end-$start);
            $link = str_replace("&amp;","&",$link);
            $html = substr_replace($html, $link, $start, $end-$start);
            ++$i;
        }
    }
    //printf("\t\tamplink() changed %d times\n",$i);
    return $html;
}

The PHP program has to handle href= and src= cases.