@@ -483,14 +483,32 @@ final class NoteStore {
483483
484484 // MARK: - Duplicate Detection
485485
486+ func isValidNoteTitle ( _ title : String ) -> Bool {
487+ FileStorage . filename ( forTitle : title ) != nil
488+ }
489+
486490 /// Whether a note title already exists in the given folder (case-insensitive filename match).
487491 func noteTitleExists ( _ title : String , in folder : String , excluding noteID : UUID ? = nil ) -> Bool {
488- let filename = " \( title ) .md "
489- return notes . contains { note in
492+ guard let filename = FileStorage . filename ( forTitle : title ) else { return false }
493+ let collidesInMemory = notes . contains { note in
490494 note . id != noteID
491495 && note . folder == folder
492496 && note . filename . caseInsensitiveCompare ( filename ) == . orderedSame
493497 }
498+ if collidesInMemory {
499+ return true
500+ }
501+ if let noteID ,
502+ let current = notes . first ( where : { $0 . id == noteID } ) ,
503+ current . folder == folder ,
504+ current . filename . caseInsensitiveCompare ( filename ) == . orderedSame
505+ {
506+ return false
507+ }
508+ let destination = folder . isEmpty
509+ ? FileStorage . rootURL . appendingPathComponent ( filename )
510+ : FileStorage . rootURL . appendingPathComponent ( folder ) . appendingPathComponent ( filename )
511+ return FileManager . default . fileExists ( atPath : destination . path )
494512 }
495513
496514 /// Checks loaded notes and the destination directory for a filename collision.
@@ -557,8 +575,8 @@ final class NoteStore {
557575 let now = Date ( )
558576 var note = Note (
559577 id : UUID ( ) ,
560- filename : " \( FileStorage . sanitizeForFilename ( title ) ) .md " ,
561- content : " # \( title ) \n \n " ,
578+ filename : " \( title ) .md " ,
579+ content : " " ,
562580 createdAt : now ,
563581 modifiedAt : now ,
564582 folder : folder ,
@@ -596,13 +614,13 @@ final class NoteStore {
596614 }
597615 }
598616
599- func renameNote ( _ note : Note , to newTitle : String ) {
600- guard let index = notes . firstIndex ( where : { $0 . id == note . id } ) else { return }
617+ @ discardableResult
618+ func renameNote ( _ note : Note , to newTitle : String ) -> Bool {
619+ guard let index = notes . firstIndex ( where : { $0 . id == note . id } ) else { return false }
601620 let trimmed = newTitle . trimmingCharacters ( in : . whitespacesAndNewlines )
602- guard ! trimmed . isEmpty else { return }
603- guard ! noteTitleExists ( trimmed , in : note . folder , excluding : note . id ) else { return }
621+ guard let newFilename = FileStorage . filename ( forTitle : trimmed ) else { return false }
622+ guard ! noteTitleExists ( trimmed , in : note . folder , excluding : note . id ) else { return false }
604623
605- let newFilename = " \( trimmed ) .md "
606624 var renamedNote = notes [ index ]
607625 renamedNote . modifiedAt = Date ( )
608626 do {
@@ -622,8 +640,10 @@ final class NoteStore {
622640 selectedNote = notes [ index ]
623641 }
624642 Log . storage . info ( " [NoteMetadata] mutation op=rename noteID= \( note . id . uuidString , privacy : . public ) committed=true " )
643+ return true
625644 } catch {
626645 Log . storage . error ( " [NoteMetadata] mutation op=rename noteID= \( note . id . uuidString , privacy : . public ) committed=false error= \( error ) " )
646+ return false
627647 }
628648 }
629649
@@ -976,11 +996,11 @@ final class NoteStore {
976996 let baseTitle = notes [ index ] . title
977997 var counter = 2
978998 var newTitle = " \( baseTitle ) \( counter ) "
979- var newFilename = " \( FileStorage . sanitizeForFilename ( newTitle ) ) .md "
999+ var newFilename = FileStorage . filename ( forTitle : newTitle ) ?? " Untitled 2 .md"
9801000 while noteFilenameWouldCollide ( newFilename , in : folder , excluding : conflict . noteID ) {
9811001 counter += 1
9821002 newTitle = " \( baseTitle ) \( counter ) "
983- newFilename = " \( FileStorage . sanitizeForFilename ( newTitle ) ) .md "
1003+ newFilename = FileStorage . filename ( forTitle : newTitle ) ?? " Untitled \( counter ) .md "
9841004 }
9851005 notes [ index ] . filename = newFilename
9861006 notes [ index ] . modifiedAt = Date ( )
0 commit comments