Use identifier case to further disambiguate notes (#1519)

Fixes #1303
This commit is contained in:
Riccardo
2025-09-25 17:29:42 +02:00
committed by GitHub
parent e1694f298b
commit 89298b9652
2 changed files with 48 additions and 3 deletions

View File

@@ -183,4 +183,41 @@ describe('Identifier computation', () => {
workspace.getIdentifier(noteABis.uri, [noteB.uri, noteA.uri])
).toEqual('note-a');
});
it('should handle case-sensitive filenames correctly (#1303)', () => {
const workspace = new FoamWorkspace('.md');
const noteUppercase = createTestNote({ uri: '/a/Note.md' });
const noteLowercase = createTestNote({ uri: '/b/note.md' });
workspace.set(noteUppercase).set(noteLowercase);
// Should find exact case matches
expect(workspace.listByIdentifier('Note').length).toEqual(1);
expect(workspace.listByIdentifier('Note')[0].uri.path).toEqual(
'/a/Note.md'
);
expect(workspace.listByIdentifier('note').length).toEqual(1);
expect(workspace.listByIdentifier('note')[0].uri.path).toEqual(
'/b/note.md'
);
// Should not treat them as the same identifier
expect(workspace.listByIdentifier('Note')[0]).not.toEqual(
workspace.listByIdentifier('note')[0]
);
});
it('should generate correct identifiers for case-sensitive files', () => {
const workspace = new FoamWorkspace('.md');
const noteUppercase = createTestNote({ uri: '/a/Note.md' });
const noteLowercase = createTestNote({ uri: '/b/note.md' });
workspace.set(noteUppercase).set(noteLowercase);
// Each should have a unique identifier without directory disambiguation
// since they differ by case, they are not considered conflicting
expect(workspace.getIdentifier(noteUppercase.uri)).toEqual('Note');
expect(workspace.getIdentifier(noteLowercase.uri)).toEqual('note');
});
});

View File

@@ -89,13 +89,12 @@ export class FoamWorkspace implements IDisposable {
public listByIdentifier(identifier: string): Resource[] {
let needle = this.getTrieIdentifier(identifier);
const mdNeedle =
getExtension(normalize(identifier)) !== this.defaultExtension
? this.getTrieIdentifier(identifier + this.defaultExtension)
: undefined;
const resources: Resource[] = [];
let resources: Resource[] = [];
this._resources.find(needle).forEach(elm => resources.push(elm[1]));
@@ -103,6 +102,15 @@ export class FoamWorkspace implements IDisposable {
this._resources.find(mdNeedle).forEach(elm => resources.push(elm[1]));
}
// if multiple resources found, try to filter exact case matches
if (resources.length > 1) {
resources = resources.filter(
r =>
r.uri.getBasename() === identifier ||
r.uri.getBasename() === identifier + this.defaultExtension
);
}
return resources.sort(Resource.sortByPath);
}
@@ -115,7 +123,7 @@ export class FoamWorkspace implements IDisposable {
const amongst = [];
const basename = forResource.getBasename();
this.listByIdentifier(basename).map(res => {
this.listByIdentifier(basename).forEach(res => {
// skip self
if (res.uri.isEqual(forResource)) {
return;