diff --git a/README.md b/README.md index 2b3194e..b42f93c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/markdown_preview/init.lua b/lua/markdown_preview/init.lua index 108ed9f..0beffc3 100644 --- a/lua/markdown_preview/init.lua +++ b/lua/markdown_preview/init.lua @@ -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, @@ -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("", function() return "\n" 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