Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor change to behavior of :ObsidianNew command #188

Merged
merged 2 commits into from
Sep 8, 2023
Merged

Conversation

epwalsh
Copy link
Owner

@epwalsh epwalsh commented Sep 7, 2023

Fixes #184.

The argument to the :ObsidianNew command can now be in one of 3 different forms which determine the behavior of the command:

  1. A raw title without any path components, e.g. :ObsidianNew Foo. In this case the command will pass the title to the note_id_func and put the note in the default location.
  2. A title prefixed by a path component, e.g. :ObsidianNew notes/Foo. In this case the command will pass the title "Foo" to the note_id_func and put the note in the directory of the path prefix "notes/".
  3. An exact path, e.g. :ObsidianNew notes/foo.md. In this case the command will put the new note at the path given and the title will be inferred from the filename ("foo").

The argument to this command can be in one of 3 different forms which determine the behavior of the command:
  1. A raw title without any path components, e.g. `:ObsidianNew Foo`. In this case the command will pass the title to the `note_id_func` and put the note in the default location.
  2. A title prefixed by a path component, e.g. `:ObsidianNew notes/Foo`. In this case the command will pass the title "Foo" to the `note_id_func` and put the note in the directory of the path prefix "notes/".
  3. An exact path, e.g. `:ObsidianNew notes/foo.md`. In this case the command will put the new note at the path given and the title will be inferred from the filename ("foo").
@peeeyow
Copy link
Contributor

peeeyow commented Sep 8, 2023

@epwalsh
Thanks for this feature. I have the tested it using the following config and inputs. Please verify if these are the intended behaviour.

require("obsidian").setup({
    -- other fields ...
    notes_subdir="",
    note_id_func = function(title)
      local suffix = ""
      if title ~= nil then
        suffix = title:gsub(" ", "-"):gsub("[^A-Za-z0-9-]", ""):lower()
      else
        for _ = 1, 4 do
          suffix = suffix .. string.char(math.random(65, 90))
        end
      end
      return tostring(os.date "%Y%m%d%H%M%S") .. "-" .. suffix
    end,
})

Path-like arguments

input output file
dailies/new note dailies/20230908073035-new-note.md
dailies/new;note ; dailies/20230908073035-newnote--.md
dailies/ dailies/20230908073035-.md
dailies/ dailies/20230908073035-----.md
dailies/new note.md dailies/new-note.md
dailies/new;note ;.md dailies/new;note ;.md
dailies/.md dailies/.md
dailies/ .md dailies/ .md

Filename only arguments

input output file
new note 20230908073035-new-note.md
new;note ; 20230908073035-newnote--.md
`` 20230908073035-NHQQ.md
20230908073035-NHQQ.md
new note.md new-note.md
new;note ;.md new;note ;.md
.md .md
.md .md

Noticeable difference between path-like and non path-like arguments

For arguments with empty title (either blank or white spaces only) without suffix .md:

input title passed on note_id_func
dailies/ "" (empty string)
dailies/ " " (string field with spaces)
`` nil
nil

For arguments with empty title (either blank or white spaces only) with suffix .md:

input output file
dailies/.md dailies/.md
dailies/ .md dailies/ .md (spaces are preserved)
.md .md
.md .md (spaces are striped)

I believe this is already great. Thank you very much.

@epwalsh
Copy link
Owner Author

epwalsh commented Sep 8, 2023

Thanks for the thorough testing @peeeyow! I've added some tests and better handling of these edge cases. 747d8ae

@epwalsh epwalsh merged commit 41bb0ca into main Sep 8, 2023
@epwalsh epwalsh deleted the ObsidianNew-changes branch September 8, 2023 16:56
FlynnD273 pushed a commit to FlynnD273/obsidian.nvim that referenced this pull request Sep 23, 2023
* Minor change to behavior of `:ObsidianNew` command

The argument to this command can be in one of 3 different forms which determine the behavior of the command:
  1. A raw title without any path components, e.g. `:ObsidianNew Foo`. In this case the command will pass the title to the `note_id_func` and put the note in the default location.
  2. A title prefixed by a path component, e.g. `:ObsidianNew notes/Foo`. In this case the command will pass the title "Foo" to the `note_id_func` and put the note in the directory of the path prefix "notes/".
  3. An exact path, e.g. `:ObsidianNew notes/foo.md`. In this case the command will put the new note at the path given and the title will be inferred from the filename ("foo").

* add tests, catch some edge cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating new notes using with path does not trigger note_id_func
2 participants