Skip to content

Commit

Permalink
Merge pull request #910 from pxgo/latest
Browse files Browse the repository at this point in the history
修复
  • Loading branch information
pxgo authored Aug 4, 2023
2 parents c36c91b + 23ecc6b commit d349101
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
12 changes: 12 additions & 0 deletions nkcModules/elasticSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const elasticSearch = require('../settings/elasticSearch');
const client = elasticSearch();
const { ThrowCommonError } = require('../nkcModules/error');
const esConfig = require('../config/elasticSearch');
const cheerio = require('cheerio');
const filterSearchContent = require('./xssFilters/filterSearchContent');

const { analyzer, searchAnalyzer, indexName } = esConfig;

Expand Down Expand Up @@ -794,6 +796,16 @@ func.updateThreadForums = async (thread) => {
});
};

func.replaceSearchResultHTMLLink = (content = '') => {
const nkcRender = require('./nkcRender');
let html = content;
const $ = cheerio.load(html);
const body = $('body');
nkcRender.replaceLinkInfo($, body[0]);
html = body.html();
return filterSearchContent(html);
};

module.exports = func;

function createMatch(property, query, boost, relation) {
Expand Down
9 changes: 4 additions & 5 deletions nkcModules/nkcRender/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,13 @@ class NKCRender {
}
}

// 替换文本中的链接为span标签,标签内容为xxx,前端再根据span属性中的链接信息恢复链接
replaceTextLinkToHTML(content = '') {
let html = content;
const $ = cheerio.load(html);

const $ = cheerio.load('');
const body = $('body');
body.text(content);
this.replaceLinkInfo($, body[0]);

html = body.html();
const html = body.html();
return htmlFilter(html);
}

Expand Down
17 changes: 17 additions & 0 deletions nkcModules/xssFilters/filterSearchContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const xss = require('xss');

module.exports = (html) => {
html = xss(html, {
whiteList: {
span: ['style', 'data-type', 'data-url'],
},
onTagAttr: function (tag, name, value) {
if (tag === 'span' && name === 'style') {
if (value !== 'color: #e85a71;') {
return '';
}
}
},
});
return html;
};
1 change: 1 addition & 0 deletions nkcModules/xssFilters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
filterAllHTML: require('./filterAllHTML'),
filterEditorContent: require('./filterEditorContent'),
filterMessageContent: require('./filterMessageContent'),
filterSearchContent: require('./filterSearchContent'),
};
2 changes: 1 addition & 1 deletion pages/thread/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ block content
.h4.text-center(style='color: #ff5a0b;') 本文已被退回修改,请作者点击编辑按钮进入编辑界面
p.text-center(style='border-bottom: 1px solid #ff5a0b; padding-bottom: 1rem;color: #ff5a0b;')= `退修原因:${thread.reason}`
if thread.type !== "fund"
.h3.thread-title.text-center= thread.firstPost.t
.h3.thread-title.text-center!= thread.firstPost.t
-const post = thread.firstPost;
if post.authorInfos && post.authorInfos.length > 0 && data.thread.type !== "product"
-var agencyIndex = 1;
Expand Down
34 changes: 19 additions & 15 deletions routes/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ router.get('/', async (ctx, next) => {
source: sources.search,
});

const { nkcRender } = nkcModules;
const { elasticSearch } = nkcModules;
let { page = 0, t = '', c = '', d = '', form = '' } = query;
const { user } = data;
// 通过mongodb精准搜索用户名
Expand Down Expand Up @@ -482,8 +482,8 @@ router.get('/', async (ctx, next) => {
anonymous: post.anonymous,
forums,
};
r.title = nkcRender.replaceTextLinkToHTML(r.title + '');
r.abstract = nkcRender.replaceTextLinkToHTML(r.abstract + '');
r.title = elasticSearch.replaceSearchResultHTMLLink(r.title + '');
r.abstract = elasticSearch.replaceSearchResultHTMLLink(r.abstract + '');
if (!post.anonymous) {
r.postUser = {
uid: postUser.uid,
Expand Down Expand Up @@ -514,8 +514,10 @@ router.get('/', async (ctx, next) => {
user: u,
uid: highlightObj[`${uid}_uid`] || u.uid,
};
r.username = nkcRender.replaceTextLinkToHTML(r.username + '');
r.description = nkcRender.replaceTextLinkToHTML(r.description + '');
r.username = elasticSearch.replaceSearchResultHTMLLink(r.username + '');
r.description = elasticSearch.replaceSearchResultHTMLLink(
r.description + '',
);
} else if (docType === 'column') {
const column = columnObj[tid];
if (!column) {
Expand All @@ -532,8 +534,8 @@ router.get('/', async (ctx, next) => {
abbr: highlightObj[`${tid}_abbr`] || column.abbr,
column,
};
r.name = nkcRender.replaceTextLinkToHTML(r.name + '');
r.abbr = nkcRender.replaceTextLinkToHTML(r.abbr + '');
r.name = elasticSearch.replaceSearchResultHTMLLink(r.name + '');
r.abbr = elasticSearch.replaceSearchResultHTMLLink(r.abbr + '');
} else if (docType === 'columnPage') {
const page = columnPageObj[tid];
if (!page) {
Expand All @@ -556,8 +558,8 @@ router.get('/', async (ctx, next) => {
column,
page,
};
r.t = nkcRender.replaceTextLinkToHTML(r.t + '');
r.c = nkcRender.replaceTextLinkToHTML(r.c + '');
r.t = elasticSearch.replaceSearchResultHTMLLink(r.t + '');
r.c = elasticSearch.replaceSearchResultHTMLLink(r.c + '');
} else if (docType === 'resource') {
let resource = resourcesObj[tid];
if (!resource) {
Expand All @@ -571,8 +573,8 @@ router.get('/', async (ctx, next) => {
c: highlightObj[`${tid}_c`] || resource.description,
resource,
};
r.t = nkcRender.replaceTextLinkToHTML(r.t + '');
r.c = nkcRender.replaceTextLinkToHTML(r.c + '');
r.t = elasticSearch.replaceSearchResultHTMLLink(r.t + '');
r.c = elasticSearch.replaceSearchResultHTMLLink(r.c + '');
} else if (docType === 'document_article') {
//article文章搜索
const article = articlesObj[tid];
Expand Down Expand Up @@ -621,8 +623,8 @@ router.get('/', async (ctx, next) => {
if (column) {
r.column = column;
}
r.title = nkcRender.replaceTextLinkToHTML(r.title + '');
r.abstract = nkcRender.replaceTextLinkToHTML(r.abstract + '');
r.title = elasticSearch.replaceSearchResultHTMLLink(r.title + '');
r.abstract = elasticSearch.replaceSearchResultHTMLLink(r.abstract + '');
} else if (docType === 'document_comment') {
//comment搜索
const comment = commentObj[tid];
Expand Down Expand Up @@ -655,8 +657,10 @@ router.get('/', async (ctx, next) => {
commentTime: commentDocument.toc,
user: commentUser,
};
r.articleTitle = nkcRender.replaceTextLinkToHTML(r.articleTitle + '');
r.abstract = nkcRender.replaceTextLinkToHTML(r.abstract + '');
r.articleTitle = elasticSearch.replaceSearchResultHTMLLink(
r.articleTitle + '',
);
r.abstract = elasticSearch.replaceSearchResultHTMLLink(r.abstract + '');
if (!commentDocument.anonymous) {
r.commentUser = {
uid: commentUser.uid,
Expand Down

0 comments on commit d349101

Please sign in to comment.