Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/gui/suggesters/FileIndex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,79 @@ describe('FileIndex', () => {
});
});

describe('alias extraction', () => {
it('should split comma-separated alias strings', async () => {
const files = [
{
path: 'test.md',
basename: 'test',
extension: 'md',
parent: { path: '' },
stat: { mtime: Date.now() }
}
] as TFile[];

(mockApp.vault.getMarkdownFiles as any).mockReturnValue(files);
mockApp.metadataCache.getFileCache = vi.fn(() => ({
frontmatter: { aliases: 'hello, world' }
}));

await fileIndex.ensureIndexed();

const helloResults = fileIndex.search('hello', {}, 10);
const worldResults = fileIndex.search('world', {}, 10);

expect(helloResults.some(result =>
result.matchType === 'alias' && result.displayText === 'hello'
)).toBe(true);
expect(worldResults.some(result =>
result.matchType === 'alias' && result.displayText === 'world'
)).toBe(true);
});

it('should read aliases from case-insensitive keys', async () => {
const files = [
{
path: 'upper.md',
basename: 'upper',
extension: 'md',
parent: { path: '' },
stat: { mtime: Date.now() }
},
{
path: 'note.md',
basename: 'note',
extension: 'md',
parent: { path: '' },
stat: { mtime: Date.now() }
}
] as TFile[];

(mockApp.vault.getMarkdownFiles as any).mockReturnValue(files);
mockApp.metadataCache.getFileCache = vi.fn((file) => {
if (file.path === 'upper.md') {
return { frontmatter: { Aliases: ['Caps'] } };
}
if (file.path === 'note.md') {
return { frontmatter: { aLiAs: 'Mixed' } };
}
return { frontmatter: {} };
});

await fileIndex.ensureIndexed();

const capsResults = fileIndex.search('caps', {}, 10);
const mixedResults = fileIndex.search('mixed', {}, 10);

expect(capsResults.some(result =>
result.matchType === 'alias' && result.displayText === 'Caps'
)).toBe(true);
expect(mixedResults.some(result =>
result.matchType === 'alias' && result.displayText === 'Mixed'
)).toBe(true);
});
});

describe('heading search', () => {
it.skip('should support global heading search with #', async () => {
// Create files with headings
Expand Down
39 changes: 30 additions & 9 deletions src/gui/suggesters/FileIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,39 @@ export class FileIndex {
this.updateUnresolvedLinks();
}

private extractAliases(frontmatter?: Record<string, unknown>): string[] {
if (!frontmatter) return [];

const aliases: string[] = [];
for (const [key, value] of Object.entries(frontmatter)) {
const lowerKey = key.toLowerCase();
if (lowerKey !== 'alias' && lowerKey !== 'aliases') continue;

if (typeof value === 'string') {
const splitAliases = value
.split(',')
.map((alias) => alias.trim())
.filter((alias) => alias.length > 0);
aliases.push(...splitAliases);
} else if (Array.isArray(value)) {
aliases.push(
...value
.filter((alias) => typeof alias === 'string')
.map((alias) => alias.trim())
.filter((alias) => alias.length > 0)
);
}
}

return aliases;
}

private createIndexedFile(file: TFile): IndexedFile {
const fileCache = this.app.metadataCache.getFileCache(file);
const frontmatter = fileCache?.frontmatter;

// Extract aliases
const aliases: string[] = [];
const aliasData = frontmatter?.alias ?? frontmatter?.aliases;
if (typeof aliasData === 'string') {
aliases.push(aliasData);
} else if (Array.isArray(aliasData)) {
aliases.push(...aliasData.filter(a => typeof a === 'string'));
}

// Extract aliases (case-insensitive keys, handle comma-separated strings)
const aliases = this.extractAliases(frontmatter as Record<string, unknown> | undefined);
const aliasesNormalized = aliases.map((alias) => normalizeForSearch(alias));

// Extract and sanitize headings at index time
Expand Down