|
| 1 | +local H = {} |
| 2 | +local D = {} |
| 3 | + |
| 4 | +---@param scope "Narrower" | "Broader" |
| 5 | +function H.goto_new_scoped_link(scope) |
| 6 | + local obsidian_api = require("obsidian.api") |
| 7 | + local obsidian_note = require("obsidian.note") |
| 8 | + |
| 9 | + local current_note = obsidian_api.current_note() |
| 10 | + if not current_note then return end |
| 11 | + |
| 12 | + local new_id = obsidian_api.input('New "' .. scope .. '" Note') |
| 13 | + if vim.fn.empty(new_id) == 1 then return end |
| 14 | + |
| 15 | + local new_note = obsidian_note.create({ id = new_id, should_write = false }):write({ |
| 16 | + template = Obsidian.opts.note.template, -- luacheck:ignore 113 |
| 17 | + update_content = function(lines) |
| 18 | + return vim.list_extend(lines, { |
| 19 | + "## " .. (scope == "Narrower" and "Broader" or "Narrower"), |
| 20 | + "", |
| 21 | + "- " .. current_note:format_link(), |
| 22 | + }) |
| 23 | + end, |
| 24 | + }) |
| 25 | + |
| 26 | + H.push_location_onto_tagstack( |
| 27 | + new_note.id, |
| 28 | + current_note:insert_text( |
| 29 | + string.format("- " .. new_note:format_link()), |
| 30 | + scope == "Narrower" and D.NARROWER_OPTS or D.BROADER_SECTION |
| 31 | + ) |
| 32 | + ) |
| 33 | + |
| 34 | + vim.schedule( |
| 35 | + function() |
| 36 | + new_note:open({ |
| 37 | + line = 1 + (new_note.has_frontmatter and new_note.frontmatter_end_line or 1), |
| 38 | + callback = vim.schedule_wrap(vim.cmd.checktime), |
| 39 | + }) |
| 40 | + end |
| 41 | + ) |
| 42 | +end |
| 43 | + |
| 44 | +---@param tagname string |
| 45 | +---@param line_num integer |
| 46 | +function H.push_location_onto_tagstack(tagname, line_num) |
| 47 | + if line_num == 0 then return end |
| 48 | + local buf = vim.api.nvim_get_current_buf() |
| 49 | + local col = 3 |
| 50 | + local off = 0 |
| 51 | + local new_item = { tagname = tagname, from = { buf, line_num, col, off } } |
| 52 | + vim.fn.settagstack(vim.fn.win_getid(), { items = { new_item } }, "t") |
| 53 | +end |
| 54 | + |
| 55 | +D.NARROWER_OPTS = { placement = "bot", section = { level = 2, header = "Narrower" } } |
| 56 | +D.BROADER_SECTION = { placement = "top", section = { level = 2, header = "Broader" } } |
| 57 | + |
| 58 | +return { |
| 59 | + goto_new_narrower_note = function() H.goto_new_scoped_link("Narrower") end, |
| 60 | + goto_new_broader_note = function() H.goto_new_scoped_link("Broader") end, |
| 61 | +} |
0 commit comments