Display full path as basename, getAliasesFromMetadata(): return an array instead of a string

This commit is contained in:
Simon Cambier
2022-05-11 16:11:14 +02:00
parent 1d4500fce6
commit 7efddcb616
3 changed files with 26 additions and 15 deletions

View File

@@ -3,21 +3,19 @@ import { getAliasesFromMetadata } from '../utils'
describe('Utils', () => { describe('Utils', () => {
describe('getAliasesFromMetadata', () => { describe('getAliasesFromMetadata', () => {
it('should return an empty string if no metadata is provided', () => { it('should return an empty array if no metadata is provided', () => {
// Act // Act
const actual = getAliasesFromMetadata(null) const actual = getAliasesFromMetadata(null)
// Assert // Assert
expect(actual).toBe('') expect(actual).toEqual([])
}) })
it('should return an empty string if no aliases are provided', () => { it('should return an empty array if no aliases are provided', () => {
// Arrange
const metadata = {} as CachedMetadata
// Act // Act
const actual = getAliasesFromMetadata(metadata) const actual = getAliasesFromMetadata({})
// Assert // Assert
expect(actual).toBe('') expect(actual).toEqual([])
}) })
it('should join aliases with a comma', () => { it('should return the aliases array as-is', () => {
// Arrange // Arrange
const metadata = { const metadata = {
frontmatter: { aliases: ['foo', 'bar'] }, frontmatter: { aliases: ['foo', 'bar'] },
@@ -25,9 +23,9 @@ describe('Utils', () => {
// Act // Act
const actual = getAliasesFromMetadata(metadata) const actual = getAliasesFromMetadata(metadata)
// Assert // Assert
expect(actual).toBe('foo, bar') expect(actual).toEqual(['foo', 'bar'])
}) })
it('should return a single alias if only one is provided', () => { it('should convert the aliases string into an array', () => {
// Arrange // Arrange
const metadata = { const metadata = {
frontmatter: { aliases: 'foo, bar' }, frontmatter: { aliases: 'foo, bar' },
@@ -35,7 +33,17 @@ describe('Utils', () => {
// Act // Act
const actual = getAliasesFromMetadata(metadata) const actual = getAliasesFromMetadata(metadata)
// Assert // Assert
expect(actual).toBe('foo, bar') expect(actual).toEqual(['foo', 'bar'])
})
it('should return an empty array if the aliases field is an empty string', () => {
// Arrange
const metadata = {
frontmatter: { aliases: '' },
} as CachedMetadata
// Act
const actual = getAliasesFromMetadata(metadata)
// Assert
expect(actual).toEqual([])
}) })
}) })
}) })

View File

@@ -228,10 +228,10 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
// Make the document and index it // Make the document and index it
const note: IndexedNote = { const note: IndexedNote = {
basename: file.basename, basename: file.path,
content, content,
path: file.path, path: file.path,
aliases: getAliasesFromMetadata(metadata), aliases: getAliasesFromMetadata(metadata).join(''),
headings1: metadata headings1: metadata
? extractHeadingsFromCache(metadata, 1).join(' ') ? extractHeadingsFromCache(metadata, 1).join(' ')
: '', : '',
@@ -242,6 +242,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
? extractHeadingsFromCache(metadata, 3).join(' ') ? extractHeadingsFromCache(metadata, 3).join(' ')
: '', : '',
} }
minisearchInstance.add(note) minisearchInstance.add(note)
indexedNotes[note.path] = note indexedNotes[note.path] = note
} }

View File

@@ -150,7 +150,9 @@ export function stripMarkdownCharacters(text: string): string {
export function getAliasesFromMetadata( export function getAliasesFromMetadata(
metadata: CachedMetadata | null, metadata: CachedMetadata | null,
): string { ): string[] {
const arrOrString = metadata?.frontmatter?.aliases ?? [] const arrOrString = metadata?.frontmatter?.aliases ?? []
return Array.isArray(arrOrString) ? arrOrString.join(', ') : arrOrString return (Array.isArray(arrOrString) ? arrOrString : arrOrString.split(','))
.map(s => s ? s.trim() : s)
.filter(s => !!s)
} }