diff --git a/CLAUDE.md b/CLAUDE.md index 4dcf83e..24e7062 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -42,7 +42,7 @@ cabal repl The application consists of: 1. **Main.hs** (app/Main.hs:1-69) - Entry point and core logic - - `processMermaid` (app/Main.hs:21-34) - Transforms Mermaid code blocks into SVG images + - `processMermaidBlock` (app/Main.hs:54-79) - Transforms Mermaid code blocks into images (SVG for HTML, high-res PNG for PDF) - `transformDoc` (app/Main.hs:36-37) - Walks the Pandoc AST to process all blocks - `compileToPDF` (app/Main.hs:47-58) - Converts Markdown to PDF using LaTeX - `compileToHTML` (app/Main.hs:60-68) - Converts Markdown to HTML @@ -50,7 +50,7 @@ The application consists of: The tool uses Pandoc's AST transformation capabilities to: 1. Parse Markdown input 2. Find Mermaid code blocks -3. Generate SVG files using mermaid-cli (mmdc) +3. Generate image files using mermaid-cli (mmdc) - SVG for HTML, high-resolution PNG for PDF 4. Replace code blocks with image references 5. Output final PDF/HTML @@ -59,7 +59,7 @@ The tool uses Pandoc's AST transformation capabilities to: External requirements: - GHC 9.12.2 and Cabal 3.16 (install via ghcup) - Pandoc library -- TeX Live (for PDF generation via pdflatex) +- TeX Live (for PDF generation via XeLaTeX) - Mermaid CLI (`npm install -g @mermaid-js/mermaid-cli`) To install GHC and Cabal: diff --git a/app/Main.hs b/app/Main.hs index 0e08f8d..203546a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -58,19 +58,23 @@ processMermaidBlock (SourceDir sourceDir) (OutputPath outputPath) (CodeBlock (id let DiagramId diagId = generateDiagramId id' contents diagIdStr = T.unpack diagId mmdFile = sourceDir diagIdStr <> ".mmd" - pngFile = sourceDir diagIdStr <> ".png" - -- Determine the correct image path based on output format - imagePath = if isHTMLOutput outputPath - then takeFileName pngFile -- Just filename for HTML - else pngFile -- Full path for PDF/LaTeX + -- Use SVG for HTML (scalable), high-res PNG for PDF (text compatibility) + (outputFile, imagePath) = if isHTMLOutput outputPath + then let svgFile = sourceDir diagIdStr <> ".svg" + in (svgFile, takeFileName svgFile) + else let pngFile = sourceDir diagIdStr <> ".png" + in (pngFile, pngFile) -- Use bracket to ensure cleanup of temporary mermaid file bracket (TIO.writeFile mmdFile contents >> return mmdFile) (\file -> removeFile file `catch` \(_ :: SomeException) -> return ()) (\_ -> do - void $ callProcess mermaidCommand ["-i", mmdFile, "-o", pngFile] - putStrLn $ successEmoji <> " Generated " <> pngFile + -- Generate with appropriate format and quality for output type + if isHTMLOutput outputPath + then void $ callProcess mermaidCommand ["-i", mmdFile, "-o", outputFile] + else void $ callProcess mermaidCommand ["-i", mmdFile, "-o", outputFile, "--scale", "3"] + putStrLn $ successEmoji <> " Generated " <> outputFile return $ Para [Image nullAttr [] (T.pack imagePath, "Mermaid diagram")]) processMermaidBlock _ _ block = return block