feat: from:me search syntax
This commit is contained in:
parent
17f7c36edf
commit
7f4d3dc999
2 changed files with 31 additions and 2 deletions
|
@ -37,3 +37,27 @@ export function countLines(str: string | undefined): number {
|
|||
if (!str) return 0
|
||||
return str.match(/\n/g)?.length ?? 0
|
||||
}
|
||||
|
||||
// Augments search query with additional syntax like `from:me`
|
||||
export function augmentSearchQuery(query: string, {did}: {did?: string}) {
|
||||
// Don't do anything if there's no DID
|
||||
if (!did) {
|
||||
return query
|
||||
}
|
||||
|
||||
// We don't want to replace substrings that are being "quoted" because those
|
||||
// are exact string matches, so what we'll do here is to split them apart
|
||||
|
||||
// Even-indexed strings are unquoted, odd-indexed strings are quoted
|
||||
const splits = query.split(/("(?:[^"\\]|\\.)*")/g)
|
||||
|
||||
return splits
|
||||
.map((str, idx) => {
|
||||
if (idx % 2 === 0) {
|
||||
return str.replaceAll(/(^|\s)from:me(\s|$)/g, `$1${did}$2`)
|
||||
}
|
||||
|
||||
return str
|
||||
})
|
||||
.join('')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue