Skip to content

Commit

Permalink
feat(rules): add 'override_rules' to override default 'pattern_rules' (
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Nov 13, 2023
1 parent 07068c7 commit 87f10a7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
62 changes: 59 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ require('gitlinker').setup({
},
},

-- regex pattern based rules
--- @type table<{[1]:string,[2]:string}>[]
-- regex pattern based rules, mapping url from 'host' to 'remote'.
pattern_rules = {
-- 'git@github' with '.git' suffix
{
Expand Down Expand Up @@ -260,7 +259,15 @@ require('gitlinker').setup({
},
},

-- higher priority rules to override the default pattern_rules.
-- override 'pattern_rules' with your own rules here.
--
-- **note**:
--
-- if you directly add your own rules in 'pattern_rules', it will remove other rules.
-- but 'override_rules' will only prepend your own rules before 'pattern_rules', e.g. override.
override_rules = nil,

-- function based rules to override the default pattern_rules.
-- function(remote_url) => host_url
--
-- here's an example:
Expand Down Expand Up @@ -306,6 +313,55 @@ require('gitlinker').setup({
})
````

To add more git hosts, or map to your own hosts, please use:

```lua
require('gitlinker').setup({
override_rules = {
{
"^git@your-personal-host%.([_%.%-%w]+):([%.%-%w]+)/([_%.%-%w]+)%.git$",
"https://github.%1/%2/%3/blob/",
},
{
"^git@your-personal-hots%.([_%.%-%w]+):([%.%-%w]+)/([_%.%-%w]+)$",
"https://github-personal.%1/%2/%3/blob/",
},
}
})
```

The above example will map `git@your-personal-host` to `https://github`, override original mapping from `git@github` to `https://github`.

To fully customize git hosts, please use:

```lua
require('gitlinker').setup({
custom_rules = function(remote_url)
local rules = {
{
"^git@github%.([_%.%-%w]+):([%.%-%w]+)/([_%.%-%w]+)%.git$",
"https://github.%1/%2/%3/blob/",
},
{
"^git@github%.([_%.%-%w]+):([%.%-%w]+)/([_%.%-%w]+)$",
"https://github.%1/%2/%3/blob/",
},
}
for _, rule in ipairs(rules) do
local pattern = rule[1]
local replace = rule[2]
if string.match(remote_url, pattern) then
local result = string.gsub(remote_url, pattern, replace)
return result
end
end
return nil
end,
})
```

The above example will technically allow you map anything (which is also the implementation of 'pattern_rules').

### Highlight Group

| Highlight Group | Default Group | Description |
Expand Down
14 changes: 13 additions & 1 deletion lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ local Defaults = {
},
},

-- higher priority rules to override the default pattern_rules.
-- override 'pattern_rules' with your own rules here.
--
-- **note**:
--
-- if you directly add your own rules in 'pattern_rules', it will remove other rules.
-- but 'override_rules' will only prepend your own rules before 'pattern_rules', e.g. override.
override_rules = nil,

-- function based rules to override the default pattern_rules.
-- function(remote_url) => host_url
--
-- here's an example:
Expand Down Expand Up @@ -275,6 +283,10 @@ local function _map_remote_to_host(remote_url)
end

logger.debug("|_map_remote_to_host| use new pattern rules schema")
pattern_rules = vim.list_extend(
vim.deepcopy(Configs.override_rules or {}),
vim.deepcopy(pattern_rules)
)
for i, rule in ipairs(pattern_rules) do
local pattern = rule[1]
local replace = rule[2]
Expand Down

0 comments on commit 87f10a7

Please sign in to comment.