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

feat: add builtin command ZkNewFromTitleAndContentSelection #210

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

NickvanDyke
Copy link

@NickvanDyke NickvanDyke commented Feb 19, 2025

I'm enjoying the plugin and appreciate the builtin commands ZkNewFromContentSelection and ZkNewFromTitleSelection to refactor and atomize growing notes. However the section I wish to extract frequently already has a markdown heading, e.g.

# Cats

...

## The origin of cats

- Nearly all cats, including lions, tigers and the domestic cat, are descended from one prehistoric creature.
- The oldest known relative, the African Wildcat, first developed over 10,000 years ago.

So I wrote a new command that will extract the selection's first line as the title (ignoring any starting non-characters like # and -), and the rest as the content. I think it complements the aforementioned builtins nicely and imagine I'm not the only one with this workflow, hence the PR. Feel free to disagree :)

local selected_text = util.get_selected_text()
assert(selected_text ~= nil, "No selected text")

local title, content = selected_text:match("%W*([^\n]+)\n+(.+)$")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a Lua newbie, there might be a better way to write this pattern. It has worked for me but may miss edge cases that my workflow doesn't hit.

assert(title ~= nil and content ~= nil, "No newline-delimited title and content found in selection")

options = options or {}
options.title = vim.fn.input("Title: ", title)
Copy link
Author

@NickvanDyke NickvanDyke Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted the ability to edit the extracted title. I know this is different from how the other commands pass { title = vim.fn.input('<user-chosen-prompt>') } rather than have the command do it internally. I considered a callback in options but thought that might be even less consistent. Let me know what you'd prefer.

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.

1 participant