能否在搜索功能中增加关键词
dhndzwxj opened this issue · 3 comments
因为我的Hexo文章的头文件里面一般是有tags的,是否能增加关键词搜索功能呢?比如在搜索框内输入
“tags 数据结构”
就是要搜索全部带“数据结构”tags的文章呢?
效果如https://github.com/yelog/hexo-theme-3-hexo
我看他的代码是
if ($('#local-search-result').length>0) {
if (val.length>3 && (val.substr(0,3).toLowerCase() === 'in:' || val.substr(0,3).toLowerCase()==='in:')) {
$outlineList.hide();
$('#title-list-nav').hide()
$('#local-search-result').show();
searchAll(val.substr(3))
} else {
$('#title-list-nav').show();
$('#local-search-result').hide();
}
} else {
$outlineList.hide();
$('#title-list-nav').show();
}
var categories = $(".nav-left ul li>div.active").data('rel').split('<--->')
// 处理特殊字符
for (i = 0; i < categories.length; i++) {
categories[i] = categories[i]
.replace(/(?=/|\|#|(|)|[|]|.)/g, "\")
}
var activeTitle = categories.join('.');
var searchType = '';
var containType = '';
$('#no-item-tips').hide()
$(".nav-right nav a .post-title .search-keyword").each(function () {
$(this).parent().html($(this).parent().attr('title'))
})
if (val === "") {
$(".nav-right nav a").css("display", "none");
$(".nav-right nav a." + activeTitle).css("display", "block");
} else if (val.substr(0, 1) === "#") {
searchType = '标签'
containType = '为'
if (val.substr(1).length !== 0) {
$(".nav-right nav a").css("display", "none");
$(".nav-right nav").find("a." + activeTitle + ":contains_tag('" + val.substr(1) + "')").css("display", "block");
}
} else if (val.substr(0, 1) === "@") {
searchType = '作者'
containType= '为'
if (val.substr(1).length !== 0) {
$(".nav-right nav a").css("display", "none");
$(".nav-right nav").find("a." + activeTitle + ":contains_author('" + val.substr(1) + "')").css("display", "block");
}
} else {
searchType = '标题'
containType = '包含'
// $(".nav-right nav a").css("display", "none");
$(".nav-right nav").find("a." + activeTitle + ":"+ ($('#search-panel > .icon-case-sensitive').hasClass('active') ? 'containsSensitive' : 'contains') + "('" + val + "')").css("display", "block");
$(".nav-right nav a").each(function () {
var title = $(this).children('.post-title').attr('title');
for (i = 0; i < categories.length; i++) {
if (!$(this).hasClass(categories[i])) {
$(this).css('display', 'none').children('.post-title').html(title)
return true;
}
}
var caseSensitive = $('#search-panel > .icon-case-sensitive').hasClass('active');
var vals = (caseSensitive ? val : val.toUpperCase()).split('');
var inputReg = new RegExp(vals.join('[\\s\\S]*'));
if (inputReg.test(caseSensitive ? title : title.toUpperCase())) {
// 给匹配到的字符添加高亮
var nowPos = 0;
var titleHtml = title.split('')
var titleCase = (caseSensitive ? title : title.toUpperCase()).split('')
for (i = 0; i < vals.length; i++) {
nowPos = titleCase.indexOf(vals[i], nowPos)
titleHtml[nowPos] = ['<span class="search-keyword">', titleHtml[nowPos], '</span>'].join('')
}
$(this).css('display', 'block').children('.post-title').html(titleHtml.join(''))
} else {
$(this).css('display', 'none').children('.post-title').html(title)
}
})
}
//ejs文件
<% site.posts.forEach(function(post, i){ %>
<% if (post.hidden === true) { return true } %>
<a <% if(post.top){%>id="top"<%}%> class="<%= __('all_articles') %> <% post.categories.forEach(function(category, i){ %><%=category.name%> <% }) %>"
href="<%- url_for(post.path) %>"
data-tag="<% post.tags.forEach(function(tag, i){ %><%=tag.name%><% if (i+1<post.tags.length){%>,<%}})%>"
data-author="<% if(theme.author && theme.author.on==true && post.author) {%><%=post.author %><%}%>" >
<%=post.title %>
<%= date(post.date, 'YYYY/MM/DD') %>
<% }) %>
</div>
</nav>
/根据搜索条件,过滤文章列表/
function inputChange() {
var i;
setTimeout(function () {
$searchInput.focus()
}, 50)
var val = $searchInput.val().trim();
$('#search-panel').show().siblings().hide()
$outlineList.hide();
if ($('#local-search-result').length>0) {
if (val.length>3 && (val.substr(0,3).toLowerCase() === 'in:' || val.substr(0,3).toLowerCase()==='in:')) {
$outlineList.hide();
$('#title-list-nav').hide()
$('#local-search-result').show();
searchAll(val.substr(3))
} else {
$('#title-list-nav').show();
$('#local-search-result').hide();
}
} else {
$outlineList.hide();
$('#title-list-nav').show();
}
var categories = $(".nav-left ul li>div.active").data('rel').split('<--->')
// 处理特殊字符
for (i = 0; i < categories.length; i++) {
categories[i] = categories[i]
.replace(/(?=/|\|#|(|)|[|]|.)/g, "\")
}
var activeTitle = categories.join('.');
var searchType = '';
var containType = '';
$('#no-item-tips').hide()
$(".nav-right nav a .post-title .search-keyword").each(function () {
$(this).parent().html($(this).parent().attr('title'))
})
if (val === "") {
$(".nav-right nav a").css("display", "none");
$(".nav-right nav a." + activeTitle).css("display", "block");
} else if (val.substr(0, 1) === "#") {
searchType = '标签'
containType = '为'
if (val.substr(1).length !== 0) {
$(".nav-right nav a").css("display", "none");
$(".nav-right nav").find("a." + activeTitle + ":contains_tag('" + val.substr(1) + "')").css("display", "block");
}
} else if (val.substr(0, 1) === "@") {
searchType = '作者'
containType= '为'
if (val.substr(1).length !== 0) {
$(".nav-right nav a").css("display", "none");
$(".nav-right nav").find("a." + activeTitle + ":contains_author('" + val.substr(1) + "')").css("display", "block");
}
} else {
searchType = '标题'
containType = '包含'
// $(".nav-right nav a").css("display", "none");
$(".nav-right nav").find("a." + activeTitle + ":"+ ($('#search-panel > .icon-case-sensitive').hasClass('active') ? 'containsSensitive' : 'contains') + "('" + val + "')").css("display", "block");
$(".nav-right nav a").each(function () {
var title = $(this).children('.post-title').attr('title');
for (i = 0; i < categories.length; i++) {
if (!$(this).hasClass(categories[i])) {
$(this).css('display', 'none').children('.post-title').html(title)
return true;
}
}
var caseSensitive = $('#search-panel > .icon-case-sensitive').hasClass('active');
var vals = (caseSensitive ? val : val.toUpperCase()).split('');
var inputReg = new RegExp(vals.join('[\\s\\S]*'));
if (inputReg.test(caseSensitive ? title : title.toUpperCase())) {
// 给匹配到的字符添加高亮
var nowPos = 0;
var titleHtml = title.split('')
var titleCase = (caseSensitive ? title : title.toUpperCase()).split('')
for (i = 0; i < vals.length; i++) {
nowPos = titleCase.indexOf(vals[i], nowPos)
titleHtml[nowPos] = ['<span class="search-keyword">', titleHtml[nowPos], '</span>'].join('')
}
$(this).css('display', 'block').children('.post-title').html(titleHtml.join(''))
} else {
$(this).css('display', 'none').children('.post-title').html(title)
}
})
}
if (val !== '') {
$('#default-panel .icon-search').addClass('active')
if (val === 'in:') {
$('#no-item-tips').show().html('正在进行全局关键字搜索,请输入关键字');
} else if (!val.startsWith('in:') && $(".nav-right nav a:visible").length === 0) {
$('#no-item-tips').show().html('未在 <span>' + activeTitle + '</span> 分类中找到'+ searchType + containType + ' <span>' + val.replace(/^[@|#]/g,'') + '</span> 的文章');
}
} else {
$('#default-panel .icon-search').removeClass('active')
}
}