2

WordPress 迁移至 Hexo 全记录

 3 years ago
source link: https://www.codesky.me/archives/wordpress-to-hexo-record.wind
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WordPress 迁移至 Hexo 全记录

本文不含 hexo 主题、配置教程,请注意。

腾讯云的一通电话,让我勤劳了起来,因为他们跟我说:您好,发现您的这个域名不在腾讯云上,如果不改解析的话会掉备案。春节后来核实——众所周知的是 .me 域名已经被拉黑了,掉绑之后再也绑不上,但在国内有备案好商量,所以我只能选择:切。

之前「空之领域」用的一直是 WordPress,但太久没用 PHP 的情况下维护 PHP 全家桶 + 数据迁移实在是太麻烦了,本身 WordPress 这种直接转成 HTML 的文章也不好备份和管理,八百年前我就想写 Markdown 了,只是实在太懒了一直没切,被逼勤劳之下开始干活,选了半天,还是选择了大家都在用的 hexo(此处心路历程不在本文的讨论中)。

另外需要注意到是,评论无了,如果是重评论内容,建议继续用。

使用 wordpress-export-to-markdown,从 WordPress 后台找到 Export,把所有内容导出来(包括博文和页面)。使用这个程序处理,会导出一系列 Markdown。

配置 hexo

这个很简单,挑一个主题,然后把文章原样放进去,之后就可以直接管理 markdown 文件:

  1. hexo new post
  2. hexo new page

主题选了 next,虽然已经是烂大街了但确实少了很多成本,我的原则依旧是写博客就关注内容本身就可以了,烂大街就烂大街吧,问题不大。

在第一次的 hexo server 启动过后,我们发现转换的文章还是有点问题的:

  1. Read More 标记无了
  2. 取得 tag 和 categories 是 slug 而不是正文内容

针对这两个问题写了两个脚本,核心思想是遍历加文本处理加重新存文件:

增加 read more 标签

在 20 行会增加 readmore,如果你的文章一行内容不是太多的话,应该断的还是比较合适的

  1. def addReadMore():
  2. for dirpath, dnames, fnames in os.walk("./source/_posts/"):
  3. for f in fnames:
  4. if f.endswith(".md"):
  5. print(dirpath + '/' + f)
  6. contents = ''
  7. with open(dirpath + '/' + f, 'r') as a:
  8. list = a.readlines()
  9. list = list[0: 20] + ['<!--more -->'] + list[20:]
  10. contents = ''.join(list)
  11. a.close()
  12. with open(dirpath + '/' + f, 'w') as a:
  13. a.write(contents)
  14. a.close()

Tag 和 Categories 重新映射

这里可以选择连接 DB or 人肉 Mapping,考虑到我的 tags 和 categories 的数据量不大,直接采用了人肉 mapping

  1. mapping = {
  2. 'rem': '特別紀念',
  3. 'diary': '天の日記',
  4. 'tsukkomi': '實時吐槽',
  5. 'novel': '小天文字',
  6. 'note': '學習筆記',
  7. 'good': '轉載好料',
  8. 'photo': '各種相冊',
  9. 'tests': '考试',
  10. 'charcater': '人格',
  11. 'friendship': '友情',
  12. 'novels': '小说',
  13. 'bizarre': '奇幻',
  14. 'wraith': '妖灵',
  15. 'trans': '转载',
  16. 'classic': '经典',
  17. 'school': '校园',
  18. 'daily': '日记',
  19. '65': '日记',
  20. 'software': '软件',
  21. 'erciyuan': '二次元',
  22. 'notice': '公告',
  23. 'renew': '更新',
  24. 'code': '代码',
  25. 'contest': '竞赛',
  26. 'beau-acticle': '美文',
  27. 'timely': '实时',
  28. 'edu': '教育',
  29. 'hotdebate': '热议',
  30. 'network': '网络',
  31. 'comment': '评论',
  32. 'essay': '随记',
  33. 'travelnotes': '游记',
  34. 'course': '教程',
  35. 'comprehension': '作文',
  36. 'teacher-student': '师生',
  37. 'fujian': '富坚义博',
  38. 'comments': '点评',
  39. 'download': '下载',
  40. 'notes': '笔记',
  41. 'itnet': '互联网',
  42. 'pack': '整合',
  43. 'photos': '相册',
  44. 'autobiography': '自传',
  45. 'personal': '人物',
  46. 'anniversary': '周年'
  47. }
  48. notIncluding = set()
  49. def mapToCh(i):
  50. if not i in mapping:
  51. notIncluding.add(i)
  52. return mapping[i] if i in mapping else i
  53. def translateTags():
  54. for dirpath, dnames, fnames in os.walk("./source/_posts/"):
  55. for f in fnames:
  56. if f.endswith(".md"):
  57. print(dirpath + '/' + f)
  58. contents = ''
  59. with open(dirpath + '/' + f, 'r') as a:
  60. list = a.readlines()
  61. idx = list.index('---\n', 1)
  62. subList = list[0:idx]
  63. currList = [' - "' + mapToCh(i[5:-2]) + '"\n'
  64. if i.startswith(' - ') else i
  65. for i in subList]
  66. list = currList + list[idx:]
  67. contents = ''.join(list)
  68. a.close()
  69. with open(dirpath + '/' + f, 'w') as a:
  70. a.write(contents)
  71. a.close()
  72. print(notIncluding)

配置 Nginx

nginx 成功从一坨 PHP fpm 配置中解放,回归到了静态资源文件的管理中,最简单的 demo 版本大致长这样,其他按照自己的需求自行修改:

  1. server {
  2. listen 80;
  3. listen 443;
  4. server_name xsky.me;
  5. root /usr/share/nginx/blog/public;
  6. index index.html;
  7. location / {
  8. }
  9. }

CI & CD

大部分人的 Hexo 用法其实是结合 GitHub Pages,这样确实轻松愉快,但是我们这里为了解决备案问题,仍然没有放弃机器,但是文章的管理、发布肯定还是结合 git + CI CD 的快,这里我用了 GitHub Actions 进行操作,本质上是 SSH 远程命令操作:

  1. name: CI
  2. # Controls when the action will run.
  3. on:
  4. # Triggers the workflow on push or pull request events but only for the master branch
  5. push:
  6. branches: [ master ]
  7. pull_request:
  8. branches: [ master ]
  9. # Allows you to run this workflow manually from the Actions tab
  10. workflow_dispatch:
  11. jobs:
  12. build:
  13. name: Build
  14. runs-on: ubuntu-latest
  15. steps:
  16. - name: executing remote ssh commands using password
  17. uses: appleboy/ssh-action@master
  18. with:
  19. host: ${{ secrets.HOST }}
  20. username: ${{ secrets.USERNAME }}
  21. password: ${{ secrets.PASSWORD }}
  22. port: 22
  23. script: |
  24. cd /usr/share/nginx/blog
  25. git pull github master --rebase
  26. npm install
  27. hexo generate

这里需要注意,secret 变量请在项目的 Settings - Secrets 中设置并引用。

Push & 测试全流程,Done。如果不确定源文件内部是否填写了一些你觉得敏感的内容,请把仓库至于 private。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK