protected function rewriteUrls($text, $pageUrl)
    {
        $relative = $this->baseurl === './';
        if ($relative || $this->uglyurls) {
            // Match restrictively urls starting with prefix, and which are
            // correctly escaped (no whitespace or quotes).
            $find = preg_quote(static::URLPREFIX) . '(\/?[^\?\&<>{}"\'\s]*)';
            $text = preg_replace_callback(
                "!$find!",
                function($found) use ($pageUrl, $relative) {
                    $url = $found[0];
                    if ($this->uglyurls) {
                        $path = $found[1];
                        if (!$path || $path === '/') {
                            $url = rtrim($url, '/') . '/index.html';
                        }
                        elseif (!Str::endsWith($url, '/') && !pathinfo($url, PATHINFO_EXTENSION)) {
                            $url .= $this->extension;
                        }
                    }
                    if ($relative) {
                        $pageUrl .= $this->extension;
                        $pageUrl = str_replace(static::URLPREFIX, '', $pageUrl);
                        $url = str_replace(static::URLPREFIX, '', $url);
                        $url = $this->relativeUrl($pageUrl, $url);
                    }
                    return $url;
                },
                $text
            );
        }
        // Except if we have converted to relative URLs, we still have
        // the placeholder prefix in the text. Swap in the base URL.
        $pattern = '!' . preg_quote(static::URLPREFIX) . '\/?!';
        return preg_replace($pattern, $this->baseurl, $text);
    }