[opt] search content
This commit is contained in:
parent
1a137826f7
commit
5fc71e2f86
|
@ -79,7 +79,6 @@ search:
|
||||||
field: all # post, page, all
|
field: all # post, page, all
|
||||||
path: /search.json # 搜索文件存放位置
|
path: /search.json # 搜索文件存放位置
|
||||||
content: true # 是否搜索内容
|
content: true # 是否搜索内容
|
||||||
codeblock: true # 是否搜索代码块(需要content: true)
|
|
||||||
|
|
||||||
|
|
||||||
######## Comments ########
|
######## Comments ########
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/**
|
/**
|
||||||
* https://github.com/wzpan/hexo-generator-search
|
* https://github.com/wzpan/hexo-generator-search
|
||||||
*/
|
*/
|
||||||
|
const { stripHTML } = require('hexo-util')
|
||||||
|
|
||||||
hexo.extend.generator.register('search_json_generator', function (locals) {
|
hexo.extend.generator.register('search_json_generator', function (locals) {
|
||||||
if (this.theme.config.search.service != 'local_search') { return {} }
|
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) {
|
if (post.path) {
|
||||||
temp_post.path = root + post.path
|
temp_post.path = root + post.path
|
||||||
}
|
}
|
||||||
if (cfg.content != false && post._content) {
|
if (cfg.content != false && post.content) {
|
||||||
var content = post._content.trim()
|
var content = stripHTML(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, '')
|
|
||||||
// 部分HTML标签
|
// 部分HTML标签
|
||||||
content = content.replace(/<iframe[\s|\S]+iframe>/g, '')
|
content = content.replace(/<iframe[\s|\S]+iframe>/g, '')
|
||||||
content = content.replace(/<hr>/g, '')
|
content = content.replace(/<hr>/g, '')
|
||||||
content = content.replace(/<br>/g, '')
|
content = content.replace(/<br>/g, '')
|
||||||
// 图片
|
// 换行符换成空格
|
||||||
content = content.replace(/\!\[(.*?)\]\((.*?)\)/g, '')
|
content = content.replace(/\\n/g, ' ')
|
||||||
// 链接
|
content = content.replace(/\n/g, ' ')
|
||||||
content = content.replace(/\[(.*?)\]\((.*?)\)/g, '$1')
|
|
||||||
// 过滤代码块
|
|
||||||
if (cfg.codeblock == false) {
|
|
||||||
content = content.replace(/```([^`]+)```/g, '')
|
|
||||||
}
|
|
||||||
// 多个连续空格换成单个空格
|
// 多个连续空格换成单个空格
|
||||||
content = content.replace(/[\s]{2,} /g, ' ')
|
content = content.replace(/[\s]{2,}/g, ' ')
|
||||||
// 特殊字符
|
|
||||||
content = content.replace(/[\r|\n]+/g, '')
|
|
||||||
temp_post.content = content.trim()
|
temp_post.content = content.trim()
|
||||||
}
|
}
|
||||||
if (post.tags && post.tags.length > 0) {
|
if (post.tags && post.tags.length > 0) {
|
||||||
|
|
|
@ -54,13 +54,10 @@ var searchFunc = function(path, filter, searchId, contentId) {
|
||||||
}
|
}
|
||||||
// perform local searching
|
// perform local searching
|
||||||
datas.forEach(function(data) {
|
datas.forEach(function(data) {
|
||||||
if (!data.title?.trim().length) { return }
|
|
||||||
if (!data.content?.trim().length) { return }
|
if (!data.content?.trim().length) { return }
|
||||||
var matches = 0;
|
var matches = 0;
|
||||||
if (filter && !data.path.includes(filter)) {
|
if (filter && !data.path.includes(filter)) { return }
|
||||||
return;
|
var dataTitle = data.title?.trim() || 'Untitled';
|
||||||
}
|
|
||||||
var dataTitle = data.title.trim();
|
|
||||||
var dataTitleLowerCase = dataTitle.toLowerCase();
|
var dataTitleLowerCase = dataTitle.toLowerCase();
|
||||||
var dataContent = data.content;
|
var dataContent = data.content;
|
||||||
var dataContentLowerCase = dataContent.toLowerCase();
|
var dataContentLowerCase = dataContent.toLowerCase();
|
||||||
|
|
Loading…
Reference in New Issue