Skip to content

Commit b967c7a

Browse files
author
Justinas Grigas
authored
fix: improved url pattern string (#30)
* 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
1 parent aab77c3 commit b967c7a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lua/gx/handlers/url.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ local M = {
88

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

1414
-- match url without http(s)
1515
if not url then
16-
pattern = "([a-zA-Z0-9_/%-%.~@\\+#]+%.[a-zA-Z0-9_/%-%.~@\\+#%=?&:]+)"
16+
pattern = "([a-zA-Z%d_/%-%.~@\\+#]+%.[a-zA-Z%d_/%%%-%.~@\\+#=?&:]+)"
1717
url = helper.find(line, mode, pattern)
1818
if url then
1919
return "https://" .. url

test/spec/gx/handlers/url_handler_spec.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,21 @@ describe("url_parser_does_work", function()
5151
assert.equals("https://github.com", handler.handle("v", "// github.com"))
5252
assert.equals("https://github.com", handler.handle("v", 'print("github.com")'))
5353
end)
54+
55+
it("encoded urls", function()
56+
assert.equals(
57+
"https://company.example.com/team-name/repo-name/-/merge_requests/new?merge_request%5Bsource_branch%5D=branch-name",
58+
handler.handle(
59+
"v",
60+
"https://company.example.com/team-name/repo-name/-/merge_requests/new?merge_request%5Bsource_branch%5D=branch-name"
61+
)
62+
)
63+
assert.equals(
64+
"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",
65+
handler.handle(
66+
"v",
67+
"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"
68+
)
69+
)
70+
end)
5471
end)

0 commit comments

Comments
 (0)