mirror of
https://github.com/flarum/framework.git
synced 2025-05-23 23:29:57 +08:00
Fix discussion search result suggestion excerpts
So that they show the relevant part of the post
This commit is contained in:
@ -29,7 +29,7 @@ export default class DiscussionsSearchResults {
|
|||||||
return m('li.discussion-search-result', {'data-index': 'discussions'+discussion.id()},
|
return m('li.discussion-search-result', {'data-index': 'discussions'+discussion.id()},
|
||||||
m('a', { href: app.route.discussion(discussion, post && post.number()), config: m.route },
|
m('a', { href: app.route.discussion(discussion, post && post.number()), config: m.route },
|
||||||
m('div.title', highlight(discussion.title(), string)),
|
m('div.title', highlight(discussion.title(), string)),
|
||||||
post ? m('div.excerpt', highlight(truncate(post.contentPlain(), 100), string)) : ''
|
post ? m('div.excerpt', highlight(post.contentPlain(), string, 100)) : ''
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}) : ''
|
}) : ''
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
export default function(string, regexp) {
|
import truncate from '../utils/truncate';
|
||||||
if (!regexp) {
|
|
||||||
|
export default function(string, phrase, length) {
|
||||||
|
if (!phrase) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(regexp instanceof RegExp)) {
|
const regexp = regexp instanceof RegExp ? phrase : new RegExp(phrase, 'gi');
|
||||||
regexp = new RegExp(regexp, 'gi');
|
|
||||||
|
let highlightedString = string;
|
||||||
|
let start = 0;
|
||||||
|
|
||||||
|
if (length) {
|
||||||
|
start = Math.max(0, string.search(regexp) - length / 2);
|
||||||
|
highlightedString = truncate(highlightedString, length, start);
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.trust(
|
highlightedString = $('<div/>').text(highlightedString).html().replace(regexp, '<mark>$&</mark>');
|
||||||
$('<div/>').text(string).html().replace(regexp, '<mark>$&</mark>')
|
|
||||||
);
|
return m.trust(highlightedString);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
export default function truncate(string, length, start) {
|
export default function truncate(string = '', length, start = 0) {
|
||||||
start = start || 0;
|
return (start > 0 ? '...' : '') +
|
||||||
string = string || '';
|
string.substring(start, start + length) +
|
||||||
|
(string.length > start + length ? '...' : '');
|
||||||
return (start > 0 ? '...' : '')+string.substring(start, start + length)+(string.length > start + length ? '...' : '');
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user