mirror of
https://github.com/flarum/framework.git
synced 2025-05-23 07:09:57 +08:00
Fix discussion search result suggestion excerpts
So that they show the relevant part of the post
This commit is contained in:
@ -1,13 +1,21 @@
|
||||
export default function(string, regexp) {
|
||||
if (!regexp) {
|
||||
import truncate from '../utils/truncate';
|
||||
|
||||
export default function(string, phrase, length) {
|
||||
if (!phrase) {
|
||||
return string;
|
||||
}
|
||||
|
||||
if (!(regexp instanceof RegExp)) {
|
||||
regexp = new RegExp(regexp, 'gi');
|
||||
const regexp = regexp instanceof RegExp ? phrase : new RegExp(phrase, '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(
|
||||
$('<div/>').text(string).html().replace(regexp, '<mark>$&</mark>')
|
||||
);
|
||||
highlightedString = $('<div/>').text(highlightedString).html().replace(regexp, '<mark>$&</mark>');
|
||||
|
||||
return m.trust(highlightedString);
|
||||
}
|
||||
|
Reference in New Issue
Block a user