Skip to content

Commit

Permalink
Add ObsidianExtractNote command (#397)
Browse files Browse the repository at this point in the history
* 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
sotte and epwalsh authored Feb 13, 2024
1 parent ca6f49a commit 228bdbb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added `:ObsidianLinks` command.
- Added `:ObsidianExtractNote` command.

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ _Keep in mind this plugin is not meant to replace Obsidian, but to complement it

- `:ObsidianLinks` to collect all links within the current buffer into a picker window.

- `:ObsidianExtractNote` to extract the visually selected text into a new note and link to it.

- `:ObsidianWorkspace [NAME]` to switch to another workspace.

- `:ObsidianPasteImg [IMGNAME]` to paste an image from the clipboard into the note at the cursor position by saving it to the vault and adding a markdown image link. You can configure the default folder to save images to with the `attachments.img_folder` option.
Expand Down
37 changes: 37 additions & 0 deletions lua/obsidian/commands/extract_note.lua
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
6 changes: 6 additions & 0 deletions lua/obsidian/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local command_lookups = {
ObsidianWorkspace = "obsidian.commands.workspace",
ObsidianRename = "obsidian.commands.rename",
ObsidianPasteImg = "obsidian.commands.paste_img",
ObsidianExtractNote = "obsidian.commands.extract_note",
}

local M = setmetatable({
Expand Down Expand Up @@ -186,4 +187,9 @@ M.register(
{ opts = { nargs = "?", complete = "file", desc = "Paste and image from the clipboard" } }
)

M.register(
"ObsidianExtractNote",
{ opts = { nargs = "?", range = true, desc = "Extract selected text to a new note and link to it" } }
)

return M

0 comments on commit 228bdbb

Please sign in to comment.