mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 05:24:24 -05:00
feat(draw)!: insert and open rnote drawings
`:TypstarOpenExcalidraw` has been renamed to `:TypstarOpenDrawing` to handle both Excalidraw and Rnote
This commit is contained in:
@@ -13,8 +13,16 @@ local default_config = {
|
||||
filename = 'drawing-%Y-%m-%d-%H-%M-%S',
|
||||
fileExtension = '.excalidraw.md',
|
||||
fileExtensionInserted = '.excalidraw.svg',
|
||||
uriOpenCommand = 'xdg-open', -- set depending on OS
|
||||
templatePath = nil,
|
||||
uriOpenCommand = 'xdg-open', -- set depending on OS; try setting it to "obsidian" directly if you encounter problems and have it in your PATH
|
||||
templatePath = {},
|
||||
},
|
||||
rnote = {
|
||||
assetsDir = 'assets',
|
||||
filename = 'drawing-%Y-%m-%d-%H-%M-%S',
|
||||
fileExtension = '.rnote',
|
||||
fileExtensionInserted = '.rnote.svg',
|
||||
uriOpenCommand = 'xdg-open', -- see comment above for excalidraw
|
||||
templatePath = {},
|
||||
},
|
||||
snippets = {
|
||||
enable = true,
|
||||
@@ -38,10 +46,12 @@ function M.merge_config(args)
|
||||
M.config.typstarRoot = M.config.typstarRoot
|
||||
or debug.getinfo(1).source:match('^@(.*)/lua/typstar/config%.lua$')
|
||||
or '~/typstar'
|
||||
M.config.excalidraw.templatePath = M.config.excalidraw.templatePath
|
||||
or {
|
||||
['%.excalidraw%.md$'] = M.config.typstarRoot .. '/res/excalidraw_template.excalidraw.md',
|
||||
}
|
||||
vim.list_extend(M.config.excalidraw.templatePath, {
|
||||
{ '%.excalidraw%.md$', M.config.typstarRoot .. '/res/excalidraw_template.excalidraw.md' },
|
||||
})
|
||||
vim.list_extend(M.config.rnote.templatePath, {
|
||||
{ '%.rnote$', M.config.typstarRoot .. '/res/rnote_template.rnote' },
|
||||
})
|
||||
end
|
||||
|
||||
M.merge_config(nil)
|
||||
|
||||
@@ -2,22 +2,29 @@ local M = {}
|
||||
local config = require('typstar.config')
|
||||
local utils = require('typstar.utils')
|
||||
|
||||
local cfg = config.config.excalidraw
|
||||
local affix = [[
|
||||
#figure(
|
||||
image("%s"),
|
||||
)
|
||||
]]
|
||||
local config_excalidraw = config.config.excalidraw
|
||||
local config_rnote = config.config.rnote
|
||||
|
||||
local function launch_obsidian(path)
|
||||
print(string.format('Opening %s in Excalidraw', path))
|
||||
local function launch_excalidraw(path)
|
||||
print(string.format('Opening %s in Obsidian Excalidraw', path))
|
||||
utils.run_shell_command(
|
||||
string.format('%s "obsidian://open?path=%s"', cfg.uriOpenCommand, utils.urlencode(path)),
|
||||
string.format('%s "obsidian://open?path=%s"', config_excalidraw.uriOpenCommand, utils.urlencode(path)),
|
||||
false
|
||||
)
|
||||
end
|
||||
|
||||
function M.insert_drawing()
|
||||
local function launch_rnote(path)
|
||||
print(string.format('Opening %s in Rnote', path))
|
||||
utils.run_shell_command(string.format('%s %s', config_rnote.uriOpenCommand, path), false)
|
||||
end
|
||||
|
||||
local function insert_drawing(provider)
|
||||
local cfg = provider[1]
|
||||
local assets_dir = vim.fn.expand('%:p:h') .. '/' .. cfg.assetsDir
|
||||
local filename = os.date(cfg.filename)
|
||||
local path = assets_dir .. '/' .. filename .. cfg.fileExtension
|
||||
@@ -25,7 +32,9 @@ function M.insert_drawing()
|
||||
|
||||
if vim.fn.isdirectory(assets_dir) == 0 then vim.fn.mkdir(assets_dir, 'p') end
|
||||
local found_match = false
|
||||
for pattern, template_path in pairs(cfg.templatePath) do
|
||||
for _, template_config in ipairs(cfg.templatePath) do
|
||||
local pattern = template_config[1]
|
||||
local template_path = template_config[2]
|
||||
if string.match(path, pattern) then
|
||||
found_match = true
|
||||
utils.run_shell_command(string.format('cat %s > %s', template_path, path), false) -- don't copy file metadata
|
||||
@@ -33,21 +42,40 @@ function M.insert_drawing()
|
||||
end
|
||||
end
|
||||
if not found_match then
|
||||
print('No matching template found for the path: ' .. path)
|
||||
print('No matching template found for path: ' .. path)
|
||||
return
|
||||
end
|
||||
|
||||
utils.insert_text_block(string.format(affix, path_inserted))
|
||||
launch_obsidian(path)
|
||||
utils.insert_text_block(string.format(provider[2], path_inserted))
|
||||
provider[3](path)
|
||||
end
|
||||
|
||||
local excalidraw = {
|
||||
config_excalidraw,
|
||||
affix,
|
||||
launch_excalidraw,
|
||||
}
|
||||
local rnote = {
|
||||
config_rnote,
|
||||
affix,
|
||||
launch_rnote,
|
||||
}
|
||||
local providers = { excalidraw, rnote }
|
||||
|
||||
function M.insert_obsidian_excalidraw() insert_drawing(excalidraw) end
|
||||
function M.insert_rnote() insert_drawing(rnote) end
|
||||
|
||||
function M.open_drawing()
|
||||
for _, provider in pairs(providers) do
|
||||
local cfg = provider[1]
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local path = vim.fn.expand('%:p:h')
|
||||
.. '/'
|
||||
.. string.match(line, '"(.*)' .. string.gsub(cfg.fileExtensionInserted, '%.', '%%%.'))
|
||||
.. '.excalidraw.md'
|
||||
launch_obsidian(path)
|
||||
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
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -13,8 +13,9 @@ M.setup = function(args)
|
||||
vim.api.nvim_create_user_command('TypstarSmartJump', function() M.smart_jump(1) end, {})
|
||||
vim.api.nvim_create_user_command('TypstarSmartJumpBack', function() M.smart_jump(-1) end, {})
|
||||
|
||||
vim.api.nvim_create_user_command('TypstarInsertExcalidraw', drawings.insert_drawing, {})
|
||||
vim.api.nvim_create_user_command('TypstarOpenExcalidraw', drawings.open_drawing, {})
|
||||
vim.api.nvim_create_user_command('TypstarInsertExcalidraw', drawings.insert_obsidian_excalidraw, {})
|
||||
vim.api.nvim_create_user_command('TypstarInsertRnote', drawings.insert_rnote, {})
|
||||
vim.api.nvim_create_user_command('TypstarOpenDrawing', drawings.open_drawing, {})
|
||||
|
||||
vim.api.nvim_create_user_command('TypstarAnkiScan', anki.scan, {})
|
||||
vim.api.nvim_create_user_command('TypstarAnkiReimport', anki.scan_reimport, {})
|
||||
|
||||
Reference in New Issue
Block a user