hephaestus/heph.nvim/lua/heph/node.lua
Erich Blume 8dc98dc9c1
Some checks failed
Build / validate (pull_request) Failing after 4m57s
feat(nvim): inline #hashtags become tags on save (§8.3)
On save, whitespace-prefixed `#hashtags` in a node's body are unioned
into its tag set (via `frontmatter.hashtags` + the existing tag diff), so
you can tag a note by writing `#kitchen` inline. A markdown `# heading`
has a space after the `#`, so it never matches. e2e covers it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-03 11:57:25 -07:00

78 lines
2.8 KiB
Lua

--- Buffer-backed nodes (tech-spec §8): a node's markdown body is edited in a
--- real buffer named `heph://node/<id>`. `:e` loads it via `node.get`
--- (with the editable YAML **frontmatter** block prepended, §8.3); `:w` diffs
--- the frontmatter into structured RPCs, then saves the body via `node.update`
--- (the backend CRDT-diffs the whole body, and strips any frontmatter echoed
--- back — so sending the stripped body is correct and idempotent).
local rpc = require("heph.rpc")
local util = require("heph.util")
local frontmatter = require("heph.frontmatter")
local M = {}
-- buf -> the frontmatter table rendered on read, so `write` can diff against it.
M._canonical = {}
--- `BufReadCmd` handler for `heph://node/<id>`: load frontmatter + body.
function M.read(buf, uri)
local _, id = util.parse_uri(uri)
if not id then
error("heph: not a node uri: " .. tostring(uri))
end
local node = rpc.call("node.get", { id = id, frontmatter = true })
local text = (node and node.body) or ""
local fm = frontmatter.parse(text)
-- `plain` split keeps a trailing "" element for a trailing newline, so the
-- text round-trips exactly through `table.concat` on write.
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(text, "\n", { plain = true }))
vim.b[buf].heph_node_id = id
vim.b[buf].heph_node_kind = (node and node.kind) or "doc"
M._canonical[buf] = fm or {}
vim.bo[buf].buftype = "acwrite" -- written via BufWriteCmd, not to a file
vim.bo[buf].filetype = "markdown"
vim.bo[buf].fileformat = "unix"
vim.bo[buf].modified = false
require("heph.link").attach(buf)
end
--- `BufWriteCmd` handler: route frontmatter edits to RPCs, then save the body.
function M.write(buf, _uri)
local id = vim.b[buf].heph_node_id
if not id then
error("heph: buffer has no heph node id")
end
local text = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), "\n")
local fm, body = frontmatter.parse(text)
local params = { id = id }
if fm then
-- A frontmatter block is present: translate its edits to RPCs. (Absent
-- block ⇒ the user removed it; treat the whole buffer as body, touch no
-- metadata.) The backend also strips defensively, so `body` is what stores.
local canonical = M._canonical[buf] or {}
frontmatter.apply(id, canonical, fm, body)
if fm.title and fm.title ~= canonical.title then
params.title = fm.title
end
params.body = body
M._canonical[buf] = fm
else
params.body = text
end
rpc.call("node.update", params)
vim.bo[buf].modified = false
end
--- Open (or focus) the buffer for node `id`.
function M.open(id)
vim.cmd.edit(util.node_uri(id))
end
--- Force-reload the buffer for node `id` from the daemon (discards local edits).
function M.reload(id)
vim.cmd("edit! " .. vim.fn.fnameescape(util.node_uri(id)))
end
return M