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
f2da47f047
commit
729746179b
@ -1,97 +0,0 @@
|
|||||||
$(function () {
|
|
||||||
var searchForm = document.getElementById('dream-search-form')
|
|
||||||
var target = DreamConfig.search_target
|
|
||||||
var searchInput = document.getElementById('halo-search-form-text-input')
|
|
||||||
var searchResult = $('#dream-search-result')
|
|
||||||
var searchResultEmpty = $('#dream-search-result-empty')
|
|
||||||
searchForm.addEventListener('submit', function (event) {
|
|
||||||
event.preventDefault()
|
|
||||||
findResult(searchInput.value)
|
|
||||||
})
|
|
||||||
document.addEventListener('keydown', function (event) {
|
|
||||||
if (event.key === 'Enter') {
|
|
||||||
if (searchForm.contains(document.activeElement)) {
|
|
||||||
event.preventDefault()
|
|
||||||
findResult(searchInput.value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// 监听输入事件
|
|
||||||
searchInput.addEventListener('input', function (event) {
|
|
||||||
findResult(event.target.value)
|
|
||||||
})
|
|
||||||
|
|
||||||
function removeHTMLTags(str) {
|
|
||||||
const regex = /<(?!\/?mark\b)[^>]*>/g
|
|
||||||
return str.replace(regex, '')
|
|
||||||
}
|
|
||||||
|
|
||||||
function findResult(keyword) {
|
|
||||||
if (!keyword) {
|
|
||||||
searchResult.empty()
|
|
||||||
searchResultEmpty.show()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Utils.request({
|
|
||||||
url: '/apis/api.halo.run/v1alpha1/indices/post',
|
|
||||||
contentType: 'application/json;charset=UTF-8',
|
|
||||||
returnRaw: true,
|
|
||||||
data: {
|
|
||||||
keyword,
|
|
||||||
limit: $('#halo-search-form-limit').val(),
|
|
||||||
highlightPreTag: '<mark>',
|
|
||||||
highlightPostTag: '</mark>'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((_res) => {
|
|
||||||
if (_res.hits.length > 0) {
|
|
||||||
searchResultEmpty.hide()
|
|
||||||
searchResult.empty()
|
|
||||||
for (var i = 0; i < _res.hits.length; i++) {
|
|
||||||
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()
|
|
||||||
searchResult.empty()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function getParameterByName(name, url = window.location.href) {
|
|
||||||
name = name.replace(/[\[\]]/g, '\\$&')
|
|
||||||
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
|
|
||||||
results = regex.exec(url)
|
|
||||||
if (!results) return null
|
|
||||||
if (!results[2]) return ''
|
|
||||||
return decodeURIComponent(results[2].replace(/\+/g, ' '))
|
|
||||||
}
|
|
||||||
|
|
||||||
var keyword = getParameterByName('keyword')
|
|
||||||
if (keyword) {
|
|
||||||
var event = new Event('input', {bubbles: true})
|
|
||||||
searchInput.dispatchEvent(event)
|
|
||||||
}
|
|
||||||
})
|
|
1
templates/assets/js/search.min.js
vendored
1
templates/assets/js/search.min.js
vendored
@ -1 +0,0 @@
|
|||||||
$(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))});
|
|
@ -106,7 +106,6 @@
|
|||||||
DreamConfig["enable_security_link"] = [[${theme.config.security_link_config.enable_security_link}]];
|
DreamConfig["enable_security_link"] = [[${theme.config.security_link_config.enable_security_link}]];
|
||||||
DreamConfig["security_link_url"] = [[${theme.config.security_link_config.security_link_url}]];
|
DreamConfig["security_link_url"] = [[${theme.config.security_link_config.security_link_url}]];
|
||||||
|
|
||||||
DreamConfig["search_target"] = [[${theme.config.page_config.search.search_target}]];
|
|
||||||
DreamConfig["theme_version"] = [[${theme.spec.version}]];
|
DreamConfig["theme_version"] = [[${theme.spec.version}]];
|
||||||
DreamConfig["theme_base"] = "[(${#theme.assets('/')})]";
|
DreamConfig["theme_base"] = "[(${#theme.assets('/')})]";
|
||||||
[(${!#strings.isEmpty(theme.config.post.code_fold_line)?'DreamConfig["code_fold_line"] = "' + theme.config.post.code_fold_line + '";': ''})]
|
[(${!#strings.isEmpty(theme.config.post.code_fold_line)?'DreamConfig["code_fold_line"] = "' + theme.config.post.code_fold_line + '";': ''})]
|
||||||
|
@ -9,7 +9,105 @@
|
|||||||
<div>[[${#strings.replace(title, ' - ' + site.title, '')}]]</div>
|
<div>[[${#strings.replace(title, ' - ' + site.title, '')}]]</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="search-box">
|
<div class="search-box">
|
||||||
<script th:src="@{/assets/js/search.min.js(mew=${theme.spec.version})}"></script>
|
<script th:inline="javascript">
|
||||||
|
$(function () {
|
||||||
|
var searchForm = document.getElementById('dream-search-form')
|
||||||
|
var target = [[${theme.config.page_config.search.search_target}]]
|
||||||
|
var searchInput = document.getElementById('halo-search-form-text-input')
|
||||||
|
var searchResult = $('#dream-search-result')
|
||||||
|
var searchResultEmpty = $('#dream-search-result-empty')
|
||||||
|
searchForm.addEventListener('submit', function (event) {
|
||||||
|
event.preventDefault()
|
||||||
|
findResult(searchInput.value)
|
||||||
|
})
|
||||||
|
document.addEventListener('keydown', function (event) {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
if (searchForm.contains(document.activeElement)) {
|
||||||
|
event.preventDefault()
|
||||||
|
findResult(searchInput.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// 监听输入事件
|
||||||
|
searchInput.addEventListener('input', function (event) {
|
||||||
|
findResult(event.target.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
function removeHTMLTags(str) {
|
||||||
|
const regex = /<(?!\/?mark\b)[^>]*>/g
|
||||||
|
return str.replace(regex, '')
|
||||||
|
}
|
||||||
|
|
||||||
|
function findResult(keyword) {
|
||||||
|
if (!keyword) {
|
||||||
|
searchResult.empty()
|
||||||
|
searchResultEmpty.show()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Utils.request({
|
||||||
|
url: '/apis/api.halo.run/v1alpha1/indices/post',
|
||||||
|
contentType: 'application/json;charset=UTF-8',
|
||||||
|
returnRaw: true,
|
||||||
|
data: {
|
||||||
|
keyword,
|
||||||
|
limit: $('#halo-search-form-limit').val(),
|
||||||
|
highlightPreTag: '<mark>',
|
||||||
|
highlightPostTag: '</mark>'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((_res) => {
|
||||||
|
if (_res.hits.length > 0) {
|
||||||
|
searchResultEmpty.hide()
|
||||||
|
searchResult.empty()
|
||||||
|
for (var i = 0; i < _res.hits.length; i++) {
|
||||||
|
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()
|
||||||
|
searchResult.empty()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function getParameterByName(name, url = window.location.href) {
|
||||||
|
name = name.replace(/[\[\]]/g, '\\$&')
|
||||||
|
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
|
||||||
|
results = regex.exec(url)
|
||||||
|
if (!results) return null
|
||||||
|
if (!results[2]) return ''
|
||||||
|
return decodeURIComponent(results[2].replace(/\+/g, ' '))
|
||||||
|
}
|
||||||
|
|
||||||
|
var keyword = getParameterByName('keyword')
|
||||||
|
if (keyword) {
|
||||||
|
var event = new Event('input', {bubbles: true})
|
||||||
|
searchInput.dispatchEvent(event)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
||||||
<form id="dream-search-form" class="search-form-inner" method="get" action="/search" role="search">
|
<form id="dream-search-form" class="search-form-inner" method="get" action="/search" role="search">
|
||||||
<input id="halo-search-form-limit" type="hidden" name="limit"
|
<input id="halo-search-form-limit" type="hidden" name="limit"
|
||||||
th:value="${theme.config.page_config.search.search_limit}">
|
th:value="${theme.config.page_config.search.search_limit}">
|
||||||
|
@ -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.beta5
|
version: 1.2.7.beta6
|
||||||
# 最低支持的 Halo 版本
|
# 最低支持的 Halo 版本
|
||||||
require: ">=2.15.0"
|
require: ">=2.15.0"
|
||||||
# 许可
|
# 许可
|
||||||
|
Loading…
x
Reference in New Issue
Block a user