#34 - aliases have the same weight as the basename
This commit is contained in:
41
src/__tests__/utils-tests.ts
Normal file
41
src/__tests__/utils-tests.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import type { CachedMetadata } from 'obsidian'
|
||||||
|
import { getAliasesFromMetadata } from '../utils'
|
||||||
|
|
||||||
|
describe('Utils', () => {
|
||||||
|
describe('getAliasesFromMetadata', () => {
|
||||||
|
it('should return an empty string if no metadata is provided', () => {
|
||||||
|
// Act
|
||||||
|
const actual = getAliasesFromMetadata(null)
|
||||||
|
// Assert
|
||||||
|
expect(actual).toBe('')
|
||||||
|
})
|
||||||
|
it('should return an empty string if no aliases are provided', () => {
|
||||||
|
// Arrange
|
||||||
|
const metadata = {} as CachedMetadata
|
||||||
|
// Act
|
||||||
|
const actual = getAliasesFromMetadata(metadata)
|
||||||
|
// Assert
|
||||||
|
expect(actual).toBe('')
|
||||||
|
})
|
||||||
|
it('should join aliases with a comma', () => {
|
||||||
|
// Arrange
|
||||||
|
const metadata = {
|
||||||
|
frontmatter: { aliases: ['foo', 'bar'] },
|
||||||
|
} as CachedMetadata
|
||||||
|
// Act
|
||||||
|
const actual = getAliasesFromMetadata(metadata)
|
||||||
|
// Assert
|
||||||
|
expect(actual).toBe('foo, bar')
|
||||||
|
})
|
||||||
|
it('should return a single alias if only one is provided', () => {
|
||||||
|
// Arrange
|
||||||
|
const metadata = {
|
||||||
|
frontmatter: { aliases: 'foo, bar' },
|
||||||
|
} as CachedMetadata
|
||||||
|
// Act
|
||||||
|
const actual = getAliasesFromMetadata(metadata)
|
||||||
|
// Assert
|
||||||
|
expect(actual).toBe('foo, bar')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -24,6 +24,7 @@ export type IndexedNote = {
|
|||||||
path: string
|
path: string
|
||||||
basename: string
|
basename: string
|
||||||
content: string
|
content: string
|
||||||
|
aliases: string
|
||||||
headings1: string
|
headings1: string
|
||||||
headings2: string
|
headings2: string
|
||||||
headings3: string
|
headings3: string
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
} from './globals'
|
} from './globals'
|
||||||
import {
|
import {
|
||||||
extractHeadingsFromCache,
|
extractHeadingsFromCache,
|
||||||
|
getAliasesFromMetadata,
|
||||||
stringsToRegex,
|
stringsToRegex,
|
||||||
stripMarkdownCharacters,
|
stripMarkdownCharacters,
|
||||||
wait,
|
wait,
|
||||||
@@ -40,7 +41,14 @@ export async function initGlobalSearchIndex(): Promise<void> {
|
|||||||
minisearchInstance = new MiniSearch({
|
minisearchInstance = new MiniSearch({
|
||||||
tokenize,
|
tokenize,
|
||||||
idField: 'path',
|
idField: 'path',
|
||||||
fields: ['basename', 'content', 'headings1', 'headings2', 'headings3'],
|
fields: [
|
||||||
|
'basename',
|
||||||
|
'aliases',
|
||||||
|
'content',
|
||||||
|
'headings1',
|
||||||
|
'headings2',
|
||||||
|
'headings3',
|
||||||
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
// Index files that are already present
|
// Index files that are already present
|
||||||
@@ -84,6 +92,7 @@ async function search(query: Query): Promise<SearchResult[]> {
|
|||||||
combineWith: 'AND',
|
combineWith: 'AND',
|
||||||
boost: {
|
boost: {
|
||||||
basename: settings.weightBasename,
|
basename: settings.weightBasename,
|
||||||
|
aliases: settings.weightBasename,
|
||||||
headings1: settings.weightH1,
|
headings1: settings.weightH1,
|
||||||
headings2: settings.weightH2,
|
headings2: settings.weightH2,
|
||||||
headings3: settings.weightH3,
|
headings3: settings.weightH3,
|
||||||
@@ -208,7 +217,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// console.log(`Omnisearch - adding ${file.path} to index`)
|
// console.log(`Omnisearch - adding ${file.path} to index`)
|
||||||
const fileCache = app.metadataCache.getFileCache(file)
|
const metadata = app.metadataCache.getFileCache(file)
|
||||||
|
|
||||||
if (indexedNotes[file.path]) {
|
if (indexedNotes[file.path]) {
|
||||||
throw new Error(`${file.basename} is already indexed`)
|
throw new Error(`${file.basename} is already indexed`)
|
||||||
@@ -222,14 +231,15 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
|
|||||||
basename: file.basename,
|
basename: file.basename,
|
||||||
content,
|
content,
|
||||||
path: file.path,
|
path: file.path,
|
||||||
headings1: fileCache
|
aliases: getAliasesFromMetadata(metadata),
|
||||||
? extractHeadingsFromCache(fileCache, 1).join(' ')
|
headings1: metadata
|
||||||
|
? extractHeadingsFromCache(metadata, 1).join(' ')
|
||||||
: '',
|
: '',
|
||||||
headings2: fileCache
|
headings2: metadata
|
||||||
? extractHeadingsFromCache(fileCache, 2).join(' ')
|
? extractHeadingsFromCache(metadata, 2).join(' ')
|
||||||
: '',
|
: '',
|
||||||
headings3: fileCache
|
headings3: metadata
|
||||||
? extractHeadingsFromCache(fileCache, 3).join(' ')
|
? extractHeadingsFromCache(metadata, 3).join(' ')
|
||||||
: '',
|
: '',
|
||||||
}
|
}
|
||||||
minisearchInstance.add(note)
|
minisearchInstance.add(note)
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
new Setting(containerEl).setName('Results weighting').setHeading()
|
new Setting(containerEl).setName('Results weighting').setHeading()
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName(`File name (default: ${DEFAULT_SETTINGS.weightBasename})`)
|
.setName(`File name & declared aliases (default: ${DEFAULT_SETTINGS.weightBasename})`)
|
||||||
.addSlider(cb => this.weightSlider(cb, 'weightBasename'))
|
.addSlider(cb => this.weightSlider(cb, 'weightBasename'))
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
|
|||||||
4
src/types.d.ts
vendored
4
src/types.d.ts
vendored
@@ -4,4 +4,8 @@ declare module 'obsidian' {
|
|||||||
interface MetadataCache {
|
interface MetadataCache {
|
||||||
isUserIgnored?(path: string): boolean
|
isUserIgnored?(path: string): boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface FrontMatterCache {
|
||||||
|
aliases?: string[] | string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -147,3 +147,10 @@ export async function filterAsync<T>(
|
|||||||
export function stripMarkdownCharacters(text: string): string {
|
export function stripMarkdownCharacters(text: string): string {
|
||||||
return text.replace(/(\*|_)+(.+?)(\*|_)+/g, (match, p1, p2) => p2)
|
return text.replace(/(\*|_)+(.+?)(\*|_)+/g, (match, p1, p2) => p2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getAliasesFromMetadata(
|
||||||
|
metadata: CachedMetadata | null,
|
||||||
|
): string {
|
||||||
|
const arrOrString = metadata?.frontmatter?.aliases ?? []
|
||||||
|
return Array.isArray(arrOrString) ? arrOrString.join(', ') : arrOrString
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user