Skip to content

Commit

Permalink
Fix: Contextual excerpt crash on tag search (#2529)
Browse files Browse the repository at this point in the history
* guard against crash from tag searches

* release notes
  • Loading branch information
codebykat authored Dec 17, 2020
1 parent f7ce363 commit 4230d90
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
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

0 comments on commit 4230d90

Please sign in to comment.