#154 - filter by file extension
This commit is contained in:
@@ -8,6 +8,7 @@ export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g
|
|||||||
export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms
|
export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms
|
||||||
export const regexStripQuotes = /^"|"$|^'|'$/g
|
export const regexStripQuotes = /^"|"$|^'|'$/g
|
||||||
export const chsRegex = /[\u4e00-\u9fa5]/
|
export const chsRegex = /[\u4e00-\u9fa5]/
|
||||||
|
export const regexExtensions = /(?:^|\s)\.(\w+)/g
|
||||||
|
|
||||||
export const excerptBefore = 100
|
export const excerptBefore = 100
|
||||||
export const excerptAfter = 300
|
export const excerptAfter = 300
|
||||||
|
|||||||
@@ -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,
|
// If the query does not return any result,
|
||||||
// retry but with a shorter prefix limit
|
// retry but with a shorter prefix limit
|
||||||
if (!results.length) {
|
if (!results.length) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { settings } from '../settings'
|
import { settings } from '../settings'
|
||||||
import { removeDiacritics, stripSurroundingQuotes } from '../tools/utils'
|
import { removeDiacritics, stripSurroundingQuotes } from '../tools/utils'
|
||||||
import { parseQuery } from '../vendor/parse-query'
|
import { parseQuery } from '../vendor/parse-query'
|
||||||
|
import { regexExtensions } from '../globals'
|
||||||
|
|
||||||
type QueryToken = {
|
type QueryToken = {
|
||||||
/**
|
/**
|
||||||
@@ -20,8 +21,13 @@ type QueryToken = {
|
|||||||
export class Query {
|
export class Query {
|
||||||
public segments: QueryToken[] = []
|
public segments: QueryToken[] = []
|
||||||
public exclusions: QueryToken[] = []
|
public exclusions: QueryToken[] = []
|
||||||
|
public extensions: string[] = []
|
||||||
|
|
||||||
constructor(text = '') {
|
constructor(text = '') {
|
||||||
|
// Extract & remove extensions from the query
|
||||||
|
this.extensions = this.extractExtensions(text)
|
||||||
|
text = this.removeExtensions(text)
|
||||||
|
|
||||||
if (settings.ignoreDiacritics) text = removeDiacritics(text)
|
if (settings.ignoreDiacritics) text = removeDiacritics(text)
|
||||||
const tokens = parseQuery(text.toLowerCase(), { tokenize: true })
|
const tokens = parseQuery(text.toLowerCase(), { tokenize: true })
|
||||||
this.exclusions = tokens.exclude.text
|
this.exclusions = tokens.exclude.text
|
||||||
@@ -59,4 +65,19 @@ export class Query {
|
|||||||
exact: stripped !== str,
|
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