local ls = {} local o = require 'zettelkasten.options' local function isDirectory(ftype) if ftype == 'directory' then return true end return false end local function cleanPath(path) if path:match("^~") then path = os.getenv("HOME") .. path:sub(2) end return path end -- TODO transform paths: -- * to ensure / at the end (or no /) gets taken into account function ls.get_anchors_and_paths(path, recursive) -- TODO check for duplicates and warn user local zettel = {} local anchorreg = '^.*/?(' .. o.anchor().regex .. ')[^/]*%' .. o.zettel().extension .. '$' path = cleanPath(path) local handle = vim.loop.fs_scandir(path) while handle do local name, ftype = vim.loop.fs_scandir_next(handle) if not name then break end if isDirectory(ftype) and recursive then local subdir = ls.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) if anchor then zettel[tostring(anchor)] = path .. "/" .. name end end return zettel end -- Returns the path to the zettel defined by the anchor argument. -- Take a list of zettel as an optional variable, without which -- it will use the (recursive) results of the zettel_root directory. function ls.get_zettel(anchor, all) all = all or ls.get_anchors_and_paths(o.zettel().rootdir, true) if not all then return end return all[anchor] end return ls