繁体   English   中英

将降价图像显示为链接

[英]Display markdown images as links

我正在使用Michel Fortin的PHP Markdown进行Markdown转换,但我想将图像显示为链接而不是内联。 因为任何人都可以从Imgur插入5MB jpg图像,从而降低页面速度。

如何将图像更改为链接到图像?

覆盖示例如下所示:

class CustomMarkdown extends Markdown
{
    function _doImages_reference_callback($matches) {
        $whole_match = $matches[1];
        $alt_text    = $matches[2];
        $link_id     = strtolower($matches[3]);

        if ($link_id == "") {
            $link_id = strtolower($alt_text); # for shortcut links like ![this][].
        }

        $alt_text = $this->encodeAttribute($alt_text);
        if (isset($this->urls[$link_id])) {
            $url = $this->encodeAttribute($this->urls[$link_id]);
            $result = "<a href=\"$url\">$alt_text</a>";
        } else {
            # If there's no such link ID, leave intact:
            $result = $whole_match;
        }

        return $result;
    }

    function _doImages_inline_callback($matches) {
        $whole_match    = $matches[1];
        $alt_text       = $matches[2];
        $url            = $matches[3] == '' ? $matches[4] : $matches[3];
        $title          =& $matches[7];

        $alt_text = $this->encodeAttribute($alt_text);
        $url = $this->encodeAttribute($url);
        return "<a href=\"$url\">$alt_text</a>";
    }
}

演示: http//codepad.viper-7.com/VVa2hP

您应该看看Parsedown 我认为,这是最新的,并且更容易扩展Markdown的实施。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM