This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
md2pdf is a Go CLI tool that converts Markdown files to PDF with GitHub-flavored styling. It supports Mermaid diagrams (rendered as inline SVG) and Japanese text via Noto Sans CJK JP fonts.
go build -o md2pdf ./cmd/md2pdf
go vet ./...# Unit tests only (no external dependencies needed)
go test ./internal/converter/ -run 'Test[^C]'
# All tests including integration (requires mmdc, python3 playwright, chromium, fonts-noto-cjk)
go test ./... -timeout 120s
# Single test
go test ./internal/converter/ -run TestSpecificName -vUses golangci-lint with config in .golangci.yml. Key enabled linters: errcheck, gosimple, govet, staticcheck, unused, gofmt, goimports, misspell, godot, gosec, noctx, wrapcheck, exhaustive. G204 (subprocess with variable) is excluded since mmdc/python invocations are intentional. Test files have relaxed rules (no wrapcheck, gosec, errcheck).
converter.go's Convert branches on Config.Format: PDF and DOCX take different pipelines because each format reads best from a different source.
- parser.go — goldmark parses Markdown to HTML, extracting fenced Mermaid code blocks into a
parsedDocstruct with placeholders - mermaid.go — each Mermaid block is rendered to inline SVG via the external
mmdcCLI (renderMermaid) - html.go — assembles a self-contained HTML file with GitHub CSS,
@font-facedeclarations, and inlined SVGs - pdf.go — headless Chromium (via Playwright Python driver) prints the HTML to PDF
DOCX is produced directly from Markdown by pandoc's gfm reader rather than from HTML, so pandoc emits clean, Word-native paragraph/list styles instead of HTML-derived ones (docx.go convertMarkdownDOCX).
- mermaid_markdown.go —
extractMermaidFromMarkdownscans the raw Markdown line by line, replacing each fenced Mermaid block with a placeholder and preserving non-Mermaid fences verbatim - mermaid.go —
renderMermaidPNGsrasterises each block to a PNG (Word cannot reliably display pandoc-embedded SVG); placeholders are then rewritten toimage references - docx.go — pandoc converts the processed Markdown with
-f gfm, running with the working directory set (generated diagrams resolve there) and--resource-pathpointing at the source dir (user images resolve there). It also builds a styled reference document (buildReferenceDoc→patchReferenceDoc): table borders (injectTableBorders), a 10.5pt body (setBodyFontSize), compact headings (shrinkHeadings), and a Japanese-friendly font for both Latin and East Asian runs via the theme (setThemeFonts, defaultYu Gothic, overridable with-docx-font). Reference-doc styling is best-effort and falls back to pandoc defaults on failure.
converter.go orchestrates both pipelines and manages a temporary working directory for intermediate files. Config struct holds all runtime options including Format ("pdf"|"docx") and DOCXFont.
cmd/md2pdf/ — CLI entry point. flags.go handles argument parsing and auto-detection of font/mmdc paths; resolveFormat derives the output format from -format or the -o extension. Pandoc is resolved in docx.go (findPandoc) unless -pandoc is provided. main.go wires flags to the converter.
Runtime: mmdc (Mermaid CLI via npm), Python 3 + Playwright + Chromium, Noto Sans CJK JP fonts. DOCX output additionally requires pandoc.
Go modules: github.com/yuin/goldmark (Markdown parsing).
- All exported symbols require GoDoc comments ending with a period (godot linter)
- Comments and GoDoc in English
- Errors crossing package boundaries must be wrapped (wrapcheck)
- Go 1.22+, CI tests against Go 1.22 and 1.23