mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 05:24:24 -05:00
Merge branch 'rnote' into dev
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
local M = {}
|
||||
|
||||
local default_config = {
|
||||
typstarRoot = nil,
|
||||
typstarRoot = nil, -- typstar installation location required to use default drawing templates (usually determined automatically)
|
||||
anki = {
|
||||
typstarAnkiCmd = 'typstar-anki',
|
||||
typstCmd = 'typst',
|
||||
@@ -13,8 +13,17 @@ 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',
|
||||
exportCommand = 'rnote-cli export selection --no-background --no-pattern --on-conflict overwrite --output-file %s all %s', -- can be modified to e.g. export full pages
|
||||
filename = 'drawing-%Y-%m-%d-%H-%M-%S',
|
||||
fileExtension = '.rnote',
|
||||
fileExtensionInserted = '.rnote.svg', -- valid rnote export type
|
||||
openCommand = 'xdg-open', -- see comment above for excalidraw
|
||||
templatePath = {},
|
||||
},
|
||||
snippets = {
|
||||
enable = true,
|
||||
@@ -38,10 +47,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)
|
||||
|
||||
108
lua/typstar/drawings.lua
Normal file
108
lua/typstar/drawings.lua
Normal file
@@ -0,0 +1,108 @@
|
||||
local M = {}
|
||||
local config = require('typstar.config')
|
||||
local utils = require('typstar.utils')
|
||||
|
||||
local affix = [[
|
||||
#figure(
|
||||
image("%s"),
|
||||
)
|
||||
]]
|
||||
local config_excalidraw = config.config.excalidraw
|
||||
local config_rnote = config.config.rnote
|
||||
|
||||
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)),
|
||||
false
|
||||
)
|
||||
end
|
||||
|
||||
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.openCommand, path), false)
|
||||
auto_export_rnote(path, path_inserted)
|
||||
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
|
||||
local path_inserted = cfg.assetsDir .. '/' .. filename .. cfg.fileExtensionInserted
|
||||
|
||||
if vim.fn.isdirectory(assets_dir) == 0 then vim.fn.mkdir(assets_dir, 'p') end
|
||||
local found_match = false
|
||||
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
|
||||
break
|
||||
end
|
||||
end
|
||||
if not found_match then
|
||||
print('No matching template found for path: ' .. path)
|
||||
return
|
||||
end
|
||||
|
||||
utils.insert_text_block(string.format(provider[2], path_inserted))
|
||||
provider[3](path, path_inserted)
|
||||
end
|
||||
|
||||
local excalidraw = {
|
||||
config_excalidraw,
|
||||
affix,
|
||||
launch_excalidraw,
|
||||
}
|
||||
local rnote = {
|
||||
config_rnote,
|
||||
affix,
|
||||
launch_rnote,
|
||||
}
|
||||
local providers = { excalidraw, rnote }
|
||||
|
||||
local open_drawing = function(prov)
|
||||
for _, provider in ipairs(prov) do
|
||||
local cfg = provider[1]
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
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
|
||||
local path_inserted = vim.fn.expand('%:p:h') .. '/' .. filename .. cfg.fileExtensionInserted
|
||||
provider[3](path, path_inserted) -- launch program
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M.insert_obsidian_excalidraw() insert_drawing(excalidraw) end
|
||||
function M.insert_rnote() insert_drawing(rnote) end
|
||||
function M.open_obsidian_excalidraw() open_drawing({ excalidraw }) end
|
||||
function M.open_rnote() open_drawing({ rnote }) end
|
||||
function M.open_drawing() open_drawing(providers) end
|
||||
|
||||
return M
|
||||
@@ -1,53 +0,0 @@
|
||||
local M = {}
|
||||
local config = require('typstar.config')
|
||||
local utils = require('typstar.utils')
|
||||
|
||||
local cfg = config.config.excalidraw
|
||||
local affix = [[
|
||||
#figure(
|
||||
image("%s"),
|
||||
)
|
||||
]]
|
||||
|
||||
local function launch_obsidian(path)
|
||||
print(string.format('Opening %s in Excalidraw', path))
|
||||
utils.run_shell_command(
|
||||
string.format('%s "obsidian://open?path=%s"', cfg.uriOpenCommand, utils.urlencode(path)),
|
||||
false
|
||||
)
|
||||
end
|
||||
|
||||
function M.insert_drawing()
|
||||
local assets_dir = vim.fn.expand('%:p:h') .. '/' .. cfg.assetsDir
|
||||
local filename = os.date(cfg.filename)
|
||||
local path = assets_dir .. '/' .. filename .. cfg.fileExtension
|
||||
local path_inserted = cfg.assetsDir .. '/' .. filename .. cfg.fileExtensionInserted
|
||||
|
||||
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
|
||||
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
|
||||
break
|
||||
end
|
||||
end
|
||||
if not found_match then
|
||||
print('No matching template found for the path: ' .. path)
|
||||
return
|
||||
end
|
||||
|
||||
utils.insert_text_block(string.format(affix, path_inserted))
|
||||
launch_obsidian(path)
|
||||
end
|
||||
|
||||
function M.open_drawing()
|
||||
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)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -6,15 +6,18 @@ local luasnip = nil
|
||||
M.setup = function(args)
|
||||
config.merge_config(args)
|
||||
local autosnippets = require('typstar.autosnippets')
|
||||
local excalidraw = require('typstar.excalidraw')
|
||||
local drawings = require('typstar.drawings')
|
||||
local anki = require('typstar.anki')
|
||||
|
||||
vim.api.nvim_create_user_command('TypstarToggleSnippets', autosnippets.toggle_autosnippets, {})
|
||||
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', excalidraw.insert_drawing, {})
|
||||
vim.api.nvim_create_user_command('TypstarOpenExcalidraw', excalidraw.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('TypstarOpenExcalidraw', drawings.open_obsidian_excalidraw, {})
|
||||
vim.api.nvim_create_user_command('TypstarOpenRnote', drawings.open_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, {})
|
||||
|
||||
@@ -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,17 @@ function M.run_shell_command(cmd, show_output, extra_handler)
|
||||
end
|
||||
end
|
||||
if show_output then
|
||||
vim.fn.jobstart(cmd, {
|
||||
on_stdout = function(_, data, _) handle_output(data, false) end,
|
||||
on_stderr = function(_, data, _) handle_output(data, true) end,
|
||||
stdout_buffered = false,
|
||||
stderr_buffered = true,
|
||||
})
|
||||
return vim.fn.jobstart(
|
||||
cmd,
|
||||
vim.tbl_deep_extend('force', {
|
||||
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)
|
||||
)
|
||||
else
|
||||
vim.fn.jobstart(cmd)
|
||||
return vim.fn.jobstart(cmd, opts)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user