Skip to content

Commit 33053c0

Browse files
committed
address PR review feedback on bottom_padding
- use util.read_text/write_text instead of io.open (codebase convention) - clamp bottom_padding to [0, 1] in setup() - inject bottom_padding via data-bottom-padding HTML attribute instead of inline JS, avoiding syntax errors if token is unsubstituted in stale cache
1 parent 50d786c commit 33053c0

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

assets/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<html lang="en" data-theme="dark">
2+
<html lang="en" data-theme="dark" data-bottom-padding="__BOTTOM_PADDING__">
33

44
<head>
55
<meta charset="utf-8" />
@@ -839,7 +839,7 @@ <h1>Markdown Preview</h1>
839839
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
840840
import markdownItGithubAlerts from 'https://unpkg.com/markdown-it-github-alerts@1.0.1/dist/index.mjs';
841841

842-
const BOTTOM_PADDING = __BOTTOM_PADDING__;
842+
const BOTTOM_PADDING = parseFloat(document.documentElement.dataset.bottomPadding) || 0.5;
843843

844844
// ── DOM refs ──────────────────────────────────────────────────
845845
const $ = (id) => document.getElementById(id);

lua/markdown_preview/init.lua

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ M.config = {
3939

4040
function 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
4344
end
4445

@@ -51,16 +52,12 @@ M._debounce_seq = 0
5152
M._workspace_dir = nil
5253
M._mmdr_available = nil -- nil = unchecked, true/false after probe
5354
M._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

5758
local 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
6562
end
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
10895
end
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"
@@ -357,13 +345,9 @@ end
357345

358346
--- Send cursor line to browser for scroll sync.
359347
local 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

Comments
 (0)