Typecho 文章内链接在新窗口打开实现方式

默认情况Typecho文章中如果有添加链接,那么是从当前窗口跳转的,并且外链没有添加nofollow标签,不利于SEO,Typecho文章内链接新窗口并添加nofollow标签如下。

方法一:修改Typecho系统文件

找到 /var/CommonMark/HtmlRenderer.php 这个文件,在105行,也就是 $attrs['href'] = $this->escape($inline->getAttribute('destination'), true); 代码之后添加如下两行代码:

  1. $attrs['target'] = $this->escape(_blank, true);
  2. $attrs['rel'] = $this->escape(nofollow, true);

最后保存查看效果,修改后的完整代码,请注意对比:

  1. case CommonMark_Element_InlineElement::TYPE_LINK:
  2. $attrs['href'] = $this->escape($inline->getAttribute('destination'), true);
  3. $attrs['target'] = $this->escape(_blank, true);
  4. $attrs['rel'] = $this->escape(nofollow, true);
  5. if ($title = $inline->getAttribute('title')) {
  6. $attrs['title'] = $this->escape($title, true);
  7. }

注:如果你是用的 Typecho 1.0 以上版本请按照下面方法更改:

修改Typecho系统代码

找到 varMarkdown.php 这个文件,这里和上面老版本文件不同。

self::$parser->hook('afterParseCode', array('Markdown', 'transerCodeClass')); 后面,添加下面代码

  1. self::$parser->hook('afterParseInline', array('Markdown', 'addLinkTargetBlank'));

然后在 public static function transerCodeClass($html){} 这个代码块后面,添加下面代码

  1. /**
  2. * addLinkTargetBlank
  3. *
  4. * @param string $html
  5. * @return string
  6. */
  7. public static function addLinkTargetBlank($html)
  8. {
  9. return preg_replace("/<a href=\"([^\"]*)\">/i", "<a href=\"\\1\" target=\"_blank\">", $html);
  10. }

如果需要添加rel=nofollow,则如下

  1. /**
  2. * addLinkTargetBlank
  3. *
  4. * @param string $html
  5. * @return string
  6. */
  7. public static function addLinkTargetBlank($html)
  8. {
  9. return preg_replace("/<a href=\"([^\"]*)\">/i", "<a href=\"\\1\" target=\"_blank\" rel=\"nofollow\">", $html);
  10. }

方法二:前端通过JS跳转

在前端,通过Vanilla JS解决

逻辑是查找某个标签的id下所有的a标签,然后给每个添加属性,如下

  1. var pageAnchors = document.getElementById('post-content').getElementsByTagName('a');
  2. for (var i=0; i<pageAnchors.length; i++){
  3. pageAnchors[i].setAttribute('target', '_blank');
  4. }

这样的是不可靠,在没有某个标签的id的情况下浏览器会报错,一报错,我就不舒服,解决如下

  1. if(document.getElementById('post-content')){
  2. var pageAnchors = document.getElementById('post-content').getElementsByTagName('a');
  3. for (var i=0; i<pageAnchors.length; i++){
  4. pageAnchors[i].setAttribute('target', '_blank');
  5. }
  6. }

套个方法,美化一下,如下

  1. var pageTagretBlank = function(pageId) {
  2. if(document.getElementById(pageId)){
  3. var pageAnchors = document.getElementById(pageId).getElementsByTagName('a');
  4. for (var i=0; i<pageAnchors.length; i++){
  5. pageAnchors[i].setAttribute('target', '_blank');
  6. }
  7. }
  8. };
  9. pageTagretBlank('post-content');

如果我还需要添加rel=nofollow,则如下

  1. var pageTagretBlank = function(pageId) {
  2. if(document.getElementById(pageId)){
  3. var pageAnchors = document.getElementById(pageId).getElementsByTagName('a');
  4. for (var i=0; i<pageAnchors.length; i++){
  5. pageAnchors[i].setAttribute('target', '_blank');
  6. postAnchors[i].setAttribute('rel', 'nofollow');
  7. }
  8. }
  9. };
  10. pageTagretBlank('post-content');

代码一般放在主题的footer.php就可以了

方法三、通过PHP函数实现【本站推荐的做法】

直接在主题里集成文章链接新窗口跳转,在function.php的添加 parseContent() 函数,函数为

  1. function parseContent($obj){
  2. $options = Typecho_Widget::widget('Widget_Options');
  3. if(!empty($options->src_add) && !empty($options->cdn_add)){
  4. $obj->content = str_ireplace($options->src_add,$options->cdn_add,$obj->content);
  5. }
  6. $obj->content = preg_replace("/<a href=\"([^\"]*)\">/i", "<a href=\"\\1\" target=\"_blank\">", $obj->content);
  7. echo trim($obj->content);
  8. }

如果需要添加rel=nofollow,则如下

  1. function parseContent($obj){
  2. $options = Typecho_Widget::widget('Widget_Options');
  3. if(!empty($options->src_add) && !empty($options->cdn_add)){
  4. $obj->content = str_ireplace($options->src_add,$options->cdn_add,$obj->content);
  5. }
  6. $obj->content = preg_replace("/<a href=\"([^\"]*)\">/i", "<a href=\"\\1\" target=\"_blank\" rel=\"nofollow\">", $obj->content);
  7. echo trim($obj->content);
  8. }

该方法的原理就是正则文章的超链接标签,然后加上相应处理即可。使用该方法需要修改主题 post.php 文件,将默认的内容输出<?php $this->content(); ?> 改成 <?php parseContent($this); ?> 。

方法四、通过修改主题Header.php文件

最近看到一种新的方法是通过修改主题header.php文件在顶部加上<base target="_blank"/>即可。

1451677858.png

文章来源:https://www.mr-mao.cn/archives/typecho-link-new-window-open-and-add-nofollow-implementation-code.html

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/225
1 条评论
4.9k

发表评论

仅有一条评论

  1. Nover     Win 10 /    Chrome
    2020-05-14 23:27

    前面的方法没有试,就是最后一个方法试了。很简单很管用。

!