muzzle-velocity/lib/search_service.dart
randogoth 84c134adc4 init
2025-02-20 22:40:41 +02:00

21 lines
No EOL
773 B
Dart

import 'dart:io';
class SearchService {
static List<File> searchNotes(String query, List<File> notes) {
if (query.isEmpty) return notes;
final lowerQuery = query.toLowerCase();
return notes.where((note) {
final fileName = note.uri.pathSegments.last.replaceAll('.md', '').toLowerCase();
final content = note.readAsStringSync().toLowerCase();
// Fuzzy search: Match if all words in the query are found in either the title or content
return _matchesFuzzy(lowerQuery, fileName) || _matchesFuzzy(lowerQuery, content);
}).toList();
}
static bool _matchesFuzzy(String query, String text) {
final words = query.split(RegExp(r'\s+')).where((word) => word.isNotEmpty);
return words.every((word) => text.contains(word));
}
}