Add recursive file listing
This commit is contained in:
parent
7e77b616ea
commit
72f84898f0
2 changed files with 44 additions and 5 deletions
|
@ -56,7 +56,7 @@ function ZK.create_link(text, date)
|
|||
return text .. (zettel_extension or ".md")
|
||||
end
|
||||
|
||||
local function _get_anchors_and_paths(path, recursive)
|
||||
function ZK._get_anchors_and_paths(path, recursive)
|
||||
-- TODO check for duplicates and warn user
|
||||
local zettel = {}
|
||||
local anchorreg = '^.*/?([%d][%d][%d][%d][%d][%d][%d][%d][%d][%d])[^/]*.md$'
|
||||
|
@ -67,7 +67,10 @@ local function _get_anchors_and_paths(path, recursive)
|
|||
if not name then break end
|
||||
|
||||
if ftype == 'directory' and recursive then
|
||||
local subdir = _get_anchors_and_paths(path .. "/" .. name, true)
|
||||
local subdir = ZK._get_anchors_and_paths(path .. "/" .. name, true)
|
||||
for k, v in pairs(subdir) do
|
||||
zettel[tostring(k)] = v
|
||||
end
|
||||
end
|
||||
|
||||
local anchor = string.match(name, anchorreg)
|
||||
|
@ -83,7 +86,10 @@ end
|
|||
-- table.
|
||||
-- Recurses into subdirectories if recursive argument is true.
|
||||
function ZK.get_zettel_list(path, recursive)
|
||||
return _get_anchors_and_paths(path, recursive or false)
|
||||
-- TODO transform paths:
|
||||
-- * to absolute value (e.g. ~ to home, scandir needs absolute)
|
||||
-- * to ensure / at the end (or no /) gets taken into account
|
||||
return ZK._get_anchors_and_paths(path, recursive or false)
|
||||
end
|
||||
|
||||
return {
|
||||
|
|
|
@ -126,7 +126,40 @@ describe("Zettelkasten", function()
|
|||
|
||||
assert.spy(vim_api_mock.loop.fs_scandir).was_called(2)
|
||||
assert.spy(vim_api_mock.loop.fs_scandir).was_called_with("path/to/startingdir/more-notes-here")
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
it("should append all notes found in subdirectories when recursing", function()
|
||||
local outer_files = { "subdir", "1234567890 myfile.md", "2345678901 another.md", }
|
||||
local inner_files = { "2222222222 should-be-present.md", "3333333333 should-also-be-present.md" }
|
||||
local files = outer_files
|
||||
-- assert.is_true("not implemented")
|
||||
local vim_api_mock = {
|
||||
g = {},
|
||||
b = {},
|
||||
loop ={
|
||||
fs_scandir = function()
|
||||
if #files == 0 then return false end
|
||||
return true
|
||||
end,
|
||||
fs_scandir_next = function()
|
||||
if #files == 0 then return nil end
|
||||
local fname, ftype = table.remove(files), 'file'
|
||||
if fname == "subdir" then
|
||||
files = inner_files
|
||||
ftype = 'directory'
|
||||
end
|
||||
return fname, ftype
|
||||
end
|
||||
}}
|
||||
ZK.init(vim_api_mock)
|
||||
local expected = {
|
||||
["1234567890"] = "1234567890 myfile.md",
|
||||
["2345678901"] = "2345678901 another.md",
|
||||
["2222222222"] = "2222222222 should-be-present.md",
|
||||
["3333333333"] = "3333333333 should-also-be-present.md",
|
||||
}
|
||||
assert.same(expected, ZK.get_zettel_list('mydirectory', true))
|
||||
end)
|
||||
|
||||
end)
|
||||
end)
|
||||
|
|
Loading…
Reference in a new issue