CodeSky 代码之空

随手记录自己的学习过程

WordPress 迁移至 Hexo 全记录

2021-02-12 12:06分类: Other评论: 1

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

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

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

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

导出数据

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

配置 hexo

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

1hexo new post
2hexo new page
3

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

处理文章

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

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

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

增加 read more 标签

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

1def 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()
15

Tag 和 Categories 重新映射

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

1mapping = {
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
49notIncluding = set()
50
51def mapToCh(i):
52    if not i in mapping:
53        notIncluding.add(i)
54    return mapping[i] if i in mapping else i
55
56def translateTags():
57    for dirpath, dnames, fnames in os.walk("./source/_posts/"):
58        for f in fnames:
59            if f.endswith(".md"):
60                print(dirpath + '/' + f)
61                contents = ''
62                with open(dirpath + '/' + f, 'r') as a:
63                    list = a.readlines()
64                    idx = list.index('---\n', 1)
65                    subList = list[0:idx]
66                    currList = ['  - "' + mapToCh(i[5:-2]) + '"\n'
67                        if i.startswith('  - ') else i
68                        for i in subList]
69                    list = currList + list[idx:]
70                    contents = ''.join(list)
71                    a.close()
72                with open(dirpath + '/' + f, 'w') as a:
73                    a.write(contents)
74                    a.close()
75    print(notIncluding)
76

配置 Nginx

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

server {
    listen 80;
    listen 443;
    server_name xsky.me;
    root /usr/share/nginx/blog/public;
    index index.html;
    location / {
    }
}

CI & CD

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

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

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

最后

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

评论 (1)

tmr2021年3月2日 01:00

hexo新站rss无了,求安排上