-
-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic values for journal templates #714
Comments
Hi @cosmicboots Not a solution directly implemented in this plugin but I have made an external module to create templates for norg file with dynamic values via LuaSnip. |
Is there a good way to hook Edit: I'm using an autocmd: vim.api.nvim_create_autocmd("BufNewFile", {
command = "Neorg templates journal",
pattern = { neorg_dir .. "journal/*.norg" },
}) |
Thanks for trying it out @gegnew ! Added your comment to the README. Thanks for the tip ;) https://github.com/pysan3/neorg-templates#autoload-with-neorg-journal |
Would be super if you could use a template of this nature:
It seems you could do this with your templates @pysan3 if they included |
The current builtin method has no dynamic value substitution mechanism and it simply copies the content of the template file into the created journal file as you can see here.
On the other hand, my module uses the LuaSnip snippet engine and dynamically loads values as well as asks for user input on the fly. This method has a huge possibility of extensibility which makes it easy to have
It would be nice if my module could be ported into the core repo, but since it depends on LuaSnip, people using different snippet engines (e.g. UltiSnips) cannot benefit from this approach and I think it would never happen. Have you tried my module? If you have any problems using my module, feel free to open an issue over there ;) |
I just found a solution that works for me using Neorg's builtin template system. augroup NORG_JOURNAL
au!
au FileReadPost */journal/*.norg 1s/DATE$/\=system("date +'%Y.%m.%d'|tr -d $'\n'")/e
augroup END |
@pysan3, I finally got around to giving this a spin, and your plugin works superb. Highly recommend to anybody stumbling upon this. |
I had issues with both of the suggestions noted above (but thank you for the direction!). This is the implementation that ended up working for me: local file_exists_and_is_empty = function(filepath)
local file = io.open(filepath, "r") -- Open the file in read mode
if file ~= nil then
local content = file:read("*all") -- Read the entire content of the file
file:close() -- Close the file
return content == "" -- Check if the content is empty
else
return false
end
end
vim.api.nvim_create_autocmd({ "BufNew", "BufNewFile" }, {
callback = function(args)
local toc = "index.norg"
vim.schedule(function()
if vim.fn.fnamemodify(args.file, ":t") == toc then
return
end
if
args.event == "BufNewFile"
or (args.event == "BufNew" and file_exists_and_is_empty(args.file))
then
vim.api.nvim_cmd({ cmd = "Neorg", args = { "templates", "fload", "journal" } }, {})
end
end)
end,
desc = "Load new workspace entries with a Neorg template",
group = vim.api.nvim_create_augroup(augroup, { clear = true }),
pattern = dir .. "/*.norg", Edited due to problems with lingering buffers after file deletion. |
A recent update broke my local file_exists_and_is_empty = function(filepath)
local file = io.open(filepath, "r") -- Open the file in read mode
if file ~= nil then
local content = file:read("*all") -- Read the entire content of the file
file:close() -- Close the file
return content == "" -- Check if the content is empty
else
return false
end
end
vim.api.nvim_create_autocmd({ "BufNew", "BufNewFile" }, {
callback = function(args)
local toc = "index.norg"
vim.schedule(function()
if vim.fn.fnamemodify(args.file, ":t") == toc then
return
end
if
args.event == "BufNewFile"
or (args.event == "BufNew" and file_exists_and_is_empty(args.file))
then
vim.api.nvim_cmd({ cmd = "Neorg", args = { "templates", "fload", "journal" } }, {})
end
end)
end,
desc = "Load new workspace entries with a Neorg template",
pattern = neorg_dir .. "/diary/*.norg",
}) |
What if Easy extension point for 3rd party plugins or users. |
Unfortunately, after some research, my previous comment doesn't work.
So.. I went back to gegnew's snippet. local function setup_loading_template_on_new_file()
local group = vim.api.nvim_create_augroup(
'NeorgLoadTemplateGroup',
{ clear = true }
)
local is_buffer_empty = function(buffer)
local content = vim.api.nvim_buf_get_lines(buffer, 0, -1, false)
return not (#content > 1 or content[1] ~= '')
end
local callback = function(args)
vim.schedule(function()
if not is_buffer_empty(args.buf) then
return
end
if string.find(args.file, '/journal/') then
debug('loading template "journal" ' .. args.event)
vim.api.nvim_cmd({ cmd = 'Neorg', args = { 'templates', 'fload', 'journal' } }, {})
else
debug('add metadata ' .. args.event)
vim.api.nvim_cmd({ cmd = 'Neorg', args = { 'inject-metadata' } }, {})
end
end)
end
vim.api.nvim_create_autocmd(
{ 'BufNewFile', 'BufNew', },
{
desc = 'Load template on new norg files',
pattern = '*.norg',
callback = callback,
group = group,
}
)
end |
Issues
Feature description
It would be nice to be able to have the journal template contain special values that get replaced when the new daily journal files are created.
Below would be and example showing a possible date substitution.
I really like the journal feature, and I think adding this (or something close to it) would greatly improve the template functionality.
I have an oversimplified concept working here: cosmicboots@e510d1e
Help
Yes
Implementation help
Past configuring my editor, I'm fairly new to Lua.
I would also like some guidance on how this feature should be structured from a high level, so it fits in with the rest of the code base.
The text was updated successfully, but these errors were encountered: