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

Fix: Contextual excerpt crash on tag search #2529

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Show error message when trying to import invalid JSON [#2446](https://github.com/Automattic/simplenote-electron/pull/2446)
- Fixed buggy cursor when hitting enter on an empty list item [#2519](https://github.com/Automattic/simplenote-electron/pull/2519)
- Made sidebar icons the correct shade of blue [#2513](https://github.com/Automattic/simplenote-electron/pull/2513)
- Fixed a crash when clicking on a tag suggestion from search [#2529](https://github.com/Automattic/simplenote-electron/pull/2529)

### Other Changes

Expand Down
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