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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ require("markdown_preview").setup({

content_name = "content.md", -- workspace content file
index_name = "index.html", -- workspace HTML file
custom_css = "", -- Absolute path, leave it empty for default style
workspace_dir = nil, -- nil = auto (shared for takeover, per-buffer for multi)

overwrite_index_on_start = true, -- copy plugin's index.html on every start
Expand Down
14 changes: 14 additions & 0 deletions lua/markdown_preview/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ M.config = {

content_name = "content.md",
index_name = "index.html",
custom_css = "",

-- nil = per-buffer workspace (recommended); set a path to override
workspace_dir = nil,
Expand Down Expand Up @@ -110,12 +111,25 @@ local function write_index(dir)
error("Could not locate assets/index.html in runtimepath. Make sure the plugin ships it.")
end
local content = util.read_text(src)

-- gsub with function replacement: avoids the "%n is a capture reference"
-- escape problem if any substituted value contains '%'.
content = content:gsub("__BOTTOM_PADDING__", function() return tostring(M.config.bottom_padding) end)
content = content:gsub("__MERMAID_ELK__", function() return M.config.mermaid_elk and "true" or "false" end)
content = content:gsub("__LIVE_TOKEN__", function() return M._token or "" end)
content = content:gsub("__THEME__", function() return M.config.default_theme end)

-- Inline custom CSS if configured
if M.config.custom_css and M.config.custom_css ~= "" then
local css_src = vim.fn.expand(M.config.custom_css)
if vim.fn.filereadable(css_src) == 1 then
local css = util.read_text(css_src)
content = content:gsub("</head>", function() return "<style>\n" .. css .. "\n</style>\n</head>" end)
else
vim.notify("Markdown Preview: custom_css not found: " .. css_src, vim.log.levels.WARN)
end
end

util.write_text(dst, content)
return dst
end
Expand Down