Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Cherry-pick from 2.4.0) Fix: Contextual excerpt crash on tag search (#2529) #2533

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions lib/utils/note-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,27 @@ const getPreview = (content: string, searchQuery?: string) => {
const terms = getTerms(searchQuery);

// use only the first term of a multi-term query
const firstTerm = terms[0].toLocaleLowerCase();
const leadingChars = 30 - firstTerm.length;

// prettier-ignore
const regExp = new RegExp(
'(?<=\\s|^)[^\n]' + // split at a word boundary (pattern must be preceded by whitespace or beginning of string)
'{0,' + leadingChars + '}' + // up to leadingChars of text before the match
escapeRegExp(firstTerm) +
'.{0,200}(?=\\s|$)', // up to 200 characters of text after the match, splitting at a word boundary
'ims'
);
const matches = regExp.exec(content);
if (matches && matches.length > 0) {
preview = matches[0];

// don't return half of a surrogate pair
return isLowSurrogate(preview.charCodeAt(0)) ? preview.slice(1) : preview;
if (terms.length > 0) {
const firstTerm = terms[0].toLocaleLowerCase();
const leadingChars = 30 - firstTerm.length;

// prettier-ignore
const regExp = new RegExp(
'(?<=\\s|^)[^\n]' + // split at a word boundary (pattern must be preceded by whitespace or beginning of string)
'{0,' + leadingChars + '}' + // up to leadingChars of text before the match
escapeRegExp(firstTerm) +
'.{0,200}(?=\\s|$)', // up to 200 characters of text after the match, splitting at a word boundary
'ims'
);
const matches = regExp.exec(content);
if (matches && matches.length > 0) {
preview = matches[0];

// don't return half of a surrogate pair
return isLowSurrogate(preview.charCodeAt(0))
? preview.slice(1)
: preview;
}
}
}

Expand Down