hexo链接持久化终极解决之道

来源:互联网 发布:php 数字转二进制 编辑:程序博客网 时间:2024/06/05 12:40

背景

大家知道hexo默认的链接是http://xxx.yy.com/2013/07/14/hello-world这种类型的,这源于站点目录下的配置_config.yml里的配置:permalink: :year/:month/:day/:title/.这种默认配置的缺点就是一般文件名是中文,导致url链接里有中文出现,这会造成很多问题,如使用gitment,也不利于seo。另外就是年月日都会有分隔符。本文介绍经过作者研究总结出的最终解决方案。

过时解决方案

在md文件的Front-matter区域,增加一个urlname属性,用来放文章的英文名字。如下图所示:

title: hexo链接持久化终极解决之道urlname: hexo_permalinkdate: 2017-08-31 23:53:24comments: truecategories:- 综合tags:- hexo

然后修改
_config.yml里的配置:

permalink: posts/:category/:year:month:day-:urlname.htmlpermalink_defaults:  urlname: index

后缀增加.html给人以静态页面感觉,利于seo.这里面:urlname表示引用的这个变量。前面是分类/年月日。另外增加一个urlname这个属性的默认值,当md文档里不填这个属性,就用这个值。这里设的是index,也就是说如果不填这个属性,其链接为:http://xxx.com/posts/uncategorized/20170828-index.html。当然最好写上这个属性,偶尔某天不写这个属性也没关系。

另外还要解决一个问题,就是:category分类有可能为中文。为此在站点下配置文件里设置:

default_category: uncategorizedcategory_map:  综合: common

这样在Front-matter里,分类该写中文写中文,最终的链接会翻译成英文。

终极方案

无意中看到http://muyunyun.cn/posts/f55182c5/#more,最终决定采用这种方案使用hexo-abbrlink这个插件,猜测是根据时间点算出的最终链接,后期不管怎么修改永久链接都不会变.一来自己不用再增加什么属性了,也不用考虑分类中文化的问题。二来后面的层级更短,更利于SEO。(一般SEO只爬三层)

npm install hexo-abbrlink --save

站点配置文件里:

permalink: post/:abbrlink.htmlabbrlink:  alg: crc32  # 算法:crc16(default) and crc32  rep: hex    # 进制:dec(default) and hex

另外可以修改scaffolds里的模版文件,修改post.md为:

---title: {{ title }}date: {{ date }}comments: truecategories:tags:---

不建议的写法

  1. 有的人采用了md文件的名字是英文,在Front-matter里将title写成正常的中文呢,这个将来文章多了,都是英文不利于收藏整理。

参考链接

  1. https://hexo.io/zh-cn/docs/permalinks.html
  2. https://yq.aliyun.com/articles/8607
  3. http://muyunyun.cn/posts/f55182c5/#more
  4. http://blog.csdn.net/mrDiordna/article/details/66472673