Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ require("markdown_preview").setup({
mermaid_renderer = "js", -- "js" (browser mermaid.js) or "rust" (mmdr CLI, ~400x faster)

scroll_sync = true, -- browser follows cursor position

-- Fraction (0–1): vertical position of the final line when scrolled to end.
-- 0.5 = middle of viewport (default), 1.0 = bottom edge (no extra space)
bottom_padding = 0.5,
})
```

Expand Down
11 changes: 10 additions & 1 deletion assets/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en" data-theme="dark">
<html lang="en" data-theme="dark" data-bottom-padding="__BOTTOM_PADDING__">

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

const BOTTOM_PADDING = parseFloat(document.documentElement.dataset.bottomPadding) || 0.5;

// ── DOM refs ──────────────────────────────────────────────────
const $ = (id) => document.getElementById(id);
const contentEl = $('content');
Expand Down Expand Up @@ -1176,6 +1178,7 @@ <h1>Markdown Preview</h1>
} else {
contentEl.innerHTML = html;
}
applyBottomPadding();

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

// ── Bottom padding ────────────────────────────────────────────
function applyBottomPadding() {
contentEl.style.paddingBottom = `${(1 - BOTTOM_PADDING) * window.innerHeight}px`;
}
window.addEventListener('resize', applyBottomPadding);

// ── Boot ──────────────────────────────────────────────────────
(async function boot() {
try {
Expand Down
11 changes: 9 additions & 2 deletions lua/markdown_preview/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ M.config = {
mermaid_renderer = "js",

scroll_sync = true, -- sync browser scroll to cursor position

-- Fraction (0–1): vertical position of the final line when scrolled to end.
-- 0.5 = middle of viewport (default), 1.0 = bottom edge (no extra space)
bottom_padding = 0.5,
}

function M.setup(opts)
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
M.config.bottom_padding = math.max(0, math.min(1, M.config.bottom_padding))
M._mmdr_available = nil -- reset so next check re-probes
end

Expand Down Expand Up @@ -83,7 +88,9 @@ local function write_index(dir)
if not src then
error("Could not locate assets/index.html in runtimepath. Make sure the plugin ships it.")
end
util.copy_file(src, dst)
local content = util.read_text(src)
content = content:gsub("__BOTTOM_PADDING__", tostring(M.config.bottom_padding))
util.write_text(dst, content)
return dst
end

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