Skip to content

Commit cd02a4b

Browse files
authored
Merge pull request #84 from eshapard/master
Add support for explicit file extensions
2 parents fc584b3 + 55f25fc commit cd02a4b

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ There are also more complex options available. You can set any configuration pro
105105

106106
Opens your `$EDITOR` of choice for a new note, with the given name. The name can include slashes, if you want to put your note in a subfolder. Leave out the name if you want one to be generated for you (e.g. `quicknote-2016-12-21.md` - format configurable with `$QUICKNOTE_FORMAT`). If you want to place a quicknote in a subfolder, use a trailing slash: `notes new subfolder/`. Shorthand alias also available with `notes n`.
107107

108+
If you do not supply an extension in `note-name`, it will be automatically appended with the default file extension (e.g. "newnote" will become "newnote.md"). However, if you include a one-to-four-letter file extension, notes will use that extension when creating the file (e.g. "newnote.tex" is created as "newnote.tex"; not "newnote.md", or "newnote.tex.md").
109+
108110
### `notes find <part-of-a-note-name>`
109111

110112
Searches note filenames and paths for the given string, and returns every single match. If no pattern is specified, this returns every single note. Shorthand alias also available with `notes f`.
@@ -129,6 +131,8 @@ Opens your notes folder in your default configured file explorer. Shorthand alia
129131

130132
Opens a given note in your `$EDITOR`. Name can be an absolute path, or a relative path in your notes (.md suffix optional). Shorthand alias also available with `notes o`.
131133

134+
If no file-suffix is given in `note-name`, the notes will attempt to open `note-name.md` (or whatever your default suffix is set to). However, if the note-name is given an suffix, the default suffix will not be appended (e.g. `notes open note-name.txt` will open `note-name.txt`; not `note-name.md` or `note-name.txt.md`).
135+
132136
### `notes mv <note-name> <destination>|<directory>`
133137

134138
Renames a given note to destination or moves the note to directory. Name can be an absolute path, or a relative path in your notes (.md suffix optional). Destination and directory have to be a relative path in your notes.

notes

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ remove_note() {
151151
local note_name="$*"
152152
local to_remove="$notes_dir/$note_name"
153153

154-
if [ -f "$notes_dir/$note_name.$NOTES_EXT" ]; then
154+
if [ ! -f "$to_remove" ] && [ -f "$notes_dir/$note_name.$NOTES_EXT" ]; then
155+
# append default extension only if no file exists with exact filename given
155156
to_remove="$notes_dir/$note_name.$NOTES_EXT"
156157
fi
157158
rm "${rm_args[@]}" "$to_remove"
@@ -173,10 +174,19 @@ handle_multiple_notes() {
173174
get_full_note_path() {
174175
local note_path=$1
175176

176-
if [[ "$note_path" != *.$NOTES_EXT ]]; then
177-
note_path="$note_path.$NOTES_EXT"
178-
fi
179-
if [ ! -f "$note_path" ]; then
177+
# first check if file exists
178+
if [ -f "$note_path" ]; then # note path given is good absolute or relative path
179+
note_path="$note_path"
180+
elif [ -f "$notes_dir/$note_path" ]; then # exists in notes_dir
181+
note_path="$notes_dir/$note_path"
182+
elif [ -f "$notes_dir/$note_path.$NOTES_EXT" ]; then # note with this name and default extension exists
183+
note_path="$notes_dir/$note_path.$NOTES_EXT"
184+
elif echo "$note_path" | grep '[.][A-Za-z]\{1,4\}$' &>/dev/null; then # given name has a 1-4 letter extension
185+
note_path="$notes_dir/$note_path"
186+
else
187+
if [[ "$note_path" != *.$NOTES_EXT ]]; then
188+
note_path="$note_path.$NOTES_EXT"
189+
fi
180190
note_path="$notes_dir/$note_path"
181191
fi
182192

test/test-new.bats

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,10 @@ notes="./notes"
7979
assert_success
8080
assert_exists "$NOTES_DIRECTORY/subfolder/quicknote-$today.md"
8181
}
82+
83+
@test "Should use explicitly named file extensions" {
84+
run $notes new explicit-ext.zzz
85+
86+
assert_success
87+
assert_exists "$NOTES_DIRECTORY/explicit-ext.zzz"
88+
}

test/test-open.bats

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,12 @@ notes="./notes"
123123
assert_failure
124124
assert_output "Please set \$EDITOR to edit notes"
125125
}
126+
127+
@test "Accepts names with other file extensions to open" {
128+
touch $NOTES_DIRECTORY/test-note.txt
129+
130+
run bash -c "$notes open test-note.txt"
131+
132+
assert_success
133+
assert_output "Editing $NOTES_DIRECTORY/test-note.txt"
134+
}

0 commit comments

Comments
 (0)