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

feat(zettelkasten): sort tags in sidebar

This commit is contained in:
Eric Wong 2024-12-14 15:31:01 +08:00
parent 6358c2a435
commit a14bd4f158
5 changed files with 136 additions and 39 deletions

View File

@ -70,7 +70,7 @@ if vim.fn.mapcheck('[I', 'n') == '' then
silent = true,
nowait = true,
callback = function()
require('zettelkasten.browser').open_tag_tree()
require('zettelkasten.sidebar').open_tag_tree()
end,
})
vim.api.nvim_buf_set_keymap(0, 'n', '<Enter>', '', {

View File

@ -6,12 +6,17 @@ vim.opt_local.cursorline = true
vim.opt_local.modifiable = false
vim.opt_local.buflisted = false
vim.opt_local.number = false
-- vim.opt_local.iskeyword:append(':')
vim.opt_local.iskeyword:append('-')
vim.opt_local.relativenumber = false
vim.opt_local.bufhidden = 'wipe'
vim.opt_local.syntax = 'zktagstree'
vim.opt_local.buftype = 'nofile'
vim.opt_local.swapfile = false
vim.opt_local.winfixwidth = true
local hi = require('spacevim.api.vim.highlight')
vim.api.nvim_buf_set_keymap(0, 'n', '<F2>', '', {
noremap = true,
silent = true,
@ -31,7 +36,7 @@ vim.api.nvim_buf_set_keymap(0, 'n', '<Enter>', '', {
0,
-1,
false,
require('zettelkasten').get_note_browser_content({ tags = { vim.fn.getline('.') } })
require('zettelkasten').get_note_browser_content({ tags = { vim.fn.trim(vim.fn.getline('.')) } })
)
end
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
@ -42,17 +47,21 @@ vim.api.nvim_buf_set_keymap(0, 'n', '<LeftRelease>', '', {
silent = true,
nowait = true,
callback = function()
local bufnr = vim.fn.bufnr('zk://browser')
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
vim.api.nvim_buf_set_lines(
bufnr,
0,
-1,
false,
require('zettelkasten').get_note_browser_content({ tags = { vim.fn.getline('.') } })
)
if hi.syntax_at() == 'zktagstreeOrg' then
require('zettelkasten.sidebar').toggle_folded_key()
else
local bufnr = vim.fn.bufnr('zk://browser')
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
vim.api.nvim_buf_set_lines(
bufnr,
0,
-1,
false,
require('zettelkasten').get_note_browser_content({ tags = { vim.fn.trim(vim.fn.getline('.')) } })
)
end
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
end
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
end,
})

View File

@ -239,30 +239,4 @@ function M.browse(opt)
vim.opt_local.buflisted = false
vim.opt_local.modifiable = false
end
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
function M.open_tag_tree()
vim.cmd('30vsplit zk://tags_tree')
vim.opt_local.filetype = 'zktagstree'
vim.opt_local.modifiable = true
local lines = {}
local result = M.get_tags()
for _, tag in ipairs(result) do
table.insert(lines, tag.name)
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, unique_string_table(lines))
vim.opt_local.buflisted = false
vim.opt_local.modifiable = false
end
return M

View File

@ -0,0 +1,98 @@
--=============================================================================
-- sidebar.lua --- sidebar for zettelkasten plugin
-- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
local browser = require('zettelkasten.browser')
local M = {}
local folded_keys = {}
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 get_sorted_keys(t)
local keys = {}
for k, _ in pairs(t) do
table.insert(keys, k)
end
return vim.fn.sort(keys)
end
local function sort_tags(tags)
local atags = {}
for _, tag in ipairs(vim.fn.sort(tags)) do
local k = string.upper(string.sub(tag, 2, 2))
if atags[k] then
table.insert(atags[k], tag)
else
atags[k] = { tag }
end
end
local lines = {}
-- ▼ functions
-- ▶ functions
for _, k in ipairs(get_sorted_keys(atags)) do
if #atags[k] > 0 then
if not folded_keys[k] then
table.insert(lines, '' .. k)
for _, t in ipairs(atags[k]) do
table.insert(lines, ' ' .. t)
end
else
table.insert(lines, '' .. k)
end
end
end
return lines
end
local function update_sidebar_context()
vim.opt_local.modifiable = true
local lines = {}
local result = browser.get_tags()
for _, tag in ipairs(result) do
table.insert(lines, tag.name)
end
vim.api.nvim_buf_set_lines(0, 0, -1, false, sort_tags(unique_string_table(lines)))
vim.opt_local.buflisted = false
vim.opt_local.modifiable = false
end
function M.open_tag_tree()
vim.cmd('30vsplit zk://tags_tree')
vim.opt_local.filetype = 'zktagstree'
folded_keys = {}
update_sidebar_context()
end
function M.toggle_folded_key()
local k = string.sub(vim.fn.getline('.'), 5, 5)
if folded_keys[k] then
folded_keys[k] = false
else
folded_keys[k] = true
end
update_sidebar_context()
end
return M

View File

@ -0,0 +1,16 @@
if "zktagstree" !=# get(b:, "current_syntax", "zktagstree")
finish
endif
" syntax match ZettelKastenID '[0-9]\+-[0-9]\+-[0-9]\+-[0-9]\+-[0-9]\+-[0-9]\+\.md'
" syntax match ZettelKastenDash '\s-\s'
syntax match zktagstreeOrg '[▼▶]\+ .*'
syntax match zktagstreeTags '#\<\k\+\>'
" highlight default link ZettelKastenID String
" highlight default link ZettelKastenDash Comment
highlight default link zktagstreeOrg Number
highlight default link zktagstreeTags Tag
let b:current_syntax = "zktagstree"