Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url escaping with jq, support nested notes. #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions lua/obsidian-sync/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,34 @@ local api_env_var_name = "OBSIDIAN_REST_API_KEY"

local default_config = {
obsidian_server_address = "http://localhost:27123",
vault_path = nil,
}

api.nvim_create_augroup("obsidian-sync.nvim", {
clear = true,
})

local function url_encode(s)
local jq = "jq"
local jq_args = { "-Rj", "@uri" }
return vim.fn.system({ jq, unpack(jq_args) }, s)
end

local function url_encode_path(path)
local split_path = vim.fn.split(path, "/", false)
local encoded_split_path = {}
for _, subpath in ipairs(split_path) do
encoded_split_path[#encoded_split_path + 1] = url_encode(subpath)
end
return vim.fn.join(encoded_split_path, "/")
end

function M.setup(config)
local final_config = vim.tbl_extend("keep", config or {}, default_config)
if final_config.vault_path == nil then
vim.api.nvim_err_writeln("Error: vault_path is not set in obsidian_sync config.")
return
end

api.nvim_create_autocmd("BufEnter", {
callback = function()
Expand All @@ -33,16 +53,23 @@ function M.setup(config)
return
end

local vault_path = vim.fn.expand(final_config.vault_path)
local file_vault_rel_path = filename_incl_path:gsub(vault_path, "", 1)

local server_address = final_config.obsidian_server_address
local filename = string.match(filename_incl_path, ".+/(.+)$")
local url = server_address .. "/open/" .. filename
local authToken = "Bearer " .. api_key
local request = 'curl -s -X POST -H "Content-Type: application/json" -H "Authorization: '
.. authToken
.. '" '
.. url
.. " >/dev/null"
local handle = io.popen(request)
local url = server_address .. "/open/" .. url_encode_path(file_vault_rel_path)
local curl = "curl"
local curl_args = {
"-s",
"-X POST",
"-H",
'"Content-Type: application/json"',
"-H",
'"Authorization: Bearer ' .. api_key .. '"',
url,
">/dev/null",
}
local handle = io.popen(curl .. " " .. vim.fn.join(curl_args, " "))
if handle ~= nil then
local result = handle:read("*a")
handle:close()
Expand Down