blog/content/zh-cn/posts/ypecho文章内的超链接在新窗口打开.md

29 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "让Typecho文章内的超链接在新窗口打开"
slug: "Typecho-target_blank"
date: 2017-05-27T12:13:00.000Z
categories:
- 分享
tags:
- typecho
---
Markdown支持两种形式的链接语法行内式和参考式两种形式。
而我们打开所生产的超链接,默认是在本窗口打开的,为了有更好的阅读体验,我们往往希望在新窗口。
要想让Typecho的文章中链接加上`“_blank”`也有很多种方法比如通过jQuery在网页搜索`<a>`标签,为其添加新窗口属性。
下面这种方式是直接修改Typecho程序源码来实现
在`\var\CommonMark\HtmlRenderer.php` 搜索
```
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')));
```
在return前加上下面这段代码
```
$attrs['target'] = '_blank'; // 给链接增加_blank属性
```