mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 05:24:24 -05:00
feat: insert & open obsidian excalidraw drawings
This commit is contained in:
21
lua/typstar/config.lua
Normal file
21
lua/typstar/config.lua
Normal file
@@ -0,0 +1,21 @@
|
||||
local M = {}
|
||||
|
||||
local default_config = {
|
||||
typstarRoot = '~/typstar',
|
||||
excalidraw = {
|
||||
assetsDir = 'assets',
|
||||
filename = 'drawing-%Y-%m-%d-%H-%M-%S',
|
||||
fileExtensionInserted = '.excalidraw.svg',
|
||||
obsidianOpenConfig = nil,
|
||||
}
|
||||
}
|
||||
|
||||
function M.merge_config(args)
|
||||
M.config = vim.tbl_deep_extend('force', default_config, args or {})
|
||||
M.config.excalidraw.obsidianOpenConfig = M.config.excalidraw.obsidianOpenConfig or
|
||||
M.config.typstarRoot .. '/res/obsidian_open_config_example.json'
|
||||
end
|
||||
|
||||
M.merge_config(nil)
|
||||
|
||||
return M
|
||||
41
lua/typstar/excalidraw.lua
Normal file
41
lua/typstar/excalidraw.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
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_open(path)
|
||||
print(string.format('Opening %s in Excalidraw', path))
|
||||
utils.run_shell_command('python3 ' ..
|
||||
config.config.typstarRoot .. '/python/obsidian_open.py ' ..
|
||||
path .. ' --config ' .. cfg.obsidianOpenConfig)
|
||||
end
|
||||
|
||||
|
||||
function M.insert_drawing()
|
||||
local assets_dir = vim.fn.expand('%:p:h') .. '/' .. cfg.assetsDir
|
||||
if vim.fn.isdirectory(assets_dir) == 0 then
|
||||
vim.fn.mkdir(assets_dir, 'p')
|
||||
end
|
||||
local filename = os.date(cfg.filename)
|
||||
local path = assets_dir .. '/' .. filename .. '.excalidraw.md'
|
||||
local path_inserted = cfg.assetsDir .. '/' .. filename .. cfg.fileExtensionInserted
|
||||
utils.insert_snippet(string.format(affix, path_inserted))
|
||||
launch_obsidian_open(path)
|
||||
end
|
||||
|
||||
function M.open_drawing()
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local path = vim.fn.expand('%:p:h') ..
|
||||
'/' .. string.match(line, 'image%("(.*)' .. string.gsub(cfg.fileExtensionInserted, '%.', '%%%.')) ..
|
||||
'.excalidraw.md'
|
||||
launch_obsidian_open(path)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,12 +1,13 @@
|
||||
local M = {}
|
||||
|
||||
local default_config = {
|
||||
}
|
||||
local config = require('typstar.config')
|
||||
local excalidraw = require('typstar.excalidraw')
|
||||
|
||||
M.config = default_config
|
||||
vim.api.nvim_create_user_command('TypstarInsertExcalidraw', excalidraw.insert_drawing, {})
|
||||
vim.api.nvim_create_user_command('TypstarOpenExcalidraw', excalidraw.open_drawing, {})
|
||||
|
||||
M.setup = function(args)
|
||||
M.config = vim.tbl_deep_extend("force", M.config, args or {})
|
||||
config.merge_config(args)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
16
lua/typstar/utils.lua
Normal file
16
lua/typstar/utils.lua
Normal file
@@ -0,0 +1,16 @@
|
||||
local M = {}
|
||||
|
||||
function M.insert_snippet(snip)
|
||||
local line_num = vim.fn.getcurpos()[2]
|
||||
local lines = {}
|
||||
for line in snip:gmatch '[^\r\n]+' do
|
||||
table.insert(lines, line)
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(0, line_num, line_num, false, lines)
|
||||
end
|
||||
|
||||
function M.run_shell_command(cmd)
|
||||
vim.fn.jobstart(cmd)
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user