fix: only match protected note title
This commit is contained in:
@@ -5,44 +5,54 @@ export function notePassesFilter(
|
|||||||
showArchived: boolean,
|
showArchived: boolean,
|
||||||
hidePinned: boolean,
|
hidePinned: boolean,
|
||||||
filterText: string
|
filterText: string
|
||||||
) {
|
): boolean {
|
||||||
|
|
||||||
const canShowArchived = showArchived;
|
const canShowArchived = showArchived;
|
||||||
const canShowPinned = !hidePinned;
|
const canShowPinned = !hidePinned;
|
||||||
if (
|
if ((note.archived && !canShowArchived) || (note.pinned && !canShowPinned)) {
|
||||||
(note.archived && !canShowArchived) ||
|
|
||||||
(note.pinned && !canShowPinned)
|
|
||||||
) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return noteMatchesQuery(note, filterText);
|
if (note.protected) {
|
||||||
|
const match = noteMatchesQuery(note, filterText);
|
||||||
|
/** Only match title to prevent leaking protected note text */
|
||||||
|
return match === Match.Title || match === Match.TitleAndText;
|
||||||
|
} else {
|
||||||
|
return noteMatchesQuery(note, filterText) !== Match.None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function noteMatchesQuery(
|
enum Match {
|
||||||
note: SNNote,
|
None = 0,
|
||||||
query: string
|
Title = 1,
|
||||||
) {
|
Text = 2,
|
||||||
|
TitleAndText = Title + Text,
|
||||||
|
Uuid = 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
function noteMatchesQuery(note: SNNote, query: string): Match {
|
||||||
if (query.length === 0) {
|
if (query.length === 0) {
|
||||||
return true;
|
return Match.TitleAndText;
|
||||||
}
|
}
|
||||||
const title = note.safeTitle().toLowerCase();
|
const title = note.safeTitle().toLowerCase();
|
||||||
const text = note.safeText().toLowerCase();
|
const text = note.safeText().toLowerCase();
|
||||||
const lowercaseText = query.toLowerCase();
|
const lowercaseText = query.toLowerCase();
|
||||||
|
const words = lowercaseText.split(' ');
|
||||||
const quotedText = stringBetweenQuotes(lowercaseText);
|
const quotedText = stringBetweenQuotes(lowercaseText);
|
||||||
if (quotedText) {
|
if (quotedText) {
|
||||||
return title.includes(quotedText) || text.includes(quotedText);
|
return (
|
||||||
|
(title.includes(quotedText) ? Match.Title : Match.None) +
|
||||||
|
(text.includes(quotedText) ? Match.Text : Match.None)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (stringIsUuid(lowercaseText)) {
|
if (stringIsUuid(lowercaseText)) {
|
||||||
return note.uuid === lowercaseText;
|
return note.uuid === lowercaseText ? Match.Uuid : Match.None;
|
||||||
}
|
}
|
||||||
const words = lowercaseText.split(" ");
|
|
||||||
const matchesTitle = words.every((word) => {
|
const matchesTitle = words.every((word) => {
|
||||||
return title.indexOf(word) >= 0;
|
return title.indexOf(word) >= 0;
|
||||||
});
|
});
|
||||||
const matchesBody = words.every((word) => {
|
const matchesBody = words.every((word) => {
|
||||||
return text.indexOf(word) >= 0;
|
return text.indexOf(word) >= 0;
|
||||||
});
|
});
|
||||||
return matchesTitle || matchesBody;
|
return (matchesTitle ? Match.Title : 0) + (matchesBody ? Match.Text : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function stringBetweenQuotes(text: string) {
|
function stringBetweenQuotes(text: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user