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

代码845 次浏览

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')));

相关文章

评论 (1)

wooleeweb

你好,通过学习你的 http://www.ywlib.com/archives/38.html 这篇博客,我解决了如何在新标签中打开超链接的问题,但是为什么我发出来的文章 http://www.wooleefeng.tk/archives/24/ 是这样的显示呢?我想让代码和你一样的显示效果,大佬请问怎么做?

发表评论

提交后需审核,请勿重复提交。