Skip to content

Commit

Permalink
feat: option to inject specific metadata instead of defaults (#1128)
Browse files Browse the repository at this point in the history
closes #1126
  • Loading branch information
Dynge authored Nov 8, 2023
1 parent 0c9f5de commit 5509079
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
47 changes: 34 additions & 13 deletions lua/neorg/modules/core/dirman/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -265,12 +278,16 @@ module.public = {
})
end)
end,

---@class core.dirman.create_file_opts
---@field no_open? boolean do not open the file after creation?
---@field force? boolean overwrite file if it already exists?
---@field metadata? core.esupports.metagen.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? core.dirman.create_file_opts additional options
create_file = function(path, workspace, opts)
opts = opts or {}

Expand Down Expand Up @@ -308,20 +325,23 @@ 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)
-- 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

if fd then
vim.loop.fs_close(fd)
end
local bufnr = module.public.get_file_bufnr(fname)
modules.broadcast_event(
modules.create_event(module, "core.dirman.events.file_created", { buffer = bufnr, opts = opts })
)

return
if not opts.no_open then
-- Begin editing that newly created file
vim.cmd("e " .. fname .. "| w")
end

-- Begin editing that newly created file
vim.cmd("e " .. fname .. " | w")
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)
Expand Down Expand Up @@ -542,6 +562,7 @@ module.events.defined = {
workspace_changed = modules.define_event(module, "workspace_changed"),
workspace_added = modules.define_event(module, "workspace_added"),
workspace_cache_empty = modules.define_event(module, "workspace_cache_empty"),
file_created = modules.define_event(module, "file_created"),
}

module.events.subscribed = {
Expand Down
30 changes: 26 additions & 4 deletions lua/neorg/modules/core/esupports/metagen/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,20 @@ module.public = {
}
end,

--- Creates the metadata contents from the configuration's template.
---@class core.esupports.metagen.metadata
---@field title? function|string the title of the note
---@field description? function|string the description of the note
---@field authors? function|string the authors of the note
---@field categories? function|string the categories of the note
---@field created? function|string a timestamp of creation time for the note
---@field updated? function|string a timestamp of last time the note was updated
---@field version? function|string the neorg version

--- Creates the metadata contents from the provided metadata table (defaulting to the configuration's template).
---@param buf number #The buffer to query potential data from
---@param metadata? core.esupports.metagen.metadata #Table of metadata, overrides defaults if present
---@return table #A table of strings that can be directly piped to `nvim_buf_set_lines`
construct_metadata = function(buf)
construct_metadata = function(buf, metadata)
local template = module.config.public.template
local whitespace = type(module.config.public.tab) == "function" and module.config.public.tab()
or module.config.public.tab
Expand All @@ -200,6 +210,10 @@ module.public = {
}

for _, data in ipairs(template) do
if metadata and metadata[data[1]] then
-- override with data from metadata table
data = { data[1], metadata[data[1]] }
end
table.insert(
result,
whitespace .. data[1] .. delimiter .. tostring(type(data[2]) == "function" and data[2]() or data[2])
Expand All @@ -218,11 +232,12 @@ module.public = {
--- Inject the metadata into a buffer
---@param buf number #The number of the buffer to inject the metadata into
---@param force? boolean #Whether to forcefully override existing metadata
inject_metadata = function(buf, force)
---@param metadata? core.esupports.metagen.metadata #Table of metadata data, overrides defaults if present
inject_metadata = function(buf, force, metadata)
local present, data = module.public.is_metadata_present(buf)

if force or not present then
local constructed_metadata = module.public.construct_metadata(buf)
local constructed_metadata = module.public.construct_metadata(buf, metadata)
vim.api.nvim_buf_set_lines(buf, data.range[1], data.range[2], false, constructed_metadata)
end
end,
Expand Down Expand Up @@ -359,6 +374,10 @@ module.on_event = function(event)
elseif event.type == "core.neorgcmd.events.update-metadata" then
module.public.update_metadata(event.buffer)
module.private.buffers[event.buffer] = true
elseif event.type == "core.dirman.events.file_created" then
if event.content.opts.metadata then
module.public.inject_metadata(event.content.buffer, true, event.content.opts.metadata)
end
end
end

Expand All @@ -373,6 +392,9 @@ module.events.subscribed = {
["inject-metadata"] = true,
["update-metadata"] = true,
},
["core.dirman"] = {
["file_created"] = true,
},
}

return module

0 comments on commit 5509079

Please sign in to comment.