hexo自定义字体

使用自定义字体

使用 霞鹜文楷 字体.

使用工具 woff2 将字体转成适用web格式的字体.

1
2
3
4
# 安装工具
brew install woff2
# 命令转换
woff2_compress LXGWWenKai-Regular.ttf

修改hexo配置

hexosource 目录下新建 fonts _data 目录,将上面转换的 woff2 格式的字体放到文件夹中;

目录结构如下

1
2
3
4
5
6
7
8
G:\hexo-doc\
├── source\
│ └── _data\
│ └── fonts\
│ └── LXGWWenKai-Regular.woff2 ✅ ← 必须放这里!
├── themes\
│ └── next\
│ └── ...

_data 目录下新建 custom.styl 文件

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
@font-face {
font-family: 'LXGWWenKai';
src: url("/fonts/LXGWWenKai-Regular.woff2") format("woff2");
font-display: swap;
}
// 全文
body {
font-family: 'LXGWWenKai', 'Noto Serif SC', 'Microsoft YaHei', sans-serif;
}
.post-body {
font-family: 'LXGWWenKai', 'Noto Serif SC', 'Microsoft YaHei', sans-serif;
}

// 针对标题的额外覆盖
h1, h2, h3, h4, h5, h6,
.post-title,
.post-title a,
.post-header,
.post-body h1,
.post-body h2,
.post-body h3,
.post-body h4,
.post-body h5,
.post-body h6 {
font-family: 'LXGWWenKai', sans-serif !important;
}

// 设置字体到所有相关标题和元信息区域
.post-title,
.post-meta,
.post-meta span,
.post-meta time,
.post-header,
.post-header span {
font-family: 'LXGWWenKai', sans-serif !important;
}

在更改next主题的config文件,找到 custom_file_path 配置项,添加如下配置

1
2
custom_file_path:
style: source/_data/custom.styl