diff options
| author | moxie <moxie@3kgcat.fi> | 2026-03-13 09:58:53 +0200 |
|---|---|---|
| committer | moxie <moxie@3kgcat.fi> | 2026-03-13 09:58:53 +0200 |
| commit | bc2944651f4dabc68d7f34c796400d80ba132016 (patch) | |
| tree | 3655716442a24ccb758983f0ddc89222916f64c1 /lua/muwiki/templates.lua | |
chore: init
Diffstat (limited to 'lua/muwiki/templates.lua')
| -rw-r--r-- | lua/muwiki/templates.lua | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lua/muwiki/templates.lua b/lua/muwiki/templates.lua new file mode 100644 index 0000000..c8dcd13 --- /dev/null +++ b/lua/muwiki/templates.lua @@ -0,0 +1,35 @@ +local config = require('muwiki.config') +local fs = require('muwiki.fs') + +local M = {} + +local function process_template(template, title) + local date_fmt = config.options.date_fmt or '%Y-%m-%d' + local date = os.date(date_fmt) + local result = template:gsub('${title}', title):gsub('${date}', date) + + if not result:match('\n$') then + result = result .. '\n' + end + + return result +end + +function M.init_file(bufnr, filepath) + if fs.file_exists(filepath) then + return + end + + local filename = vim.fs.basename(filepath) + + if config.options.use_template then + local title = filename:gsub('%.md$', ''):gsub('_', ' ') + local content = process_template(config.options.template, title) + local lines = vim.split(content, '\n', { plain = true }) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) + end + + vim.notify(string.format('%s (unsaved)', filename), vim.log.levels.INFO) +end + +return M |
