fix: ensure tags and categories are arrays before iterating (#512)

This commit is contained in:
Li Linchao 2024-09-03 18:10:23 +08:00 committed by GitHub
parent 0ca983f992
commit 9a96310660
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 4 deletions

View File

@ -37,7 +37,7 @@ function generate_description() {
function generate_keywords() { function generate_keywords() {
if (page.keywords && page.keywords.length > 0) { if (page.keywords && page.keywords.length > 0) {
return '<meta name="keywords" content="' + page.keywords + '">'; return '<meta name="keywords" content="' + page.keywords + '">';
} else if (page.tags && page.tags.length > 0) { } else if (page.tags && Array.isArray(page.tags) && page.tags.length > 0) {
return '<meta name="keywords" content="' + page.tags.map(tag => tag.name).join(',') + '">'; return '<meta name="keywords" content="' + page.tags.map(tag => tag.name).join(',') + '">';
} else if (config.keywords && config.keywords.length > 0) { } else if (config.keywords && config.keywords.length > 0) {
return '<meta name="keywords" content="' + config.keywords + '">'; return '<meta name="keywords" content="' + config.keywords + '">';

View File

@ -43,14 +43,14 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
content = content.replace(/[\s]{2,}/g, ' ') content = content.replace(/[\s]{2,}/g, ' ')
temp_post.content = content.trim() temp_post.content = content.trim()
} }
if (post.tags && post.tags.length > 0) { if (post.tags && Array.isArray(post.tags) && post.tags.length > 0) {
var tags = [] var tags = []
post.tags.forEach(function (tag) { post.tags.forEach(function (tag) {
tags.push(tag.name) tags.push(tag.name)
}) })
temp_post.tags = tags temp_post.tags = tags
} }
if (post.categories && post.categories.length > 0) { if (post.categories && Array.isArray(post.categories) && post.categories.length > 0) {
var categories = [] var categories = []
post.categories.forEach(function (cate) { post.categories.forEach(function (cate) {
categories.push(cate.name) categories.push(cate.name)
@ -78,4 +78,4 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
path: cfg.path, path: cfg.path,
data: JSON.stringify(res) data: JSON.stringify(res)
} }
}) })