blog从wordpress迁移至hexo+github page

主要参考了这篇博客,还综合网上其他教程做了优化。

wp导出

wordpress方面,在【后台–工具–导出】可导出xml文件。存储这个文件为wordpress.xml,后续我们用hexo-migrator-wordpress插件将它导入hexo。

hexo安装

  1. 创建文件夹blog作为博客文件的存储地

  2. 打开git bash,安装hexo-cli(默认已安装git

    1
    npm install -g hexo-cli
  3. 使用 hexo-cli 在本地创建 hexo 工程

    1
    2
    3
    cd blog  # 定位到blog
    hexo init
    npm install
  4. 导入wp数据

    1
    2
    npm install hexo-migrator-wordpress --save  # 安装转换插件
    hexo migrate wordpress [path to wordpress.xml] # 输入xml文件的绝对路径。我这里是hexo migrate wordpress "..\wordpress_blog_data\lonetrialantares.WordPress.2024-09-22.xml"
  5. github repositery建立

    名称 性质 作用
    weblog public 静态文件
    blog private 博客程序
  6. git push同步

    1
    2
    3
    4
    5
    6
    git init
    git checkout -b main # 建main分支(因为github repo的default branch是main)
    git remote add origin [SSH of blog] # attention : origin & blog
    git add .
    git commit -m "init"
    git push --set-upstream origin main --force # 建议先git push,否则一来就force也会报错
  7. 下载配置主题

    hexo官网可选择主题。多番对比后我选择了yuzu的theme,因为适合我学术博客与随笔并行的写作风格。之后有时间也可能fork并个性化一下。为完整展示安装流程和注意事项,我把theme-yuzu的readme搬过来演示:

    1. 安装主题与必要的依赖
    1
    2
    3
    4
    # 在blog内运行
    npm i hexo-renderer-pug
    mkdir -p themes && cd themes
    git clone https://github.com/Cerallin/hexo-theme-yuzu
    1. 修改blog根目录下的 config.yml

    config.ymltheme 字段改为 hexo-theme-yuzu。将主题文件夹下 _root_config_example.yml 的内容添加到 config.yml 中。

    1. 根据自己的需要修改主题配置

    可创建一个_config.[theme name].yml文件来覆盖主题的默认设置。对于上述安装方法来说,就是_config.hexo-theme-yuzu.yml

    更新:这个主题加载比较慢,换成solar试一下:

    1. In the root directory of Hexo:
    1
    2
    $ git clone https://github.com/tzvetkov75/solar-theme-hexo.git themes/solar
    $ npm install hexo-pagination --save
    1. Change the theme property in the config.yml file (hexo config file).
    1
    theme: solar

    solar是基于cactus改写的,性能比较好,加载比较快。

    尝试更换主题时,需耐心等待,电脑端加载比较慢(也可能是因为cookie残留之类的?)用手机预览会比较好。

构建与发布

  1. 更改config.ymldeploy设置:

    1
    2
    3
    4
    deploy:
    type: git
    repo: git@github.com:yourname/weblog.git # yourname 替换为 github 用户名
    branch: gh-pages
  2. 安装发布插件

    1
    npm install hexo-deployer-git --save
  3. 构建、发布

    1
    hexo g -d
  4. github绑定域名

    • 域名方面

    以阿里云购买的域名为例,需添加5条域名解析记录:

    记录类型 主机记录-域名前缀 记录值
    A @ 185.199.108.153
    A @ 185.199.109.153
    A @ 185.199.110.153
    A @ 185.199.111.153
    CNAME www yourname.github.io
    • github方面

    weblog的【setting–page–custom domain】处添加自己的域名即可。解析记录添加过了就能通过DNS审核。记得勾选Enforce HTTPS

    想要以后每次构建发布自动添加域名,可在【blog/source】添加一个无后缀的文件【CNAME】,内容为你的域名。

至于自动化构建与发布,感觉意义不大,之后再弄。现今阶段更改完文件直接hexo g -d就行了,已经很方便。

后续建设(个性化)

typora图片插入

总体参考这篇博客,更改typora的偏好设置(也就是在YAML Front Matter加一句typora-root-url: ./..)、在source文件夹新建一个images文件夹即可插入图片。

给已有文章添加根目录地址时,可参考下面的python程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import re

def add_typora_root_url_to_md_files(folder_path):
# 遍历文件夹中的所有文件
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".md"):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()

# 查找YAML Front Matter(以 --- 开头和结尾)
yaml_pattern = re.compile(r'^---\n(.*?)\n---', re.DOTALL)
match = yaml_pattern.search(content)

if match:
yaml_content = match.group(1)
# 检查是否已经存在 typora-root-url
if 'typora-root-url:' not in yaml_content:
# 在YAML内容中添加 typora-root-url
new_yaml_content = yaml_content.rstrip() + '\ntypora-root-url: ./..'
# 替换原YAML内容
new_content = content[:match.start(1)] + new_yaml_content + content[match.end(1):]
# 写回文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Updated: {file_path}")
else:
# 如果没有YAML Front Matter,则在文件开头添加
new_content = '---\ntypora-root-url: ./..\n---\n' + content
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Added YAML Front Matter to: {file_path}")

# 使用示例
folder_path = '..' # 替换为你的文件夹路径
add_typora_root_url_to_md_files(folder_path)

公式渲染

1
2
npm un hexo-renderer-marked --save
npm i hexo-renderer-pandoc --save