簡體   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