Skip to content

Commit

Permalink
#34 - aliases have the same weight as the basename
Browse files Browse the repository at this point in the history
  • Loading branch information
scambier committed May 11, 2022
1 parent a395a77 commit 0cdb3c8
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 9 deletions.
41 changes: 41 additions & 0 deletions src/__tests__/utils-tests.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
1 change: 1 addition & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type IndexedNote = {
path: string
basename: string
content: string
aliases: string
headings1: string
headings2: string
headings3: string
Expand Down
26 changes: 18 additions & 8 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './globals'
import {
extractHeadingsFromCache,
getAliasesFromMetadata,
stringsToRegex,
stripMarkdownCharacters,
wait,
Expand Down Expand Up @@ -40,7 +41,14 @@ export async function initGlobalSearchIndex(): Promise<void> {
minisearchInstance = new MiniSearch({
tokenize,
idField: 'path',
fields: ['basename', 'content', 'headings1', 'headings2', 'headings3'],
fields: [
'basename',
'aliases',
'content',
'headings1',
'headings2',
'headings3',
],
})

// Index files that are already present
Expand Down Expand Up @@ -84,6 +92,7 @@ async function search(query: Query): Promise<SearchResult[]> {
combineWith: 'AND',
boost: {
basename: settings.weightBasename,
aliases: settings.weightBasename,
headings1: settings.weightH1,
headings2: settings.weightH2,
headings3: settings.weightH3,
Expand Down Expand Up @@ -208,7 +217,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
}
try {
// console.log(`Omnisearch - adding ${file.path} to index`)
const fileCache = app.metadataCache.getFileCache(file)
const metadata = app.metadataCache.getFileCache(file)

if (indexedNotes[file.path]) {
throw new Error(`${file.basename} is already indexed`)
Expand All @@ -222,14 +231,15 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
basename: file.basename,
content,
path: file.path,
headings1: fileCache
? extractHeadingsFromCache(fileCache, 1).join(' ')
aliases: getAliasesFromMetadata(metadata),
headings1: metadata
? extractHeadingsFromCache(metadata, 1).join(' ')
: '',
headings2: fileCache
? extractHeadingsFromCache(fileCache, 2).join(' ')
headings2: metadata
? extractHeadingsFromCache(metadata, 2).join(' ')
: '',
headings3: fileCache
? extractHeadingsFromCache(fileCache, 3).join(' ')
headings3: metadata
? extractHeadingsFromCache(metadata, 3).join(' ')
: '',
}
minisearchInstance.add(note)
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class SettingsTab extends PluginSettingTab {
new Setting(containerEl).setName('Results weighting').setHeading()

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'))

new Setting(containerEl)
Expand Down
4 changes: 4 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ declare module 'obsidian' {
interface MetadataCache {
isUserIgnored?(path: string): boolean
}

interface FrontMatterCache {
aliases?: string[] | string
}
}
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,10 @@ export async function filterAsync<T>(
export function stripMarkdownCharacters(text: string): string {
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
}

0 comments on commit 0cdb3c8

Please sign in to comment.