Skip to content

Commit c5440c8

Browse files
authored
add bottom_padding option for end-of-document scroll position
Adds a bottom_padding config option (fraction 0-1) controlling how far from the top of the viewport the final line sits when scrolled to end. Default 0.5 means the last line is scrollable to the vertical midpoint. Value injected into index.html via __BOTTOM_PADDING__ template token with safe data-attribute fallback.
1 parent e7d8e6c commit c5440c8

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ require("markdown_preview").setup({
115115
mermaid_renderer = "js", -- "js" (browser mermaid.js) or "rust" (mmdr CLI, ~400x faster)
116116

117117
scroll_sync = true, -- browser follows cursor position
118+
119+
-- Fraction (0–1): vertical position of the final line when scrolled to end.
120+
-- 0.5 = middle of viewport (default), 1.0 = bottom edge (no extra space)
121+
bottom_padding = 0.5,
118122
})
119123
```
120124

assets/index.html

Lines changed: 10 additions & 1 deletion
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,6 +839,8 @@ <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 = parseFloat(document.documentElement.dataset.bottomPadding) || 0.5;
843+
842844
// ── DOM refs ──────────────────────────────────────────────────
843845
const $ = (id) => document.getElementById(id);
844846
const contentEl = $('content');
@@ -1176,6 +1178,7 @@ <h1>Markdown Preview</h1>
11761178
} else {
11771179
contentEl.innerHTML = html;
11781180
}
1181+
applyBottomPadding();
11791182

11801183
const blocks = contentEl.querySelectorAll('.mermaid-block[data-mermaid-source]:not(.mermaid-rendered)');
11811184
for (const block of blocks) {
@@ -1406,6 +1409,12 @@ <h1>Markdown Preview</h1>
14061409
}
14071410
}).observe(contentEl);
14081411

1412+
// ── Bottom padding ────────────────────────────────────────────
1413+
function applyBottomPadding() {
1414+
contentEl.style.paddingBottom = `${(1 - BOTTOM_PADDING) * window.innerHeight}px`;
1415+
}
1416+
window.addEventListener('resize', applyBottomPadding);
1417+
14091418
// ── Boot ──────────────────────────────────────────────────────
14101419
(async function boot() {
14111420
try {

lua/markdown_preview/init.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ M.config = {
3131
mermaid_renderer = "js",
3232

3333
scroll_sync = true, -- sync browser scroll to cursor position
34+
35+
-- Fraction (0–1): vertical position of the final line when scrolled to end.
36+
-- 0.5 = middle of viewport (default), 1.0 = bottom edge (no extra space)
37+
bottom_padding = 0.5,
3438
}
3539

3640
function M.setup(opts)
3741
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))
3843
M._mmdr_available = nil -- reset so next check re-probes
3944
end
4045

@@ -83,7 +88,9 @@ local function write_index(dir)
8388
if not src then
8489
error("Could not locate assets/index.html in runtimepath. Make sure the plugin ships it.")
8590
end
86-
util.copy_file(src, dst)
91+
local content = util.read_text(src)
92+
content = content:gsub("__BOTTOM_PADDING__", tostring(M.config.bottom_padding))
93+
util.write_text(dst, content)
8794
return dst
8895
end
8996

@@ -254,7 +261,7 @@ local function get_content(bufnr)
254261
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
255262
text = table.concat(lines, "\n")
256263
elseif vim.api.nvim_buf_get_name(bufnr):match("%.mmd$")
257-
or vim.api.nvim_buf_get_name(bufnr):match("%.mermaid$") then
264+
or vim.api.nvim_buf_get_name(bufnr):match("%.mermaid$") then
258265
-- .mmd / .mermaid files: treat entire buffer as mermaid
259266
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
260267
text = "```mermaid\n" .. table.concat(lines, "\n") .. "\n```\n"

0 commit comments

Comments
 (0)