feat(draw): auto export rnote on save

This commit is contained in:
arne314
2025-08-01 15:52:58 +02:00
parent 5827f6e553
commit 7c862f2fdc
3 changed files with 35 additions and 9 deletions

View File

@@ -18,9 +18,10 @@ local default_config = {
},
rnote = {
assetsDir = 'assets',
exportCommand = 'rnote-cli export doc --no-background --no-pattern --on-conflict overwrite --output-file %s %s',
filename = 'drawing-%Y-%m-%d-%H-%M-%S',
fileExtension = '.rnote',
fileExtensionInserted = '.rnote.svg',
fileExtensionInserted = '.rnote.svg', -- valid rnote export type
uriOpenCommand = 'xdg-open', -- see comment above for excalidraw
templatePath = {},
},

View File

@@ -10,7 +10,7 @@ local affix = [[
local config_excalidraw = config.config.excalidraw
local config_rnote = config.config.rnote
local function launch_excalidraw(path)
local function launch_excalidraw(path, path_inserted)
print(string.format('Opening %s in Obsidian Excalidraw', path))
utils.run_shell_command(
string.format('%s "obsidian://open?path=%s"', config_excalidraw.uriOpenCommand, utils.urlencode(path)),
@@ -18,9 +18,32 @@ local function launch_excalidraw(path)
)
end
local function launch_rnote(path)
local rnote_watched = {}
local function auto_export_rnote(path, path_inserted)
if rnote_watched[path] then return end
rnote_watched[path] = true
local job_id = -1
local last_export = 0
local run_export = function(err, filename)
local time = vim.uv.now()
if err ~= nil or time - last_export < 800 then return end
if job_id == -1 then
last_export = time
local cmd = string.format(config_rnote.exportCommand, path_inserted, path)
job_id = utils.run_shell_command(cmd, false, nil, { on_exit = function() job_id = -1 end })
end
end
local watcher = vim.uv.new_fs_event()
watcher:start(path, {}, vim.schedule_wrap(run_export))
end
local function launch_rnote(path, path_inserted)
print(string.format('Opening %s in Rnote', path))
utils.run_shell_command(string.format('%s %s', config_rnote.uriOpenCommand, path), false)
auto_export_rnote(path, path_inserted)
end
local function insert_drawing(provider)
@@ -47,7 +70,7 @@ local function insert_drawing(provider)
end
utils.insert_text_block(string.format(provider[2], path_inserted))
provider[3](path)
provider[3](path, path_inserted)
end
local excalidraw = {
@@ -72,7 +95,8 @@ function M.open_drawing()
local filename = line:match('"(.*)' .. string.gsub(cfg.fileExtensionInserted, '%.', '%%%.'))
if filename ~= nil and filename:match('^%s*$') == nil then
local path = vim.fn.expand('%:p:h') .. '/' .. filename .. cfg.fileExtension
provider[3](path) -- launch program
local path_inserted = vim.fn.expand('%:p:h') .. '/' .. filename .. cfg.fileExtensionInserted
provider[3](path, path_inserted) -- launch program
break
end
end

View File

@@ -26,8 +26,9 @@ function M.insert_text_block(snip)
vim.api.nvim_buf_set_lines(vim.api.nvim_get_current_buf(), line_num, line_num, false, lines)
end
function M.run_shell_command(cmd, show_output, extra_handler)
function M.run_shell_command(cmd, show_output, extra_handler, opts)
extra_handler = extra_handler or function(msg) end
opts = opts or { on_exit = function() end }
local handle_output = function(data, err)
local msg = table.concat(data, '\n')
if not string.match(msg, '^%s*$') then
@@ -37,14 +38,14 @@ function M.run_shell_command(cmd, show_output, extra_handler)
end
end
if show_output then
vim.fn.jobstart(cmd, {
return vim.fn.jobstart(cmd, vim.tbl_deep_extend({
on_stdout = function(_, data, _) handle_output(data, false) end,
on_stderr = function(_, data, _) handle_output(data, true) end,
stdout_buffered = false,
stderr_buffered = true,
})
}, opts, "force"))
else
vim.fn.jobstart(cmd)
return vim.fn.jobstart(cmd, opts)
end
end