Feature/40 key value current folder (#218)

* #40 - Reworked Query

* #40 - added a "path:" option in the query field

* #40 - folder exclusion

* Cleaner code
This commit is contained in:
Simon Cambier
2023-04-02 13:00:52 +02:00
committed by GitHub
parent 60f56452dc
commit 56fc8157fb
7 changed files with 95 additions and 438 deletions

View File

@@ -72,8 +72,8 @@ export class Omnisearch {
}
private minisearch: MiniSearch
private indexedDocuments: Map<string, number> = new Map()
private previousResults: SearchResult[] = []
private previousQuery: Query | null = null
// private previousResults: SearchResult[] = []
// private previousQuery: Query | null = null
constructor() {
this.minisearch = new MiniSearch(Omnisearch.options)
@@ -175,8 +175,8 @@ export class Omnisearch {
options: { prefixLength: number; singleFilePath?: string }
): Promise<SearchResult[]> {
if (query.isEmpty()) {
this.previousResults = []
this.previousQuery = null
// this.previousResults = []
// this.previousQuery = null
return []
}
@@ -210,6 +210,22 @@ export class Omnisearch {
})
}
// Filter query results that match the path
if (query.query.path) {
results = results.filter(r =>
query.query.path?.some(p =>
(r.id as string).toLowerCase().includes(p.toLowerCase())
)
)
}
if (query.query.exclude.path) {
results = results.filter(r =>
!query.query.exclude.path?.some(p =>
(r.id as string).toLowerCase().includes(p.toLowerCase())
)
)
}
// If the query does not return any result,
// retry but with a shorter prefix limit
if (!results.length) {
@@ -243,9 +259,7 @@ export class Omnisearch {
}
// Extract tags from the query
const tags = query.segments
.filter(s => s.value.startsWith('#'))
.map(s => s.value)
const tags = query.getTags()
// Put the results with tags on top
for (const tag of tags) {
@@ -280,14 +294,14 @@ export class Omnisearch {
}
// If the search query contains exclude terms, filter out results that have them
const exclusions = query.exclusions
const exclusions = query.query.exclude.text
if (exclusions.length) {
logDebug('Filtering with exclusions')
results = results.filter(r => {
const content = stripMarkdownCharacters(
documents.find(d => d.path === r.id)?.content ?? ''
).toLowerCase()
return exclusions.every(q => !content.includes(q.value))
return exclusions.every(q => !content.includes(q))
})
}
@@ -298,8 +312,8 @@ export class Omnisearch {
(result, index, arr) => arr.findIndex(t => t.id === result.id) === index
)
this.previousQuery = query
this.previousResults = results
// this.previousQuery = query
// this.previousResults = results
return results
}
@@ -375,16 +389,6 @@ export class Omnisearch {
} as IndexedDocument
}
// Remove '#' from tags, for highlighting
query.segments.forEach(s => {
s.value = s.value.replace(/^#/, '')
})
// Extract tags from the query
const tags = query.segments
.filter(s => s.value.startsWith('#'))
.map(s => s.value)
// Clean search matches that match quoted expressions,
// and inject those expressions instead
const foundWords = [
@@ -393,10 +397,10 @@ export class Omnisearch {
...Object.keys(result.match),
// Quoted expressions
...query.segments.filter(s => s.exact).map(s => s.value),
...query.getExactTerms(),
// Tags, starting with #
...tags,
...query.getTags(),
].filter(w => w.length > 1 || /\p{Emoji}/u.test(w))
logDebug('Matching tokens:', foundWords)