1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 07:10:06 +08:00

feat(zettelkasten): filter zk tags

This commit is contained in:
Eric Wong 2024-12-10 22:45:40 +08:00
parent b89c9f320a
commit 4b61259a80
7 changed files with 126 additions and 40 deletions

View File

@ -10,7 +10,7 @@ function! SpaceVim#layers#zettelkasten#plugins() abort
let plugins = []
call add(plugins, [g:_spacevim_root_dir . 'bundle/vim-zettelkasten',
\ {
\ 'merged' : 0, 'on_cmd' : ['ZkNew', 'ZkBrowse', 'ZkListTemplete'], 'loadconf' : 1,
\ 'merged' : 0, 'on_cmd' : ['ZkNew', 'ZkBrowse', 'ZkListTemplete', 'ZkListTags'], 'loadconf' : 1,
\ }])
return plugins
endfunction
@ -30,6 +30,7 @@ function! SpaceVim#layers#zettelkasten#config() abort
let g:_spacevim_mappings_space.m.z = {'name' : '+zettelkasten'}
call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'n'], 'ZkNew', 'create-new-zettel-note', 1)
call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 't'], 'ZkListTemplete', 'zettel-template', 1)
call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'g'], 'ZkListTags', 'zettel-tags', 1)
call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'b'], 'ZkBrowse', 'open-zettelkasten-browse', 1)
endfunction

View File

@ -8,7 +8,6 @@ vim.opt_local.buflisted = false
vim.opt_local.syntax = 'zkbrowser'
vim.opt_local.buftype = 'nofile'
vim.opt_local.swapfile = false
vim.opt_local.buflisted = false
vim.opt_local.iskeyword:append(':')
vim.opt_local.iskeyword:append('-')
vim.opt_local.suffixesadd:append('.md')
@ -46,22 +45,17 @@ if vim.fn.mapcheck('[I', 'n') == '' then
end
end,
})
vim.api.nvim_buf_set_keymap(0, 'n', '<C-l>', '', {
noremap = true,
silent = true,
nowait = true,
callback = function()
vim.cmd('ZkBrowse')
end,
})
end
local config = require('zettelkasten.config')
if config.zettel_dir ~= '' then
vim.cmd('lcd ' .. config.zettel_dir)
end
vim.api.nvim_create_autocmd({ 'BufEnter' }, {
group = vim.api.nvim_create_augroup('zettelkasten_browser_events', { clear = true }),
buffer = vim.api.nvim_get_current_buf(),
callback = function(opts)
vim.opt_local.syntax = ''
vim.opt_local.modifiable = true
vim.api.nvim_buf_set_lines(0, 0, -1, false, require('zettelkasten').get_note_browser_content())
vim.opt_local.syntax = 'zkbrowser'
vim.opt_local.buflisted = false
vim.opt_local.modifiable = false
end,
})

View File

@ -0,0 +1,60 @@
local action_state = require("telescope.actions.state")
local actions = require("telescope.actions")
local conf = require("telescope.config").values
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local browser = require('zettelkasten.browser')
local function unique_string_table(t)
local temp = {}
for _, k in ipairs(t) do
temp[k] = true
end
local rst = {}
for m, _ in pairs(temp) do
table.insert(rst, m)
end
return rst
end
local function prepare_output_table()
local lines = {}
local result = browser.get_tags()
for _, tag in ipairs(result) do
table.insert(lines, tag.name)
end
return unique_string_table(lines)
end
local function show_script_names(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "ZettelKasten Tags",
finder = finders.new_table {
results = prepare_output_table()
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local entry = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.cmd('ZkBrowse --tags ' .. entry.value)
vim.cmd('stopinsert')
end)
return true
end,
}):find()
end
local function run()
show_script_names()
end
return require("telescope").register_extension({
exports = {
-- Default when to argument is given, i.e. :Telescope scriptnames
zettelkasten_tags = run,
},
})

View File

@ -257,23 +257,40 @@ function M.get_toc(note_id, format)
return formatter.format(lines, format)
end
function M.get_note_browser_content()
function M.get_note_browser_content(opt)
if config.zettel_dir == '' then
log.notify("'notes_path' option is required for note browsing.", log_levels.WARN, {})
return {}
end
local filter_tags = {}
for _, tag in ipairs(opt.tags) do
filter_tags[tag] = true
end
local all_notes = browser.get_notes()
local lines = {}
for _, note in ipairs(all_notes) do
table.insert(lines, {
file_name = note.file_name,
id = note.id,
references = note.references,
back_references = note.back_references,
tags = note.tags,
title = note.title,
})
local has_tag
if #opt.tags == 0 then
has_tag = true
else
for _, tag in ipairs(note.tags) do
if filter_tags[tag.name] then
has_tag = true
break
end
end
end
if has_tag then
table.insert(lines, {
file_name = note.file_name,
id = note.id,
references = note.references,
back_references = note.back_references,
tags = note.tags,
title = note.title,
})
end
end
return formatter.format(lines, config.browseformat)

View File

@ -225,4 +225,21 @@ function M.get_tags()
return tags
end
function M.browse(opt)
vim.cmd('edit zk://browser')
vim.opt_local.syntax = ''
vim.opt_local.modifiable = true
vim.api.nvim_buf_set_lines(
0,
0,
-1,
false,
require('zettelkasten').get_note_browser_content({ tags = opt })
)
vim.opt_local.syntax = 'zkbrowser'
vim.opt_local.buflisted = false
vim.opt_local.modifiable = false
end
return M

View File

@ -5,23 +5,19 @@
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
if vim.fn.exists(":ZkNew") == 0 then
vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]])
end
vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]])
if vim.fn.exists(":ZkBrowse") == 0 then
vim.cmd([[command ZkBrowse :lua _G.zettelkasten.zkbrowse()]])
end
if vim.fn.exists(":ZkListTemplete") == 0 then
vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]])
end
vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]])
vim.cmd([[command ZkListTags :Telescope zettelkasten_tags]])
vim.api.nvim_create_user_command('ZkBrowse', function(opt)
require('zettelkasten.browser').browse(opt.fargs)
end, { nargs = '*' })
_G.zettelkasten = {
tagfunc = require("zettelkasten").tagfunc,
completefunc = require("zettelkasten").completefunc,
zknew = require("zettelkasten").zknew,
zkbrowse = function()
vim.cmd("edit zk://browser")
end,
tagfunc = require('zettelkasten').tagfunc,
completefunc = require('zettelkasten').completefunc,
zknew = require('zettelkasten').zknew,
zkbrowse = function()
vim.cmd('edit zk://browser')
end,
}

View File

@ -7,6 +7,7 @@ lua require('telescope').load_extension('task')
lua require('telescope').load_extension('neomru')
if SpaceVim#layers#isLoaded('zettelkasten')
lua require('telescope').load_extension('zettelkasten_template')
lua require('telescope').load_extension('zettelkasten_tags')
endif
if SpaceVim#layers#isLoaded('tools')
lua require('telescope').load_extension('bookmarks')