HEXO Next SEO 添加 Description 和 Keywords

目前我使用 HEXO 搭配 Next 主题,最近我的网页优化建议中,总是提醒我缺少 Description 和 Keywords。

我在网上找了很多资料,发现 Next 主题的配置文件中并没有提供这两个字段的配置项。

而 Description 的配置项在主题的 _config.yml

1
excerpt_description: true

1. 修改 scaffolds

scaffolds 目录下,找到 post.md 文件,添加如下代码:

1
2
3
4
5
6
---
<!-- existed code -->
description: none
keywords:
<!-- existed code -->
---

这样在你新建文章的时候,就会有这两个字段了。

2. 修改布局文件

layout 目录下,找到 layout/layout.njk 文件,添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<head>
{{ partial('_partials/head/head.njk', {}, {cache: theme.cache.enable}) }}
<!-- existed code -->

{% if page.description %}
<meta name="description" content="{{ page.description }}">
{% else %}
<meta name="description" content="{{ theme.description }}">
{% endif %}

{% if page.keywords %}
<meta name="keywords" content="{{ page.keywords }}">
{% else %}
<meta name="keywords" content="{{ theme.keywords }}">
{% endif %}

<!-- existed code -->
</head>

关于 description,某些版本是支持 SEO 的,支持的话就不需要添加了。