-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Smarter parsing of aliases in frontmatter
- Loading branch information
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { CachedMetadata, FrontMatterCache } from 'obsidian'; | ||
|
||
import { getAliases } from './getAliases'; | ||
|
||
describe('getAliases', () => { | ||
it('Returns an empty array if no frontmatter is defined', () => { | ||
const metadata: CachedMetadata = {}; | ||
const aliases = getAliases(metadata); | ||
expect(aliases).toEqual([]); | ||
}); | ||
|
||
it('Returns an empty array if no aliases are defined', () => { | ||
const metadata: CachedMetadata = { | ||
frontmatter: {} as FrontMatterCache, | ||
}; | ||
|
||
const aliases = getAliases(metadata); | ||
expect(aliases).toEqual([]); | ||
}); | ||
|
||
it('Parses aliases defined as a string split by comma', () => { | ||
const metadata: CachedMetadata = { | ||
frontmatter: { | ||
aliases: 'foo, bar ', | ||
} as unknown as FrontMatterCache, | ||
}; | ||
|
||
const aliases = getAliases(metadata); | ||
expect(aliases).toEqual(['foo', 'bar']); | ||
}); | ||
|
||
it('Parses aliases defined as an array of values', () => { | ||
const metadata: CachedMetadata = { | ||
frontmatter: { | ||
aliases: ['foo', 'bar'], | ||
} as unknown as FrontMatterCache, | ||
}; | ||
|
||
const aliases = getAliases(metadata); | ||
expect(aliases).toEqual(['foo', 'bar']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CachedMetadata } from 'obsidian'; | ||
|
||
export const getAliases = (metadata: CachedMetadata): string[] => { | ||
const frontmatterAliases = metadata?.frontmatter?.['aliases']; | ||
|
||
if (typeof frontmatterAliases === 'string') { | ||
return frontmatterAliases.split(',').map((alias: string) => alias.trim()); | ||
} else if (Array.isArray(frontmatterAliases)) { | ||
return frontmatterAliases as string[]; | ||
} | ||
|
||
return []; | ||
}; |