binxly_Atomizer/src/notes-manager.ts
Mark Ayers a476ad6cd1 Fix critical bug: usedTitles Set never populated
The getUniqueTitle() method was checking for duplicate titles but never
adding titles to the usedTitles Set, making duplicate prevention completely
ineffective. This caused file creation failures when OpenAI generated
multiple notes with identical titles.

Fix: Add title to usedTitles Set after generating unique name.

Fixes critical issue C1 from CODE_REVIEW.md

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-12 19:41:53 -04:00

52 lines
1.2 KiB
TypeScript

import { App, normalizePath } from "obsidian";
/**
* Manages the creation and storage of atomic notes
*/
export class NotesManager {
private usedTitles = new Set<string>();
constructor(
private app: App,
private folderPath: string,
) {}
/**
* Creates the output folder if it doesn't exist
*/
async createFolder(): Promise<void> {
if (!(await this.app.vault.adapter.exists(this.folderPath))) {
await this.app.vault.createFolder(this.folderPath);
}
}
/**
* Saves a note to the filesystem
*/
async saveNote(note: string): Promise<void> {
const titleMatch = note.match(/^#\s+(.+)$/m);
if (!titleMatch || !titleMatch[1]) {
console.warn("No title found in note:", note.slice(0, 100));
return;
}
const baseTitle = titleMatch[1].trim();
const title = this.getUniqueTitle(baseTitle);
const fileName = `${this.folderPath}/${normalizePath(title)}.md`;
await this.app.vault.create(fileName, note.trim());
}
/**
* Ensures titles are unique by adding a counter if needed
*/
private getUniqueTitle(baseTitle: string): string {
let title = baseTitle;
let counter = 1;
while (this.usedTitles.has(title)) {
title = `${baseTitle} ${counter}`;
counter++;
}
this.usedTitles.add(title);
return normalizePath(title);
}
}