站内搜索优化

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" > <div id="search-result-box" >
<ul class="search-result-list" v-if="searchResult.length"> <ul class="search-result-list" v-if="searchResult.length">
<li v-for="(article,index) in searchResult" :key="index"> <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> <p class="search-result" v-html="article.matchContent"></p>
</li> </li>
</ul> </ul>

View File

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

View File

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