编辑
2026-04-01
undefined
00

目录

背景
从WordPress导出数据
搭建临时Hexo博客
安装node.js
安装Git
安装Hexo
修改文章属性
安装Hugo
创建目录
配置环境变量
搭建博客
创建第一篇文章
启动博客
添加主题
构建网站
public上传到github

背景

为什么抛弃WordPress?

  1. 网站访问量极少,大部分是自用记录些笔记
  2. 不想再买服务器了

为什么选择Hugo?

  1. Hugo编译速度贼快
  2. 有一款喜欢的Hugo主题

从WordPress导出数据

在 WordPress 仪表盘中,[工具] -> [导出] -> [下载导出的文件]。

搭建临时Hexo博客

安装node.js

node.js官方下载地址:https://nodejs.org/en/

设置npm淘宝镜像站:npm config set registry https://registry.npm.taobao.org

切回官方镜像用:npm config set registry http://www.npmjs.org

安装Git

Git官方下载地址:https://git-scm.com/downloads

安装Hexo

运行下列命令安装Hexo:

npm install -g hexo

新建一个Hexo博客:

hexo init <folder> cd <folder> npm install

再安装 hexo-migrator-wordpress 插件:

npm install hexo-migrator-wordpress --save

插件安装完成后,执行下列命令来迁移所有文章,source 可以是 WordPress 导出的文件路径或网址。

hexo migrate wordpress <source>

文章很快就能出现在source/_posts目录下,转换效果非常棒。

修改文章属性

刚迁移完的每篇文章都有yaml配置,像这样:

title: 3G壁纸 tags: - 网站 id: '73' categories: - - 软件 comments: false date: 2022-01-10 22:05:31

直接丢到Hugo的posts文件夹里也没什么问题,但最好还是改一下。比如改成这样:

author: abee categories: - 软件 date: 2022-01-10 22:05:31 draft: false title: 3G壁纸

可以使用下面的Python脚本来实现:

# pip install pyyaml 安装 yaml 包 import os import yaml import re path = "C:\Users\ec\Desktop\_posts" cnt = 0 for file in os.listdir(path):     if file[-3:] == ".md":  # 判断是否为md文件         cnt += 1         print(cnt, file) with open(os.path.join(path, file), "r+", encoding='utf-8') as f:             all_text = f.read() yaml_text = re.findall("---([\s\S]*?)---", all_text)[0]  # 找到yaml字符串         old_dic = yaml.safe_load(yaml_text)  # 用yaml库转字典对象 new_dic = {             "title": old_dic["title"],             "author": "abee",             "draft": False,             "categories": old_dic['categories'][0],             "tags": old_dic['tags'],             "date": old_dic["date"]         } yaml_res = yaml.safe_dump(new_dic, allow_unicode=True)  # 用yaml库将字典解析为yaml字符串         res = all_text.replace(yaml_text, "\n"+yaml_res) post_id = old_dic["id"]         os.mkdir(os.path.join(path, post_id))         path_md = os.path.join(path, post_id)         with open(os.path.join(path_md, 'index.md'), "w+", encoding='utf-8') as f:             f.write(res)

安装Hugo

Hugo有两个版本:hugo和hugo_extended,怎么选?建议安装hugo_extended,很多功能,包括一些主题,都需要hugo_extended的支持。

Hugo官方下载地址:https://github.com/gohugoio/hugo/releases

创建目录

例如,在D盘创建文件夹Hugo,在文件夹Hugo下创建文件夹bin和sites。

Hugo下载后,解压,将hugo.exe文件放到D:\Hugo\bin目录下。

配置环境变量

将Hugo添加到Windows的环境变量 PATH 中。

[此电脑] -> [属性] -> [高级系统设置] -> [环境变量] -> [系统变量] -> [Path] -> [编辑] -> [新建],把D:\Hugo\bin加入环境变量。

打开cmd,输入hugo version,查看hugo是否添加成功。

搭建博客

cd Hugo\sites hugo new site myblog

创建第一篇文章

cd myblog hugo new posts/first_post.md

启动博客

hugo serve -e production -D

-D 表示将草稿文件也进行渲染。 -e production,hugo serve 默认运行环境是 development,而 hugo 默认运行环境是 production。

添加主题

hugo没有自带主题,所以需要自己导入主题文件。

官网主题库:https://themes.gohugo.io/

导入主题可以用 git clone 的方式,也可以到相应主题的github中下载zip文件然后解压到自己博客的themes文件夹中。

Hugo全局配置文件支持三种格式:toml,yaml,yml。这个配置文件可以直接从主题文件中的exampleSite 里copy到博客文件夹下,然后进行修改。

构建网站

hugo

默认会在站点根目录的public/目录下生成对应的静态页面,可通过在命令行指定-d或者--destination参数来改变静态页面的存放路径,也可以通过在配置文件中设置publishDir来指定。

public上传到github

git init git add . # 添加当前目录下的所有文件到暂存区 git commit -m "add post" # 生成版本 git branch -M main git remote add origin git@github.com:用户名/仓库 git push -u origin main

本文作者:a

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!