Hexo and NexT Theme
· 阅读需 5 分钟
1. 安装 NexT 主题
方法一:使用 Git(推荐)
# 进入你的 Hexo 站点目录
cd your-hexo-site
# 克隆 NexT 主题到 themes/next 目录
git clone https://github.com/next-theme/hexo-theme-next themes/next
方法二:使用 npm
# 使用 npm 安装
npm install hexo-theme-next
2. 启用 NexT 主题
2.1 修改主配置文件 _config.yml
# 站点配置
title: 你的网站标题
subtitle: 网站副标题
description: 网站描述
keywords: 关键词1, 关键词2
author: 你的名字
language: zh-CN
timezone: Asia/Shanghai
# URL
url: https://yourusername.github.io
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
trailing_index: true
trailing_html: true
# 目录配置
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:
# 主题配置
theme: next
3. 验证主题安装
# 清理并重新生成
hexo clean
# 生成静态文件
hexo generate
# 启动本地服务器预览
hexo server
访问 http://localhost:4000 查看主题效果。
4. NexT 主题配置
4.1 了解配置文件结构
NexT 有两个主要配置文件:
- 主配置文件:
_config.yml(Hexo 根目录) - 主题配置文件:
themes/next/_config.yml
4.2 配置主题方案
NexT 提供四种主题方案,在 themes/next/_config.yml 中:
# Schemes
# scheme: Muse
# scheme: Mist
scheme: Gemini
# scheme: Pisces
取消注释你喜欢的方案。
4.3 基本外观配置
# 在 themes/next/_config.yml 中
# 网站图标
favicon:
small: /images/favicon-16x16-next.png
medium: /images/favicon-32x32-next.png
apple_touch_icon: /images/apple-touch-icon-next.png
safari_pinned_tab: /images/logo.svg
# 网站徽标
logo:
enabled: true
image: /images/logo.svg
# 菜单配置
menu:
home: / || fa fa-home
about: /about/ || fa fa-user
tags: /tags/ || fa fa-tags
categories: /categories/ || fa fa-th
archives: /archives/ || fa fa-archive
# schedule: /schedule/ || fa fa-calendar
# sitemap: /sitemap/ || fa fa-sitemap
# commonweal: /404/ || fa fa-heartbeat
# 社交链接
social:
GitHub: https://github.com/yourusername || fab fa-github
E-Mail: mailto:your-email@example.com || fa fa-envelope
# Weibo: https://weibo.com/yourusername || fab fa-weibo
# Twitter: https://twitter.com/yourusername || fab fa-twitter
# 侧边栏设置
sidebar:
position: left
display: post
offset: 12
b2t: false
scrollpercent: true
# 头像设置
avatar:
enabled: true
url: /images/avatar.png
rounded: true
rotated: false
5. 创建必要的页面
5.1 创建关于页面
hexo new page about
编辑 source/about/index.md:
---
title: 关于我
date: 2024-01-01 00:00:00
type: about
---
这里写关于我的内容...
5.2 创建标签页面
hexo new page tags
编辑 source/tags/index.md:
---
title: 标签
date: 2024-01-01 00:00:00
type: tags
---
5.3 创建分类页面
hexo new page categories
编辑 source/categories/index.md:
---
title: 分类
date: 2024-01-01 00:00:00
type: categories
---
6. 高级功能配置
6.1 代码高亮
# 在 themes/next/_config.yml 中
codeblock:
# 代码块主题
theme:
light: default
dark: darker
prism:
light: prism
dark: prism-dark
copy_button:
enable: true
show_result: true
style: flat
6.2 阅读进度
# 阅读进度条
reading_progress:
enable: true
color: "#37c6c0"
height: 3px
6.3 数学公式支持
# Math Formulas Render Support
math:
enable: true
per_page: true
mathjax:
enable: true
# Available values: none | ams | all
tags: none
katex:
enable: false
copy_tex: false
6.4 评论系统
# 多种评论系统可选
comments:
# 可用的值: utterances | giscus | disqus | disqusjs | changyan | livere | remark42 | valine
# 选择一种启用
provider: utterances
# Utterances 配置
utterances:
repo: yourusername/your-repo # 你的 GitHub 仓库
issue_term: pathname
label: utterances
theme: github-light
6.5 搜索功能
# 安装搜索插件
npm install hexo-generator-searchdb
在 Hexo 主配置文件中添加:
# 搜索配置
search:
path: search.xml
field: post
content: true
format: html
在主题配置中启用:
# Local Search
local_search:
enable: true
trigger: auto
top_n_per_article: 1
unescape: false
preload: false
7. 自定义样式和布局
7.1 自定义 CSS
创建 source/_data/styles.styl:
// 自定义样式
// 网站背景
body {
background-color: #f7f9fa;
}
// 文章样式
.post-block {
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
padding: 20px;
margin-bottom: 20px;
}
// 链接颜色
a {
color: #3498db;
&:hover {
color: #2980b9;
}
}
在主题配置中启用自定义样式:
custom_file_path:
style: source/_data/styles.styl
7.2 自定义页面布局
创建自定义布局文件 themes/next/layout/_custom/custom-layout.swig:
{% extends '_layout.swig' %}
{% block content %}
<div class="custom-container">
<h1>{{ page.title }}</h1>
<div class="custom-content">
{{ page.content }}
</div>
</div>
{% endblock %}
8. 优化和部署
8.1 安装必要插件
# 安装常用插件
npm install hexo-deployer-git --save
npm install hexo-generator-sitemap --save
npm install hexo-generator-feed --save
8.2 部署配置
在 Hexo 主配置文件中添加:
# 部署配置
deploy:
type: git
repo: https://github.com/yourusername/yourusername.github.io.git
branch: main
message: "Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }}"
8.3 部署命令
# 清理、生成、部署
hexo clean && hexo generate && hexo deploy
# 或者使用简写
hexo clean && hexo g -d
9. 故障排除
9.1 常见问题
-
主题不生效:
- 检查
theme: next配置 - 运行
hexo clean
- 检查
-
页面 404:
- 检查菜单配置的路径
- 确认页面已创建
-
样式异常:
- 检查自定义 CSS 语法
- 确认字体文件路径
9.2 调试技巧
# 详细调试信息
hexo generate --debug
# 检查配置
hexo config
10. 最佳实践
- 版本控制:将整个 Hexo 项目推送到一个单独的仓库
- 备份配置:定期备份主题配置文件
- 渐进式配置:一次只修改一个功能,测试后再继续
- 使用 CDN:对于静态资源使用 CDN 加速