epwalsh / obsidian.nvim

Obsidian 🤝 Neovim
Apache License 2.0
3.91k stars 178 forks source link

ObsidianNew // note_id_func error when title is nil #52

Closed xd999e closed 1 year ago

xd999e commented 1 year ago

🐛 Describe the bug

Hi, I'm using this code:

  dir = "~/Notes/",
  completion = {
    nvim_cmp = true, -- if using nvim-cmp, otherwise set to false
  },
  daily_notes = {
    folder = "daily_notes",
},
 note_id_func = function(title)
    -- Create note IDs in a Zettelkasten format with a timestamp and a suffix.
    local suffix = ""
    if title ~= nil then
      -- If title is given, transform it into valid file name.
      suffix = title:gsub(" ", "-"):gsub("[^A-Za-z0-9-]", ""):lower()
    else
      -- If title is nil, just add 4 random uppercase letters to the suffix.
      for _ in 1, 4 do
        suffix = suffix .. string.char(math.random(65, 90))
      end
    end
    return tostring(os.time()) .. "-" .. suffix
  end
})

When I'm create a new note with :ObsidianNew (without Title), I get the following error:

Error executing Lua callback: /home/user/.config/nvim/lua/user/obsidian.lua:18: attempt to call a number value stack traceback: /home/user/.config/nvim/lua/user/obsidian.lua:18: in function 'note_id_func' ...te/pack/packer/start/obsidian.nvim/lua/obsidian/init.lua:182: in function 'new_note_id' ...te/pack/packer/start/obsidian.nvim/lua/obsidian/init.lua:198: in function 'new_note' ...pack/packer/start/obsidian.nvim/lua/obsidian/command.lua:66: in function 'func' ...pack/packer/start/obsidian.nvim/lua/obsidian/command.lua:375: in function <...pack/packer/start/obsidian.nvim/lua/obsidian/command.lua:374>

When I'm using :ObsidianNew with a title, everything work fine.

Versions

YanniPapandreou commented 1 year ago

Hi, I've also come across this. I fixed it by changing the for loop in the README from:

for _ in 1, 4 do
    suffix = suffix .. string.char(math.random(65, 90))
end

to:

for _ = 1, 4 do
    suffix = suffix .. string.char(math.random(65, 90))
end

Not sure if this is a typo or potentially some change depending on your version of Lua as I am quite new to Lua.

epwalsh commented 1 year ago

Ah, yeup, thanks @YanniPapandreou, just fixed: 40f0979