zk-org / zk-nvim

Neovim extension for zk
https://github.com/zk-org/zk
GNU General Public License v3.0
530 stars 42 forks source link

Auto rename file by note title on save #27

Closed zhengpd closed 2 years ago

zhengpd commented 2 years ago

Hi, I want to auto rename the file based on note title on saving buffer, if title changed. And only rename if current buffer is a note.

Is there any buffer local flag which can tell if current buffer is a ZK note buffer?

kabouzeid commented 2 years ago

You can use

vim.bo.filetype == "markdown" and require("zk.util").notebook_root(vim.api.nvim_buf_get_name(0)) ~= nil

to check if the current buffer is a ZK note.

Instead of checking vim.bo.filetype == "markdown" you could also place your code in a ftplugin ~/.config/nvim/ftplugin/markdown.lua, see (https://github.com/mickael-menu/zk-nvim#example-mappings).

eric-hansen commented 2 years ago

Can't you use https://github.com/mickael-menu/zk-nvim/blob/main/lua/zk/util.lua#L7 and the function below it as well?

kabouzeid commented 2 years ago

Right now there's no built-in way to synchronously get the title of a note. But you can use the async api:

function get_buf_zk_title()
  local filename = vim.api.nvim_buf_get_name(0)
  require("zk.api").list(filename, { select = { "title" }, hrefs = { filename } }, function(notes)
    local title = notes[1].title
    -- do something with the title here
  end)
end
zhengpd commented 2 years ago

@kabouzeid @eric-hansen Thanks for the help. I didn't think about the notebook_root earlier. Will try it later.

mickael-menu commented 2 years ago

For the original feature request, I think this would best be served with a "rename" refactoring code action in the zk LSP server.

kabouzeid commented 2 years ago

This would be a code action that renames the current file to match the note title? We would also need to update all links to the note then. Could be tedious to implement.

Btw, renaming files is possible via LSP (workspace/applyEdit and https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#renameFile). Not sure which clients support "RenameFile" though.

mickael-menu commented 2 years ago

No I thought of two different refactorings:

Both of these would update the links in other notes if possible.

This would be a zk rename/mv command that could be accessible through LSP.

I don't think we need a command dedicated to update the file to match the current note title. If there's a refactoring code action, you should use it to retitle and propagate the changes.

kabouzeid commented 2 years ago

I don't think zk rename file would add any value; why wouldn't I just use mv <src> <dst>?

zk rename title on the other hand could be useful if it also updates affected links. Otherwise I don't see why I wouldn't just directly update the title in the file.

mickael-menu commented 2 years ago

zk rename file would also update the links in other notes (I'm personally using regular Markdown links, not wiki links).

And yes the idea for changing the title is also to update the filename and also all the links.

kabouzeid commented 2 years ago

Ah yes, I confused file and title in my last comment. So for file rename, zk rename "foo" "bar" should

  1. mv foo.md bar.md
  2. [description](foo) -> [description](bar)
  3. [[foo]] -> [[bar]]
mickael-menu commented 2 years ago

Yes that's right.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in the next 5 days.

github-actions[bot] commented 2 years ago

This issue was closed because it has been stalled for 5 days with no activity.

piotryordanov commented 2 years ago

Any updates on this? I'm also using markdown links, and would like to be able to update the title in backlinks, when the note's title is updated. Is that something supported in zk or zk-vim ?

Thx guys!🙏

mickael-menu commented 2 years ago

@piotryordanov It's currently not supported. Given I don't have much time for zk lately, it probably won't be available soon. Expect if some contributor wants to tackle this. 😉

piotryordanov commented 2 years ago

@mickael-menu no worries at all. Thx for keeping us posted :)

mickael-menu commented 2 years ago

I created a new issue to track this feature: https://github.com/mickael-menu/zk/issues/200

mickael-menu commented 2 years ago

I wrote a quick spec for the rename/move features here: https://github.com/mickael-menu/zk/issues/200#issuecomment-1126803588

Feel free to review and comment if you're interested in this!