Typecho 1.0 Markdown文章链接从新窗口中打开(target="_blank")

Typecho很好用,但是原生Markdown有一个缺陷,超级链接默认在当前页跳转。如果在文章中添加了参考链接,阅读起来就很不方便。

在Typecho 1.0版本中,要给文章的超级链接添加 target="_blank" 属性其实也是相当简单的。

在 \var\CommonMark\HtmlRenderer.php 的104行,有以下一段代码:

        case CommonMark_Element_InlineElement::TYPE_LINK:
        $attrs['href'] = $this->escape($inline->getAttribute('destination'), true);
        if ($title = $inline->getAttribute('title')) {
            $attrs['title'] = $this->escape($title, true);
        }

        return $this->inTags('a', $attrs, $this->renderInlines($inline->getAttribute('label')));

这就是处理超级链接的部分,我们只要添加一行$attrs['target'] = '_blank';即可:

        case CommonMark_Element_InlineElement::TYPE_LINK:
        $attrs['href'] = $this->escape($inline->getAttribute('destination'), true);
        if ($title = $inline->getAttribute('title')) {
            $attrs['title'] = $this->escape($title, true);
        }
$attrs['target'] = '_blank'; #添加这一行代码
        return $this->inTags('a', $attrs, $this->renderInlines($inline->getAttribute('label')));

当然,如果你想加上rel="nofollow"也依葫芦画瓢即可

        case CommonMark_Element_InlineElement::TYPE_LINK:
        $attrs['href'] = $this->escape($inline->getAttribute('destination'), true);
        if ($title = $inline->getAttribute('title')) {
            $attrs['title'] = $this->escape($title, true);
        }
$attrs['target'] = '_blank'; #在新窗口打开属性
$attrs['rel'] = 'nofollow';  #添加nofollow属性
        return $this->inTags('a', $attrs, $this->renderInlines($inline->getAttribute('label')));
发表新评论