Hexo 產生的 Meta Description

blog 跑 Lighthouse 測試的時候發現 meta description 有點怪怪的, 追蹤一下才發現 cactus themehead.ejs 裡是使用 hexo 內建的 open_graph helper 來產生 description, 如果沒有指定 page.description/page.excerpt 兩個變數, 就會直接抓網頁內容前 200 個字做為 description(似乎是因為 og description 建議在 200 字以內)

雖然 官方文件列出的 Variables 有 page.description/page.excerpt 兩個變數, 但並沒有提到該怎麼自動產生, 再深入追查找到 About variable “page.excerpt” 這個 issue, 有人提到可使用

1
2
3
4
5
---
Front-matter...
---
some text
<!-- more -->

在 more tag 以上就會被指定為 post.excerpt, 另外也有 plugin 可以用, 但總覺得寫完文章還要在前面放個 tl;dr 有點麻煩, 只是想改一下首頁的 meta description, 應該有更簡單方法, 後來就直接改 cactus theme 裡面的 index.ejs, 直接指定 page.description = config.description 收工

註: open_graph 取 description 相關程式碼
hexo/lib/plugins/helper/open_graph.js

1
2
3
4
5
6
7
let description = options.description || page.description || page.excerpt || content || config.description;
...
if (description) {
description = escapeHTML(stripHTML(description).substring(0, 200)
.trim() // Remove prefixing/trailing spaces
).replace(/\n/g, ' '); // Replace new lines by spaces
}