pimterry/notes

Edit and save a file that is not of the default NOTES_EXT

Newchair2644 opened this issue · 3 comments

My NOTES_EXT variable is set to markdown, which is good, because that's what I take most of my notes in. However many of my "notes" are .c or .py files filled with comments on however the code in the file works. Whenever I open one of these files, notes makes a new blank .md file and names it exactly the same as the file I was trying to open instead of just opening the file. So, for example, if I were trying to open file.c, notes will make a new file titled file.c.md. I want to be able to edit .md notes along with .c and .py. Thanks!

Hi @NumenHackerCracker good suggestion, thanks. I think this sounds like an easy & sensible fix: if the exact filename exists, notes open should use that rather than appending an extension.

Would you be interested in putting together a PR to fix that? I think the main place the extension is used is here:

notes/notes

Lines 173 to 184 in 428946e

get_full_note_path() {
local note_path=$1
if [[ "$note_path" != *.$NOTES_EXT ]]; then
note_path="$note_path.$NOTES_EXT"
fi
if [ ! -f "$note_path" ]; then
note_path="$notes_dir/$note_path"
fi
echo "$note_path"
}
and it should be easy to add another test for this similar to:
@test "Accepts names without .md to open" {
touch $NOTES_DIRECTORY/note.md
run bash -c "$notes open note"
assert_success
assert_output "Editing $NOTES_DIRECTORY/note.md"
}

I've been doing some hacking on the script recently and I actually implemented this feature.

I think all I had to do was change get_full_note_path to just not add the file extension if $notes_dir/$notes_path is a file. If not, then test for the extension and add if not there.

Looking at the code you posted, I'm wondering about this:

if [ ! -f "$note_path" ]; then
    note_path="$notes_dir/$note_path"
fi

Is this there in case a user supplies an absolute path or something? it seems like the $nodes_dir should always be prepended, shouldn't it?

If you've got a fix for this issue in general, please do put in a PR, that'd be great.

Is this there in case a user supplies an absolute path or something?

Yes, I'm pretty sure that's why that's there. I don't remember the original reasoning for that (that line was last touched 5 years ago!) but it's a useful option to have, especially for composing notes with other CLI tools.