This commit is contained in:
Simon Cambier
2024-03-14 21:33:58 +01:00
3 changed files with 56 additions and 1 deletions

View File

@@ -136,7 +136,7 @@ abstract class OmnisearchModal extends Modal {
}) })
// Context // Context
this.scope.register(['Ctrl'], 'H', _e => { this.scope.register(['Ctrl'], 'G', _e => {
eventBus.emit(EventNames.ToggleExcerpts) eventBus.emit(EventNames.ToggleExcerpts)
}) })
} }

View File

@@ -252,6 +252,40 @@ export class Omnisearch {
}) })
} }
logDebug(
'searching with downranked folders',
settings.downrankedFoldersFilters
)
// downrank files that are in folders listed in the downrankedFoldersFilters
if (settings.downrankedFoldersFilters.length > 0) {
results.forEach(result => {
const path = result.id
let downrankingFolder = false
settings.downrankedFoldersFilters.forEach(filter => {
if (path.startsWith(filter)) {
// we don't want the filter to match the folder sources, e.g.
// it needs to match a whole folder name
if (path === filter || path.startsWith(filter + '/')) {
logDebug('searching with downranked folders in path: ', path)
downrankingFolder = true
}
}
})
if (downrankingFolder) {
result.score /= 10
}
const pathParts = path.split('/')
const pathPartsLength = pathParts.length
for (let i = 0; i < pathPartsLength; i++) {
const pathPart = pathParts[i]
if (settings.downrankedFoldersFilters.includes(pathPart)) {
result.score /= 10
break
}
}
})
}
// Extract tags from the query // Extract tags from the query
const tags = query.getTags() const tags = query.getTags()

View File

@@ -29,6 +29,8 @@ export interface OmnisearchSettings extends WeightingSettings {
useCache: boolean useCache: boolean
/** Respect the "excluded files" Obsidian setting by downranking results ignored files */ /** Respect the "excluded files" Obsidian setting by downranking results ignored files */
hideExcluded: boolean hideExcluded: boolean
/** downrank files in the given folders */
downrankedFoldersFilters: string[]
/** Ignore diacritics when indexing files */ /** Ignore diacritics when indexing files */
ignoreDiacritics: boolean ignoreDiacritics: boolean
/** Extensions of plain text files to index, in addition to .md */ /** Extensions of plain text files to index, in addition to .md */
@@ -268,6 +270,24 @@ export class SettingsTab extends PluginSettingTab {
}) })
) )
// Downranked files
new Setting(containerEl)
.setName('Folders to downrank in search results')
.setDesc(
`Folders to downrank in search results. Files in these folders will be downranked in results. They will still be indexed for tags, unlike excluded files. Folders should be comma delimited.`
)
.addText(component => {
component
.setValue(settings.downrankedFoldersFilters.join(','))
.setPlaceholder('Example: src,p2/dir')
.onChange(async v => {
let folders = v.split(',')
folders = folders.map(f => f.trim())
settings.downrankedFoldersFilters = folders
await saveSettings(this.plugin)
})
})
// Split CamelCaseWords // Split CamelCaseWords
const camelCaseDesc = new DocumentFragment() const camelCaseDesc = new DocumentFragment()
camelCaseDesc.createSpan({}, span => { camelCaseDesc.createSpan({}, span => {
@@ -621,6 +641,7 @@ export class SettingsTab extends PluginSettingTab {
export const DEFAULT_SETTINGS: OmnisearchSettings = { export const DEFAULT_SETTINGS: OmnisearchSettings = {
useCache: true, useCache: true,
hideExcluded: false, hideExcluded: false,
downrankedFoldersFilters: [] as string[],
ignoreDiacritics: true, ignoreDiacritics: true,
indexedFileTypes: [] as string[], indexedFileTypes: [] as string[],
PDFIndexing: false, PDFIndexing: false,