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()
|
|
|
|
}
|
2024-09-03 18:10:23 +08:00
|
|
|
if (post.tags && Array.isArray(post.tags) && post.tags.length > 0) {
|
2022-11-29 21:54:15 +08:00
|
|
|
var tags = []
|
|
|
|
post.tags.forEach(function (tag) {
|
|
|
|
tags.push(tag.name)
|
|
|
|
})
|
|
|
|
temp_post.tags = tags
|
|
|
|
}
|
2024-09-03 18:10:23 +08:00
|
|
|
if (post.categories && Array.isArray(post.categories) && post.categories.length > 0) {
|
2022-11-29 21:54:15 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-09 10:06:21 +08:00
|
|
|
function matchAndExit(path, patterns) {
|
|
|
|
for (let pattern of patterns) {
|
|
|
|
const regexPattern = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
|
|
|
|
if (path.match(regexPattern)) {
|
|
|
|
// console.log("Matched pattern:", pattern);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-29 21:54:15 +08:00
|
|
|
if (posts) {
|
|
|
|
posts.each(function(post) {
|
2024-10-09 10:06:21 +08:00
|
|
|
var layout_list = ["post"]
|
|
|
|
if (!layout_list.includes(post.layout)) return
|
|
|
|
if (matchAndExit(post.path, cfg.skip_search)) return
|
2022-11-29 21:54:15 +08:00
|
|
|
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) {
|
2024-10-09 10:06:21 +08:00
|
|
|
var layout_list = ["page", "wiki"]
|
|
|
|
if (!layout_list.includes(page.layout)) return
|
|
|
|
if (matchAndExit(page.path, cfg.skip_search)) return
|
2022-11-29 21:54:15 +08:00
|
|
|
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
|
|
|
}
|
2024-09-03 18:10:23 +08:00
|
|
|
})
|