mirror of
https://github.com/Ascyii/telekasten.nvim.git
synced 2026-01-01 06:14:23 -05:00
follo links optionally creates non-existing notes, honoring templates
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
# Backlog
|
# Backlog
|
||||||
|
|
||||||
- follow links: create non-existing ones
|
- create new note dialog: choose template
|
||||||
- extend markdown syntax highlights for [[links]]
|
- extend markdown syntax highlights for [[links]]
|
||||||
- get rid of `daily_finder.sh`
|
- get rid of `daily_finder.sh`
|
||||||
|
|
||||||
## Dones
|
## Dones
|
||||||
|
- [x] follow links: create non-existing ones
|
||||||
- [x] ,[ to insert link --> we can escape out and type double brackets
|
- [x] ,[ to insert link --> we can escape out and type double brackets
|
||||||
- [x] shortcuts for todo and done in init.vim
|
- [x] shortcuts for todo and done in init.vim
|
||||||
- [x] Readme search based navigation
|
- [x] Readme search based navigation
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ local actions = require("telescope.actions")
|
|||||||
local action_state = require "telescope.actions.state"
|
local action_state = require "telescope.actions.state"
|
||||||
|
|
||||||
-- declare locals for the nvim api stuff to avoid more lsp warnings
|
-- declare locals for the nvim api stuff to avoid more lsp warnings
|
||||||
local vim = vim
|
local vim = vim
|
||||||
local api = api
|
|
||||||
|
|
||||||
|
|
||||||
|
-- ----------------------------------------------------------------------------
|
||||||
-- DEFAULT CONFIG
|
-- DEFAULT CONFIG
|
||||||
|
-- ----------------------------------------------------------------------------
|
||||||
ZkCfg = {
|
ZkCfg = {
|
||||||
home = vim.fn.expand("~/zettelkasten"),
|
home = vim.fn.expand("~/zettelkasten"),
|
||||||
dailies = vim.fn.expand("~/zettelkasten/daily"),
|
dailies = vim.fn.expand("~/zettelkasten/daily"),
|
||||||
@@ -21,17 +23,39 @@ ZkCfg = {
|
|||||||
-- download tool for daily_finder installation: curl or wget
|
-- download tool for daily_finder installation: curl or wget
|
||||||
downloader = 'curl',
|
downloader = 'curl',
|
||||||
-- downloader = 'wget', -- wget is supported, too
|
-- downloader = 'wget', -- wget is supported, too
|
||||||
}
|
|
||||||
|
-- following a link to a non-existing note will create it
|
||||||
|
follow_creates_nonexisting = true,
|
||||||
|
|
||||||
|
-- templates for new notes
|
||||||
|
template_new_note = ZkCfg.home .. '/' .. 'templates/new_note.md',
|
||||||
|
-- currently unused, hardcoded in daily_finder.sh:
|
||||||
|
template_new_daily = ZkCfg.home .. '/' .. 'templates/daily.md',
|
||||||
|
-- currently unused
|
||||||
|
template_new_weekly= ZkCfg.home .. '/' .. 'templates/weekly.md',
|
||||||
|
}
|
||||||
|
-- ----------------------------------------------------------------------------
|
||||||
|
|
||||||
local downloader2cmd = {
|
local downloader2cmd = {
|
||||||
curl = 'curl -o',
|
curl = 'curl -o',
|
||||||
wget = 'wget -O',
|
wget = 'wget -O',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local note_type_templates = {
|
||||||
|
normal = ZkCfg.template_new_note,
|
||||||
|
daily = ZkCfg.template_new_daily,
|
||||||
|
weekly = ZkCfg.template_new_weekly,
|
||||||
|
}
|
||||||
|
|
||||||
-- InstallDailyFinder
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- InstallDailyFinder:
|
||||||
|
-- -------------------
|
||||||
|
--
|
||||||
-- downloads the daily finder scripts to the configured `my_bin` directory
|
-- downloads the daily finder scripts to the configured `my_bin` directory
|
||||||
-- and makes it executable
|
-- and makes it executable
|
||||||
|
--
|
||||||
InstallDailyFinder = function()
|
InstallDailyFinder = function()
|
||||||
local destpath = ZkCfg.my_bin .. '/' .. ZkCfg.daily_finder
|
local destpath = ZkCfg.my_bin .. '/' .. ZkCfg.daily_finder
|
||||||
local cmd = downloader2cmd[ZkCfg.downloader]
|
local cmd = downloader2cmd[ZkCfg.downloader]
|
||||||
@@ -62,8 +86,11 @@ local check_local_finder = function()
|
|||||||
-- return vim.fn.executable(ZkCfg.daily_finder) == 1
|
-- return vim.fn.executable(ZkCfg.daily_finder) == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- FindDailyNotes:
|
-- FindDailyNotes:
|
||||||
|
-- ---------------
|
||||||
--
|
--
|
||||||
-- Select from daily notes
|
-- Select from daily notes
|
||||||
--
|
--
|
||||||
@@ -76,69 +103,140 @@ FindDailyNotes = function(opts)
|
|||||||
find_command = { ZkCfg.daily_finder },
|
find_command = { ZkCfg.daily_finder },
|
||||||
entry_maker = zk_entry_maker,
|
entry_maker = zk_entry_maker,
|
||||||
})
|
})
|
||||||
|
else
|
||||||
|
print("Daily finder not found. Try :lua require('telekasten').install_daily_finder()")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- InsertLink:
|
-- InsertLink:
|
||||||
|
-- -----------
|
||||||
--
|
--
|
||||||
-- Select from all notes and put a link in the current buffer
|
-- Select from all notes and put a link in the current buffer
|
||||||
--
|
--
|
||||||
InsertLink = function(opts)
|
InsertLink = function(opts)
|
||||||
opts = {} or opts
|
opts = {} or opts
|
||||||
builtin.find_files({
|
if (check_local_finder() == true) then
|
||||||
prompt_title = "Insert link to note",
|
builtin.find_files({
|
||||||
cwd = ZkCfg.home,
|
prompt_title = "Insert link to note",
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
cwd = ZkCfg.home,
|
||||||
map = map -- get rid of lsp error
|
attach_mappings = function(prompt_bufnr, map)
|
||||||
actions.select_default:replace(function()
|
map = map -- get rid of lsp error
|
||||||
actions.close(prompt_bufnr)
|
actions.select_default:replace(function()
|
||||||
local selection = action_state.get_selected_entry()
|
actions.close(prompt_bufnr)
|
||||||
local fn = path_to_linkname(selection.value)
|
local selection = action_state.get_selected_entry()
|
||||||
vim.api.nvim_put({ "[["..fn.."]]" }, "", false, true)
|
local fn = path_to_linkname(selection.value)
|
||||||
end)
|
vim.api.nvim_put({ "[["..fn.."]]" }, "", false, true)
|
||||||
return true
|
end)
|
||||||
end,
|
return true
|
||||||
find_command = { ZkCfg.daily_finder },
|
end,
|
||||||
entry_maker = zk_entry_maker,
|
find_command = { ZkCfg.daily_finder },
|
||||||
})
|
entry_maker = zk_entry_maker,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
print("Daily finder not found. Try :lua require('telekasten').install_daily_finder()")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- FollowLink:
|
-- FollowLink:
|
||||||
|
-- -----------
|
||||||
--
|
--
|
||||||
-- find the file linked to by the word under the cursor
|
-- find the file linked to by the word under the cursor
|
||||||
--
|
--
|
||||||
FollowLink = function(opts)
|
local function file_exists(fname)
|
||||||
opts = {} or opts
|
local f=io.open(fname,"r")
|
||||||
vim.cmd('normal yi]')
|
if f~=nil then io.close(f) return true else return false end
|
||||||
local word = vim.fn.getreg('"0')
|
end
|
||||||
builtin.find_files({
|
|
||||||
prompt_title = "Follow link to note...",
|
local function linesubst(line, title)
|
||||||
cwd = ZkCfg.home,
|
local substs = {
|
||||||
default_text = word,
|
date = os.date('%Y-%m-%d'),
|
||||||
find_command = { ZkCfg.daily_finder },
|
title = title,
|
||||||
entry_maker = zk_entry_maker,
|
}
|
||||||
})
|
for k, v in pairs(substs) do
|
||||||
|
line = line:gsub("{{"..k.."}}", v)
|
||||||
|
end
|
||||||
|
|
||||||
|
return line
|
||||||
|
end
|
||||||
|
|
||||||
|
local create_note_from_template = function (title, filepath, templatefn)
|
||||||
|
-- first, read the template file
|
||||||
|
local lines = {}
|
||||||
|
for line in io.lines(templatefn) do
|
||||||
|
lines[#lines+1] = line
|
||||||
|
end
|
||||||
|
|
||||||
|
-- now write the output file, substituting vars line by line
|
||||||
|
local ofile = io.open(filepath, 'a')
|
||||||
|
for _, line in pairs(lines) do
|
||||||
|
ofile:write(linesubst(line, title) .. '\n')
|
||||||
|
end
|
||||||
|
|
||||||
|
ofile:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
FollowLink = function(opts)
|
||||||
|
opts = {} or opts
|
||||||
|
vim.cmd('normal yi]')
|
||||||
|
local title = vim.fn.getreg('"0')
|
||||||
|
local fname = ZkCfg.home .. '/' .. title .. ZkCfg.extension
|
||||||
|
|
||||||
|
local fexists = file_exists(fname)
|
||||||
|
if ((fexists ~= true) and ((opts.follow_creates_nonexisting == true) or ZkCfg.follow_creates_nonexisting == true)) then
|
||||||
|
create_note_from_template(title, fname, note_type_templates.normal)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (check_local_finder() == true) then
|
||||||
|
builtin.find_files({
|
||||||
|
prompt_title = "Follow link to note...",
|
||||||
|
cwd = ZkCfg.home,
|
||||||
|
default_text = title,
|
||||||
|
find_command = { ZkCfg.daily_finder },
|
||||||
|
entry_maker = zk_entry_maker,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
print("Daily finder not found. Try :lua require('telekasten').install_daily_finder()")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
-- GotoToday:
|
||||||
|
-- ----------
|
||||||
|
--
|
||||||
|
-- find today's daily note and create it if necessary.
|
||||||
|
--
|
||||||
GotoToday = function(opts)
|
GotoToday = function(opts)
|
||||||
print('local version')
|
print('local version')
|
||||||
opts = {} or opts
|
opts = {} or opts
|
||||||
local word = os.date("%Y-%m-%d")
|
local word = os.date("%Y-%m-%d")
|
||||||
builtin.find_files({
|
|
||||||
prompt_title = "Follow link to note...",
|
if (check_local_finder() == true) then
|
||||||
cwd = ZkCfg.home,
|
builtin.find_files({
|
||||||
default_text = word,
|
prompt_title = "Follow link to note...",
|
||||||
find_command = { ZkCfg.daily_finder },
|
cwd = ZkCfg.home,
|
||||||
entry_maker = zk_entry_maker,
|
default_text = word,
|
||||||
})
|
find_command = { ZkCfg.daily_finder },
|
||||||
|
entry_maker = zk_entry_maker,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
print("Daily finder not found. Try :lua require('telekasten').install_daily_finder()")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- FindNotes:
|
-- FindNotes:
|
||||||
|
-- ----------
|
||||||
--
|
--
|
||||||
-- Select from notes
|
-- Select from notes
|
||||||
--
|
--
|
||||||
@@ -152,73 +250,33 @@ FindNotes = function(opts)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- SearchNotes:
|
-- SearchNotes:
|
||||||
|
-- ------------
|
||||||
--
|
--
|
||||||
-- find the file linked to by the word under the cursor
|
-- find the file linked to by the word under the cursor
|
||||||
--
|
--
|
||||||
SearchNotes = function(opts)
|
SearchNotes = function(opts)
|
||||||
opts = {} or opts
|
opts = {} or opts
|
||||||
builtin.live_grep({
|
|
||||||
prompt_title = "Search in notes",
|
if (check_local_finder() == true) then
|
||||||
cwd = ZkCfg.home,
|
builtin.live_grep({
|
||||||
search_dirs = { ZkCfg.home },
|
prompt_title = "Search in notes",
|
||||||
default_text = vim.fn.expand("<cword>"),
|
cwd = ZkCfg.home,
|
||||||
find_command = { ZkCfg.daily_finder },
|
search_dirs = { ZkCfg.home },
|
||||||
})
|
default_text = vim.fn.expand("<cword>"),
|
||||||
|
find_command = { ZkCfg.daily_finder },
|
||||||
|
})
|
||||||
|
else
|
||||||
|
print("Daily finder not found. Try :lua require('telekasten').install_daily_finder()")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function file_exists(name)
|
|
||||||
local f=io.open(name,"r")
|
|
||||||
if f~=nil then io.close(f) return true else return false end
|
|
||||||
end
|
|
||||||
|
|
||||||
local create_note_by_tilte = function (title)
|
|
||||||
-- set the filename based on the current date
|
|
||||||
-- local filepath = vim.g['wiki_root']..'journal/'..os.date('%Y-%m-%d')..'.md'
|
|
||||||
local filepath = ZkCfg.home
|
|
||||||
|
|
||||||
-- if the file doesn't exist
|
|
||||||
-- then created file and write date to the top of the file
|
|
||||||
if not file_exists(filepath) then
|
|
||||||
local file = io.open(filepath, 'a')
|
|
||||||
io.output(file)
|
|
||||||
io.write(os.date("# %a, %d %B '%y"))
|
|
||||||
io.close(file)
|
|
||||||
end
|
|
||||||
api.nvim_command('edit '..filepath)
|
|
||||||
end
|
|
||||||
|
|
||||||
--[[
|
|
||||||
-- interesting snippet:
|
|
||||||
function file_exists(name)
|
|
||||||
local f=io.open(name,"r")
|
|
||||||
if f~=nil then io.close(f) return true else return false end
|
|
||||||
end
|
|
||||||
|
|
||||||
local api = vim.api
|
|
||||||
local M = {}
|
|
||||||
function M.currentEntry()
|
|
||||||
|
|
||||||
-- set the filename based on the current date
|
|
||||||
local filepath = vim.g['wiki_root']..'journal/'..os.date('%Y-%m-%d')..'.md'
|
|
||||||
|
|
||||||
-- if the file doesn't exist
|
|
||||||
-- then created file and write date to the top of the file
|
|
||||||
if not file_exists(filepath) then
|
|
||||||
file = io.open(filepath, 'a')
|
|
||||||
io.output(file)
|
|
||||||
io.write(os.date("# %a, %d %B '%y"))
|
|
||||||
io.close(file)
|
|
||||||
end
|
|
||||||
api.nvim_command('edit '..filepath)
|
|
||||||
end
|
|
||||||
return M
|
|
||||||
--]]
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Setup(cfg)
|
||||||
-- setup(cfg)
|
|
||||||
--
|
--
|
||||||
-- Overrides config with elements from cfg
|
-- Overrides config with elements from cfg
|
||||||
-- Valid keys are:
|
-- Valid keys are:
|
||||||
@@ -248,3 +306,25 @@ local M = {
|
|||||||
}
|
}
|
||||||
print('local version')
|
print('local version')
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--[[
|
||||||
|
-- interesting snippet:
|
||||||
|
-- set the filename based on the current date
|
||||||
|
local filepath = vim.g['wiki_root']..'journal/'..os.date('%Y-%m-%d')..'.md'
|
||||||
|
|
||||||
|
-- if the file doesn't exist
|
||||||
|
-- then created file and write date to the top of the file
|
||||||
|
if not file_exists(filepath) then
|
||||||
|
file = io.open(filepath, 'a')
|
||||||
|
io.output(file)
|
||||||
|
io.write(os.date("# %a, %d %B '%y"))
|
||||||
|
io.close(file)
|
||||||
|
end
|
||||||
|
api.nvim_command('edit '..filepath)
|
||||||
|
end
|
||||||
|
return M
|
||||||
|
--]]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user