mirror of
https://ghfast.top/https://github.com/zsjy/halo-theme-dream2.0-plus.git
synced 2025-03-16 00:49:40 +08:00
更改搜索功能
This commit is contained in:
parent
df8c7589d7
commit
da4b5c8c36
@ -22,8 +22,8 @@ $(function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
function removeHTMLTags(str) {
|
function removeHTMLTags(str) {
|
||||||
// 保留 <mark> 和 </mark> 标签,移除其他所有标签
|
const regex = /<(?!\/?mark\b)[^>]*>/g
|
||||||
return str.replace(/<(?!(?:mark\b[^<>]*)<\/mark>|mark\b)[^<>]*>/gi, '')
|
return str.replace(regex, '')
|
||||||
}
|
}
|
||||||
|
|
||||||
function findResult(keyword) {
|
function findResult(keyword) {
|
||||||
@ -44,26 +44,32 @@ $(function () {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then((_res) => {
|
.then((_res) => {
|
||||||
console.log(_res)
|
|
||||||
if (_res.hits.length > 0) {
|
if (_res.hits.length > 0) {
|
||||||
searchResultEmpty.hide()
|
searchResultEmpty.hide()
|
||||||
searchResult.empty()
|
searchResult.empty()
|
||||||
for (var i = 0; i < _res.hits.length; i++) {
|
for (var i = 0; i < _res.hits.length; i++) {
|
||||||
|
try {
|
||||||
var hit = _res.hits[i]
|
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' +
|
searchResult.append('<div class="widget card search">\n' +
|
||||||
'<div class="card-content main">\n' +
|
'<div class="card-content main">\n' +
|
||||||
'<a href="' + hit.permalink + '" ' + ' target="' + target + '">\n' +
|
'<a href="' + hit.permalink + '" ' + ' target="' + target + '">\n' +
|
||||||
'<h2 class="title">' + removeHTMLTags(hit.title) + '</h2>\n' +
|
'<h2 class="title">' + title + '</h2>\n' +
|
||||||
'</a>\n' +
|
'</a>\n' +
|
||||||
'<div class="main-content not-toc description">\n' +
|
(description ?
|
||||||
removeHTMLTags(hit.description) +
|
('<div class="main-content not-toc description">\n' + description +
|
||||||
'\n</div>\n' +
|
'\n</div>\n') : '') +
|
||||||
'<hr/>\n' +
|
'<hr/>\n' +
|
||||||
'<div class="meta">\n' +
|
'<div class="meta">\n' +
|
||||||
'<div></div>\n' +
|
'<div></div>\n' +
|
||||||
'<em text="最后更新于 ' + Utils.formatDate(hit.updateTimestamp, 'yyyy年MM月dd日 HH:mm:ss') + '"></em>\n' +
|
'<em text="最后更新于 ' + Utils.formatDate(hit.updateTimestamp, 'yyyy年MM月dd日 HH:mm:ss') + '"></em>\n' +
|
||||||
'</div>\n' +
|
'</div>\n' +
|
||||||
'</div>\n')
|
'</div>\n')
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
searchResultEmpty.show()
|
searchResultEmpty.show()
|
||||||
|
2
templates/assets/js/search.min.js
vendored
2
templates/assets/js/search.min.js
vendored
@ -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))});
|
@ -30,7 +30,7 @@ spec:
|
|||||||
settingName: theme-dream2-plus-setting
|
settingName: theme-dream2-plus-setting
|
||||||
configMapName: theme-dream2-plus-configMap
|
configMapName: theme-dream2-plus-configMap
|
||||||
# 版本号
|
# 版本号
|
||||||
version: 1.2.7.beta4
|
version: 1.2.7.beta5
|
||||||
# 最低支持的 Halo 版本
|
# 最低支持的 Halo 版本
|
||||||
require: ">=2.15.0"
|
require: ">=2.15.0"
|
||||||
# 许可
|
# 许可
|
||||||
|
Loading…
x
Reference in New Issue
Block a user