站内搜索优化

This commit is contained in:
结发受长生 2018-05-15 10:53:17 +08:00
parent ea8e8d6bb0
commit 8e47988504
3 changed files with 24 additions and 27 deletions

View File

@ -25,7 +25,7 @@
<div id="search-result-box" >
<ul class="search-result-list" v-if="searchResult.length">
<li v-for="(article,index) in searchResult" :key="index">
<a :href='article.url' class='search-result-title' target='_blank'>{{article.title}}</a>
<a :href='article.url' class='search-result-title' target='_blank'>{{article.title || '无标题'}}</a>
<p class="search-result" v-html="article.matchContent"></p>
</li>
</ul>

View File

@ -117,14 +117,15 @@ var JELON = window.JELON || {};
}
return format;
};
var htmlEncode = function (str) {
/**
* 过滤字符串中的style link script标签, 防止注入
* @param {String} str 需要处理的字符串
*/
var htmlFilter = function (str) {
if (typeof str !== 'string') return;
str = str.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
// .replace(/>/g, '&gt;')
.replace(/\"/g, '&quot;')
.replace(/\'/g, '&#39;')
.replace(/ /g, '&nbsp;');
str = str.replace(/(<style.*?<\/style>)/g, '')
.replace(/(<link.*\s+href=(?:"[^"]*"|'[^']*')[^<]*>)/g, '')
.replace(/<script.*?>.*?<\/script>/g, '');
return str;
};
@ -594,8 +595,7 @@ var JELON = window.JELON || {};
removeClass('JELON__editBox', 'show');
addClass('JELON__previewBox', 'show');
var text = $('JELON__editBox').value.trim();
// 安全转义
text = htmlEncode(text);
text = htmlFilter(text);
if (text) {
JL.Requests.markdown({
text: text,
@ -617,8 +617,7 @@ var JELON = window.JELON || {};
return;
}
var body = $('JELON__editBox').value.trim();
// 安全转义
body = htmlEncode(body);
body = htmlFilter(body);
if (body) {
JL.Renders.loading.create();
if (JL.issueNumber !== 0) {

View File

@ -49,22 +49,20 @@ new Vue({
var index_title = -1;
var index_content = -1;
var first_occur = -1; //关键字在正文当中第一次出现的位置
if(title && content) {
keywords.forEach(function(keyword, i) {
index_title = title.indexOf(keyword);
index_content = content.indexOf(keyword);
if( index_title < 0 && index_content < 0 ){
isMatch = false;
} else {
if (index_content < 0) {
index_content = 0;
}
if (i == 0) {
first_occur = index_content;
}
keywords.forEach(function(keyword, i) {
index_title = title ? title.indexOf(keyword) : -1;
index_content = content ? content.indexOf(keyword) : -1;
if( index_title < 0 && index_content < 0 ){
isMatch = false;
} else {
if (index_content < 0) {
index_content = 0;
}
});
}
if (i == 0) {
first_occur = index_content;
}
}
});
if (isMatch) {
var resultItem = {};
resultItem.url = article.url;