Skip to content

Commit

Permalink
fix: improved url pattern string (#30)
Browse files Browse the repository at this point in the history
* fix: improved url pattern string

replaced 0-9 with %d
added %% which is used for matching safe string urls

* test: added test for encoded urls
  • Loading branch information
jstnas authored Sep 29, 2023
1 parent aab77c3 commit b967c7a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lua/gx/handlers/url.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ local M = {

-- get url from line (with http/s)
function M.handle(mode, line, _)
local pattern = "(https?://[a-zA-Z0-9_/%-%.~@\\+#=?&:]+)"
local pattern = "(https?://[a-zA-Z%d_/%%%-%.~@\\+#=?&:]+)"
local url = helper.find(line, mode, pattern)

-- match url without http(s)
if not url then
pattern = "([a-zA-Z0-9_/%-%.~@\\+#]+%.[a-zA-Z0-9_/%-%.~@\\+#%=?&:]+)"
pattern = "([a-zA-Z%d_/%-%.~@\\+#]+%.[a-zA-Z%d_/%%%-%.~@\\+#=?&:]+)"
url = helper.find(line, mode, pattern)
if url then
return "https://" .. url
Expand Down
17 changes: 17 additions & 0 deletions test/spec/gx/handlers/url_handler_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,21 @@ describe("url_parser_does_work", function()
assert.equals("https://github.com", handler.handle("v", "// github.com"))
assert.equals("https://github.com", handler.handle("v", 'print("github.com")'))
end)

it("encoded urls", function()
assert.equals(
"https://company.example.com/team-name/repo-name/-/merge_requests/new?merge_request%5Bsource_branch%5D=branch-name",
handler.handle(
"v",
"https://company.example.com/team-name/repo-name/-/merge_requests/new?merge_request%5Bsource_branch%5D=branch-name"
)
)
assert.equals(
"http://example.com/?encoded=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF%21%22%C2%A3%24%25%5E%26%2A%28%29-_%3D%2B%7B%5B%7D%5D%23~%27",
handler.handle(
"v",
"http://example.com/?encoded=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF%21%22%C2%A3%24%25%5E%26%2A%28%29-_%3D%2B%7B%5B%7D%5D%23~%27"
)
)
end)
end)

0 comments on commit b967c7a

Please sign in to comment.