Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 50 additions & 51 deletions src/notesViewProvider.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -58,67 +57,67 @@ export class NotesViewProvider implements vscode.TreeDataProvider<Note> {
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
false, // isDirectory
{
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;
}
Expand Down