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.
58 lines
1.4 KiB
Lua
58 lines
1.4 KiB
Lua
describe('reload', function()
|
|
local reload
|
|
|
|
before_each(function()
|
|
package.loaded['preview.reload'] = nil
|
|
reload = require('preview.reload')
|
|
end)
|
|
|
|
after_each(function()
|
|
reload.stop()
|
|
end)
|
|
|
|
describe('inject', function()
|
|
it('injects script before </body>', function()
|
|
local path = os.tmpname()
|
|
local f = io.open(path, 'w')
|
|
f:write('<html><body><p>hello</p></body></html>')
|
|
f:close()
|
|
|
|
reload.inject(path)
|
|
|
|
local fr = io.open(path, 'r')
|
|
local content = fr:read('*a')
|
|
fr:close()
|
|
os.remove(path)
|
|
|
|
assert.is_truthy(content:find('EventSource', 1, true))
|
|
local script_pos = content:find('EventSource', 1, true)
|
|
local body_pos = content:find('</body>', 1, true)
|
|
assert.is_truthy(body_pos)
|
|
assert.is_true(script_pos < body_pos)
|
|
end)
|
|
|
|
it('appends script when no </body>', function()
|
|
local path = os.tmpname()
|
|
local f = io.open(path, 'w')
|
|
f:write('<html><p>hello</p></html>')
|
|
f:close()
|
|
|
|
reload.inject(path)
|
|
|
|
local fr = io.open(path, 'r')
|
|
local content = fr:read('*a')
|
|
fr:close()
|
|
os.remove(path)
|
|
|
|
assert.is_truthy(content:find('EventSource', 1, true))
|
|
end)
|
|
end)
|
|
|
|
describe('broadcast', function()
|
|
it('does not error with no clients', function()
|
|
assert.has_no.errors(function()
|
|
reload.broadcast()
|
|
end)
|
|
end)
|
|
end)
|
|
end)
|