-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbacklinkGenerator.ts
33 lines (29 loc) · 1.39 KB
/
backlinkGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { RAGManager } from './rag';
import { TFile, Vault } from 'obsidian';
export class BacklinkGenerator {
constructor(private ragManager: RAGManager, private vault: Vault) {}
async generateBacklinks(selectedText: string): Promise<string[]> {
const similarNotes = await this.ragManager.findSimilarNotes(selectedText);
console.log("Similar notes:", similarNotes);
const backlinks: string[] = [];
// Split the similarNotes string into individual note entries
const noteEntries = similarNotes.split('\n').filter(entry => entry.trim() !== '');
for (const entry of noteEntries) {
console.log("Processing note entry:", entry);
// Extract the file path from the entry (assuming it's in the format [[filepath]]: content)
const match = entry.match(/\[\[(.*?)\]\]/);
if (match && match[1]) {
const notePath = match[1];
const file = this.vault.getAbstractFileByPath(notePath);
if (file instanceof TFile) {
console.log("File found:", file.path);
backlinks.push(`[[${file.path}|${file.basename}]]`);
} else {
console.log("File not found or not a TFile:", notePath);
}
}
}
console.log("Generated backlinks:", backlinks);
return backlinks;
}
}