博客由Hexo转向Hugo

时间:2022-07-24
本文章向大家介绍博客由Hexo转向Hugo,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

昨天(2019/12/11),感觉自己博客速度实在是太慢了,而主题太复杂则是我觉得速度太慢的主要原因.于是心血来潮,想给博客换一个主题,也找到了比较喜欢的(哎,我真是一个喜新厌旧的人.),无奈是hugo的,那还能怎么办,只好换了,还好二者兼容性很好.下面记下折腾中遇到的一些坑..

hugo中的参数需要[]包裹

例如categories: ["技术"],原先hexo没有这个要求,写了一个py轻松解决

import os 
for md in os.listdir("./"):
	if(md.endswith(".md")):
		with open(md) as f:
			tmp = f.read()
			tmp = tmp.replace('categories: 技术','categories: ["技术"]')
			tmp = tmp.replace('categories: 生活','categories: ["生活"]')
		with open(md,'w') as f1:
			f1.write(tmp)

(当时写的已经删除了,这个随便写的 不保证能用- -)

hugo生成的文章目录不支持大小写区分,且会把空格替换为-

原先用hexo时候目录路径是完全按照自己的标题生成的,而hugo则转换成小写.好不容易有了几篇文章被收录,这样不就完蛋了.评论也是用的gitalk,路径变了,评论也丢了.同样的还有使用busuanzi的访客统计,也丢了. 解决办法:至于gitalk和busuanzi我是没办法了.

  • 我在404页面加了一段JS,来保证被收录的页面还能够正常访问
<script>
  var currenturl=location.href.toLocaleLowerCase().replace("%20","-");
  if(currenturl!=location.href){
 	location.href=currenturl;
  }
</script>
  • 2019/12/30 更新一个新的方法,那就是利用CloudFlare的workers服务,关键部分如下
async function handleRequest(request) {
  let requestURL = new URL(request.url)
  let path = decodeURIComponent(requestURL.pathname)
  let location = path.toLowerCase().replace("%20","-")
  if (location != path) {
    return Response.redirect("https://huai.pub/"+location, 301)
  }
  return fetch(request)
}
addEventListener('fetch', async event => {
  event.respondWith(handleRequest(event.request))
})

友情链接样式丢了

这个…没有解决..等以后有时间了再扣

其他 估计 也许 没了吧..