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

feat(zettelkasten): filter zk title via telescope

This commit is contained in:
Eric Wong 2024-12-23 23:14:50 +08:00
parent 9d3b3b5a77
commit 716f835da8
6 changed files with 99 additions and 7 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', 'ZkListTags'], 'loadconf' : 1,
\ 'merged' : 0, 'on_cmd' : ['ZkNew', 'ZkBrowse', 'ZkListTemplete', 'ZkListTags', 'ZkListNotes'], 'loadconf' : 1,
\ }])
return plugins
endfunction
@ -32,6 +32,7 @@ function! SpaceVim#layers#zettelkasten#config() abort
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', 'f'], 'ZkListNotes', 'fuzzy-find-zettels', 1)
endfunction
function! SpaceVim#layers#zettelkasten#set_variable(var) abort

View File

@ -43,6 +43,7 @@ let g:zettelkasten_template_directory = 'D:\me\zettelkasten_template'
| `:ZkBrowse` | list note in browser window |
| `:ZkListTags` | filter tags in telescope |
| `:ZkListTemplete` | filte note templates in telescope |
| `:ZkListNotes` | filte note title in telescope |
**Key bindings in browser window:**

View File

@ -0,0 +1,87 @@
local action_state = require('telescope.actions.state')
local actions = require('telescope.actions')
local conf = require('telescope.config').values
local entry_display = require('telescope.pickers.entry_display')
local finders = require('telescope.finders')
local pickers = require('telescope.pickers')
local browser = require('zettelkasten.browser')
local file = require('spacevim.api.file')
local function get_all_zettelkasten_notes()
local p = {}
local notes = browser.get_notes()
for _, k in pairs(notes) do
table.insert(p, k)
end
return p
end
local function concat_tags(t)
local tags = {}
for _, tag in ipairs(t) do
if vim.tbl_contains(tags, tag.name) == false then
table.insert(tags, tag.name)
end
end
return table.concat(tags, ' ')
end
local function show_changes(opts)
opts = opts or {}
local displayer = entry_display.create({
separator = ' ',
items = {
{ width = 23 },
{ width = vim.o.columns - 100 },
{ remaining = true },
},
})
local function make_display(entry)
-- print(vim.inspect(entry))
return displayer({
{ vim.fn.fnamemodify(entry.value.file_name, ':t'), 'String' },
{ entry.value.title, 'Normal' },
{
concat_tags(entry.value.tags),
'Tag',
},
})
end
pickers
.new(opts, {
prompt_title = 'ZettelKasten Notes',
finder = finders.new_table({
results = get_all_zettelkasten_notes(),
entry_maker = function(entry)
return {
value = entry,
display = make_display,
ordinal = entry.title,
}
end,
}),
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('e ' .. entry.value.file_name)
end)
return true
end,
})
:find()
end
local function run()
show_changes()
end
return require('telescope').register_extension({
exports = {
-- Default when to argument is given, i.e. :Telescope project
zettelkasten = run,
},
})

View File

@ -9,6 +9,7 @@ vim.cmd([[command ZkNew :lua require('zettelkasten').zknew({})]])
vim.cmd([[command ZkListTemplete :Telescope zettelkasten_template]])
vim.cmd([[command ZkListTags :Telescope zettelkasten_tags]])
vim.cmd([[command ZkListNotes :Telescope zettelkasten]])
vim.api.nvim_create_user_command('ZkBrowse', function(opt)
require('zettelkasten.browser').browse(opt.fargs)

View File

@ -8,6 +8,7 @@ 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')
lua require('telescope').load_extension('zettelkasten')
endif
if SpaceVim#layers#isLoaded('tools')
lua require('telescope').load_extension('bookmarks')

View File

@ -39,12 +39,13 @@ update your custom configuration file with:
## Key bindings
| Key bindings | description |
| ------------ | -------------------------------------- |
| `SPC m z n` | create new note |
| `SPC m z t` | create new note with template |
| `SPC m z b` | open zettelkasten browse |
| `SPC m z g` | filter zettelkasten tags via telescope |
| Key bindings | description |
| ------------ | --------------------------------------- |
| `SPC m z n` | create new note |
| `SPC m z t` | create new note with template |
| `SPC m z b` | open zettelkasten browse |
| `SPC m z g` | filter zettelkasten tags via telescope |
| `SPC m z f` | filter zettelkasten title via telescope |
In the zettelkasten browse buffer: