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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ With **lazy.nvim**:
scale = 1, -- nil | 1 (default) | 2 | 3 | ...
width = nil, -- nil | 800 | 400 | ...
height = nil, -- nil | 600 | 300 | ...
pupperteer_path = "home/<user>/.config/puppeteer/puppeteer.config.json", --nil
},
plantuml = {
charset = nil,
Expand All @@ -64,6 +65,19 @@ With **lazy.nvim**:
},
},
```
> [!info]
> pupperteer_path should be the path to your puppeteer.config.json, set the correct path and user.

#### puppeteer options
Puppeteer config file can be found in ~/.config/puppeteer/puppeteer.config.json and able to set chrome-headless-shell versión, required by mermaid-cli.
```json
{
"executablePath": "/home/<user>/.cache/puppeteer/chrome-headless-shell/linux-132.0.6834.110/chrome-headless-shell-linux64/chrome-headless-shell",
"puppeteer": {
"chromiumRevision": "132.0.6834.110"
}
}
```

### Usage

Expand Down
98 changes: 55 additions & 43 deletions lua/diagram/renderers/mermaid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
---@field background? string
---@field theme? string
---@field scale? number
---@field pupperteer_path? string

---@type table<string, string>
local cache = {} -- session cache

---@class Renderer<MermaidOptions>
local M = {
id = "mermaid",
id = "mermaid",
}

-- fs cache
Expand All @@ -19,54 +20,65 @@ vim.fn.mkdir(tmpdir, "p")
---@param options MermaidOptions
---@return string|nil
M.render = function(source, options)
local hash = vim.fn.sha256(M.id .. ":" .. source)
if cache[hash] then return cache[hash] end
local hash = vim.fn.sha256(M.id .. ":" .. source)
if cache[hash] then
return cache[hash]
end

local path = vim.fn.resolve(tmpdir .. "/" .. hash .. ".png")
if vim.fn.filereadable(path) == 1 then return path end
local path = vim.fn.resolve(tmpdir .. "/" .. hash .. ".png")
if vim.fn.filereadable(path) == 1 then
return path
end

if not vim.fn.executable("mmdc") then error("diagram/mermaid: mmdc not found in PATH") end
if not vim.fn.executable("mmdc") then
error("diagram/mermaid: mmdc not found in PATH")
end

local tmpsource = vim.fn.tempname()
vim.fn.writefile(vim.split(source, "\n"), tmpsource)
local tmpsource = vim.fn.tempname()
vim.fn.writefile(vim.split(source, "\n"), tmpsource)

local command_parts = {
"mmdc",
"-i",
tmpsource,
"-o",
path,
}
if options.background then
table.insert(command_parts, "-b")
table.insert(command_parts, options.background)
end
if options.theme then
table.insert(command_parts, "-t")
table.insert(command_parts, options.theme)
end
if options.scale then
table.insert(command_parts, "-s")
table.insert(command_parts, options.scale)
end
if options.width then
table.insert(command_parts, "--width")
table.insert(command_parts, options.width)
end
if options.height then
table.insert(command_parts, "--height")
table.insert(command_parts, options.height)
end
local command_parts = {
"mmdc",
"-i",
tmpsource,
"-o",
path,
}
if options.background then
table.insert(command_parts, "-b")
table.insert(command_parts, options.background)
end
if options.theme then
table.insert(command_parts, "-t")
table.insert(command_parts, options.theme)
end
if options.scale then
table.insert(command_parts, "-s")
table.insert(command_parts, options.scale)
end
if options.width then
table.insert(command_parts, "--width")
table.insert(command_parts, options.width)
end
if options.height then
table.insert(command_parts, "--height")
table.insert(command_parts, options.height)
end
if options.pupperteer_path then
table.insert(command_parts, "-p")
table.insert(command_parts, options.pupperteer_path)
end

local command = table.concat(command_parts, " ")
vim.fn.system(command)
if vim.v.shell_error ~= 0 then
vim.notify("diagram/mermaid: mmdc failed to render diagram", vim.log.levels.ERROR)
return nil
end
local command = table.concat(command_parts, " ")
-- vim.notify(command)
vim.fn.system(command)
if vim.v.shell_error ~= 0 then
vim.notify("diagram/mermaid: mmdc failed to render diagram", vim.log.levels.ERROR)
return nil
end

cache[hash] = path
return path
cache[hash] = path
return path
end

return M