2022-11-28 23:07:50 +08:00
|
|
|
/**
|
|
|
|
* https://github.com/wzpan/hexo-generator-search
|
|
|
|
*/
|
2022-12-19 22:32:08 +08:00
|
|
|
const { stripHTML } = require('hexo-util')
|
2022-11-28 23:07:50 +08:00
|
|
|
|
|
|
|
hexo.extend.generator.register('search_json_generator', function (locals) {
|
2022-11-29 21:54:15 +08:00
|
|
|
if (this.theme.config.search.service != 'local_search') { return {} }
|
|
|
|
const { root } = this.config
|
|
|
|
const { local_search: cfg } = this.theme.config.search
|
|
|
|
cfg.sort = '-date'
|
|
|
|
cfg.field = cfg.field?.trim()
|
2022-11-28 23:07:50 +08:00
|
|
|
|
|
|
|
var posts, pages
|
2022-11-29 21:54:15 +08:00
|
|
|
if (cfg.field == 'post') {
|
|
|
|
posts = locals.posts?.filter(p => p.content?.length > 0).sort(cfg.sort)
|
|
|
|
} else if (cfg.field == 'page') {
|
|
|
|
pages = locals.pages?.filter(p => p.content?.length > 0)
|
|
|
|
} else {
|
|
|
|
posts = locals.posts?.filter(p => p.content?.length > 0).sort(cfg.sort)
|
|
|
|
pages = locals.pages?.filter(p => p.content?.length > 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
var res = new Array()
|
2022-11-28 23:07:50 +08:00
|
|
|
|
2022-11-29 21:54:15 +08:00
|
|
|
function generateJson(post) {
|
|
|
|
var temp_post = new Object()
|
|
|
|
if (post.title) {
|
|
|
|
temp_post.title = post.title.trim()
|
|
|
|
}
|
|
|
|
if (post.path) {
|
|
|
|
temp_post.path = root + post.path
|
|
|
|
}
|
2022-12-19 22:32:08 +08:00
|
|
|
if (cfg.content != false && post.content) {
|
|
|
|
var content = stripHTML(post.content).trim()
|
2022-12-19 21:40:14 +08:00
|
|
|
// 部分HTML标签
|
|
|
|
content = content.replace(/<iframe[\s|\S]+iframe>/g, '')
|
|
|
|
content = content.replace(/<hr>/g, '')
|
|
|
|
content = content.replace(/<br>/g, '')
|
2022-12-19 22:32:08 +08:00
|
|
|
// 换行符换成空格
|
|
|
|
content = content.replace(/\\n/g, ' ')
|
|
|
|
content = content.replace(/\n/g, ' ')
|
2022-12-19 21:40:14 +08:00
|
|
|
// 多个连续空格换成单个空格
|
2022-12-19 22:32:08 +08:00
|
|
|
content = content.replace(/[\s]{2,}/g, ' ')
|
2022-11-29 21:54:15 +08:00
|
|
|
temp_post.content = content.trim()
|
|
|
|
}
|
|
|
|
if (post.tags && post.tags.length > 0) {
|
|
|
|
var tags = []
|
|
|
|
post.tags.forEach(function (tag) {
|
|
|
|
tags.push(tag.name)
|
|
|
|
})
|
|
|
|
temp_post.tags = tags
|
|
|
|
}
|
|
|
|
if (post.categories && post.categories.length > 0) {
|
|
|
|
var categories = []
|
|
|
|
post.categories.forEach(function (cate) {
|
|
|
|
categories.push(cate.name)
|
|
|
|
})
|
|
|
|
temp_post.categories = categories
|
|
|
|
}
|
|
|
|
return temp_post
|
2022-11-28 23:07:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 21:54:15 +08:00
|
|
|
if (posts) {
|
|
|
|
posts.each(function(post) {
|
|
|
|
if (post.indexing == false) return
|
|
|
|
let temp_post = generateJson(post)
|
|
|
|
res.push(temp_post)
|
2022-11-28 23:07:50 +08:00
|
|
|
})
|
|
|
|
}
|
2022-11-29 21:54:15 +08:00
|
|
|
if (pages) {
|
|
|
|
pages.each(function(page) {
|
|
|
|
if (page.indexing == false) return
|
|
|
|
let temp_post = generateJson(page)
|
|
|
|
res.push(temp_post)
|
|
|
|
})
|
|
|
|
}
|
2022-11-28 23:07:50 +08:00
|
|
|
return {
|
2022-11-29 21:54:15 +08:00
|
|
|
path: cfg.path,
|
|
|
|
data: JSON.stringify(res)
|
2022-11-28 23:07:50 +08:00
|
|
|
}
|
|
|
|
})
|