diff --git a/src/notesViewProvider.ts b/src/notesViewProvider.ts index c40105d..c108e9f 100644 --- a/src/notesViewProvider.ts +++ b/src/notesViewProvider.ts @@ -1,6 +1,5 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; -import * as gl from 'glob'; import * as path from 'path'; import { Note } from './note'; @@ -58,32 +57,34 @@ export class NotesViewProvider implements vscode.TreeDataProvider { getNotes(notesLocation: string, notesExtensions: string): Note[] { // if the notes location exists if (this.pathExists(notesLocation)) { - const result: Note[] = []; + let entries: fs.Dirent[]; - // First, add all folders try { - const items = fs.readdirSync(notesLocation, { withFileTypes: true }); - - // Add folders first - for (const item of items) { - if (item.isDirectory()) { - const folderPath = path.join(notesLocation, item.name); - const folderNote = new Note( - item.name, - notesLocation, - '', // category - '', // tags - true // isDirectory - ); - result.push(folderNote); - } - } - - // Then add notes - const listOfNotes = (note: string): Note => { + entries = fs.readdirSync(notesLocation, { withFileTypes: true }); + } catch (err) { + console.error('Error reading directory:', err); + entries = []; + } + + const result: Note[] = entries + .filter(entry => { + return entry.name != ".DS_Store" && entry.name != ".git" && (notesExtensions === '*' || entry.isDirectory()|| notesExtensions.includes(path.extname(entry.name))) + }) + .map(entry => { + let note: Note; + + if (entry.isDirectory()) { + note = new Note( + entry.name, + notesLocation, + '', // category + '', // tags + true // isDirectory + ); + } else { // return a note with the given note name, notes location, empty category, empty tags, and the command to open the note - return new Note( - path.basename(note), + note = new Note( + entry.name, notesLocation, '', // category '', // tags @@ -91,34 +92,32 @@ export class NotesViewProvider implements vscode.TreeDataProvider { { command: 'Notes.openNote', title: '', - arguments: [path.join(notesLocation, note)] + arguments: [path.join(notesLocation, entry.name)] }); - }; - - // get the list of notes in the notes location - let notes; - if (notesExtensions === '*') { - // If '*' is specified, get all files (excluding directories) - notes = gl.sync('*', { cwd: notesLocation, nodir: true, nocase: true }).map(listOfNotes); - } else { - // Otherwise, filter by the specified extensions - notes = gl.sync(`*.{${notesExtensions}}`, { cwd: notesLocation, nodir: true, nocase: true }).map(listOfNotes); - } - result.push(...notes); - } catch (err) { - console.error('Error reading directory:', err); - } - - // Sort: folders first, then notes alphabetically - result.sort((a, b) => { - if (a.isFolder && !b.isFolder) { - return -1; - } - if (!a.isFolder && b.isFolder) { - return 1; - } - return a.name.localeCompare(b.name); - }); + } + + return { note, time: fs.statSync(note.fullPath).mtime.getTime() }; + }) + // Sort: folders first, then notes alphabetically + .sort((a, b) => { + if (a.note.isFolder && !b.note.isFolder) { + return -1; + } + + if (!a.note.isFolder && b.note.isFolder) { + return 1; + } + + // Sort descending newest to oldest + const timeDelta = b.time - a.time; + + if (timeDelta !== 0) { + return timeDelta; + } + + return a.note.name.localeCompare(b.note.name); + }) + .map(({ note }) => note); return result; }