[opt] search content

This commit is contained in:
xaoxuu 2022-12-19 22:32:08 +08:00
parent 1a137826f7
commit 5fc71e2f86
3 changed files with 9 additions and 41 deletions

View File

@ -79,7 +79,6 @@ search:
field: all # post, page, all
path: /search.json # 搜索文件存放位置
content: true # 是否搜索内容
codeblock: true # 是否搜索代码块需要content: true)
######## Comments ########

View File

@ -1,6 +1,7 @@
/**
* https://github.com/wzpan/hexo-generator-search
*/
const { stripHTML } = require('hexo-util')
hexo.extend.generator.register('search_json_generator', function (locals) {
if (this.theme.config.search.service != 'local_search') { return {} }
@ -29,46 +30,17 @@ hexo.extend.generator.register('search_json_generator', function (locals) {
if (post.path) {
temp_post.path = root + post.path
}
if (cfg.content != false && post._content) {
var content = post._content.trim()
// 过滤掉标签和注释
if (content.includes('{%')) {
// 需要保留内容的的标签
content = content.replace(/{%\s*mark\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*folding\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*copy\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*note\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*kbd\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*emp\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*wavy\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*sub\s*(.*?)\s*%}/g, '$1')
content = content.replace(/{%\s*sup\s*(.*?)\s*%}/g, '$1')
content = content.replace(/<!--\s*folder(.*?)\s*-->/g, '$1')
content = content.replace(/<!--\s*tab(.*?)\s*-->/g, '$1')
content = content.replace(/<!--\s*node(.*?)\s*-->/g, '$1')
// 不保留内容的标签
content = content.replace(/{%\s*(.*?)\s*%}/g, '')
}
// 注释
content = content.replace(/<!--\s*(.*?)\s*-->/g, '')
// ## 标题
content = content.replace(/[#]{2,} /g, '')
if (cfg.content != false && post.content) {
var content = stripHTML(post.content).trim()
// 部分HTML标签
content = content.replace(/<iframe[\s|\S]+iframe>/g, '')
content = content.replace(/<hr>/g, '')
content = content.replace(/<br>/g, '')
// 图片
content = content.replace(/\!\[(.*?)\]\((.*?)\)/g, '')
// 链接
content = content.replace(/\[(.*?)\]\((.*?)\)/g, '$1')
// 过滤代码块
if (cfg.codeblock == false) {
content = content.replace(/```([^`]+)```/g, '')
}
// 换行符换成空格
content = content.replace(/\\n/g, ' ')
content = content.replace(/\n/g, ' ')
// 多个连续空格换成单个空格
content = content.replace(/[\s]{2,}/g, ' ')
// 特殊字符
content = content.replace(/[\r|\n]+/g, '')
temp_post.content = content.trim()
}
if (post.tags && post.tags.length > 0) {

View File

@ -54,13 +54,10 @@ var searchFunc = function(path, filter, searchId, contentId) {
}
// perform local searching
datas.forEach(function(data) {
if (!data.title?.trim().length) { return }
if (!data.content?.trim().length) { return }
var matches = 0;
if (filter && !data.path.includes(filter)) {
return;
}
var dataTitle = data.title.trim();
if (filter && !data.path.includes(filter)) { return }
var dataTitle = data.title?.trim() || 'Untitled';
var dataTitleLowerCase = dataTitle.toLowerCase();
var dataContent = data.content;
var dataContentLowerCase = dataContent.toLowerCase();