mirror of
https://github.com/jiriks74/presence.nvim
synced 2024-12-28 19:02:35 +01:00
Validate and correct repo url for discord button (#34)
* match ssh url format and generate https url for discord buttons * only check for ssh url format after nil check * kind of validate repository urls * small fixes * use actual function Resolves #33
This commit is contained in:
parent
e632306af1
commit
bcd4b98de6
1 changed files with 31 additions and 0 deletions
|
@ -693,6 +693,37 @@ function Presence:get_buttons(buffer, parent_dirpath)
|
|||
|
||||
-- Default behavior to show a "View Repository" button if the repo URL is valid
|
||||
if repo_url then
|
||||
|
||||
-- Check if repo url uses short ssh syntax
|
||||
local domain, project = repo_url:match("^git@(.+):(.+)$")
|
||||
if domain and project then
|
||||
self.log:debug(string.format("Repository URL uses short ssh syntax: %s", repo_url))
|
||||
repo_url = string.format("https://%s/%s", domain, project)
|
||||
end
|
||||
|
||||
-- Check if repo url uses a valid protocol
|
||||
local protocols = {
|
||||
"ftp",
|
||||
"git",
|
||||
"http",
|
||||
"https",
|
||||
"ssh",
|
||||
}
|
||||
local protocol, relative = repo_url:match("^(.+)://(.+)$")
|
||||
if not vim.tbl_contains(protocols, protocol) or not relative then
|
||||
self.log:debug(string.format("Repository URL uses invalid protocol: %s", repo_url))
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Check if repo url has the user specified
|
||||
local user, path = relative:match("^(.+)@(.+)$")
|
||||
if user and path then
|
||||
self.log:debug(string.format("Repository URL has user specified: %s", repo_url))
|
||||
repo_url = string.format("https://%s", path)
|
||||
else
|
||||
repo_url = string.format("https://%s", relative)
|
||||
end
|
||||
|
||||
self.log:debug(string.format("Adding button with repository URL: %s", repo_url))
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue