@@ -39,6 +39,7 @@ M.config = {
3939
4040function M .setup (opts )
4141 M .config = vim .tbl_deep_extend (" force" , M .config , opts or {})
42+ M .config .bottom_padding = math.max (0 , math.min (1 , M .config .bottom_padding ))
4243 M ._mmdr_available = nil -- reset so next check re-probes
4344end
4445
@@ -51,16 +52,12 @@ M._debounce_seq = 0
5152M ._workspace_dir = nil
5253M ._mmdr_available = nil -- nil = unchecked, true/false after probe
5354M ._last_scroll_line = nil
54- M ._is_primary = nil -- true/false/nil (takeover mode)
55- M ._takeover_port = nil -- port of primary server (secondary uses for HTTP events)
55+ M ._is_primary = nil -- true/false/nil (takeover mode)
56+ M ._takeover_port = nil -- port of primary server (secondary uses for HTTP events)
5657
5758local function effective_port ()
58- if M .config .port ~= 0 then
59- return M .config .port
60- end
61- if M .config .instance_mode == " takeover" then
62- return 8421
63- end
59+ if M .config .port ~= 0 then return M .config .port end
60+ if M .config .instance_mode == " takeover" then return 8421 end
6461 return 0
6562end
6663
@@ -91,19 +88,9 @@ local function write_index(dir)
9188 if not src then
9289 error (" Could not locate assets/index.html in runtimepath. Make sure the plugin ships it." )
9390 end
94- local f = io.open (src , " r" )
95- if not f then
96- error (" Could not open " .. src )
97- end
98- local content = f :read (" *a" )
99- f :close ()
91+ local content = util .read_text (src )
10092 content = content :gsub (" __BOTTOM_PADDING__" , tostring (M .config .bottom_padding ))
101- local out = io.open (dst , " w" )
102- if not out then
103- error (" Could not write " .. dst )
104- end
105- out :write (content )
106- out :close ()
93+ util .write_text (dst , content )
10794 return dst
10895end
10996
@@ -273,7 +260,8 @@ local function get_content(bufnr)
273260 if ft == " markdown" then
274261 local lines = vim .api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
275262 text = table.concat (lines , " \n " )
276- elseif vim .api .nvim_buf_get_name (bufnr ):match (" %.mmd$" ) or vim .api .nvim_buf_get_name (bufnr ):match (" %.mermaid$" ) then
263+ elseif vim .api .nvim_buf_get_name (bufnr ):match (" %.mmd$" )
264+ or vim .api .nvim_buf_get_name (bufnr ):match (" %.mermaid$" ) then
277265 -- .mmd / .mermaid files: treat entire buffer as mermaid
278266 local lines = vim .api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
279267 text = " ```mermaid\n " .. table.concat (lines , " \n " ) .. " \n ```\n "
357345
358346--- Send cursor line to browser for scroll sync.
359347local function send_scroll_sync (bufnr )
360- if not M .config .scroll_sync then
361- return
362- end
348+ if not M .config .scroll_sync then return end
363349 local cursor_line = vim .api .nvim_win_get_cursor (0 )[1 ] -- 1-based
364- if cursor_line == M ._last_scroll_line then
365- return
366- end
350+ if cursor_line == M ._last_scroll_line then return end
367351 M ._last_scroll_line = cursor_line
368352 local total = vim .api .nvim_buf_line_count (bufnr )
369353 local payload = vim .json .encode ({ line = cursor_line - 1 , total = total })
@@ -401,9 +385,7 @@ local function set_autocmds_for_buffer(bufnr)
401385 vim .api .nvim_create_autocmd (ev , {
402386 group = M ._augroup ,
403387 buffer = bufnr ,
404- callback = function ()
405- send_scroll_sync (bufnr )
406- end ,
388+ callback = function () send_scroll_sync (bufnr ) end ,
407389 desc = " Markdown Preview scroll sync" ,
408390 })
409391 end
0 commit comments