From 00caad18bfb2196fd6ed79a95315d0a4a5b5af82 Mon Sep 17 00:00:00 2001 From: Barrett Ruth <62671086+barrettruth@users.noreply.github.com> Date: Thu, 5 Mar 2026 12:05:34 -0500 Subject: [PATCH] refactor(presets): simplify `mermaid` error parser (#49) * feat: add `mermaid` preset Problem: no built-in support for compiling mermaid diagrams via `mmdc`. Solution: add a `mermaid` preset that compiles `.mmd` files to SVG and parses `Parse error on line N` diagnostics from stderr. Add `mermaid-cli` to the nix dev shell. * refactor(presets): simplify `mermaid` error parser Problem: the inline `mermaid` error_parser looped over every line and used the `Parse error on line N:` header as the message, losing the useful `Expecting ..., got ...` token detail. Solution: extract `parse_mermaid` alongside the other parse functions, use a single `output:match` (mermaid's JISON parser stops at the first error), and surface the `Expecting ..., got ...` line as the message. * ci: format * ci: format --- flake.nix | 6 ++++-- lua/preview/presets.lua | 32 +++++++++++++++++++------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/flake.nix b/flake.nix index 636f4d0..f6c279b 100644 --- a/flake.nix +++ b/flake.nix @@ -19,7 +19,8 @@ { formatter = forEachSystem (pkgs: pkgs.nixfmt-tree); - devShells = forEachSystem (pkgs: + devShells = forEachSystem ( + pkgs: let devTools = [ (pkgs.luajit.withPackages ( @@ -50,6 +51,7 @@ pkgs.mermaid-cli ]; }; - }); + } + ); }; } diff --git a/lua/preview/presets.lua b/lua/preview/presets.lua index 1b5333e..d4b03cc 100644 --- a/lua/preview/presets.lua +++ b/lua/preview/presets.lua @@ -115,6 +115,24 @@ local function parse_asciidoctor(output) return diagnostics end +---@param output string +---@return preview.Diagnostic[] +local function parse_mermaid(output) + local lnum = output:match('Parse error on line (%d+)') + if not lnum then + return {} + end + local msg = output:match('(Expecting .+)') or 'parse error' + return { + { + lnum = tonumber(lnum) - 1, + col = 0, + message = msg, + severity = vim.diagnostic.severity.ERROR, + }, + } +end + ---@type preview.ProviderConfig M.typst = { ft = 'typst', @@ -313,19 +331,7 @@ M.mermaid = { return (ctx.file:gsub('%.mmd$', '.svg')) end, error_parser = function(output) - local diagnostics = {} - for line in output:gmatch('[^\r\n]+') do - local lnum = line:match('^%s*Parse error on line (%d+)') - if lnum then - table.insert(diagnostics, { - lnum = tonumber(lnum) - 1, - col = 0, - message = line, - severity = vim.diagnostic.severity.ERROR, - }) - end - end - return diagnostics + return parse_mermaid(output) end, clean = function(ctx) return { 'rm', '-f', (ctx.file:gsub('%.mmd$', '.svg')) }