* feat(reload): add SSE live-reload server module
Problem: HTML output from pandoc has no live-reload; the browser must
be refreshed manually after each compile.
Solution: add lua/preview/reload.lua — a minimal SSE-only TCP server.
start() binds 127.0.0.1:5554 and keeps EventSource connections alive;
broadcast() pushes a reload event to all clients; inject() appends an
EventSource script before </body> (or at EOF) on every compile so
pandoc overwrites do not lose the tag.
* refactor(presets): add reload field, remove synctex field
Problem: the synctex field only handled PDF forward search and left
HTML live-preview and typst watch mode unsupported.
Solution: add reload = function(ctx) returning { 'typst', 'watch',
ctx.file } to typst (long-running watch mode), reload = true to
markdown and github (SSE push after each pandoc compile), and remove
synctex = true from latex (the -synctex=1 arg in latex.args remains
for .synctex.gz generation).
* refactor(init): replace synctex field and validation with reload
Problem: ProviderConfig still declared synctex and validated it, but
the field is being dropped in favour of the general-purpose reload.
Solution: replace the synctex annotation and vim.validate call with the
reload field, accepting boolean | string[] | function.
* feat(compiler): support long-running watch processes and SSE reload
Problem: compile() only supports one-shot invocations, requiring a
BufWritePost autocmd for watch mode and leaving HTML without live-
reload.
Solution: resolve_reload_cmd() maps provider.reload (function or table)
to a command; when present, compile() spawns it as a long-running
process instead of building a one-shot cmd from provider.cmd + args.
toggle() detects long-running providers and toggles the process
directly instead of registering a BufWritePost autocmd. When
reload = true and output is .html, the SSE server is invoked after
each successful compile. status() reports is_reload processes as
watching, not compiling. stop_all() also stops the SSE server.
* fix(compiler): format is_longrunning and annotate is_reload field
Problem: stylua required is_longrunning to be on one line; lua-ls
warned about undefined field is_reload on preview.Process.
Solution: inline the boolean expression and add is_reload? to the
preview.Process annotation.
* refactor: rename compile/toggle commands to build/watch
Problem: `compile` and `toggle` are accurate but unintuitive — `compile`
sounds academic and `toggle` says nothing about what it toggles.
Solution: rename the public API and `:Preview` subcommands to `build`
(one-shot) and `watch` (live preview). Internal compiler functions are
unchanged. No aliases for old names — clean break.
108 lines
2.4 KiB
Lua
108 lines
2.4 KiB
Lua
local M = {}
|
|
|
|
local PORT = 5554
|
|
local server_handle = nil
|
|
local clients = {}
|
|
|
|
local function make_script(port)
|
|
return '<script>(function(){'
|
|
.. 'var es=new EventSource("http://localhost:'
|
|
.. tostring(port)
|
|
.. '/__live/events");'
|
|
.. 'es.addEventListener("reload",function(){location.reload();});'
|
|
.. '})()</script>'
|
|
end
|
|
|
|
function M.start(port)
|
|
port = port or PORT
|
|
if server_handle then
|
|
return
|
|
end
|
|
local server = vim.uv.new_tcp()
|
|
server:bind('127.0.0.1', port)
|
|
server:listen(128, function(err)
|
|
if err then
|
|
return
|
|
end
|
|
local client = vim.uv.new_tcp()
|
|
server:accept(client)
|
|
local buf = ''
|
|
client:read_start(function(read_err, data)
|
|
if read_err or not data then
|
|
if not client:is_closing() then
|
|
client:close()
|
|
end
|
|
return
|
|
end
|
|
buf = buf .. data
|
|
if buf:find('\r\n\r\n') or buf:find('\n\n') then
|
|
client:read_stop()
|
|
local first_line = buf:match('^([^\r\n]+)')
|
|
if first_line and first_line:find('/__live/events', 1, true) then
|
|
local response = 'HTTP/1.1 200 OK\r\n'
|
|
.. 'Content-Type: text/event-stream\r\n'
|
|
.. 'Cache-Control: no-cache\r\n'
|
|
.. 'Access-Control-Allow-Origin: *\r\n'
|
|
.. '\r\n'
|
|
client:write(response)
|
|
table.insert(clients, client)
|
|
else
|
|
client:close()
|
|
end
|
|
end
|
|
end)
|
|
end)
|
|
server_handle = server
|
|
end
|
|
|
|
function M.stop()
|
|
for _, c in ipairs(clients) do
|
|
if not c:is_closing() then
|
|
c:close()
|
|
end
|
|
end
|
|
clients = {}
|
|
if server_handle then
|
|
server_handle:close()
|
|
server_handle = nil
|
|
end
|
|
end
|
|
|
|
function M.broadcast()
|
|
local event = 'event: reload\ndata: {}\n\n'
|
|
local alive = {}
|
|
for _, c in ipairs(clients) do
|
|
if not c:is_closing() then
|
|
local ok = pcall(function()
|
|
c:write(event)
|
|
end)
|
|
if ok then
|
|
table.insert(alive, c)
|
|
end
|
|
end
|
|
end
|
|
clients = alive
|
|
end
|
|
|
|
function M.inject(path, port)
|
|
port = port or PORT
|
|
local f = io.open(path, 'r')
|
|
if not f then
|
|
return
|
|
end
|
|
local content = f:read('*a')
|
|
f:close()
|
|
local script = make_script(port)
|
|
local new_content, n = content:gsub('</body>', script .. '\n</body>', 1)
|
|
if n == 0 then
|
|
new_content = content .. '\n' .. script
|
|
end
|
|
local fw = io.open(path, 'w')
|
|
if not fw then
|
|
return
|
|
end
|
|
fw:write(new_content)
|
|
fw:close()
|
|
end
|
|
|
|
return M
|