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

Auto rename file by note title on save #27

Closed
zhengpd opened this issue Jan 20, 2022 · 18 comments
Closed

Auto rename file by note title on save #27

zhengpd opened this issue Jan 20, 2022 · 18 comments
Labels

Comments

@zhengpd
Copy link
Contributor

zhengpd commented Jan 20, 2022

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
Copy link
Collaborator

kabouzeid commented Jan 20, 2022

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
Copy link
Collaborator

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
Copy link
Collaborator

kabouzeid commented Jan 20, 2022

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
Copy link
Contributor Author

zhengpd commented Jan 20, 2022

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

@mickael-menu
Copy link
Member

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

@kabouzeid
Copy link
Collaborator

kabouzeid commented Jan 23, 2022

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
Copy link
Member

No I thought of two different refactorings:

  • rename file which would update the file path
  • rename title which would update the note title, and also regenerate the file path if it uses the title

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
Copy link
Collaborator

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
Copy link
Member

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
Copy link
Collaborator

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
Copy link
Member

Yes that's right.

@github-actions
Copy link

github-actions bot commented Mar 1, 2022

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 github-actions bot added the stale label Mar 1, 2022
@github-actions
Copy link

github-actions bot commented Mar 7, 2022

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

@github-actions github-actions bot closed this as completed Mar 7, 2022
@piotryordanov
Copy link

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
Copy link
Member

@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
Copy link

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

@mickael-menu
Copy link
Member

I created a new issue to track this feature: zk-org/zk#200

@mickael-menu
Copy link
Member

I wrote a quick spec for the rename/move features here: zk-org/zk#200 (comment)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants