优化文档搜索词高亮方案

This commit is contained in:
yangjian 2023-04-04 10:52:04 +08:00
parent 0550db4697
commit bb3c0cd9ba
5 changed files with 16 additions and 18 deletions

7
static/markjs/mark.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -550,20 +550,7 @@ function keyLight(id, key, bgColor){
// console.log(id,key,decodeURI(key))
if(key != false){
key = decodeURI(key);
var oDiv = document.getElementById(id),
sText = oDiv.innerHTML,
num = -1,
rStr = new RegExp(key, "ig"),
rHtml = new RegExp("\<.*?\>","ig"), //匹配html元素
aHtml = sText.match(rHtml), //存放html元素的数组
sText = sText.replace(rHtml, '{~}'); //替换html标签
sText = sText.replace(rStr,function(text){
return "<mark>" + text +"</mark>"
}); //替换key
sText = sText.replace(/{~}/g,function(){ //恢复html标签
num++;
return aHtml[num];
});
oDiv.innerHTML = sText;
var markInstance = new Mark(document.getElementById(id));
markInstance.mark(key);
}
};

View File

@ -305,6 +305,7 @@
<script src="{% static 'viewerjs/viewer.min.js' %}?version={{mrdoc_version}}"></script>
<script src="{% static 'qrcodejs/qrcode.min.js' %}?version={{mrdoc_version}}"></script>
<script src="{% static 'darkmode-js/darkmode-js.min.js' %}?version={{mrdoc_version}}"></script>
<script src="{% static '/markjs/mark.min.js' %}?version={{mrdoc_version}}" charset="UTF-8"></script>
<script src="{% static 'mrdoc/mrdoc-docs.js' %}?version={{mrdoc_version}}"></script>
<script src="{% static 'iconFont/iconfont.js' %}?version={{mrdoc_version}}"></script>
<script src="{% static 'editor.md/lib/purify.min.js' %}?version={{mrdoc_version}}"></script>
@ -455,7 +456,7 @@
{% if doc.editor_mode != 2 %}
setTimeout(function(){
keyLight('doc-content',getQueryVariable("highlight"))
},1000)
},500)
{% endif %}
</script>
</body>

View File

@ -22,7 +22,7 @@
{% block page_content %}
{% for result in search_result %}
<div>
<p><a href="{% url 'doc_id' doc_id=result.id %}" target="_blank">{{ result.name }}</a> - <span style="font-size: 14px;color: #999;">{{result.modify_time}}</span></p>
<p><a href="{% url 'doc_id' doc_id=result.id %}?highlight={{kw}}" target="_blank">{{ result.name }}</a> - <span style="font-size: 14px;color: #999;">{{result.modify_time}}</span></p>
<p>{{ result|remove_doc_tag }}</p>
</div>
<hr>
@ -31,6 +31,8 @@
{% block custom_script %}
<script>
keyLight('doc-content','{{kw}}')
setTimeout(function(){
keyLight('doc-content','{{kw}}')
},500)
</script>
{% endblock %}

View File

@ -275,6 +275,7 @@
<script src="{% static 'jquery/3.1.1/jquery.min.js' %}"></script>
<script src="{% static 'layui/layui.js' %}"></script>
<script src="{% static '/markjs/mark.min.js' %}?version={{mrdoc_version}}" charset="UTF-8"></script>
{% block custom_script %}
<script>
$.ajaxSetup({