-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ObsidianExtractNote
command (#397)
* feat: ObsidianExtractNote * code review * CR: only replace the selected text, not the entire line Co-authored-by: Pete <[email protected]> --------- Co-authored-by: Pete <[email protected]>
- Loading branch information
Showing
4 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
local log = require "obsidian.log" | ||
local util = require "obsidian.util" | ||
|
||
---Extract the selected text into a new note | ||
---and replace the selection with a link to the new note. | ||
--- | ||
---@param client obsidian.Client | ||
return function(client, data) | ||
local viz = util.get_visual_selection() | ||
if viz.lines == nil or #viz.lines == 0 then | ||
log.err "ObsidianExtractNote must be called with visual selection" | ||
return | ||
end | ||
|
||
local content | ||
if data.args ~= nil and string.len(data.args) > 0 then | ||
content = { data.args } | ||
else | ||
content = viz.lines | ||
end | ||
|
||
-- new note with title from user | ||
local title = vim.fn.input { prompt = "Enter title (optional): " } | ||
if string.len(title) == 0 then | ||
title = nil | ||
end | ||
local note = client:new_note(title) | ||
|
||
-- replace selection with link to new note | ||
local link = client:format_link(note) | ||
vim.api.nvim_buf_set_text(0, viz.csrow - 1, viz.cscol - 1, viz.cerow - 1, viz.cecol, { link }) | ||
|
||
-- add the selected text to the end of the new note | ||
local open_in = util.get_open_strategy(client.opts.open_notes_in) | ||
vim.cmd(open_in .. tostring(note.path)) | ||
vim.api.nvim_buf_set_lines(0, -1, -1, false, content) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters