From bc2944651f4dabc68d7f34c796400d80ba132016 Mon Sep 17 00:00:00 2001 From: moxie Date: Fri, 13 Mar 2026 09:58:53 +0200 Subject: chore: init --- lua/muwiki/templates.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lua/muwiki/templates.lua (limited to 'lua/muwiki/templates.lua') 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 -- cgit v1.2.3