-
-
Notifications
You must be signed in to change notification settings - Fork 221
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
feat: option to inject specific metadata instead of defaults #1128
Changes from 9 commits
113176b
de38773
13d8426
ab1c93b
64b7824
21fc667
0ac07f2
196c2e9
eac464a
680b909
214db65
cb81fb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,19 @@ To query the current workspace, run `:Neorg workspace`. To set the workspace, ru | |
### Changing the Current Working Directory | ||
After a recent update `core.dirman` will no longer change the current working directory after switching | ||
workspace. To get the best experience it's recommended to set the `autochdir` Neovim option. | ||
|
||
|
||
### Create a new note | ||
You can use dirman to create new notes in your workspaces. | ||
|
||
```lua | ||
local dirman = require('neorg').modules.get_module("core.dirman") | ||
dirman.create_file("my_file", "my_ws", { | ||
no_open = false, -- open file after creation? | ||
force = false, -- overwrite file if exists | ||
metadata = {} -- key-value table for metadata fields | ||
}) | ||
``` | ||
--]] | ||
|
||
local neorg = require("neorg.core") | ||
|
@@ -265,12 +278,16 @@ module.public = { | |
}) | ||
end) | ||
end, | ||
|
||
---@class create_file_opts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. classes are kinda global so you might want to use e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it go even deeper then and name it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. up to you There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated names to match the apparent naming convention used in the rest of the codebase (as far as i can tell :)) |
||
---@field no_open? boolean do not open the file after creation? | ||
---@field force? boolean overwrite file if it already exists? | ||
---@field metadata? metadata metadata fields, if provided inserts metadata - an empty table uses default values | ||
|
||
--- Takes in a path (can include directories) and creates a .norg file from that path | ||
---@param path string a path to place the .norg file in | ||
---@param workspace? string workspace name | ||
---@param opts? table additional options | ||
--- - opts.no_open (bool) if true, will not open the file in neovim after creating it | ||
--- - opts.force (bool) if true, will overwrite existing file content | ||
---@param opts? create_file_opts additional options | ||
create_file = function(path, workspace, opts) | ||
opts = opts or {} | ||
|
||
|
@@ -308,20 +325,24 @@ module.public = { | |
fname = fname .. ".norg" | ||
end | ||
|
||
if opts.no_open then | ||
-- Create the file | ||
local fd = vim.loop.fs_open(fname, opts.force and "w" or "a", 438) | ||
|
||
if fd then | ||
vim.loop.fs_close(fd) | ||
end | ||
-- Create the file | ||
local fd = vim.loop.fs_open(fname, opts.force and "w" or "a", 438) | ||
if fd then | ||
vim.loop.fs_close(fd) | ||
end | ||
|
||
return | ||
local metagen = neorg.modules.get_module("core.esupports.metagen") | ||
if opts.metadata and metagen then | ||
local bufnr = module.public.get_file_bufnr(fname) | ||
metagen.write_metadata(bufnr, true, opts.metadata) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of the code looks good to me apart from this bit. This intertwines the dirman and metagen modules more than it should and doesn't extend to other modules which might also want to do something special when a file is created. Maybe it would be best to create a If you're unsure about how to go around implementing it, feel free to ask! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the detailed reply. That sounds like a good solution. I am a little unsure how to implement an event. I would appreciate any help you can give. 😄 |
||
|
||
-- Begin editing that newly created file | ||
vim.cmd("e " .. fname .. " | w") | ||
if not opts.no_open then | ||
-- Begin editing that newly created file | ||
vim.cmd("e " .. fname .. "| w") | ||
end | ||
end, | ||
|
||
--- Takes in a workspace name and a path for a file and opens it | ||
---@param workspace_name string #The name of the workspace to use | ||
---@param path string #A path to open the file (e.g directory/filename.norg) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens when you pass an empty table there? will just default metadata be generated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
It overrides default if there's a match in the key names. Otherwise it does nothing.