Closed zhengpd closed 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).
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?
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
@kabouzeid @eric-hansen Thanks for the help. I didn't think about the notebook_root
earlier. Will try it later.
For the original feature request, I think this would best be served with a "rename" refactoring code action in the zk
LSP server.
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.
No I thought of two different refactorings:
rename file
which would update the file pathrename title
which would update the note title, and also regenerate the file path if it uses the titleBoth 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.
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.
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.
Ah yes, I confused file
and title
in my last comment.
So for file rename, zk rename "foo" "bar"
should
mv foo.md bar.md
[description](foo)
-> [description](bar)
[[foo]]
-> [[bar]]
Yes that's right.
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.
This issue was closed because it has been stalled for 5 days with no activity.
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!🙏
@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. 😉
@mickael-menu no worries at all. Thx for keeping us posted :)
I created a new issue to track this feature: https://github.com/mickael-menu/zk/issues/200
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!
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?