From 5fc71e2f862d7e82f18a3f37709f29865d491923 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Mon, 19 Dec 2022 22:32:08 +0800 Subject: [PATCH] [opt] search content --- _config.yml | 1 - scripts/generators/search.js | 42 ++++++-------------------------- source/js/search/local-search.js | 7 ++---- 3 files changed, 9 insertions(+), 41 deletions(-) diff --git a/_config.yml b/_config.yml index 8dbb59b..9aaa6ec 100755 --- a/_config.yml +++ b/_config.yml @@ -79,7 +79,6 @@ search: field: all # post, page, all path: /search.json # 搜索文件存放位置 content: true # 是否搜索内容 - codeblock: true # 是否搜索代码块(需要content: true) ######## Comments ######## diff --git a/scripts/generators/search.js b/scripts/generators/search.js index 6b8f007..ea6cb15 100644 --- a/scripts/generators/search.js +++ b/scripts/generators/search.js @@ -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(//g, '$1') - content = content.replace(//g, '$1') - content = content.replace(//g, '$1') - // 不保留内容的标签 - content = content.replace(/{%\s*(.*?)\s*%}/g, '') - } - // 注释 - content = content.replace(//g, '') - // ## 标题 - content = content.replace(/[#]{2,} /g, '') + if (cfg.content != false && post.content) { + var content = stripHTML(post.content).trim() // 部分HTML标签 content = content.replace(//g, '') content = content.replace(/
/g, '') content = content.replace(/
/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, '') + content = content.replace(/[\s]{2,}/g, ' ') temp_post.content = content.trim() } if (post.tags && post.tags.length > 0) { diff --git a/source/js/search/local-search.js b/source/js/search/local-search.js index 246c0f6..694d8e2 100644 --- a/source/js/search/local-search.js +++ b/source/js/search/local-search.js @@ -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();