amend local search logic (#521)

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

View File

@ -197,6 +197,7 @@ search:
field: all # post, page, all field: all # post, page, all
path: /search.json # 搜索文件存放位置 path: /search.json # 搜索文件存放位置
content: true # 是否搜索内容 content: true # 是否搜索内容
skip_search: # 指定 path 中的内容不被搜索。
algolia_search: # Docsearch https://docsearch.algolia.com/apply/ 申请 algolia_search: # Docsearch https://docsearch.algolia.com/apply/ 申请
appId: appId:
apiKey: apiKey:

View File

@ -60,8 +60,22 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
return temp_post return temp_post
} }
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;
}
if (posts) { if (posts) {
posts.each(function(post) { posts.each(function(post) {
var layout_list = ["post"]
if (!layout_list.includes(post.layout)) return
if (matchAndExit(post.path, cfg.skip_search)) return
if (post.indexing == false) return if (post.indexing == false) return
let temp_post = generateJson(post) let temp_post = generateJson(post)
res.push(temp_post) res.push(temp_post)
@ -69,6 +83,9 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
} }
if (pages) { if (pages) {
pages.each(function(page) { pages.each(function(page) {
var layout_list = ["page", "wiki"]
if (!layout_list.includes(page.layout)) return
if (matchAndExit(page.path, cfg.skip_search)) return
if (page.indexing == false) return if (page.indexing == false) return
let temp_post = generateJson(page) let temp_post = generateJson(page)
res.push(temp_post) res.push(temp_post)