Add anchor extraction function

`anchor.extract(string, anchor)` will find and extract a zettel anchor
from any string passed in.

It can be given an optional anchor regex which will supersede any regex
set in the plugin options.
This commit is contained in:
Marty Oehme 2020-11-06 15:47:33 +01:00
parent 65e3b0bc1c
commit 44a985cce6
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
3 changed files with 36 additions and 2 deletions

View File

@ -8,9 +8,10 @@ next up:
[ ] fix link following:
* [x] empty space (e.g. in link text, or link itself) disrupts link regex search
* [x] line-end following breaks if cursor is in the MIDDLE of the link
* [ ] extract anchor from link/string (anchor.lua)
* [x] extract anchor from link/string (anchor.lua)
* [x] probably stop hardcoding anchor regex, make an option
* [ ] implement custom anchor creation function to go with custom regex
* [ ] opening zettel should use generated link table for full filename anchor search
* [ ] probably stop hardcoding anchor regex, make an option
* [ ] implement fallback to filename
## TODO: needed functionality

View File

@ -26,4 +26,15 @@ function A.prepend(anchor, text)
return text
end
-- Returns anchor contents if an anchor is contained in the input string.
-- It takes an optional regex parameter with which the plugin option
-- of which anchor to look for can be overwritten.
--
-- If multiple anchors are contained, returns the contents of the first
-- one encountered.
function A.extract(input, regex)
regex = regex or o.anchor().regex
return input:match(regex)
end
return A

View File

@ -39,3 +39,25 @@ describe("prepend", function()
assert.same("1910291645---arated", A.prepend("1910291645", "arated"))
end)
end)
describe("extract", function()
it("should get the default anchor from a string of text", function()
assert.same("2010261208", A.extract(
"/home/office/docs/2010261208 we are the champions.md"))
end)
it("should return nil when default anchor not contained", function()
assert.same(nil, A.extract(
"/home/office/docs/10261208 we are the champions.md"))
end)
it("should use the anchor set in options", function()
vim.g.zettel_anchor_regex = '[%u][%l][%d][%d][%d][%d]'
assert.same("Fa1984", A.extract(
"/home/office/docs/Fa1984_we are the champions.md"))
end)
it("should use the anchor regex argument if one is passed", function()
assert.same("bO133T",
A.extract(
"/home/office/docs/bO133T-we are the champions.md",
"[%l][%u][%d][%d][%d][%u]"))
end)
end)