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

29 lines
1.1 KiB
Markdown
Raw Normal View History

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