#154 - filter by file extension
This commit is contained in:
@@ -175,6 +175,13 @@ export class Omnisearch {
|
||||
},
|
||||
})
|
||||
|
||||
// Filter query results to only keep files that match query.extensions (if any)
|
||||
if (query.extensions.length) {
|
||||
results = results.filter(r =>
|
||||
query.extensions.some(e => r.id.endsWith(e))
|
||||
)
|
||||
}
|
||||
|
||||
// If the query does not return any result,
|
||||
// retry but with a shorter prefix limit
|
||||
if (!results.length) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { settings } from '../settings'
|
||||
import { removeDiacritics, stripSurroundingQuotes } from '../tools/utils'
|
||||
import { parseQuery } from '../vendor/parse-query'
|
||||
import { regexExtensions } from '../globals'
|
||||
|
||||
type QueryToken = {
|
||||
/**
|
||||
@@ -20,8 +21,13 @@ type QueryToken = {
|
||||
export class Query {
|
||||
public segments: QueryToken[] = []
|
||||
public exclusions: QueryToken[] = []
|
||||
public extensions: string[] = []
|
||||
|
||||
constructor(text = '') {
|
||||
// Extract & remove extensions from the query
|
||||
this.extensions = this.extractExtensions(text)
|
||||
text = this.removeExtensions(text)
|
||||
|
||||
if (settings.ignoreDiacritics) text = removeDiacritics(text)
|
||||
const tokens = parseQuery(text.toLowerCase(), { tokenize: true })
|
||||
this.exclusions = tokens.exclude.text
|
||||
@@ -59,4 +65,19 @@ export class Query {
|
||||
exact: stripped !== str,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts an array of extensions like ".png" from a string
|
||||
*/
|
||||
private extractExtensions(str: string): string[] {
|
||||
const extensions = (str.match(regexExtensions) ?? []).map(o => o.trim())
|
||||
if (extensions) {
|
||||
return extensions.map(ext => ext.toLowerCase())
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
private removeExtensions(str: string): string {
|
||||
return str.replace(regexExtensions, '')
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user