feat: add support for file paths starts with ../ ./ and /

To implement this I extended the existing regex variable in the `src/config` dir. In a few test cases, extra space is added intentionally to verify we don't include space in the URL as it looks & feels odd.
This commit is contained in:
Mustaque Ahmed
2025-01-07 01:06:40 +05:30
parent 037de64ea2
commit 85743aebd5

View File

@ -24,7 +24,7 @@ const oni = @import("oniguruma");
/// handling them well requires a non-regex approach. /// handling them well requires a non-regex approach.
pub const regex = pub const regex =
"(?:" ++ url_schemes ++ "(?:" ++ url_schemes ++
\\)(?:[\w\-.~:/?#@!$&*+,;=%]+(?:[\(\[]\w*[\)\]])?)+(?<![,.]) \\)(?:[\w\-.~:/?#@!$&*+,;=%]+(?:[\(\[]\w*[\)\]])?)+(?<![,.])|(?:\.\.\/|\.\/*|\/)[\w\-.~:\/?#@!$&*+,;=%]+(?:\/[\w\-.~:\/?#@!$&*+,;=%]*)*
; ;
const url_schemes = const url_schemes =
\\https?://|mailto:|ftp://|file:|ssh:|git://|ssh://|tel:|magnet:|ipfs://|ipns://|gemini://|gopher://|news: \\https?://|mailto:|ftp://|file:|ssh:|git://|ssh://|tel:|magnet:|ipfs://|ipns://|gemini://|gopher://|news:
@ -166,6 +166,34 @@ test "url regex" {
.input = "match news:comp.infosystems.www.servers.unix news links", .input = "match news:comp.infosystems.www.servers.unix news links",
.expect = "news:comp.infosystems.www.servers.unix", .expect = "news:comp.infosystems.www.servers.unix",
}, },
.{
.input = "/Users/ghostty.user/code/example.py",
.expect = "/Users/ghostty.user/code/example.py",
},
.{
.input = "/Users/ghostty.user/code/../example.py",
.expect = "/Users/ghostty.user/code/../example.py",
},
.{
.input = "/Users/ghostty.user/code/../example.py hello world",
.expect = "/Users/ghostty.user/code/../example.py",
},
.{
.input = "../example.py",
.expect = "../example.py",
},
.{
.input = "../example.py ",
.expect = "../example.py",
},
.{
.input = "first time ../example.py contributor ",
.expect = "../example.py",
},
.{
.input = "[link](/home/user/ghostty.user/example)",
.expect = "/home/user/ghostty.user/example",
},
}; };
for (cases) |case| { for (cases) |case| {