更改搜索功能

This commit is contained in:
j m 2024-08-04 07:23:47 +08:00
parent df8c7589d7
commit da4b5c8c36
3 changed files with 26 additions and 20 deletions

View File

@ -22,8 +22,8 @@ $(function () {
})
function removeHTMLTags(str) {
// 保留 <mark> 和 </mark> 标签,移除其他所有标签
return str.replace(/<(?!(?:mark\b[^<>]*)<\/mark>|mark\b)[^<>]*>/gi, '')
const regex = /<(?!\/?mark\b)[^>]*>/g
return str.replace(regex, '')
}
function findResult(keyword) {
@ -44,26 +44,32 @@ $(function () {
}
})
.then((_res) => {
console.log(_res)
if (_res.hits.length > 0) {
searchResultEmpty.hide()
searchResult.empty()
for (var i = 0; i < _res.hits.length; i++) {
var hit = _res.hits[i]
searchResult.append('<div class="widget card search">\n' +
'<div class="card-content main">\n' +
'<a href="' + hit.permalink + '" ' + ' target="' + target + '">\n' +
'<h2 class="title">' + removeHTMLTags(hit.title) + '</h2>\n' +
'</a>\n' +
'<div class="main-content not-toc description">\n' +
removeHTMLTags(hit.description) +
'\n</div>\n' +
'<hr/>\n' +
'<div class="meta">\n' +
'<div></div>\n' +
'<em text="最后更新于 ' + Utils.formatDate(hit.updateTimestamp, 'yyyy年MM月dd日 HH:mm:ss') + '"></em>\n' +
'</div>\n' +
'</div>\n')
try {
var hit = _res.hits[i]
var title = removeHTMLTags(hit.title)
var description = hit.content ? removeHTMLTags(hit.content) : ''
searchResult.append('<div class="widget card search">\n' +
'<div class="card-content main">\n' +
'<a href="' + hit.permalink + '" ' + ' target="' + target + '">\n' +
'<h2 class="title">' + title + '</h2>\n' +
'</a>\n' +
(description ?
('<div class="main-content not-toc description">\n' + description +
'\n</div>\n') : '') +
'<hr/>\n' +
'<div class="meta">\n' +
'<div></div>\n' +
'<em text="最后更新于 ' + Utils.formatDate(hit.updateTimestamp, 'yyyy年MM月dd日 HH:mm:ss') + '"></em>\n' +
'</div>\n' +
'</div>\n')
// eslint-disable-next-line no-empty
} catch (e) {
}
}
} else {
searchResultEmpty.show()

View File

@ -1 +1 @@
$(function(){var e,t=document.getElementById("dream-search-form"),a=DreamConfig.search_target,n=document.getElementById("halo-search-form-text-input"),i=$("#dream-search-result"),r=$("#dream-search-result-empty");function s(e){return e.replace(/<(?!(?:mark\b[^<>]*)<\/mark>|mark\b)[^<>]*>/gi,"")}function c(e){if(!e)return i.empty(),r.show();Utils.request({url:"/apis/api.halo.run/v1alpha1/indices/post",contentType:"application/json;charset=UTF-8",returnRaw:!0,data:{keyword:e,limit:$("#halo-search-form-limit").val(),highlightPreTag:"<mark>",highlightPostTag:"</mark>"}}).then(e=>{if(console.log(e),0<e.hits.length){r.hide(),i.empty();for(var t=0;t<e.hits.length;t++){var n=e.hits[t];i.append('<div class="widget card search">\n<div class="card-content main">\n<a href="'+n.permalink+'" target="'+a+'">\n<h2 class="title">'+s(n.title)+'</h2>\n</a>\n<div class="main-content not-toc description">\n'+s(n.description)+'\n</div>\n<hr/>\n<div class="meta">\n<div></div>\n<em text="最后更新于 '+Utils.formatDate(n.updateTimestamp,"yyyy年MM月dd日 HH:mm:ss")+'"></em>\n</div>\n</div>\n')}}else r.show(),i.empty()}).catch(e=>{})}t.addEventListener("submit",function(e){e.preventDefault(),c(n.value)}),document.addEventListener("keydown",function(e){"Enter"===e.key&&t.contains(document.activeElement)&&(e.preventDefault(),c(n.value))}),n.addEventListener("input",function(e){c(e.target.value)}),function(e,t=window.location.href){e="keyword".replace(/[\[\]]/g,"\\$&");e=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(t);return e&&e[2]&&decodeURIComponent(e[2].replace(/\+/g," "))}()&&(e=new Event("input",{bubbles:!0}),n.dispatchEvent(e))});
$(function(){var e,t=document.getElementById("dream-search-form"),r=DreamConfig.search_target,n=document.getElementById("halo-search-form-text-input"),c=$("#dream-search-result"),s=$("#dream-search-result-empty");function o(e){return e.replace(/<(?!\/?mark\b)[^>]*>/g,"")}function a(e){if(!e)return c.empty(),s.show();Utils.request({url:"/apis/api.halo.run/v1alpha1/indices/post",contentType:"application/json;charset=UTF-8",returnRaw:!0,data:{keyword:e,limit:$("#halo-search-form-limit").val(),highlightPreTag:"<mark>",highlightPostTag:"</mark>"}}).then(e=>{if(0<e.hits.length){s.hide(),c.empty();for(var t=0;t<e.hits.length;t++)try{var n=e.hits[t],a=o(n.title),i=n.content?o(n.content):"";c.append('<div class="widget card search">\n<div class="card-content main">\n<a href="'+n.permalink+'" target="'+r+'">\n<h2 class="title">'+a+"</h2>\n</a>\n"+(i?'<div class="main-content not-toc description">\n'+i+"\n</div>\n":"")+'<hr/>\n<div class="meta">\n<div></div>\n<em text="最后更新于 '+Utils.formatDate(n.updateTimestamp,"yyyy年MM月dd日 HH:mm:ss")+'"></em>\n</div>\n</div>\n')}catch(e){}}else s.show(),c.empty()}).catch(e=>{})}t.addEventListener("submit",function(e){e.preventDefault(),a(n.value)}),document.addEventListener("keydown",function(e){"Enter"===e.key&&t.contains(document.activeElement)&&(e.preventDefault(),a(n.value))}),n.addEventListener("input",function(e){a(e.target.value)}),function(e,t=window.location.href){e="keyword".replace(/[\[\]]/g,"\\$&");e=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(t);return e&&e[2]&&decodeURIComponent(e[2].replace(/\+/g," "))}()&&(e=new Event("input",{bubbles:!0}),n.dispatchEvent(e))});

View File

@ -30,7 +30,7 @@ spec:
settingName: theme-dream2-plus-setting
configMapName: theme-dream2-plus-configMap
# 版本号
version: 1.2.7.beta4
version: 1.2.7.beta5
# 最低支持的 Halo 版本
require: ">=2.15.0"
# 许可