mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-24 18:52:22 +08:00
feat(zettelkasten): filter zk tags
This commit is contained in:
parent
b89c9f320a
commit
4b61259a80
@ -10,7 +10,7 @@ function! SpaceVim#layers#zettelkasten#plugins() abort
|
|||||||
let plugins = []
|
let plugins = []
|
||||||
call add(plugins, [g:_spacevim_root_dir . 'bundle/vim-zettelkasten',
|
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
|
return plugins
|
||||||
endfunction
|
endfunction
|
||||||
@ -30,6 +30,7 @@ function! SpaceVim#layers#zettelkasten#config() abort
|
|||||||
let g:_spacevim_mappings_space.m.z = {'name' : '+zettelkasten'}
|
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', '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', '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)
|
call SpaceVim#mapping#space#def('nnoremap', ['m', 'z', 'b'], 'ZkBrowse', 'open-zettelkasten-browse', 1)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ vim.opt_local.buflisted = false
|
|||||||
vim.opt_local.syntax = 'zkbrowser'
|
vim.opt_local.syntax = 'zkbrowser'
|
||||||
vim.opt_local.buftype = 'nofile'
|
vim.opt_local.buftype = 'nofile'
|
||||||
vim.opt_local.swapfile = false
|
vim.opt_local.swapfile = false
|
||||||
vim.opt_local.buflisted = false
|
|
||||||
vim.opt_local.iskeyword:append(':')
|
vim.opt_local.iskeyword:append(':')
|
||||||
vim.opt_local.iskeyword:append('-')
|
vim.opt_local.iskeyword:append('-')
|
||||||
vim.opt_local.suffixesadd:append('.md')
|
vim.opt_local.suffixesadd:append('.md')
|
||||||
@ -46,22 +45,17 @@ if vim.fn.mapcheck('[I', 'n') == '' then
|
|||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
local config = require('zettelkasten.config')
|
local config = require('zettelkasten.config')
|
||||||
if config.zettel_dir ~= '' then
|
if config.zettel_dir ~= '' then
|
||||||
vim.cmd('lcd ' .. config.zettel_dir)
|
vim.cmd('lcd ' .. config.zettel_dir)
|
||||||
end
|
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,
|
|
||||||
})
|
|
||||||
|
@ -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,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
@ -257,15 +257,31 @@ function M.get_toc(note_id, format)
|
|||||||
return formatter.format(lines, format)
|
return formatter.format(lines, format)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_note_browser_content()
|
function M.get_note_browser_content(opt)
|
||||||
if config.zettel_dir == '' then
|
if config.zettel_dir == '' then
|
||||||
log.notify("'notes_path' option is required for note browsing.", log_levels.WARN, {})
|
log.notify("'notes_path' option is required for note browsing.", log_levels.WARN, {})
|
||||||
return {}
|
return {}
|
||||||
end
|
end
|
||||||
|
local filter_tags = {}
|
||||||
|
for _, tag in ipairs(opt.tags) do
|
||||||
|
filter_tags[tag] = true
|
||||||
|
end
|
||||||
|
|
||||||
local all_notes = browser.get_notes()
|
local all_notes = browser.get_notes()
|
||||||
local lines = {}
|
local lines = {}
|
||||||
for _, note in ipairs(all_notes) do
|
for _, note in ipairs(all_notes) do
|
||||||
|
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, {
|
table.insert(lines, {
|
||||||
file_name = note.file_name,
|
file_name = note.file_name,
|
||||||
id = note.id,
|
id = note.id,
|
||||||
@ -275,6 +291,7 @@ function M.get_note_browser_content()
|
|||||||
title = note.title,
|
title = note.title,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return formatter.format(lines, config.browseformat)
|
return formatter.format(lines, config.browseformat)
|
||||||
end
|
end
|
||||||
|
@ -225,4 +225,21 @@ function M.get_tags()
|
|||||||
return tags
|
return tags
|
||||||
end
|
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
|
return M
|
||||||
|
@ -5,23 +5,19 @@
|
|||||||
-- URL: https://spacevim.org
|
-- URL: https://spacevim.org
|
||||||
-- License: GPLv3
|
-- License: GPLv3
|
||||||
--=============================================================================
|
--=============================================================================
|
||||||
if vim.fn.exists(":ZkNew") == 0 then
|
vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]])
|
||||||
vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]])
|
|
||||||
end
|
|
||||||
|
|
||||||
if vim.fn.exists(":ZkBrowse") == 0 then
|
vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]])
|
||||||
vim.cmd([[command ZkBrowse :lua _G.zettelkasten.zkbrowse()]])
|
vim.cmd([[command ZkListTags :Telescope zettelkasten_tags]])
|
||||||
end
|
|
||||||
|
|
||||||
if vim.fn.exists(":ZkListTemplete") == 0 then
|
|
||||||
vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]])
|
|
||||||
end
|
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command('ZkBrowse', function(opt)
|
||||||
|
require('zettelkasten.browser').browse(opt.fargs)
|
||||||
|
end, { nargs = '*' })
|
||||||
_G.zettelkasten = {
|
_G.zettelkasten = {
|
||||||
tagfunc = require("zettelkasten").tagfunc,
|
tagfunc = require('zettelkasten').tagfunc,
|
||||||
completefunc = require("zettelkasten").completefunc,
|
completefunc = require('zettelkasten').completefunc,
|
||||||
zknew = require("zettelkasten").zknew,
|
zknew = require('zettelkasten').zknew,
|
||||||
zkbrowse = function()
|
zkbrowse = function()
|
||||||
vim.cmd("edit zk://browser")
|
vim.cmd('edit zk://browser')
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ lua require('telescope').load_extension('task')
|
|||||||
lua require('telescope').load_extension('neomru')
|
lua require('telescope').load_extension('neomru')
|
||||||
if SpaceVim#layers#isLoaded('zettelkasten')
|
if SpaceVim#layers#isLoaded('zettelkasten')
|
||||||
lua require('telescope').load_extension('zettelkasten_template')
|
lua require('telescope').load_extension('zettelkasten_template')
|
||||||
|
lua require('telescope').load_extension('zettelkasten_tags')
|
||||||
endif
|
endif
|
||||||
if SpaceVim#layers#isLoaded('tools')
|
if SpaceVim#layers#isLoaded('tools')
|
||||||
lua require('telescope').load_extension('bookmarks')
|
lua require('telescope').load_extension('bookmarks')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user