From 44a985cce64f74bee7f1203babd043b380169686 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 6 Nov 2020 15:47:33 +0100 Subject: [PATCH] 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. --- README.md | 5 +++-- lua/zettelkasten/anchor.lua | 11 +++++++++++ lua/zettelkasten/anchor_spec.lua | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e6e70c0..417cc06 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/zettelkasten/anchor.lua b/lua/zettelkasten/anchor.lua index c509db4..cedb6d6 100644 --- a/lua/zettelkasten/anchor.lua +++ b/lua/zettelkasten/anchor.lua @@ -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 diff --git a/lua/zettelkasten/anchor_spec.lua b/lua/zettelkasten/anchor_spec.lua index e5f9592..6ab084c 100644 --- a/lua/zettelkasten/anchor_spec.lua +++ b/lua/zettelkasten/anchor_spec.lua @@ -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)