2013-02-14 69 views
0

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

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

+0

我试过正则表达式,很好的旧str_ireplace和substr。但图像可以替代文字和标题,或只有标题等。不只有一个条件。 str_ireplace也在代码块内改变了img文本。我也很惭愧地承认,但我甚至试图用XML工具更改结果HTML DOM,但我放弃了,因为它不是一个可行的选择。 – cnkt 2013-02-14 19:43:43

+0

转换之后,您不需要尝试做你的东西,而是考虑查看降价来源并搜索idunno一种名为['doImages()']的方法(https://github.com/michelf/php-markdown/blob /lib/Michelf/Markdown.php#L646)? – PeeHaa 2013-02-14 19:46:57

+0

我很抱歉。我不知道我是如何忘记简单的OO原则:)我认为我可以重写_doImages_reference_callback函数,并实现我想要的:)谢谢:) – cnkt 2013-02-14 19:59:41

回答

1

一个覆盖的例子看起来有点像follwing:

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

0

你应该看看Parsedown。这是一个更近的时期,我相信,更容易扩展Markdown的实施。